combine get_minval() and get_maxval()

This simplifies the code a bit, and slightly speeds up using
option "-P, --debug-output".
This commit is contained in:
Erik Auerswald
2025-03-18 20:42:38 +01:00
parent ac060de96b
commit 67abe0fba8
3 changed files with 23 additions and 80 deletions
+13 -66
View File
@@ -613,92 +613,39 @@ double iterative_threshold(Imlib_Image *source_image, double thresh,
return new_thresh * 100;
}
/* get minimum lum value */
double get_minval(Imlib_Image *source_image, int x, int y, int w, int h,
luminance_t lt)
/* get minimum and maximum lum values */
void get_minmaxval(Imlib_Image *source_image, luminance_t lt,
double *min, double *max)
{
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int w, h; /* image dimensions */
int xi,yi; /* iteration variables */
Imlib_Color color; /* Imlib2 RGBA color structure */
int minval = MAXRGB;
int lum = 0;
*min = MAXRGB;
*max = 0;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* get image dimensions */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
/* special value -1 for width or height means image width/height */
if(w == -1) w = width;
if(h == -1) h = width;
/* assure valid coordinates */
if(x+w > width) x = width-w;
if(y+h > height) y = height-h;
if(x<0) x=0;
if(y<0) y=0;
h = imlib_image_get_height();
w = imlib_image_get_width();
/* find the minimum value in the image */
for(xi=0; (xi<w) && (xi<width); xi++) {
for(yi=0; (yi<h) && (yi<height); yi++) {
for(xi=0; xi<w; xi++) {
for(yi=0; yi<h; yi++) {
imlib_image_query_pixel(xi, yi, &color);
lum = clip(get_lum(&color, lt),0,255);
if(lum < minval) minval = lum;
if(lum < *min) *min = lum;
if(lum > *max) *max = lum;
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
return minval;
}
/* get maximum luminance value */
double get_maxval(Imlib_Image *source_image, int x, int y, int w, int h,
luminance_t lt)
{
Imlib_Image current_image; /* save image pointer */
int height, width; /* image dimensions */
int xi,yi; /* iteration variables */
Imlib_Color color; /* Imlib2 RGBA color structure */
int lum = 0;
int maxval = 0;
/* save pointer to current image */
current_image = imlib_context_get_image();
/* get image dimensions */
imlib_context_set_image(*source_image);
height = imlib_image_get_height();
width = imlib_image_get_width();
/* special value -1 for width or height means image width/height */
if(w == -1) w = width;
if(h == -1) h = width;
/* assure valid coordinates */
if(x+w > width) x = width-w;
if(y+h > height) y = height-h;
if(x<0) x=0;
if(y<0) y=0;
/* find the minimum value in the image */
for(xi=0; (xi<w) && (xi<width); xi++) {
for(yi=0; (yi<h) && (yi<height); yi++) {
imlib_image_query_pixel(xi, yi, &color);
lum = clip(get_lum(&color, lt),0,255);
if(lum > maxval) maxval = lum;
}
}
/* restore image from before function call */
imlib_context_set_image(current_image);
return maxval;
}
/* draw a white (background) border around image, overwriting image contents
+3 -7
View File
@@ -141,13 +141,9 @@ double get_threshold(Imlib_Image *source_image, double fraction, luminance_t lt,
double iterative_threshold(Imlib_Image *source_image, double thresh,
luminance_t lt, int x, int y, int w, int h);
/* get minimum gray value */
double get_minval(Imlib_Image *source_image, int x, int y, int w, int h,
luminance_t lt);
/* get maximum gray value */
double get_maxval(Imlib_Image *source_image, int x, int y, int w, int h,
luminance_t lt);
/* get minimum and maximum gray (luminace) values */
void get_minmaxval(Imlib_Image *source_image, luminance_t lt,
double *min, double *max);
/* compute luminance from RGB values */
int get_lum(Imlib_Color *color, luminance_t lt);
+7 -7
View File
@@ -727,9 +727,10 @@ int main(int argc, char **argv)
/* get minimum and maximum "value" values */
if((flags & DEBUG_OUTPUT) || (flags & PRINT_INFO)) {
double min, max;
get_minmaxval(&image, lt, &min, &max);
fprintf(stderr, "%.2f <= lum <= %.2f (lum should be in [0,255])\n",
get_minval(&image, 0, 0, -1, -1, lt),
get_maxval(&image, 0, 0, -1, -1, lt));
min, max);
}
/* adapt threshold to image */
@@ -989,8 +990,7 @@ int main(int argc, char **argv)
fprintf(stderr, " adjusting T1=%.2f and T2=%.2f to image\n",
t1, t2);
}
min = get_minval(&image, 0, 0, -1, -1, lt);
max = get_maxval(&image, 0, 0, -1, -1, lt);
get_minmaxval(&image, lt, &min, &max);
t1 = min + t1/100.0 * (max - min);
t2 = min + t2/100.0 * (max - min);
if(flags & VERBOSE) {
@@ -1046,10 +1046,10 @@ int main(int argc, char **argv)
}
/* get minimum and maximum "value" values in cropped image */
if((flags&DEBUG_OUTPUT) || (flags&PRINT_INFO) || (flags&VERBOSE)) {
double min, max;
get_minmaxval(&image, lt, &min, &max);
fprintf(stderr, " %.2f <= lum <= %.2f in cropped image"
" (lum should be in [0,255])\n",
get_minval(&image, 0, 0, -1, -1, lt),
get_maxval(&image, 0, 0, -1, -1, lt));
" (lum should be in [0,255])\n", min, max);
}
/* adapt threshold to cropped image */
thresh = adapt_threshold(&image, thresh, lt, 0, 0, -1, -1, flags);