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.
This commit is contained in:
Erik Auerswald
2025-02-02 20:32:48 +01:00
parent b44a4ad72a
commit f012c14d93
3 changed files with 46 additions and 44 deletions
+29 -31
View File
@@ -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<shift; x++) {
draw_bg_pixel(new_image, x, y);
draw_bg_pixel(&new_image, x, y);
}
}
@@ -803,14 +811,10 @@ Imlib_Image rotate(Imlib_Image *source_image, double theta)
sy = (y-height/2) * cos(theta) - (x-width/2) * sin(theta) + height/2;
if((sx >= 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);
}
}
}
+3
View File
@@ -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);
+14 -13
View File
@@ -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);