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.
This commit is contained in:
dotCooCoo committed 2026-12-02 13:26:00 -08:00
1 parent aeddf2f03a
commit 5ce02bb64b
2 files changed
+48 -1

No files matched your search

+13 -1
View File
@@ -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 })
}
+35
View File
@@ -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();