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 <[email protected]>
This commit is contained in:
Jason DandClaude Opus 5.5 committed 2026-10-22 00:48:24 -04:00
1 parent 9ec668e86d
commit 6e3fe3dd9e
3 files changed
+22 -11

No files matched your search

+17 -10
View File
@@ -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]
+4
View File
@@ -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()))),
}
+1 -1
View File
@@ -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)));