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.
@@ -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;
