From b71d5dff571f70066319a20e49a771bb9b0816b1 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sun, 23 Jan 2022 00:26:22 +0100 Subject: Make truncated WebP parts always transparent --- fiv-io.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/fiv-io.c b/fiv-io.c index f141879..fa2da7a 100644 --- a/fiv-io.c +++ b/fiv-io.c @@ -1281,18 +1281,35 @@ load_libwebp_nonanimated(WebPDecoderConfig *config, const WebPData *wd, } VP8StatusCode err = WebPIUpdate(idec, wd->bytes, wd->size); + cairo_surface_mark_dirty(surface); + int x = 0, y = 0, w = 0, h = 0; + (void) WebPIDecodedArea(idec, &x, &y, &w, &h); WebPIDelete(idec); - if (err == VP8_STATUS_SUSPENDED) { - g_warning("partial WebP"); - } else if (err) { + if (err == VP8_STATUS_OK) + return surface; + + if (err != VP8_STATUS_SUSPENDED) { g_set_error(error, FIV_IO_ERROR, FIV_IO_ERROR_OPEN, "%s: %s", "WebP decoding error", load_libwebp_error(err)); cairo_surface_destroy(surface); return NULL; } - cairo_surface_mark_dirty(surface); - return surface; + g_warning("partial WebP"); + if (config->input.has_alpha) + return surface; + + // Always use transparent black, rather than opaque black. + cairo_surface_t *masked = cairo_image_surface_create( + CAIRO_FORMAT_ARGB32, config->input.width, config->input.height); + cairo_t *cr = cairo_create(masked); + cairo_set_source_surface(cr, surface, 0, 0); + cairo_rectangle(cr, x, y, w, h); + cairo_clip(cr); + cairo_paint(cr); + cairo_destroy(cr); + cairo_surface_destroy(surface); + return masked; } static cairo_surface_t * -- cgit v1.2.3-70-g09d2