From f012c14d938acdc7306230dfbcf9041449bec103 Mon Sep 17 00:00:00 2001 From: Erik Auerswald Date: Sun, 2 Feb 2025 20:32:48 +0100 Subject: [PATCH] refactoring and type consistency fixes * The draw_pixel() function was called with an "image" parameter of type "Imlib_Image" instead of "Imlib_Image *". This type error did not result in a compilation error, and thus stayed undetected in the code. * Introduce a new function draw_color_pixel() similar to draw_pixel(), and use it instead of repeatedly open-coding this operation. --- imgproc.c | 60 +++++++++++++++++++++++++++---------------------------- imgproc.h | 3 +++ ssocr.c | 27 +++++++++++++------------ 3 files changed, 46 insertions(+), 44 deletions(-) diff --git a/imgproc.c b/imgproc.c index 036fef2..14d20a9 100644 --- a/imgproc.c +++ b/imgproc.c @@ -84,7 +84,7 @@ void draw_pixel(Imlib_Image *image, int x, int y, fg_bg_t color) Imlib_Image *current_image; /* save current image */ current_image = imlib_context_get_image(); - imlib_context_set_image(image); + imlib_context_set_image(*image); ssocr_set_color(color); imlib_image_draw_pixel(x,y,0); imlib_context_set_image(current_image); @@ -102,6 +102,18 @@ void draw_bg_pixel(Imlib_Image *image, int x, int y) draw_pixel(image, x, y, BG); } +/* draw a pixel of a given color */ +void draw_color_pixel(Imlib_Image *image, int x, int y, Imlib_Color color) +{ + Imlib_Image *current_image; /* save current image */ + + current_image = imlib_context_get_image(); + imlib_context_set_image(*image); + imlib_context_set_color(color.red, color.green, color.blue, color.alpha); + imlib_image_draw_pixel(x, y, 0); + imlib_context_set_image(current_image); +} + /* check if a pixel is set regarding current foreground/background colors */ int is_pixel_set(int value, double threshold) { @@ -168,9 +180,9 @@ Imlib_Image set_pixels_filter(Imlib_Image *source_image, double thresh, } /* set pixel if at least mask pixels around it are set */ if(set_pixel >= mask) { - draw_fg_pixel(new_image, x, y); + draw_fg_pixel(&new_image, x, y); } else { - draw_bg_pixel(new_image, x, y); + draw_bg_pixel(&new_image, x, y); } } } @@ -282,9 +294,9 @@ Imlib_Image keep_pixels_filter(Imlib_Image *source_image, double thresh, /* set pixel if at least mask pixels around it are set */ /* mask = 1 keeps all pixels */ if(set_pixel > mask) { - draw_fg_pixel(new_image, x, y); + draw_fg_pixel(&new_image, x, y); } else { - draw_bg_pixel(new_image, x, y); + draw_bg_pixel(&new_image, x, y); } } } @@ -399,9 +411,9 @@ Imlib_Image dynamic_threshold(Imlib_Image *source_image,double t,luminance_t lt, lum = get_lum(&color, lt); thresh = get_threshold(source_image, t/100.0, lt, x-ww/2, y-ww/2, ww, wh); if(is_pixel_set(lum, thresh)) { - draw_fg_pixel(new_image, x, y); + draw_fg_pixel(&new_image, x, y); } else { - draw_bg_pixel(new_image, x, y); + draw_bg_pixel(&new_image, x, y); } } } @@ -438,9 +450,9 @@ Imlib_Image make_mono(Imlib_Image *source_image, double thresh, luminance_t lt) imlib_image_query_pixel(x, y, &color); lum = get_lum(&color, lt); if(is_pixel_set(lum, thresh)) { - draw_fg_pixel(new_image, x, y); + draw_fg_pixel(&new_image, x, y); } else { - draw_bg_pixel(new_image, x, y); + draw_bg_pixel(&new_image, x, y); } } } @@ -754,15 +766,11 @@ Imlib_Image shear(Imlib_Image *source_image, int offset) /* copy pixels */ for(x=width-1; x>=shift; x--) { imlib_image_query_pixel(x-shift, y, &color_return); - imlib_context_set_image(new_image); - imlib_context_set_color(color_return.red, color_return.green, - color_return.blue, color_return.alpha); - imlib_image_draw_pixel(x,y,0); - imlib_context_set_image(*source_image); + draw_color_pixel(&new_image, x, y, color_return); } /* fill with background */ for(x=0; x= 0) && (sx <= width) && (sy >= 0) && (sy <= height)) { imlib_image_query_pixel(sx, sy, &c); - imlib_context_set_image(new_image); - imlib_context_set_color(c.red, c.green, c.blue, c.alpha); + draw_color_pixel(&new_image, x, y, c); } else { - imlib_context_set_image(new_image); - ssocr_set_color(BG); + draw_bg_pixel(&new_image, x, y); } - imlib_image_draw_pixel(x,y,0); - imlib_context_set_image(*source_image); } } @@ -844,20 +848,14 @@ Imlib_Image mirror(Imlib_Image *source_image, direction_t direction) for(x = width-1; x>=0; x--) { for(y = 0; y < height; y++) { imlib_image_query_pixel(width - 1 - x, y, &c); - imlib_context_set_image(new_image); - imlib_context_set_color(c.red, c.green, c.blue, c.alpha); - imlib_image_draw_pixel(x,y,0); - imlib_context_set_image(*source_image); + draw_color_pixel(&new_image, x, y, c); } } } else if(direction == VERTICAL) { for(x = 0; x < width; x++) { for(y = height-1; y >= 0; y--) { imlib_image_query_pixel(x, height - 1 - y, &c); - imlib_context_set_image(new_image); - imlib_context_set_color(c.red, c.green, c.blue, c.alpha); - imlib_image_draw_pixel(x,y,0); - imlib_context_set_image(*source_image); + draw_color_pixel(&new_image, x, y, c); } } } @@ -932,9 +930,9 @@ Imlib_Image invert(Imlib_Image *source_image, double thresh, luminance_t lt) imlib_image_query_pixel(x, y, &color); lum = get_lum(&color, lt); if(is_pixel_set(lum, thresh)) { - draw_bg_pixel(new_image, x, y); + draw_bg_pixel(&new_image, x, y); } else { - draw_fg_pixel(new_image, x, y); + draw_fg_pixel(&new_image, x, y); } } } diff --git a/imgproc.h b/imgproc.h index 711320e..991c574 100644 --- a/imgproc.h +++ b/imgproc.h @@ -40,6 +40,9 @@ void draw_fg_pixel(Imlib_Image *image, int x, int y); /* draw a background pixel */ void draw_bg_pixel(Imlib_Image *image, int x, int y); +/* draw a pixel of a given color */ +void draw_color_pixel(Imlib_Image *image, int x, int y, Imlib_Color color); + /* check if a pixel is set regarding current foreground/background colors */ int is_pixel_set(int value, double threshold); diff --git a/ssocr.c b/ssocr.c index e6e6cc7..00b1a09 100644 --- a/ssocr.c +++ b/ssocr.c @@ -114,16 +114,20 @@ static char * tmp_imgfile(unsigned int flags) } /* return number of foreground pixels in a scanline */ -static unsigned int scanline(Imlib_Image *image, Imlib_Image *debug_image, +static unsigned int scanline(Imlib_Image *debug_image, int x, int y, int len, direction_t dir, color_struct d_color, double thresh, luminance_t lt, unsigned int flags) { - Imlib_Color imlib_color; + Imlib_Color imlib_color, debug_color; int lum, i, ix=x, iy=y, start, end; unsigned int found_pixels = 0; start = (dir == HORIZONTAL) ? x : y; end = start + len; + debug_color.red = d_color.R; + debug_color.green = d_color.G; + debug_color.blue = d_color.B; + debug_color.alpha = d_color.A; for (i = start; i <= end; i++) { if (dir == HORIZONTAL) ix = i; else iy = i; @@ -131,10 +135,7 @@ static unsigned int scanline(Imlib_Image *image, Imlib_Image *debug_image, lum = get_lum(&imlib_color, lt); if(is_pixel_set(lum, thresh)) { if(flags & USE_DEBUG_IMAGE) { - imlib_context_set_image(*debug_image); - imlib_context_set_color(d_color.R, d_color.G, d_color.B, d_color.A); - imlib_image_draw_pixel(ix, iy, 0); - imlib_context_set_image(*image); + draw_color_pixel(debug_image, ix, iy, debug_color); } found_pixels++; } @@ -1607,14 +1608,14 @@ int main(int argc, char **argv) /* check horizontal segments (vertical scan, x == middle) */ d_color.R = d_color.A = 255; d_color.G = d_color.B = 0; - found_pixels = scanline(&image, &debug_image, middle, digits[d].y1, + found_pixels = scanline(&debug_image, middle, digits[d].y1, d_height/3, VERTICAL, d_color, thresh, lt, flags); if(found_pixels >= need_pixels) { digits[d].digit |= HORIZ_UP; /* add upper segment */ } d_color.G = d_color.A = 255; d_color.R = d_color.B = 0; - found_pixels = scanline(&image, &debug_image, middle, + found_pixels = scanline(&debug_image, middle, digits[d].y1 + d_height/3, d_height/3, VERTICAL, d_color, thresh, lt, flags); if(found_pixels >= need_pixels) { @@ -1622,7 +1623,7 @@ int main(int argc, char **argv) } d_color.B = d_color.A = 255; d_color.R = d_color.G = 0; - found_pixels = scanline(&image, &debug_image, middle, + found_pixels = scanline(&debug_image, middle, digits[d].y1 + 2*d_height/3, d_height/3, VERTICAL, d_color, thresh, lt, flags); if(found_pixels >= need_pixels) { @@ -1631,7 +1632,7 @@ int main(int argc, char **argv) /* check upper vertical segments (horizontal scan, y == quarter) */ d_color.R = d_color.A = 255; d_color.G = d_color.B = 0; - found_pixels = scanline(&image, &debug_image, digits[d].x1, quarter, + found_pixels = scanline(&debug_image, digits[d].x1, quarter, (digits[d].x2 - digits[d].x1) / 2, HORIZONTAL, d_color, thresh, lt, flags); if (found_pixels >= need_pixels) { @@ -1639,7 +1640,7 @@ int main(int argc, char **argv) } d_color.G = d_color.A = 255; d_color.R = d_color.B = 0; - found_pixels = scanline(&image, &debug_image, + found_pixels = scanline(&debug_image, (digits[d].x1 + digits[d].x2) / 2 + 1, quarter, (digits[d].x2 - digits[d].x1) / 2 - 1, HORIZONTAL, d_color, thresh, lt, flags); @@ -1649,7 +1650,7 @@ int main(int argc, char **argv) /* check lower vertical segments (horizontal scan, y == three_quarters) */ d_color.R = d_color.A = 255; d_color.G = d_color.B = 0; - found_pixels = scanline(&image, &debug_image, digits[d].x1, + found_pixels = scanline(&debug_image, digits[d].x1, three_quarters, (digits[d].x2 - digits[d].x1) / 2, HORIZONTAL, d_color, thresh, lt, flags); if (found_pixels >= need_pixels) { @@ -1657,7 +1658,7 @@ int main(int argc, char **argv) } d_color.G = d_color.A = 255; d_color.R = d_color.B = 0; - found_pixels = scanline(&image, &debug_image, + found_pixels = scanline(&debug_image, (digits[d].x1 + digits[d].x2) / 2 + 1, three_quarters, (digits[d].x2-digits[d].x1)/2 - 1, HORIZONTAL, d_color, thresh, lt, flags);