diff --git a/crates/pdf/src/export/images.rs b/crates/pdf/src/export/images.rs index dfaf9a2c7c..7e7dbbd5fc 100644 --- a/crates/pdf/src/export/images.rs +++ b/crates/pdf/src/export/images.rs @@ -246,6 +246,34 @@ 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; + // A color-masked image keeps the colors it hides hidden. + let shown = self.doc.shown_image(n, im); + if !n.is_distorted_image() { + return self.masked_image(im, &shown, 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: shown.key.clone(), width: shown.width, height: shown.height, size: s.size }); + let outer = shared.map(|w| self.warp_size.replace(w)); + let img = self.masked_image(im, &shown, 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 d84e8f7124..1d514a569d 100644 --- a/crates/pdf/src/export/nodes.rs +++ b/crates/pdf/src/export/nodes.rs @@ -373,13 +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). - // A color-masked image keeps the colors it hides hidden. - (NodeKind::Image(im), Some(m)) if n.image_quad() == m.art.image_quad() => { - let shown = self.doc.shown_image(n, im); - self.masked_image(im, &shown, 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 @@ -506,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 8c41809ebd..ef20585154 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 color-masked CMYK image with a gray mask of its grid keeps its inks and its hidden colors /// hidden: the image's `/SMask` is the mask's alpha times the alpha of the pixels the color mask /// shows, so a hidden pixel stays clear where the mask's alpha is not zero, for a distorted image