From 3397155da4e615cc93fb96a05404d5d882d4927f Mon Sep 17 00:00:00 2001 From: Simon Lees Date: Thu, 22 Feb 2024 10:57:05 +1030 Subject: [PATCH] lua-imlib2: Improve Error handling Each of the following cases cause large numbers of cryptic imlib2 prints, so detect them and print a single error for each. --- lua/libcairo_imlib2_helper.h | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/lua/libcairo_imlib2_helper.h b/lua/libcairo_imlib2_helper.h index a1b39ba2..62be3fbc 100644 --- a/lua/libcairo_imlib2_helper.h +++ b/lua/libcairo_imlib2_helper.h @@ -36,19 +36,40 @@ void cairo_draw_image(const char *file, cairo_surface_t *cs, int x, int y, Imlib_Image premul; cairo_surface_t *result; cairo_t *cr; - auto image = imlib_load_image(file); - if (!image) { return; } + Imlib_Image *image = imlib_load_image(file); + if (!image) { + printf("Error: CairoImageHelper: Couldn't load %s\n", file); + return; + } + + if ((scale_x <= 0.0) && (scale_y <= 0.0)) { + printf("Error: CairoImageHelper: Image Scale is 0, %s\n", file); + return; + } imlib_context_set_image(image); w = imlib_image_get_width(); h = imlib_image_get_height(); + if ((w <= 0) && (h <= 0)) { + printf("Error: CairoImageHelper: %s has 0 size\n", file); + return; + } + scaled_w = *return_scale_w = scale_x * (double)w; scaled_h = *return_scale_h = scale_y * (double)h; + if ((scaled_w <= 0.0) && (scaled_h <= 0.0)) { + printf("Error: CairoImageHelper: %s scaled image has 0 size\n", file); + return; + } + /* create temporary image */ premul = imlib_create_image(scaled_w, scaled_h); - /* FIXME: add error handling */ + if (!premul) { + printf("Error: CairoImageHelper: Couldn't create premul image for %s\n", file); + return; + } /* fill with opaque black */ imlib_context_set_image(premul);