From 095b14d3a95f652948ef8f91e583d04c9a5cf694 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Thu, 3 Apr 2025 16:25:27 +0200 Subject: [PATCH] Create utils.cpp --- CMakeLists.txt | 2 ++ src/seam-carving.cpp | 36 ++++-------------------------------- src/utils.cpp | 21 +++++++++++++++++++++ src/utils.hpp | 19 +++++++++++++++++++ 4 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 src/utils.cpp create mode 100644 src/utils.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a21dbf..483fb68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ project (seam-carving) # Add an executable with the above sources add_executable(seam-carving src/seam-carving.cpp) +add_library(utils src/utils.cpp) # Set the directories that should be included in the build command for this target # when running g++ these will be included as -I/directory/path/ @@ -16,3 +17,4 @@ target_include_directories(seam-carving PRIVATE ${PROJECT_SOURCE_DIR}/deps ) +target_link_libraries(seam-carving utils) \ No newline at end of file diff --git a/src/seam-carving.cpp b/src/seam-carving.cpp index 6891f07..f28498e 100644 --- a/src/seam-carving.cpp +++ b/src/seam-carving.cpp @@ -11,6 +11,7 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #include #include +#include "utils.hpp" #define DEFAULT_SEAMS 1 @@ -24,35 +25,6 @@ int max_step = 1; // : dim_long, b : dim_large #define im_index(a, b) (vertical ? (width * a + b) : (width * b + a)) -bool nearly_equal(float a, float b) { - return std::nextafter(a, std::numeric_limits::lowest()) <= b && - std::nextafter(a, std::numeric_limits::max()) >= b; -} - -std::pair operator+(std::pair& p1, std::pair& p2) { - return { - // If one of the two pixels is "protected" (ie 2), we want to prevent this line removing - // Else, we want to keep the information of "is there a pixel to remove in this seam" (ie 0) - (p1.first==2) || (p2.first==2) ? 2 : std::min(p1.first, p2.first), - p1.second+p2.second - }; -} - -void operator+=(std::pair& p1, std::pair& p2) { - p1 = p1+p2; -} - -namespace limits { - struct max_energy { - template operator T() { - return std::numeric_limits::max(); - } - operator std::pair() { - return {3, std::numeric_limits::max()}; - } - }; -} - void export_image(const char *filename, const void *data, int width, int height, int nbChannels) { @@ -133,9 +105,9 @@ template std::vector optimal_seam(std::vector energy, int w for (auto k = lower_bound; k <= upper_bound; k++) { // Compute energy based on predecessors - dyn_energy[dim_large * i + j] = std::min( - dyn_energy[dim_large * i + j], dyn_energy[dim_large * (i - 1) + k] - ); + if (dyn_energy[dim_large*(i-1)+k] < dyn_energy[dim_large*i+j]) { + dyn_energy[dim_large*i+j] = dyn_energy[dim_large*(i-1)+k]; + } } dyn_energy[dim_large * i + j] += energy[im_index(i, j)]; } diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..3344a94 --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +bool nearly_equal(float a, float b) { + return std::nextafter(a, std::numeric_limits::lowest()) <= b && + std::nextafter(a, std::numeric_limits::max()) >= b; +} + +std::pair operator+(std::pair& p1, std::pair& p2) { + return { + // If one of the two pixels is "protected" (ie 2), we want to prevent this line removing + // Else, we want to keep the information of "is there a pixel to remove in this seam" (ie 0) + (p1.first==2) || (p2.first==2) ? 2 : std::min(p1.first, p2.first), + p1.second+p2.second + }; +} + +void operator+=(std::pair& p1, std::pair& p2) { + p1 = p1+p2; +} \ No newline at end of file diff --git a/src/utils.hpp b/src/utils.hpp new file mode 100644 index 0000000..1d53aa4 --- /dev/null +++ b/src/utils.hpp @@ -0,0 +1,19 @@ +#include + + +bool nearly_equal(float a, float b); + +std::pair operator+(std::pair& p1, std::pair& p2); + +void operator+=(std::pair& p1, std::pair& p2); + +namespace limits { + struct max_energy { + template operator T() { + return std::numeric_limits::max(); + } + operator std::pair() { + return {3, std::numeric_limits::max()}; + } + }; +} \ No newline at end of file