From 6e3fe3dd9e3f0b147cc6512b4e4b1300a7e2455a Mon Sep 17 00:00:00 2001 From: Jason D Date: Wed, 21 Oct 2026 16:26:58 -0400 Subject: [PATCH] io: drop 1053 on PSD open; never copy a kept one on save Every save now writes 1053 for the extra channels, so a copy kept on open grew the document's metadata on each round trip (the roundtrip and tiff_layers suites caught it), as #7415 found. Multichannel keeps its 1053. The layered writer never copies a kept 1053: next to the fresh one it would be a second, and without channels it would list channels that are gone. Co-Authored-By: Claude Opus 5.5 --- crates/io/src/psd_export.rs | 27 +++++++++++++++++---------- crates/io/src/psd_import.rs | 4 ++++ crates/io/src/tiff_layers.rs | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index daa25ca2bc..256f578236 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -1826,8 +1826,10 @@ fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32, c if !keep_imported_resource(doc, *id, data) { continue; } - // A fresh 1053 was written with the extra channels; a preserved copy would be a second one. - if *id == crate::channel_map::ALPHA_IDENTIFIERS && !extra.is_empty() { + // 1053 is written afresh with the extra channels. A kept copy (from a Multichannel file, or + // a document saved by an earlier version) would be a second one, or list channels that + // are gone. + if *id == crate::channel_map::ALPHA_IDENTIFIERS { continue; } let mut r = ImageResource::new(*id, data.to_vec()); @@ -2030,20 +2032,25 @@ mod tests { assert_eq!(alpha_ids(&file), Some(vec![0, 3, 4])); } - /// A Photoshop file's own 1053 is kept on open (as every unmapped resource is), but a save - /// writes one fresh 1053 for the channels the document has, never the kept one as well. The + /// A save writes one fresh 1053 for the channels the document has. A Photoshop file's own is + /// not kept on open (it would be new metadata after every round trip), and one a document + /// carries anyway (kept by an earlier version) is never written: next to the fresh one it + /// would be a second, and without channels it would list channels that are gone. The /// identifiers here, 3 and 2, are those of psd-tools' `cmyk-alpha-spot.psd` (Photoshop 24.7). #[test] - fn a_kept_alpha_identifiers_resource_is_not_written_twice() { + fn a_kept_alpha_identifiers_resource_is_not_written() { + let kept = || (crate::channel_map::ALPHA_IDENTIFIERS, String::new(), std::sync::Arc::new([0, 0, 0, 3, 0, 0, 0, 2].to_vec())); let mut file = document_to_psd(&spot_and_alpha_document()); file.resources.retain(|r| r.id != crate::channel_map::ALPHA_IDENTIFIERS); - file.resources.push(ImageResource::new(crate::channel_map::ALPHA_IDENTIFIERS, [0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 2].to_vec())); - let opened = crate::import("ps.psd", &file.to_bytes().unwrap()).unwrap().document; - assert!(opened.metadata.psd_resources.iter().any(|(id, ..)| *id == crate::channel_map::ALPHA_IDENTIFIERS)); - let saved = crate::export(&opened, "x.psd", &crate::ExportOptions::default()).unwrap(); - let again = PsdFile::from_bytes(&saved.bytes).unwrap(); + file.resources.push(ImageResource::new(kept().0, kept().2.to_vec())); + let mut opened = crate::import("ps.psd", &file.to_bytes().unwrap()).unwrap().document; + assert!(!opened.metadata.psd_resources.iter().any(|(id, ..)| *id == crate::channel_map::ALPHA_IDENTIFIERS)); + opened.metadata.psd_resources.push(kept()); + let again = PsdFile::from_bytes(&crate::export(&opened, "x.psd", &crate::ExportOptions::default()).unwrap().bytes).unwrap(); assert_eq!(again.resources.iter().filter(|r| r.id == crate::channel_map::ALPHA_IDENTIFIERS).count(), 1); assert_eq!(alpha_ids(&again), Some(vec![0, 3, 4])); + opened.channels.clear(); + assert_eq!(alpha_ids(&document_to_psd(&opened)), None, "no channels left"); } #[test] diff --git a/crates/io/src/psd_import.rs b/crates/io/src/psd_import.rs index 0fa5470f4b..fbfa691c62 100644 --- a/crates/io/src/psd_import.rs +++ b/crates/io/src/psd_import.rs @@ -873,6 +873,10 @@ pub(crate) fn psd_to_document_decoded( doc.clipping_path = Some(photocraft_doc::ClippingPath { name, flatness: 0.0 }); doc.metadata.psd_resources.push((r.id, String::from_utf8_lossy(&r.name).into_owned(), Arc::new(r.data.clone()))); } + // Resource 1053 is written afresh with the extra channels on every save, so a kept copy + // would be new metadata after each round trip. Multichannel has no such rewrite: the + // imported bytes are the channel ids, copied back while they still match. + crate::channel_map::ALPHA_IDENTIFIERS if !multichannel => {} id if MAPPED_RESOURCES.contains(&id) => {} id => doc.metadata.psd_resources.push((id, String::from_utf8_lossy(&r.name).into_owned(), Arc::new(r.data.clone()))), } diff --git a/crates/io/src/tiff_layers.rs b/crates/io/src/tiff_layers.rs index 2764ad83ce..401495a1ec 100644 --- a/crates/io/src/tiff_layers.rs +++ b/crates/io/src/tiff_layers.rs @@ -296,7 +296,7 @@ pub(crate) fn export_layered(doc: &Document, opts: &ExportOptions, ctl: &photocr // it, so a plain transparent TIFF still opens with alpha. let write_alpha = has_alpha && kept == 0; // One identifier per extra sample actually stored. The PSD model's 1053 lists the merged - // transparency too, and a kept one lists an opened file's channels, so neither is copied. + // transparency too, which the IFD leaves out next to extra samples, so it is not copied. resources.retain(|r| r.id != crate::channel_map::ALPHA_IDENTIFIERS); if kept > 0 { resources.push(ImageResource::new(crate::channel_map::ALPHA_IDENTIFIERS, crate::channel_map::alpha_identifiers(false, kept)));