From 5a9eb05b6a5cac017b41d1b7f18bb8e8dc9c2778 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 2 Apr 2025 18:26:43 +0200 Subject: [PATCH] Use valid limits --- src/seam-carving.cpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/seam-carving.cpp b/src/seam-carving.cpp index aadebb1..e138076 100644 --- a/src/seam-carving.cpp +++ b/src/seam-carving.cpp @@ -37,6 +37,18 @@ 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 {2, std::numeric_limits::max()}; + } + }; +} + + void export_image(const char *filename, const void *data, int width, int height, int nbChannels) { if (!silent) @@ -110,7 +122,7 @@ template std::vector optimal_seam(std::vector energy, int w for (auto i = 1; i < dim_long; i++) { // Propagate dyn_energy for (auto j = 0; j < dim_large; j++) { - dyn_energy[dim_large * i + j] = std::numeric_limits::max(); + dyn_energy[dim_large * i + j] = limits::max_energy(); int lower_bound = std::max(j - max_step, 0); int upper_bound = std::min(j + max_step, dim_large - 1); @@ -127,7 +139,8 @@ template std::vector optimal_seam(std::vector energy, int w std::vector result(dim_long); // Find the seam end int min_idx = -1; - T min_val = std::numeric_limits::max(); + T min_val = limits::max_energy(); + for (auto j = 0; j < dim_large; j++) { if (min_val > dyn_energy[dim_large * (dim_long - 1) + j]) { min_idx = j; @@ -138,17 +151,17 @@ template std::vector optimal_seam(std::vector energy, int w //* Backtracking to find the path for (auto i = dim_long - 1; i > 0; i--) { -// We want to find either (bot_l, bot_c, bot_r) with dyn_energy[.] = min_val - -// energy[cur] + // We want to find either (bot_l, bot_c, bot_r) with dyn_energy[.] = min_val - + // energy[cur] -// Idea : float next_energy = min_val - energy[width*i + min_idx]; -//! With floats, we don't always have x + y - y == x, so we check is x+y == x+y -// This define is a bit ugly but 200x faster than using a lambda function -#define is_next_idx(idx) \ - (dyn_energy[(i - 1) * dim_large + idx] + energy[im_index(i, min_idx)] == \ - min_val) + // Idea : float next_energy = min_val - energy[width*i + min_idx]; + //! With floats, we don't always have x + y - y == x, so we check is x+y == x+y + // This define is a bit ugly but 200x faster than using a lambda function + #define is_next_idx(idx) \ + (dyn_energy[(i - 1) * dim_large + idx] + energy[im_index(i, min_idx)] == \ + min_val) - // This is not the nicest way to do thiss but we want to check in priority + // This is not the nicest way to do this but we want to check in priority // at the center to have straight seams bool found = false; if (is_next_idx(min_idx)) {