diff --git a/crates/pdf/src/export/images.rs b/crates/pdf/src/export/images.rs index 70cc868c23..a91e6f8a0f 100644 --- a/crates/pdf/src/export/images.rs +++ b/crates/pdf/src/export/images.rs @@ -140,7 +140,8 @@ impl Exporter<'_> { let out = self.out.clone(); let profiles = !out.standard.cmyk_only(); let then = out.converts_tagged_pixels().then_some(&*out); - let to_srgb = out.image_srgb_intent().map(|intent| crate::images::ToSrgb { intent, sixteen_bit: self.sixteen_bit, then }); + let grey = out.converts_grey_images(); + let to_srgb = out.image_srgb_intent().map(|intent| crate::images::ToSrgb { intent, sixteen_bit: self.sixteen_bit, then, grey }); let img = doc.image_blob(im).filter(|_| !self.left_out.contains(key)).and_then(|blob| { let cmyk_image = blob.is_cmyk(); if !cmyk_image && self.convert && doc.color_mode == vectorcraft_doc::ColorMode::Cmyk { diff --git a/crates/pdf/src/images.rs b/crates/pdf/src/images.rs index 2f81d84571..c578c23176 100644 --- a/crates/pdf/src/images.rs +++ b/crates/pdf/src/images.rs @@ -335,6 +335,9 @@ pub(crate) struct ToSrgb<'a> { /// `convert`): to an RGB destination whose untagged images keep their numbers (Preserve /// Numbers). Its table is built when the first such image needs it. pub then: Option<&'a crate::output::ColorOut>, + /// Gray images are converted too: with an sRGB destination + /// ([`crate::output::ColorOut::converts_grey_images`]). In the others they keep their profile. + pub grey: bool, } /// The ICC profile image `bytes` carries ([`source_icc`]), when it is a well-formed profile of @@ -484,8 +487,9 @@ fn resample_channels(img: DynamicImage, r: &Rule, (w, h): (u32, u32)) -> Dynamic /// they carry when `profiles` allows it (not in PDF/X-1a, which allows no ICC-based color spaces); /// with `to_srgb`, pixels in a gray or RGB profile other than sRGB are converted from it to sRGB /// instead ([`ToSrgb`]), and colour ones then by `convert`, or without it by the conversion -/// `to_srgb` gives ([`ToSrgb::then`]); grey ones that either leaves as they are keep their profile. `interpolate` lets viewers smooth it. An image that can't be -/// decoded is kept as it is, with a warning when its pixels were to be converted to sRGB. +/// `to_srgb` gives ([`ToSrgb::then`]); grey ones keep their profile unless the destination is +/// sRGB ([`ToSrgb::grey`]). `interpolate` lets viewers smooth it. An image that can't be decoded +/// is kept as it is, with a warning when its pixels were to be converted to sRGB. pub(crate) fn recode( bytes: &[u8], size: (f64, f64), @@ -503,9 +507,9 @@ pub(crate) fn recode( use ExtendedColorType as T; let grey = matches!(decoder.original_color_type(), T::L1 | T::La1 | T::L2 | T::La2 | T::L4 | T::La4 | T::L8 | T::La8 | T::L16 | T::La16); // Pixels in a profile they are converted from are decoded, whatever the Compression settings. - // Grey pixels, which `convert` leaves as they are, keep their profile when it converts. + // Grey pixels keep their profile unless the destination is sRGB ([`ToSrgb::grey`]). let then = to_srgb.and_then(|t| t.then); - let to_srgb = to_srgb.filter(|_| !(grey && (convert.is_some() || then.is_some()))); + let to_srgb = to_srgb.filter(|t| !grey || t.grey); // Converted to the destination as sRGB, as before, when the profile can't be read. let mut note = None; let source = match to_srgb.and_then(|t| Some((source_profile(bytes, if grey { *b"GRAY" } else { *b"RGB " })?, t))) { diff --git a/crates/pdf/src/output.rs b/crates/pdf/src/output.rs index dce763e9d6..d1365dc8d9 100644 --- a/crates/pdf/src/output.rs +++ b/crates/pdf/src/output.rs @@ -367,6 +367,13 @@ impl ColorOut { (rgb || self.pixels().is_some()).then_some(self.intent) } + /// Are gray images that carry an ICC profile converted from it to sRGB grays too + /// ([`Self::image_srgb_intent`])? Only with an sRGB destination; in the others, RGB tagged as + /// sRGB included, grey pixels are not converted and keep their profile. + pub(crate) fn converts_grey_images(&self) -> bool { + self.dest.as_ref().is_some_and(|d| d.model == Model::Rgb && d.srgb) + } + /// The destination a colour image converted from its ICC profile to sRGB /// ([`Self::image_srgb_intent`]) is then converted to when other colour images are not /// ([`Self::pixels`]): with Preserve Numbers, an RGB destination other than sRGB, whose diff --git a/crates/pdf/src/tests/compression.rs b/crates/pdf/src/tests/compression.rs index 8ad11de270..871ab42bcb 100644 --- a/crates/pdf/src/tests/compression.rs +++ b/crates/pdf/src/tests/compression.rs @@ -776,7 +776,7 @@ fn convert_to_srgb_converts_tagged_images_from_their_profile() { png }; let c: CompressionSettings = serde_json::from_value(json!({})).unwrap(); - let srgb_out = Some(crate::images::ToSrgb { intent: Intent::RelativeColorimetric, sixteen_bit: true, then: None }); + let srgb_out = Some(crate::images::ToSrgb { intent: Intent::RelativeColorimetric, sixteen_bit: true, then: None, grey: true }); for (what, png) in [("RGB", declared(&colour(), &p3)), ("gray", declared(&colour().grayscale(), &gray))] { let r = crate::images::recode(&png, (100.0, 100.0), &c, false, None, true, srgb_out); assert!(r.image.is_none() && r.warning.is_some_and(|w| w.contains("not converted to sRGB")), "{what}: {:?}", r.warning); @@ -1069,16 +1069,18 @@ fn preserve_numbers_converts_tagged_images_from_their_profile() { let samples = image_samples(&pdf); let want = converted_samples(&img, |rgb| from_p3(rgb).to_vec()); assert!(samples.len() == 1 && samples_close(&samples[0], &want, false, 1), "tagged as sRGB, {}", max_diff(&samples[0], &want)); - // A gray image keeps its gray profile, in a CMYK and in an RGB destination. + // A gray image keeps its gray profile, in a CMYK and in an RGB destination, and in an RGB one + // with RGB colours ICC-tagged as sRGB (`all` profiles, PDF/A-2b). let gray = gray_profile(&[0x01CD]); let gray_doc = doc(tagged(&colour().grayscale(), &gray, ImageFormat::Tiff), "image/tiff"); - for destination in [GENERIC_CMYK, WIDE_GAMUT_RGB] { - let settings = json!({"output": {"conversion": "preserveNumbers", "destination": destination}}); + for (what, settings) in [ + (GENERIC_CMYK, json!({"output": {"conversion": "preserveNumbers", "destination": GENERIC_CMYK}})), + (WIDE_GAMUT_RGB, json!({"output": {"conversion": "preserveNumbers", "destination": WIDE_GAMUT_RGB}})), + ("all profiles", json!({"output": {"conversion": "preserveNumbers", "destination": WIDE_GAMUT_RGB, "profiles": "all"}})), + ("PDF/A-2b", json!({"standard": "pdfA2b", "output": {"conversion": "preserveNumbers", "destination": WIDE_GAMUT_RGB}})), + ] { let (spaces, _) = cms::with_active(active.clone(), || export_images(&gray_doc, settings)); - assert!( - spaces.len() == 1 && spaces[0].space == "ICCBased" && spaces[0].icc.as_deref() == Some(gray.as_slice()), - "gray, {destination}: {spaces:?}" - ); + assert!(spaces.len() == 1 && spaces[0].space == "ICCBased" && spaces[0].icc.as_deref() == Some(gray.as_slice()), "gray, {what}: {spaces:?}"); } // In an sRGB destination it is converted to sRGB grays and written without it. let settings = json!({"output": {"conversion": "preserveNumbers", "destination": SRGB}});