From f2f91db131e7c7df1e87bcd41ae17c07429bbcb8 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Mon, 21 Apr 2008 18:20:16 +0100 Subject: [PATCH] [cairo-png] Create an ARGB32 surface for paletted PNGs. jeremie54 reported a regression in the handling of transparent paletted PNGs beween 1.4.14 and 1.6.4. This was caused by the change to load opaque PNGs into a RGB24 image surface, which made the assumption that indexed PNGs were opaque. However, alpha/transparency in PNG is more complicated, as PNG uses a tRNS chunk to supply transparency data for paletted images and other image types that don't need a full alpha channel. Therefore if the PNG contains a tRNS chunk always generate an ARGB32 surface. --- src/cairo-png.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/cairo-png.c b/src/cairo-png.c index 0db4b0907..3ec85c068 100644 --- a/src/cairo-png.c +++ b/src/cairo-png.c @@ -434,6 +434,7 @@ read_png (png_rw_ptr read_func, unsigned int i; cairo_format_t format; cairo_status_t status; + cairo_bool_t need_alpha; /* XXX: Perhaps we'll want some other error handlers? */ png = png_create_read_struct (PNG_LIBPNG_VER_STRING, @@ -485,8 +486,11 @@ read_png (png_rw_ptr read_func, } /* transform transparency to alpha */ - if (png_get_valid (png, info, PNG_INFO_tRNS)) + need_alpha = FALSE; + if (png_get_valid (png, info, PNG_INFO_tRNS)) { png_set_tRNS_to_alpha (png); + need_alpha = TRUE; + } if (depth == 16) png_set_strip_16 (png); @@ -513,11 +517,16 @@ read_png (png_rw_ptr read_func, break; case PNG_COLOR_TYPE_GRAY: - case PNG_COLOR_TYPE_PALETTE: case PNG_COLOR_TYPE_RGB: - format = CAIRO_FORMAT_RGB24; - png_set_read_user_transform_fn (png, convert_bytes_to_data); - png_set_filler (png, 0xff, PNG_FILLER_AFTER); + case PNG_COLOR_TYPE_PALETTE: + if (need_alpha) { + format = CAIRO_FORMAT_ARGB32; + png_set_read_user_transform_fn (png, premultiply_data); + } else { + format = CAIRO_FORMAT_RGB24; + png_set_read_user_transform_fn (png, convert_bytes_to_data); + png_set_filler (png, 0xff, PNG_FILLER_AFTER); + } break; }