From 7e6e491907b97162d98b731b5a5f3cab1a80b0e9 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 12 Mar 2025 10:21:04 +0100 Subject: [PATCH] oops overflow --- .../c++/equalization.cpp | 58 ++++++++++++++++--- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp b/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp index 57d3403..9a74203 100644 --- a/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp +++ b/image-processing/1-SlicedOptimalTransport/c++/equalization.cpp @@ -37,6 +37,7 @@ #include #define STB_IMAGE_WRITE_IMPLEMENTATION #include +#define DEBUG (0) unsigned char min(unsigned char a, unsigned char b) { return a < b ? a : b; @@ -81,10 +82,10 @@ int main(int argc, char **argv) //As an example, we just scan the pixels of the source image //and swap the color channels. - unsigned int cum_r[255]; - unsigned int cum_g[255]; - unsigned int cum_b[255]; - for (auto i=0; i < 255; i++) { + unsigned int cum_r[256]; + unsigned int cum_g[256]; + unsigned int cum_b[256]; + for (auto i=0; i < 256; i++) { cum_r[i] = cum_g[i] = cum_b[i] = 0; } @@ -97,20 +98,61 @@ int main(int argc, char **argv) } } - unsigned int distrib_r[255]; - unsigned int distrib_g[255]; - unsigned int distrib_b[255]; + if DEBUG { + bool seenB, seenR, seenG; + seenB = seenR = seenG = false; + int i=256; + while (i>0 && (!seenR || !seenG || !seenB)) { + if (!seenR && cum_r[i] != 0) { + printf("maxR: %d\n", i); + seenR=true; + } + if (!seenG && cum_g[i] != 0) { + printf("maxG: %d\n", i); + seenG=true; + } + if (!seenB && cum_b[i] != 0) { + printf("maxB: %d\n", i); + seenB=true; + } + i--; + } + + seenB = seenR = seenG = false; + i=0; + while (i<=256 && (!seenR || !seenG || !seenB)) { + if (!seenR && cum_r[i] != 0) { + printf("minR: %d\n", i); + seenR=true; + } + if (!seenG && cum_g[i] != 0) { + printf("minG: %d\n", i); + seenG=true; + } + if (!seenB && cum_b[i] != 0) { + printf("minB: %d\n", i); + seenB=true; + } + i++; + } + } + + unsigned int distrib_r[256]; + unsigned int distrib_g[256]; + unsigned int distrib_b[256]; int total_px = width*height; float cur_cum_r, cur_cum_g, cur_cum_b; cur_cum_r, cur_cum_g, cur_cum_b = 0; - for (auto i=0; i < 255; i++) { + if DEBUG printf("Distrib: R G B\n"); + for (auto i=0; i < 256; i++) { cur_cum_r += cum_r[i]/(float)total_px; cur_cum_g += cum_g[i]/(float)total_px; cur_cum_b += cum_b[i]/(float)total_px; distrib_r[i] = 255*cur_cum_r; distrib_g[i] = 255*cur_cum_g; distrib_b[i] = 255*cur_cum_b; + if DEBUG printf("%d\t%3d %3d %3d\n", i,distrib_r[i], distrib_g[i], distrib_b[i]); } for(auto i = 0 ; i < width ; ++i)