Skip to content

Commit

Permalink
Make gAMA and cHRM fallback optional for sRGB
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Dec 8, 2024
1 parent 2232f83 commit 1b6d5ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
25 changes: 21 additions & 4 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,17 @@ impl Info<'_> {
.raw_row_length_from_width(self.bit_depth, width)
}

/// Mark the image data as conforming to the SRGB color space with the specified rendering intent.
///
/// Any ICC profiles will be ignored.
///
/// Source gamma and chromaticities will be written only if they're set to fallback
/// values specified in [11.3.2.5](https://www.w3.org/TR/png-3/#sRGB-gAMA-cHRM).
pub(crate) fn set_source_srgb(&mut self, rendering_intent: SrgbRenderingIntent) {
self.srgb = Some(rendering_intent);
self.icc_profile = None;
}

/// Encode this header to the writer.
///
/// Note that this does _not_ include the PNG signature, it starts with the IHDR chunk and then
Expand Down Expand Up @@ -753,11 +764,17 @@ impl Info<'_> {

// If specified, the sRGB information overrides the source gamma and chromaticities.
if let Some(srgb) = &self.srgb {
let gamma = crate::srgb::substitute_gamma();
let chromaticities = crate::srgb::substitute_chromaticities();
srgb.encode(&mut w)?;
gamma.encode_gama(&mut w)?;
chromaticities.encode(&mut w)?;

// gAMA and cHRM are optional, for backwards compatibility
let srgb_gamma = crate::srgb::substitute_gamma();
if Some(srgb_gamma) == self.source_gamma {
srgb_gamma.encode_gama(&mut w)?
}
let srgb_chromaticities = crate::srgb::substitute_chromaticities();
if Some(srgb_chromaticities) == self.source_chromaticities {
srgb_chromaticities.encode(&mut w)?;
}
} else {
if let Some(gma) = self.source_gamma {
gma.encode_gama(&mut w)?
Expand Down
18 changes: 16 additions & 2 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,23 @@ impl<'a, W: Write> Encoder<'a, W> {
/// Mark the image data as conforming to the SRGB color space with the specified rendering intent.
///
/// Matching source gamma and chromaticities chunks are added automatically.
/// Any manually specified source gamma or chromaticities will be ignored.
/// Any manually specified source gamma, chromaticities, or ICC profiles will be ignored.
#[doc(hidden)]
#[deprecated(note = "use set_source_srgb")]
pub fn set_srgb(&mut self, rendering_intent: super::SrgbRenderingIntent) {
self.info.srgb = Some(rendering_intent);
self.info.set_source_srgb(rendering_intent);
self.info.source_gamma = Some(crate::srgb::substitute_gamma());
self.info.source_chromaticities = Some(crate::srgb::substitute_chromaticities());
}

/// Mark the image data as conforming to the SRGB color space with the specified rendering intent.
///
/// Any ICC profiles will be ignored.
///
/// Source gamma and chromaticities will be written only if they're set to fallback
/// values specified in [11.3.2.5](https://www.w3.org/TR/png-3/#sRGB-gAMA-cHRM).
pub fn set_source_srgb(&mut self, rendering_intent: super::SrgbRenderingIntent) {
self.info.set_source_srgb(rendering_intent);
}

/// Start encoding by writing the header data.
Expand Down

0 comments on commit 1b6d5ca

Please sign in to comment.