Avoid overflow on large images
Some checks failed
c++/cmake / build (macOS-latest) (push) Has been cancelled
c++/cmake / build (ubuntu-latest) (push) Has been cancelled
c++/cmake / build (windows-latest) (push) Has been cancelled

This commit is contained in:
augustin64 2025-03-04 15:28:45 +01:00
parent b5c2318646
commit 048652e215

View File

@ -58,10 +58,14 @@ int main(int argc, char **argv)
app.add_option("-o,--output", outputImage, "Output image")->required(); app.add_option("-o,--output", outputImage, "Output image")->required();
unsigned int nbSteps = 3; unsigned int nbSteps = 3;
app.add_option("-n,--nbsteps", nbSteps, "Number of sliced steps (3)"); app.add_option("-n,--nbsteps", nbSteps, "Number of sliced steps (3)");
float ratio = 1.0;
app.add_option("-r,--ratio", ratio, "Ratio of base/objective color (1.0)");
silent = false; silent = false;
app.add_flag("--silent", silent, "No verbose messages"); app.add_flag("--silent", silent, "No verbose messages");
CLI11_PARSE(app, argc, argv); CLI11_PARSE(app, argc, argv);
assert(ratio <= 1 && ratio >= 0);
//Image loading //Image loading
int width,height, nbChannels; int width,height, nbChannels;
unsigned char *source = stbi_load(sourceImage.c_str(), &width, &height, &nbChannels, 0); unsigned char *source = stbi_load(sourceImage.c_str(), &width, &height, &nbChannels, 0);
@ -98,15 +102,15 @@ int main(int argc, char **argv)
unsigned int distrib_b[255]; unsigned int distrib_b[255];
int total_px = width*height; int total_px = width*height;
int cur_cum_r, cur_cum_g, cur_cum_b; float cur_cum_r, cur_cum_g, cur_cum_b;
cur_cum_r, cur_cum_g, cur_cum_b = 0; cur_cum_r, cur_cum_g, cur_cum_b = 0;
for (auto i=0; i < 255; i++) { for (auto i=0; i < 255; i++) {
cur_cum_r += cum_r[i]; cur_cum_r += cum_r[i]/(float)total_px;
cur_cum_g += cum_g[i]; cur_cum_g += cum_g[i]/(float)total_px;
cur_cum_b += cum_b[i]; cur_cum_b += cum_b[i]/(float)total_px;
distrib_r[i] = 255*cur_cum_r/total_px; distrib_r[i] = 255*cur_cum_r;
distrib_g[i] = 255*cur_cum_g/total_px; distrib_g[i] = 255*cur_cum_g;
distrib_b[i] = 255*cur_cum_b/total_px; distrib_b[i] = 255*cur_cum_b;
} }
for(auto i = 0 ; i < width ; ++i) for(auto i = 0 ; i < width ; ++i)
@ -118,9 +122,9 @@ int main(int argc, char **argv)
unsigned char g = source[ indexPixel + 1]; unsigned char g = source[ indexPixel + 1];
unsigned char b = source[ indexPixel + 2]; unsigned char b = source[ indexPixel + 2];
//Swapping the channels //Swapping the channels
output[ indexPixel ] = distrib_r[r]; output[ indexPixel ] = ratio*distrib_r[r]+(1-ratio)*r;
output[ indexPixel + 1 ] = distrib_g[g]; output[ indexPixel + 1 ] = ratio*distrib_g[g]+(1-ratio)*g;
output[ indexPixel + 2 ] = distrib_b[b]; output[ indexPixel + 2 ] = ratio*distrib_b[b]+(1-ratio)*b;
if (nbChannels == 4) //just copying the alpha value if any if (nbChannels == 4) //just copying the alpha value if any
output[ indexPixel + 3] = source[ indexPixel + 3]; output[ indexPixel + 3] = source[ indexPixel + 3];
} }