Merge pull request #9778 from dotCooCoo/fix/pdf-cmyk-count

PDF export: the CMYK document warning counts only RGB images written to the file
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-27 21:41:17 -08:00
commit b7cec34427
2 files changed
+65 -5

No files matched your search

+10 -5
View File
@@ -173,11 +173,9 @@ impl Exporter<'_> {
let cmyk_image = blob.is_cmyk();
// RGB images of a CMYK document go to CMYK only with a CMYK destination; grey ones stay
// grey ([`crate::images::recode`]).
if !cmyk_image && self.convert && doc.color_mode == vectorcraft_doc::ColorMode::Cmyk && !crate::images::is_grey(&blob.bytes) {
let to_cmyk = matches!(out.pixels(), Some(crate::output::Pixels::Cmyk(_)));
let set = if to_cmyk { &mut self.rgb_in_cmyk.converted } else { &mut self.rgb_in_cmyk.kept };
set.insert(key.to_string());
}
let rgb_in_cmyk =
!cmyk_image && self.convert && doc.color_mode == vectorcraft_doc::ColorMode::Cmyk && !crate::images::is_grey(&blob.bytes);
let to_cmyk = matches!(out.pixels(), Some(crate::output::Pixels::Cmyk(_)));
let r = if cmyk_image {
let icc = out.cmyk_image_icc(blob);
crate::images::recode_cmyk(blob, size, c, interpolate, out.cmyk_pixels(), None, out.cmyk_tag(icc.as_deref()))
@@ -190,7 +188,14 @@ impl Exporter<'_> {
unconverted = embed(blob, interpolate, self.sixteen_bit, profiles).is_some();
return None;
}
// An RGB image of a CMYK document is counted once it is written: converted when the
// recoded image goes to CMYK, kept when it is written in RGB, as it is or recoded.
let converted = to_cmyk && r.image.is_some();
let img = r.image.or_else(|| embed(blob, interpolate, self.sixteen_bit, profiles));
if rgb_in_cmyk && img.is_some() {
let set = if converted { &mut self.rgb_in_cmyk.converted } else { &mut self.rgb_in_cmyk.kept };
set.insert(key.to_string());
}
// An image left out gets the warning below instead.
for w in [r.warning, r.note].into_iter().flatten().filter(|_| img.is_some()) {
self.warn(w);
+55
View File
@@ -764,3 +764,58 @@ fn the_cmyk_document_warning_says_whether_rgb_images_are_converted() {
assert!(w.contains("1 RGB colour") && w.contains("converted to CMYK") && w.contains("rich blacks"), "{w}");
assert!(w.contains("1 RGB image") && w.contains("written in RGB"), "{w}");
}
/// The CMYK document warning counts an RGB image by what is written: an image left out of the
/// file is not counted, and one whose pixels can't be read for the conversion, and that is written
/// as it is, is counted as written in RGB even with a CMYK destination.
#[test]
fn the_cmyk_document_warning_counts_only_rgb_images_that_are_written() {
let warning = |d: &Document, settings: Value| {
let r = pdf(d, settings);
let w: Vec<_> = r.warnings.iter().filter(|w| w.contains("this CMYK document")).cloned().collect();
assert!(w.len() <= 1, "{w:?}");
(w.into_iter().next(), String::from_utf8_lossy(&r.bytes).into_owned())
};
let to_cmyk = json!({"output": {"conversion": "destination", "destination": GENERIC_CMYK}});
let jpeg_doc = |bytes: Vec<u8>| {
let mut d = image_doc(vec![]);
d.images.insert("img".into(), ImageBlob::new("image/jpeg", bytes));
d.color_mode = ColorMode::Cmyk;
d
};
let mut jpeg = vec![];
let img = image::RgbImage::from_fn(4, 4, |x, y| image::Rgb([200, 30 * x as u8, 40 * y as u8]));
image::DynamicImage::ImageRgb8(img).write_to(&mut std::io::Cursor::new(&mut jpeg), image::ImageFormat::Jpeg).unwrap();
// The start and length of the first segment with marker `m`.
let segment = |b: &[u8], m: u8| {
let p = b.windows(2).position(|w| w == [0xFF, m])?;
Some((p, 2 + usize::from(u16::from_be_bytes([*b.get(p + 2)?, *b.get(p + 3)?]))))
};
// Images that can't be read are left out, with or without a conversion: nothing to count.
// One whose bytes are no image at all, and a color JPEG cut off before its scan.
let mut unread = image_doc(vec![1, 2, 3]);
unread.color_mode = ColorMode::Cmyk;
let (sos, _) = segment(&jpeg, 0xDA).unwrap();
let cut = jpeg_doc([&jpeg[..sos], &[0xFF, 0xD9]].concat());
for (d, settings) in [(&unread, json!({})), (&unread, to_cmyk.clone()), (&cut, json!({})), (&cut, to_cmyk.clone())] {
let (w, text) = warning(d, settings.clone());
assert!(!text.contains("/Subtype/Image"), "{settings}: an image was written");
assert_eq!(w, None, "{settings}");
}
// A color JPEG without its Huffman tables: its pixels can't be decoded for the conversion,
// so its JPEG data is written as it is, in RGB.
let mut untabled = jpeg.clone();
while let Some((p, len)) = segment(&untabled, 0xC4) {
untabled.drain(p..p + len);
}
let (w, text) = warning(&jpeg_doc(untabled), to_cmyk);
assert!(
text.contains("/Subtype/Image") && text.contains("/DCTDecode") && !text.contains("/ColorSpace/DeviceCMYK"),
"the JPEG was not written as it is"
);
let w = w.unwrap();
assert!(w.contains("1 RGB image") && w.contains("written in RGB"), "{w}");
assert!(!w.contains("converted to CMYK"), "{w}");
}