diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index 699bcb5e225..c7b50fd4174 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -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(); diff --git a/crates/io/tests/psd_structure.rs b/crates/io/tests/psd_structure.rs index 9cc3e48f9ed..5ec4cb680e6 100644 --- a/crates/io/tests/psd_structure.rs +++ b/crates/io/tests/psd_structure.rs @@ -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;