From 5ce02bb64bd7aae18ea65c6b90f2bb8c82bdd6a9 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 2 Dec 2026 13:26:00 -0800 Subject: [PATCH] Flatten Transparency: write rasterized areas as fast-compressed PNGs Since #7669 the flattener's rasters have been encoded through the shared image codec with its default compression (zlib level 6). EPS export, and any other export that flattens, encodes every rasterized area this way and then decodes it again right away, so a document with many live effects exported about 170 ms slower to EPS. The rasters are now written with PngCompression::Fast, as the SVG filter raster already is (crates/effects/src/svg_filter.rs). The pixels and the EPS output bytes do not change. --- crates/engine/src/cmd/flatten/apply.rs | 14 ++++++++++- crates/engine/src/tests/flatten.rs | 35 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/flatten/apply.rs b/crates/engine/src/cmd/flatten/apply.rs index fe1e183d16..8a884b67ea 100644 --- a/crates/engine/src/cmd/flatten/apply.rs +++ b/crates/engine/src/cmd/flatten/apply.rs @@ -692,7 +692,19 @@ fn render_raster(doc: &Document, art: &[Node], rect: Rect, ppi: f64, clip: Optio } } } - let png = img.to_png().map_err(|e| EngineError::Other(format!("Flatten Transparency: {e}")))?; + // The PNG is written with fast compression: the exports that flatten (EPS, PDF 1.3) decode it + // again right away. + let raw = craft_image_codecs::Image::from_raw( + img.width, + img.height, + craft_image_codecs::ChannelLayout::Rgba, + craft_image_codecs::SampleType::U8, + img.to_straight(), + ) + .map_err(|e| EngineError::Other(format!("Flatten Transparency: {e}")))?; + let opts = craft_image_codecs::EncodeOptions { png_compression: craft_image_codecs::PngCompression::Fast, ..Default::default() }; + let png = craft_image_codecs::encode(&raw, craft_image_codecs::Format::Png, &opts) + .map_err(|e| EngineError::Other(format!("Flatten Transparency: {e}")))?; Ok(Raster { blob: ImageBlob::new("image/png", png), width: img.width, height: img.height, xf, clip }) } diff --git a/crates/engine/src/tests/flatten.rs b/crates/engine/src/tests/flatten.rs index 042f741e0e..959c58e9f3 100644 --- a/crates/engine/src/tests/flatten.rs +++ b/crates/engine/src/tests/flatten.rs @@ -350,6 +350,41 @@ fn gradients_rasterize_and_flat_areas_stay_vector() { } } +/// A rasterized area is stored as a PNG written with fast compression: its zlib header (RFC 1950) +/// gives compression level 0, the fastest. +#[test] +fn rasterized_areas_are_png_with_fast_compression() { + let _settings = settings_lock(); + let mut s = session(); + let g = rect(&mut s, 10.0, 10.0, 100.0, 100.0, "#000000"); + run( + &mut s, + "paint.setFill", + json!({"ids": [g], "gradient": {"kind": "linear", "stops": [{"offset": 0, "color": "#ff0000"}, {"offset": 1, "color": "#0000ff"}]}}), + ); + run(&mut s, "transparency.set", json!({"ids": [g], "opacity": 40})); + let (ids, _, rasterized) = flatten(&mut s, json!({"ids": [g], "preset": "high"})); + assert_eq!(rasterized, 1); + let mut keys = vec![]; + node(&s, ids[0]).walk(&mut |c| { + if let NodeKind::Image(im) = &c.kind { + keys.push(im.key.clone()); + } + }); + let [key] = &keys[..] else { panic!("one image: {keys:?}") }; + let png = &s.doc().unwrap().doc.images[key].bytes; + assert!(png.starts_with(b"\x89PNG\r\n\x1a\n"), "a PNG"); + let mut at = 8; + let level = loop { + let (Some(len), Some(kind)) = (png.get(at..at + 4), png.get(at + 4..at + 8)) else { panic!("a PNG with image data") }; + if kind == b"IDAT" { + break png[at + 9] >> 6; + } + at += 12 + u32::from_be_bytes(len.try_into().unwrap()) as usize; + }; + assert_eq!(level, 0, "zlib FLEVEL of the stored PNG"); +} + #[test] fn a_long_rasterized_area_is_placed_whole() { let _settings = settings_lock();