From a46fdbf014e084d0fbdce38b4f0c246c2779d9fe Mon Sep 17 00:00:00 2001 From: Kornel Date: Wed, 18 Dec 2024 18:09:24 +0000 Subject: [PATCH] Don't use source_ fields for output --- src/common.rs | 18 ++++++++++++++++++ src/decoder/stream.rs | 16 ++-------------- src/encoder.rs | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/common.rs b/src/common.rs index 18b80884..9751e4e8 100644 --- a/src/common.rs +++ b/src/common.rs @@ -731,6 +731,24 @@ impl Info<'_> { .raw_row_length_from_width(self.bit_depth, width) } + /// Gamma dependent on sRGB chunk + pub fn gamma(&self) -> Option { + if self.srgb.is_some() { + Some(crate::srgb::substitute_gamma()) + } else { + self.gama_chunk + } + } + + /// Chromaticities dependent on sRGB chunk + pub fn chromaticities(&self) -> Option { + if self.srgb.is_some() { + Some(crate::srgb::substitute_chromaticities()) + } else { + self.chrm_chunk + } + } + /// Mark the image data as conforming to the SRGB color space with the specified rendering intent. /// /// Any ICC profiles will be ignored. diff --git a/src/decoder/stream.rs b/src/decoder/stream.rs index 88044b74..a2296783 100644 --- a/src/decoder/stream.rs +++ b/src/decoder/stream.rs @@ -1315,11 +1315,6 @@ impl StreamingDecoder { }; info.chrm_chunk = Some(source_chromaticities); - // Ignore chromaticities if sRGB profile is used. - if info.srgb.is_none() { - info.source_chromaticities = Some(source_chromaticities); - } - Ok(Decoded::Nothing) } } @@ -1340,11 +1335,6 @@ impl StreamingDecoder { let source_gamma = ScaledFloat::from_scaled(source_gamma); info.gama_chunk = Some(source_gamma); - // Ignore chromaticities if sRGB profile is used. - if info.srgb.is_none() { - info.source_gamma = Some(source_gamma); - } - Ok(Decoded::Nothing) } } @@ -1368,8 +1358,6 @@ impl StreamingDecoder { // Set srgb and override source gamma and chromaticities. info.srgb = Some(rendering_intent); - info.source_gamma = Some(crate::srgb::substitute_gamma()); - info.source_chromaticities = Some(crate::srgb::substitute_chromaticities()); Ok(Decoded::Nothing) } } @@ -1846,7 +1834,7 @@ mod tests { fn trial(path: &str, expected: Option) { let decoder = crate::Decoder::new(File::open(path).unwrap()); let reader = decoder.read_info().unwrap(); - let actual: Option = reader.info().source_gamma; + let actual: Option = reader.info().gamma(); assert!(actual == expected); } trial("tests/pngsuite/f00n0g08.png", None); @@ -1887,7 +1875,7 @@ mod tests { fn trial(path: &str, expected: Option) { let decoder = crate::Decoder::new(File::open(path).unwrap()); let reader = decoder.read_info().unwrap(); - let actual: Option = reader.info().source_chromaticities; + let actual: Option = reader.info().chromaticities(); assert!(actual == expected); } trial( diff --git a/src/encoder.rs b/src/encoder.rs index 0f687131..dc3bfc5b 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -2183,7 +2183,7 @@ mod tests { let decoder = crate::Decoder::new(Cursor::new(buffer)); let mut reader = decoder.read_info()?; assert_eq!( - reader.info().source_gamma, + reader.info().gamma(), gamma, "Deviation with gamma {:?}", gamma