From 7a1970fc9dea52480291e31e2fd2edc84332ad1e Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Mon, 18 Sep 2023 20:36:44 -0700 Subject: [PATCH 1/2] Add PngDecoder::gamma_value method --- src/codecs/png.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/codecs/png.rs b/src/codecs/png.rs index 9384451ce1..c3d81dac01 100644 --- a/src/codecs/png.rs +++ b/src/codecs/png.rs @@ -193,6 +193,27 @@ impl PngDecoder { Ok(PngDecoder { color_type, reader }) } + /// Returns the gamma value of the image or None if no gamma value is indicated. + /// + /// If an sRGB chunk is present this method returns a gamma value of 0.45455 and ignores the + /// value in the gAMA chunk. This is the recommended behavior according to the PNG standard: + /// + /// > When the sRGB chunk is present, [...] decoders that recognize the sRGB chunk but are not + /// > capable of colour management are recommended to ignore the gAMA and cHRM chunks, and use + /// > the values given above as if they had appeared in gAMA and cHRM chunks. + pub fn gamma_value(&self) -> ImageResult> { + let gamma = if self.reader.info().srgb.is_some() { + Some(0.45455) + } else { + self.reader + .info() + .gama_chunk + .map(|x| x.into_scaled() as f64 / 100000.0) + }; + + Ok(gamma) + } + /// Turn this into an iterator over the animation frames. /// /// Reading the complete animation requires more memory than reading the data from the IDAT From b8437dc4c081dd480ee53d6f9ff9e446c06d8d11 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Tue, 19 Sep 2023 18:37:37 -0700 Subject: [PATCH 2/2] Simplify PngDecoder::gamma_value to use source_gamma field --- src/codecs/png.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/codecs/png.rs b/src/codecs/png.rs index c3d81dac01..b3b1e5e575 100644 --- a/src/codecs/png.rs +++ b/src/codecs/png.rs @@ -202,16 +202,11 @@ impl PngDecoder { /// > capable of colour management are recommended to ignore the gAMA and cHRM chunks, and use /// > the values given above as if they had appeared in gAMA and cHRM chunks. pub fn gamma_value(&self) -> ImageResult> { - let gamma = if self.reader.info().srgb.is_some() { - Some(0.45455) - } else { - self.reader - .info() - .gama_chunk - .map(|x| x.into_scaled() as f64 / 100000.0) - }; - - Ok(gamma) + Ok(self + .reader + .info() + .source_gamma + .map(|x| x.into_scaled() as f64 / 100000.0)) } /// Turn this into an iterator over the animation frames.