From 78318b30e5d31dafc2a9544ff3cb684560499809 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Fri, 5 Jan 2018 04:34:50 -0800 Subject: [PATCH] PNG decoder: handle gAMA chunk Apply gamma correction to the decoded RGB values. This handles corner cases where the PNG file doesn't have a standard 1/2.2 gamma value. BUG=webp:369 Change-Id: I9907b6e2c458002de7c26d0b9e416278cca33990 --- imageio/pngdec.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/imageio/pngdec.c b/imageio/pngdec.c index 22e60f3c..46223533 100644 --- a/imageio/pngdec.c +++ b/imageio/pngdec.c @@ -185,7 +185,6 @@ static int ExtractMetadataFromPNG(png_structp png, } } } - return 1; } @@ -265,6 +264,16 @@ int ReadPNG(const uint8_t* const data, size_t data_size, has_alpha = !!(color_type & PNG_COLOR_MASK_ALPHA); } + // Apply gamma correction if needed. + { + double image_gamma = 1 / 2.2, screen_gamma = 2.2; + int srgb_intent; + if (png_get_sRGB(png, info, &srgb_intent) || + png_get_gAMA(png, info, &image_gamma)) { + png_set_gamma(png, screen_gamma, image_gamma); + } + } + if (!keep_alpha) { png_set_strip_alpha(png); has_alpha = 0;