From 2e54b294508eb1be63bcc3b94a6fd1f823699df9 Mon Sep 17 00:00:00 2001 From: Simon Lees Date: Thu, 22 Feb 2024 16:12:33 +1030 Subject: [PATCH] lua-imlib2: Fix alpha mask so that its scaled Previously if the source image was a different to the displayed image the alpha channel was incorrect, as it wasn't scaled leading to a series of artifacts. --- lua/libcairo_imlib2_helper.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lua/libcairo_imlib2_helper.h b/lua/libcairo_imlib2_helper.h index f5730ae3..59c23d0f 100644 --- a/lua/libcairo_imlib2_helper.h +++ b/lua/libcairo_imlib2_helper.h @@ -33,7 +33,7 @@ void cairo_draw_image(const char *file, cairo_surface_t *cs, int x, int y, double *return_scale_h) { int w, h, stride; double scaled_w, scaled_h; - Imlib_Image premul; + Imlib_Image alpha, premul; cairo_surface_t *result; cairo_t *cr; Imlib_Image *image = imlib_load_image(file); @@ -64,6 +64,9 @@ void cairo_draw_image(const char *file, cairo_surface_t *cs, int x, int y, return; } + /* create scaled version of image to later extract the alpha channel */ + alpha = imlib_create_cropped_scaled_image (0, 0, w, h, scaled_w, scaled_h); + /* create temporary image */ premul = imlib_create_image(scaled_w, scaled_h); if (!premul) { @@ -81,7 +84,7 @@ void cairo_draw_image(const char *file, cairo_surface_t *cs, int x, int y, imlib_blend_image_onto_image(image, 0, 0, 0, w, h, 0, 0, scaled_w, scaled_h); /* and use the alpha channel of the source image */ - imlib_image_copy_alpha_to_image(image, 0, 0); + imlib_image_copy_alpha_to_image(alpha, 0, 0); stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, scaled_w);