mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
PSD: decode the merged composite only when something consumes it (#846)
Every PSD/PSB open decoded the merged image unconditionally, but a normal layered Photoshop save carries a composite nobody reads: with layers present and nothing behind the colour channels, the decoded planes went straight to the drop. That composite is typically about half the file's bytes, and for ZIP-compressed files the parse-time validation had already inflated the same stream once - so layered ZIP opens paid the composite twice for nothing (P18: opening a 20 MP layered PSD measures 1753 ms p50 against a 1000 ms budget). The composite is now decoded only when a consumer exists: a flattened or multichannel file (the composite IS the image), or extra alpha/spot channels behind the colour ones. Layered files without extra channels skip the decode entirely; everything a layered import shows comes from the layer records either way. The parse-time ZIP validation stays: deferring it would break the corrupt-stream rejection the fuzz tests pin down, and with the decode skipped, layered ZIP opens now inflate the composite once (validation) instead of twice. Co-authored-by: Brandon Thomas <[email protected]>
This commit is contained in:
1 parent
5902c878e5
commit
fdd7141788
2 files changed
+45
-12
No files matched your search
+22
-12
@@ -606,18 +606,28 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt)
|
||||
let (w, hh) = (h.width as usize, h.height as usize);
|
||||
let n = w * hh;
|
||||
let canvas = Rect::new(0, 0, h.width as i32, h.height as i32);
|
||||
let merged = file.decode_merged();
|
||||
if ctl.cancelled() {
|
||||
return None;
|
||||
}
|
||||
ctl.progress(0.1);
|
||||
if let Err(e) = &merged {
|
||||
cx.warn(format!("merged image could not be decoded: {e}"));
|
||||
}
|
||||
// The merged composite is decoded only when something consumes it: a flattened or
|
||||
// multichannel file (it *is* the image), or extra alpha/spot channels behind the colour
|
||||
// ones. A normal layered Photoshop save carries a merged composite nobody reads - roughly
|
||||
// half the file's bytes, decoded and thrown away on every open before this.
|
||||
let extra = usize::from(h.channels) > cc + usize::from(file.merged_has_alpha());
|
||||
let merged = if !layered || file.layers().is_empty() || extra {
|
||||
let m = file.decode_merged();
|
||||
if ctl.cancelled() {
|
||||
return None;
|
||||
}
|
||||
ctl.progress(0.1);
|
||||
if let Err(e) = &m {
|
||||
cx.warn(format!("merged image could not be decoded: {e}"));
|
||||
}
|
||||
Some(m)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if multichannel {
|
||||
// Ink channels only (no layers); see `multichannel_map`.
|
||||
if let Ok(all) = &merged {
|
||||
if let Some(Ok(all)) = &merged {
|
||||
let names = file.resource(1045).map(|r| unicode_names(&r.data)).unwrap_or_default();
|
||||
crate::multichannel_map::import(file, all, &mut doc, &names, &mut cx.warnings);
|
||||
}
|
||||
@@ -634,7 +644,7 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt)
|
||||
apply_link_groups(&tree, &mut doc.layers, &groups);
|
||||
}
|
||||
if doc.layers.is_empty()
|
||||
&& let Ok(all) = &merged
|
||||
&& let Some(Ok(all)) = &merged
|
||||
{
|
||||
// Flattened file: the merged image becomes the background layer.
|
||||
let plane = h.row_bytes() * hh;
|
||||
@@ -698,7 +708,7 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt)
|
||||
}
|
||||
let rgba = file.composite_rgba8().or_else(|_| {
|
||||
// Multichannel: show the first channels as RGB.
|
||||
let all = merged.clone()?;
|
||||
let all = merged.clone().ok_or_else(|| photocraft_psd::PsdError::Invalid("no merged image".into()))??;
|
||||
let plane = h.row_bytes() * hh;
|
||||
let mut data = vec![255u8; n * 4];
|
||||
for c in 0..3.min(usize::from(h.channels)) {
|
||||
@@ -728,7 +738,7 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt)
|
||||
}
|
||||
|
||||
// Extra (alpha / spot) channels of the merged image.
|
||||
if layered && let Ok(all) = &merged {
|
||||
if layered && let Some(Ok(all)) = &merged {
|
||||
let first = cc + usize::from(file.merged_has_alpha());
|
||||
let plane = h.row_bytes() * hh;
|
||||
let names = file.resource(1045).map(|r| unicode_names(&r.data)).unwrap_or_default();
|
||||
|
||||
@@ -290,6 +290,29 @@ fn group_nesting_at_the_cap_imports() {
|
||||
assert!(!r.warnings.iter().any(|w| w.contains("deeper")), "{:?}", r.warnings);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layered_import_skips_the_unused_merged_composite() {
|
||||
use photocraft_psd::{LayerSpec, PixelData, PsdBuilder};
|
||||
// A layered file whose merged composite carries nothing behind the colour channels never
|
||||
// decodes it - that composite is roughly half a Photoshop save's bytes. Here its data is
|
||||
// garbage: the layered import does not touch it, while a file with an extra channel does
|
||||
// decode (and warn), which is what makes the skip observable.
|
||||
let mut b = PsdBuilder::new(4, 4);
|
||||
b.push_layer(LayerSpec::new("a", 0, 0, 2, 2, PixelData::Rgba8(vec![7u8; 16])));
|
||||
b.composite(PixelData::Rgba8(vec![128; 64]));
|
||||
let mut f = photocraft_psd::PsdFile::from_bytes(&b.to_bytes().unwrap()).unwrap();
|
||||
f.image_data.data.clear();
|
||||
f.layer_info.as_mut().unwrap().merged_alpha = true;
|
||||
let (d, warnings) = psd_to_document(&f);
|
||||
assert_eq!(d.layers.len(), 1);
|
||||
assert!(!warnings.iter().any(|w| w.contains("merged image")), "an unused composite is not decoded: {warnings:?}");
|
||||
// A real extra channel behind the colour ones is part of the composite: it is decoded,
|
||||
// and the garbage above surfaces as a warning.
|
||||
f.layer_info.as_mut().unwrap().merged_alpha = false;
|
||||
let (_, warnings) = psd_to_document(&f);
|
||||
assert!(warnings.iter().any(|w| w.contains("merged image")), "{warnings:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn locks_and_labels_from_psd() {
|
||||
use photocraft_psd::TaggedBlock;
|
||||
|
||||
Reference in new issue
Block a user