From e57ee674ae01670440d6ec85980a8db35111ea6e Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Sat, 10 Oct 2026 21:42:00 +0200 Subject: [PATCH] PSD: warn when the Lr16/Lr32 layer block is unreadable, save one layer list (#2700) (#2709) A 16/32-bit file whose global Lr16/Lr32/Layr block failed to parse opened as a single Background layer from the merged image with no warning: the parse error was discarded in PsdFile::from_bytes. The raw block stayed in the preserved global blocks, so a PSD save wrote the document's fresh Lr16 plus the unreadable original: two layer lists in one file. - psd: PsdFile::unreadable_layer_block() reports the kept block and its parse error. - io import: a warning names the block and the error and says only the merged image was opened. - io export: a preserved Lr16/Lr32/Layr block is not re-emitted when the document's own layers are written. Co-authored-by: Claude Opus 5.5 --- crates/io/src/psd_export.rs | 22 ++++++++++++++++++++++ crates/io/src/psd_import.rs | 9 +++++++++ crates/psd/src/file.rs | 12 ++++++++++++ 3 files changed, 43 insertions(+) diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index 23580283ca9..b872019c90d 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -1475,6 +1475,12 @@ fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32) - if txt2.is_some() && key == *b"Txt2" { continue; } + // A kept layer block (one the import could not read, #2700) describes layers the + // document doesn't have. The document's own layers are written below, and a file holds + // one layer list: a second `Lr16`/`Lr32` would contradict it. + if !ex.records.is_empty() && matches!(&key, b"Lr16" | b"Lr32" | b"Layr") { + continue; + } let mut tb = TaggedBlock::new(key, data.to_vec()); tb.signature = sig; // Photoshop pads document-level (global) blocks to a multiple of 4, and readers such as @@ -1584,6 +1590,22 @@ mod tests { Document::new("size estimate", photocraft_geom::Size::new(width, height), ColorMode::Rgb, SampleType::U8) } + #[test] + fn unreadable_lr16_warns_on_import_and_is_not_saved_twice() { + // #2700: a 16-bit file whose Lr16 block claims five layers but holds none. + let mut file = photocraft_psd::testgen::merged_only(Version::Psd, PsdMode::Rgb, 16, Compression::Raw, 8, 6); + file.global_layer_mask = Some(GlobalLayerMask::default()); + file.global_blocks.push(TaggedBlock::new(*b"Lr16", vec![0, 5, 0, 0])); + let imported = crate::import("broken.psd", &file.to_bytes().unwrap()).unwrap(); + assert!(imported.warnings.iter().any(|w| w.contains("Lr16") && w.contains("could not be read")), "{:?}", imported.warnings); + + let saved = PsdFile::from_bytes(&document_to_psd(&imported.document).to_bytes().unwrap()).unwrap(); + let ours = matches!(saved.layer_info_placement, LayerInfoPlacement::GlobalBlock { key, .. } if key == *b"Lr16"); + let kept = saved.global_blocks.iter().filter(|b| b.key == *b"Lr16").count(); + assert_eq!((ours, kept), (true, 0), "exactly one Lr16 block: the document's own layers"); + assert_eq!(saved.layers().len(), 1); + } + #[test] fn photo_filter_psd_is_four_byte_aligned_and_round_trips() { let color = [60000u16, 30000, 0].map(|x| x as f32 / 65535.0); diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index a61c1909b64..67f62cf473a 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -608,6 +608,15 @@ pub fn psd_to_document_with(file: &PsdFile, ctl: &photocraft_raster::Interrupt) for b in &file.global_blocks { doc.metadata.psd_global_blocks.push((b.signature, b.key, Arc::new(b.data.clone()))); } + // A 16/32-bit file whose layer block can't be parsed would otherwise open as a flattened + // file without a word (#2700). The raw block is kept, but a PSD save writes the document's + // own layers in its place (see `psd_export`). + if let Some((b, e)) = file.unreadable_layer_block() { + warnings.push(format!( + "the layers ({} block) could not be read ({e}); only the merged image was opened. Saving as PSD replaces the unreadable layer data", + b.key_str() + )); + } doc.patterns = crate::pattern_map::from_global_blocks(&doc); // Notes (`Anno`) and the measurement scale (resource 1074); raw data stays for verbatim export. doc.notes = crate::annotations_map::notes_from_blocks(&doc); diff --git a/crates/psd/src/file.rs b/crates/psd/src/file.rs index ef15813885b..7425dc8a949 100644 --- a/crates/psd/src/file.rs +++ b/crates/psd/src/file.rs @@ -207,6 +207,18 @@ impl PsdFile { self.resources.iter().find(|r| r.id == id) } + /// The global `Lr16` / `Lr32` / `Layr` block that should have held the layers but could not + /// be parsed, with the parse error. [`Self::from_bytes`] keeps such a block verbatim in + /// [`Self::global_blocks`] and leaves [`Self::layer_info`] `None`, so the file reads as + /// flattened; this tells a reader that its layers were lost rather than absent. + pub fn unreadable_layer_block(&self) -> Option<(&TaggedBlock, PsdError)> { + if self.layer_info.is_some() { + return None; + } + let block = self.global_blocks.iter().find(|b| matches!(&b.key, b"Lr16" | b"Lr32" | b"Layr"))?; + LayerInfo::read_body(&block.data, self.header.version).err().map(|e| (block, e)) + } + /// First global block with the given key. pub fn global_block(&self, key: &[u8; 4]) -> Option<&TaggedBlock> { self.global_blocks.iter().find(|b| &b.key == key)