New coding style

parent 9192e892
---
BasedOnStyle: Chromium
AlignEscapedNewlinesLeft: 'true'
AlignTrailingComments: 'true'
AllowShortBlocksOnASingleLine: 'true'
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakBeforeMultilineStrings: 'true'
AlwaysBreakTemplateDeclarations: 'true'
BreakBeforeBinaryOperators: 'true'
BreakBeforeBraces: Stroustrup
BreakBeforeTernaryOperators: 'true'
BreakConstructorInitializersBeforeComma: 'false'
ColumnLimit: '80'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
Cpp11BracedListStyle: 'true'
IndentCaseLabels: 'true'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
Language: Cpp
MaxEmptyLinesToKeep: '2'
PointerAlignment: Left
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInParentheses: 'false'
Standard: Cpp11
TabWidth: '2'
UseTab: ForIndentation
...
#ifndef GENETIC_IMAGE_INCLUDE_GENIMG_COMMON_HH_
#define GENETIC_IMAGE_INCLUDE_GENIMG_COMMON_HH_
#include <spdlog/spdlog.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <spdlog/spdlog.h>
#include <string>
#include <utility>
[[nodiscard]] std::pair<cv::Mat, cv::Mat>
init_image(std::string const &) noexcept;
[[nodiscard]] std::pair<cv::Mat, cv::Mat> init_image(
std::string const&) noexcept;
[[nodiscard]] double euclidian_distance(cv::Mat const &, cv::Mat const &);
[[nodiscard]] double euclidian_distance(cv::Mat const&, cv::Mat const&);
#endif /* GENETIC_IMAGE_INCLUDE_GENIMG_COMMON_HH_ */
......@@ -6,7 +6,10 @@
enum class Shapes { Square, Circle };
void draw_shape(cv::Mat &, cv::Point const &, int const, cv::Scalar const &,
Shapes const &);
void draw_shape(cv::Mat&,
cv::Point const&,
int const,
cv::Scalar const&,
Shapes const&);
#endif /* GENETIC_IMAGE_INCLUDE_GENIMG_DRAWING_HH_ */
#ifndef GENETIC_IMAGE_INCLUDE_GENIMG_METHODS_HH_
#define GENETIC_IMAGE_INCLUDE_GENIMG_METHODS_HH_
#include <spdlog/spdlog.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <spdlog/spdlog.h>
void method1(cv::Mat const &, cv::Mat &, int);
void method1(cv::Mat const&, cv::Mat&, int);
void method2(cv::Mat const &, cv::Mat &, int);
void method2(cv::Mat const&, cv::Mat&, int);
void method3(cv::Mat const &, cv::Mat &, int);
void method3(cv::Mat const&, cv::Mat&, int);
#endif /* GENETIC_IMAGE_INCLUDE_GENIMG_METHODS_HH_ */
......@@ -4,8 +4,8 @@
#include <filesystem>
#include <tuple>
[[nodiscard]] std::tuple<std::filesystem::path, std::filesystem::path, int, int,
bool>
parse_args(int, char **);
[[nodiscard]] std::
tuple<std::filesystem::path, std::filesystem::path, int, int, bool>
parse_args(int, char**);
#endif /* GENETIC_IMAGE_INCLUDE_GENIMG_PARSEARGS_HH_ */
......@@ -3,29 +3,31 @@
#include <cmath>
#include <cstdlib>
[[nodiscard]] std::pair<cv::Mat, cv::Mat>
init_image(std::string const &t_input_file) noexcept {
cv::Mat input_image = cv::imread(t_input_file, cv::IMREAD_COLOR);
if (!input_image.data) {
spdlog::critical("Could not open or find image!\n");
exit(-1);
}
spdlog::debug("Image loaded!");
spdlog::debug("Width:\t\t{}", input_image.size().width);
spdlog::debug("Height:\t{}", input_image.size().height);
cv::Mat process_image(input_image.size().height, input_image.size().width,
CV_8UC3, cv::Scalar(0, 0, 0));
return std::make_pair(std::move(input_image), process_image);
[[nodiscard]] std::pair<cv::Mat, cv::Mat> init_image(
std::string const& t_input_file) noexcept
{
cv::Mat input_image = cv::imread(t_input_file, cv::IMREAD_COLOR);
if (!input_image.data) {
spdlog::critical("Could not open or find image!\n");
exit(-1);
}
spdlog::debug("Image loaded!");
spdlog::debug("Width:\t\t{}", input_image.size().width);
spdlog::debug("Height:\t{}", input_image.size().height);
cv::Mat process_image(input_image.size().height, input_image.size().width,
CV_8UC3, cv::Scalar(0, 0, 0));
return std::make_pair(std::move(input_image), process_image);
}
[[nodiscard]] double euclidian_distance(cv::Mat const &t_img1,
cv::Mat const &t_img2) {
double euclidian = 0.0;
for (auto itr1 = t_img1.begin<uchar>(), itr2 = t_img2.begin<uchar>();
itr1 != t_img1.end<uchar>() && itr2 != t_img2.end<uchar>();
++itr1, ++itr2) {
euclidian += std::pow(*itr1 - *itr2, 2);
}
euclidian = std::sqrt(euclidian);
return euclidian;
[[nodiscard]] double euclidian_distance(cv::Mat const& t_img1,
cv::Mat const& t_img2)
{
double euclidian = 0.0;
for (auto itr1 = t_img1.begin<uchar>(), itr2 = t_img2.begin<uchar>();
itr1 != t_img1.end<uchar>() && itr2 != t_img2.end<uchar>();
++itr1, ++itr2) {
euclidian += std::pow(*itr1 - *itr2, 2);
}
euclidian = std::sqrt(euclidian);
return euclidian;
}
#include "drawing.hh"
#include <spdlog/spdlog.h>
#include <cstdlib>
#include <memory>
#include <opencv2/imgproc.hpp>
#include <spdlog/spdlog.h>
void drawSquare(cv::Mat &t_img, cv::Point const &t_top_left, int const t_size,
cv::Scalar const &t_color) {
auto points = std::make_unique<cv::Point[]>(4);
points.get()[0] = t_top_left;
points.get()[1] = cv::Point{t_top_left.x, t_top_left.y + t_size};
points.get()[2] = cv::Point{t_top_left.x + t_size, t_top_left.y + t_size};
points.get()[3] = cv::Point{t_top_left.x + t_size, t_top_left.y};
// spdlog::debug("Size:{} 1[{},{}] 2[{},{}] 3[{},{}] 4[{},{}]", t_size,
// points[0].x, points[0].y, points[1].x, points[1].y, points[2].x,
// points[2].y, points[3].x, points[3].y);
fillConvexPoly(t_img, points.get(), 4, t_color);
void drawSquare(cv::Mat& t_img,
cv::Point const& t_top_left,
int const t_size,
cv::Scalar const& t_color)
{
auto points = std::make_unique<cv::Point[]>(4);
points.get()[0] = t_top_left;
points.get()[1] = cv::Point{t_top_left.x, t_top_left.y + t_size};
points.get()[2] = cv::Point{t_top_left.x + t_size, t_top_left.y + t_size};
points.get()[3] = cv::Point{t_top_left.x + t_size, t_top_left.y};
fillConvexPoly(t_img, points.get(), 4, t_color);
}
void draw_shape(cv::Mat &t_img, cv::Point const &t_top_left, int const t_size,
cv::Scalar const &t_color, Shapes const &t_shape) {
switch (t_shape) {
case Shapes::Square: {
drawSquare(t_img, t_top_left, t_size, t_color);
break;
}
default:
spdlog::error("Shape does not exist. Aborting...");
std::exit(1);
}
void draw_shape(cv::Mat& t_img,
cv::Point const& t_top_left,
int const t_size,
cv::Scalar const& t_color,
Shapes const& t_shape)
{
switch (t_shape) {
case Shapes::Square: {
drawSquare(t_img, t_top_left, t_size, t_color);
break;
}
default:
spdlog::error("Shape does not exist. Aborting...");
std::exit(1);
}
}
#include "common.hh"
#include "methods.hh"
#include "parseargs.hh"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include "common.hh"
#include "methods.hh"
#include "parseargs.hh"
int main(int ac, char **av) {
std::srand(std::time(nullptr));
auto const [input_file, output_file, iterations, method, verbose] =
parse_args(ac, av);
spdlog::set_level(verbose ? spdlog::level::debug : spdlog::level::info);
spdlog::debug("Input file:\t{}", input_file.native());
spdlog::debug("Output file:\t{}", output_file.native());
spdlog::debug("Iterations:\t{}", iterations);
auto [input_image, process_image] = init_image(input_file.native());
int main(int ac, char** av)
{
std::srand(std::time(nullptr));
auto const [input_file, output_file, iterations, method, verbose]
= parse_args(ac, av);
spdlog::set_level(verbose ? spdlog::level::debug : spdlog::level::info);
spdlog::debug("Input file:\t{}", input_file.native());
spdlog::debug("Output file:\t{}", output_file.native());
spdlog::debug("Iterations:\t{}", iterations);
auto [input_image, process_image] = init_image(input_file.native());
switch (method) {
case 1: {
method1(input_image, process_image, iterations);
break;
}
case 2: {
method2(input_image, process_image, iterations);
break;
}
case 3: {
method3(input_image, process_image, iterations);
break;
}
default:
spdlog::error("Requested method {} is not implemented.");
std::exit(-1);
}
switch (method) {
case 1: {
method1(input_image, process_image, iterations);
break;
}
case 2: {
method2(input_image, process_image, iterations);
break;
}
case 3: {
method3(input_image, process_image, iterations);
break;
}
default:
spdlog::error("Requested method {} is not implemented.");
std::exit(-1);
}
cv::imwrite(output_file.native(), process_image);
return 0;
cv::imwrite(output_file.native(), process_image);
return 0;
}
This diff is collapsed.
......@@ -8,43 +8,46 @@ constexpr int DEFAULT_ITERATIONS = 5000;
using path = std::filesystem::path;
namespace po = boost::program_options;
void processFilenames(po::variables_map const &vm, path const &t_input,
path &t_output) {
if (!vm.count("output")) {
t_output.replace_filename("output_" +
std::string{t_input.filename().string()});
} else if (!t_output.has_extension()) {
t_output.replace_extension(".png");
}
void processFilenames(po::variables_map const& vm,
path const& t_input,
path& t_output)
{
if (!vm.count("output")) {
t_output.replace_filename("output_"
+ std::string{t_input.filename().string()});
}
else if (!t_output.has_extension()) {
t_output.replace_extension(".png");
}
}
[[nodiscard]] std::tuple<path, path, int, int, bool> parse_args(int t_ac,
char **t_av) {
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "Display this help message")
("input,i", po::value<path>(), "Input image")
("output,o", po::value<path>(),
"Image output path (default: input path + \"_output\")")
("method,m", po::value<int>(), "Method number to be used (default: 1)")
("iterations,n", po::value<int>(), "Number of iterations (default: 5000)")
("verbose,v", "Enables verbosity");
po::variables_map vm;
po::store(po::parse_command_line(t_ac, t_av, desc), vm);
po::notify(vm);
if (vm.count("help") || !vm.count("input")) {
std::cout << desc << "\n";
std::exit(1);
}
char** t_av)
{
po::options_description desc("Allowed options");
desc.add_options()("help,h", "Display this help message")(
"input,i", po::value<path>(), "Input image")(
"output,o", po::value<path>(),
"Image output path (default: input path + \"_output\")")(
"method,m", po::value<int>(), "Method number to be used (default: 1)")(
"iterations,n", po::value<int>(), "Number of iterations (default: 5000)")(
"verbose,v", "Enables verbosity");
po::variables_map vm;
po::store(po::parse_command_line(t_ac, t_av, desc), vm);
po::notify(vm);
if (vm.count("help") || !vm.count("input")) {
std::cout << desc << "\n";
std::exit(1);
}
auto const input_path = vm["input"].as<path>();
auto output_path =
vm.count("output") ? vm["output"].as<path>() : input_path.filename();
processFilenames(vm, input_path, output_path);
auto const input_path = vm["input"].as<path>();
auto output_path
= vm.count("output") ? vm["output"].as<path>() : input_path.filename();
processFilenames(vm, input_path, output_path);
return std::make_tuple(input_path, output_path,
vm.count("iterations") ? vm["iterations"].as<int>()
: DEFAULT_ITERATIONS,
vm.count("method") ? vm["method"].as<int>() : 1,
vm.count("verbose") ? true : false);
return std::make_tuple(
input_path, output_path,
vm.count("iterations") ? vm["iterations"].as<int>() : DEFAULT_ITERATIONS,
vm.count("method") ? vm["method"].as<int>() : 1,
vm.count("verbose") ? true : false);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment