From 5132eaebfeb33043584dbbc92f734a1e9708f476 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 19:42:31 -0800 Subject: [PATCH] PDF: a distorted CMYK image with an image mask keeps the resolution its pieces need A distorted CMYK image whose opacity mask is a gray image with the same corners writes the mask as the image's /SMask, and every piece of its mesh draws that one image. The image was resampled for the size its transform places it at, before the pieces' shared size was known, so with downsampling on every piece stretched a smaller image across the corners. The masked image is now resampled for the size the pieces share, as the same image without a mask is, and the pieces are drawn from the same expansion they were measured on. --- crates/pdf/src/export/images.rs | 25 ++++++++++++++++ crates/pdf/src/export/nodes.rs | 6 ++-- crates/pdf/src/tests/cmykimages.rs | 46 ++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/crates/pdf/src/export/images.rs b/crates/pdf/src/export/images.rs index 3da2079b9b..b43e758377 100644 --- a/crates/pdf/src/export/images.rs +++ b/crates/pdf/src/export/images.rs @@ -223,6 +223,31 @@ impl Exporter<'_> { img } + /// [`Self::masked_image`] of image `im` of node `n`. A distorted image's is resampled for the + /// size every piece of its mesh shares ([`Self::warp_image_size`]), since each piece draws this + /// one image; its expanded art, whose pieces were measured, is left in `expanded` for drawing. + pub(super) fn node_masked_image( + &mut self, + n: &Node, + im: &vectorcraft_doc::ImageObject, + m: &vectorcraft_doc::OpacityMask, + expanded: &mut Option, + ) -> Option { + let interpolate = !n.extras().pixelated; + if !n.is_distorted_image() { + return self.masked_image(im, m, interpolate); + } + let g = expanded.insert(vectorcraft_effects::expand_live_deep(Some(self.doc), n)); + let pieces = g.children().and_then(|c| c.first()).map_or(&[][..], |art| vectorcraft_doc::image_quad::distorted_pieces(art)); + let shared = self.warp_image_size(pieces).map(|s| SharedSize { key: im.key.clone(), width: im.width, height: im.height, size: s.size }); + let outer = shared.map(|w| self.warp_size.replace(w)); + let img = self.masked_image(im, m, interpolate); + if let Some(outer) = outer { + self.warp_size = outer; + } + img + } + /// Draw image `im`: `img` when given (its pixels already written), else as it is loaded; with /// `pixelated` ([`vectorcraft_doc::NodeExtras::pixelated`]) viewers are asked not to smooth it. pub(super) fn image(&mut self, s: &mut Surface, im: &vectorcraft_doc::ImageObject, img: Option, pixelated: bool) { diff --git a/crates/pdf/src/export/nodes.rs b/crates/pdf/src/export/nodes.rs index 94b1c819c5..1d514a569d 100644 --- a/crates/pdf/src/export/nodes.rs +++ b/crates/pdf/src/export/nodes.rs @@ -373,9 +373,11 @@ impl Exporter<'_> { let enclosing = std::mem::replace(&mut self.knockout, knockout); let (mut pushes, isolated_children) = transparency(s, n, page, container, knockout); let mask = n.extras().mask.as_deref().filter(|m| !m.disabled); + // A distorted image's art, when its masked image needs the size its pieces share. + let mut expanded = None; let alpha = match (&n.kind, mask) { // The mask's image shares the pixel grid only when it has the same corners (or none). - (NodeKind::Image(im), Some(m)) if n.image_quad() == m.art.image_quad() => self.masked_image(im, m, !n.extras().pixelated), + (NodeKind::Image(im), Some(m)) if n.image_quad() == m.art.image_quad() => self.node_masked_image(n, im, m, &mut expanded), _ => None, }; if let Some(m) = mask @@ -502,7 +504,7 @@ impl Exporter<'_> { | NodeKind::PlacedDocument(_) | NodeKind::Intertwine { .. } | NodeKind::Image(_) => { - let g = vectorcraft_effects::expand_live_deep(Some(self.doc), n); + let g = expanded.unwrap_or_else(|| vectorcraft_effects::expand_live_deep(Some(self.doc), n)); // The pieces draw the image as it is shown, under its color mask's key. let piece = match (&n.kind, &alpha) { (NodeKind::Image(im), Some(img)) => Some((self.doc.shown_image(n, im).key.clone(), img.clone())), diff --git a/crates/pdf/src/tests/cmykimages.rs b/crates/pdf/src/tests/cmykimages.rs index b5f6185ea9..83f9340dc0 100644 --- a/crates/pdf/src/tests/cmykimages.rs +++ b/crates/pdf/src/tests/cmykimages.rs @@ -1210,6 +1210,52 @@ fn a_distorted_cmyk_image_and_its_grid_mask_with_the_same_corners_write_it_as_it } } +/// A distorted CMYK image masked on its pixel grid is resampled for the size its mesh's pieces +/// share, as it is without the mask, and not for the smaller size its transform places it at: +/// every piece draws the one image, so a smaller one would lower the resolution of every piece. +#[test] +fn a_distorted_cmyk_image_with_its_grid_mask_is_resampled_for_the_size_of_its_pieces() { + let side = 400u32; + let cmyk_widths = |masked: bool| { + let mut d = Document::new_with_mode(400.0, 400.0, ColorMode::Cmyk); + let inks = Inks::new(side, side, flat([0u8, 102, 255, 0], (side * side) as usize)).unwrap(); + d.images.insert("img".into(), ImageBlob::cmyk_tiff(&inks).unwrap()); + let alpha = image::GrayImage::from_fn(side, side, |x, y| image::Luma([((x + y) % 256) as u8])); + d.images.insert("alpha".into(), ImageBlob::new("image/png", png(alpha))); + let image = |key: &str| { + NodeKind::Image(Box::new(ImageObject { + key: key.into(), + width: side, + height: side, + // 50 pt: smaller than the corners below. + xf: Affine::scale(0.125), + link: None, + placement: Default::default(), + pixels: None, + })) + }; + let q = [(0.0, 0.0), (150.0, 0.0), (125.0, 150.0), (25.0, 150.0)].map(|(x, y)| Point::new(x, y)); + let mut n = Node::new(d.alloc_id(), image("img")); + if masked { + let mut art = Node::new(d.alloc_id(), image("alpha")); + art.set_image_corners(q).unwrap(); + n = n.with_mask(Some(Box::new(vectorcraft_doc::OpacityMask::new(art, true)))); + } + n.set_image_corners(q).unwrap(); + let l = d.layers[0].id; + d.insert(Some(l), 0, n).unwrap(); + let s = json!({"compression": {"color": {"downsample": "bicubic", "ppi": 72.0, "abovePpi": 100.0}}}); + let out = export_with_report(&d, &opts(s)).unwrap(); + let w = written(&out.bytes); + assert_eq!(w.iter().filter(|w| w.space == "DeviceCMYK" && w.smask).count(), usize::from(masked), "{w:?}"); + w.iter().filter(|w| w.space == "DeviceCMYK").map(|w| w.width).collect::>() + }; + let plain = cmyk_widths(false); + assert_eq!(plain.len(), 1, "one image for every piece: {plain:?}"); + assert!(plain[0] > 100 && plain[0] < side, "resampled for the pieces, not for the transform's 50 pt: {plain:?}"); + assert_eq!(cmyk_widths(true), plain); +} + /// A grey mask image with a color mask masks the CMYK image with the pixels it shows: its hidden /// pixels are clear in the image's `/SMask`. #[test]