diff --git a/crates/pdf/src/export.rs b/crates/pdf/src/export.rs index 0efa1942c..54e871928 100644 --- a/crates/pdf/src/export.rs +++ b/crates/pdf/src/export.rs @@ -122,6 +122,14 @@ impl CoverageImage { Some(Self { grey: Arc::new(grey), size }) } + /// The coverage as a grey JPEG (quality 90), for exports that compress images. + fn jpeg(&self, interpolate: bool) -> Option { + let img = craft_image_codecs::Image::from_u8(self.size.0, self.size.1, craft_image_codecs::ChannelLayout::Gray, self.grey.to_vec()).ok()?; + let options = craft_image_codecs::EncodeOptions { jpeg_quality: 90, ..Default::default() }; + let buf = craft_image_codecs::encode(&img, craft_image_codecs::Format::Jpeg, &options).ok()?; + Image::from_jpeg(buf.into(), interpolate).ok() + } + /// Coverage given directly, one byte per pixel; `None` when the size doesn't match. fn from_grey(grey: Arc>, size: (u32, u32)) -> Option { let n = usize::try_from(u64::from(size.0) * u64::from(size.1)).ok()?; diff --git a/crates/pdf/src/export/graphics.rs b/crates/pdf/src/export/graphics.rs index 609061121..5710101e2 100644 --- a/crates/pdf/src/export/graphics.rs +++ b/crates/pdf/src/export/graphics.rs @@ -344,7 +344,8 @@ impl Exporter<'_> { /// A greyscale or bitmap image coloured by the swatch applied to it (#1094): the swatch is /// painted through the image's ink coverage (its darkness), over 0% ink for a greyscale /// image, whose white is opaque. `false`, drawing nothing, when the image keeps its own - /// colours: no swatch, one that no longer exists, a colour image. + /// colours: no swatch, one that no longer exists, a colour image. With `compress_images` the + /// coverage is a grey JPEG, as the image itself would be. fn colorized(&mut self, s: &mut Surface, g: &designcraft_doc::Graphic) -> bool { if g.fill.is_none() { return false; @@ -354,7 +355,8 @@ impl Exporter<'_> { let Some(paint) = self.swatch_color(&g.fill.swatch, g.fill.tint) else { return false }; let Some(coverage) = designcraft_images::decode_rgba(&bytes).and_then(|img| CoverageImage::new(&img)) else { return false }; let interpolate = RasterMode::for_standard(self.opts.standard).interpolate; - let Ok(mask) = Image::from_custom(coverage, interpolate) else { return false }; + let mask = if self.opts.compress_images && tone == designcraft_images::Tone::Gray { coverage.jpeg(interpolate) } else { None }; + let Some(mask) = mask.or_else(|| Image::from_custom(coverage, interpolate).ok()) else { return false }; let size = Size::from_wh(g.size.0.max(1e-3) as f32, g.size.1.max(1e-3) as f32); let (Some(size), Some(rect)) = (size, to_path(&Rect::new(0.0, 0.0, g.size.0, g.size.1).to_path(0.1))) else { return false }; s.push_transform(&tf(g.xf)); diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index 45090648a..42ff51a4e 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -1178,3 +1178,37 @@ fn an_item_a_parent_overrode_and_deleted_is_not_exported() { std::sync::Arc::make_mut(&mut d.parents[1]).pages[0].overridden.push(box_id); assert!(!has_box(&d), "removed on B, so the page using B exports no box"); } + +/// Compress Images left a greyscale photo coloured by a swatch, as catalogues place them, at full +/// size and lossless: the swatch is painted through the photo's coverage, which went in as a +/// Flate image of every pixel whatever the option said. +#[test] +fn compressed_images_include_greyscale_tiffs_coloured_by_a_swatch() { + let px: Vec = (0..32u32).flat_map(|y| (0..64u32).map(move |x| ((x * 7 + y * 3) % 256) as u8)).collect(); + let mut tif = Vec::new(); + tiff::encoder::TiffEncoder::new(std::io::Cursor::new(&mut tif)) + .unwrap() + .with_compression(tiff::encoder::Compression::Lzw) + .write_image::(64, 32, &px) + .unwrap(); + for compress_images in [true, false] { + let mut d = doc_with_text("x"); + d.swatches.push(Swatch { + name: "PANTONE Test".into(), + value: SwatchValue::Color { color: Color::cmyk(1.0, 0.5, 0.0, 0.0), color_type: ColorType::Spot }, + locked: false, + named: true, + hidden: false, + }); + place(&mut d, tif.clone(), (64, 32)); + let id = d.spreads[0].items.last().unwrap().id; + if let designcraft_doc::Content::Graphic(g) = &mut d.item_mut(id).unwrap().content { + g.fill = Fill::swatch("PANTONE Test"); + } + let r = export_pdf_with_report(&d, &Cache::new(), &PdfOptions { compress_images, compress: false, ..Default::default() }).unwrap(); + assert!(r.warnings.is_empty(), "{:?}", r.warnings); + let s = String::from_utf8_lossy(&r.bytes); + assert!(s.contains("/PANTONE#20Test"), "still on the spot plate"); + assert_eq!(s.contains("/DCTDecode"), compress_images, "the coverage is a JPEG only when images are compressed"); + } +}