Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make gAMA and cHRM fallback optional for sRGB #547

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading