From 478c986972792531ccc6669bc4a57dde80a1956f Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Wed, 14 Oct 2026 18:04:46 +1100 Subject: [PATCH 1/4] IO: open .psd/.psb/.psdt through the PSD reader, not codecs (#5258) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit File › Open sent Photoshop extensions that lacked an 8BPS prefix (or reached import by name) to photocraft-codecs, which reports "unrecognized image format". Route them like other native formats, and offer a Photoshop filter in the open dialog. --- apps/photocraft-web/src/web.rs | 5 ++++- apps/photocraft/src/services.rs | 8 ++++++++ crates/io/src/lib.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/apps/photocraft-web/src/web.rs b/apps/photocraft-web/src/web.rs index a5c9d8a565..14105cc6c1 100644 --- a/apps/photocraft-web/src/web.rs +++ b/apps/photocraft-web/src/web.rs @@ -362,7 +362,10 @@ fn services(inbox: Inbox) -> Services { let dialog = if let Some(exts) = extensions { rfd::AsyncFileDialog::new().add_filter("Supported Files", &exts) } else { - rfd::AsyncFileDialog::new().add_filter("All Formats", OPEN_EXTS).add_filter("SVG", SVG_EXTS) + rfd::AsyncFileDialog::new() + .add_filter("All Formats", OPEN_EXTS) + .add_filter("Photoshop", &["psd", "psb", "psdt"]) + .add_filter("SVG", SVG_EXTS) }; let answer = if multiple { match dialog.pick_files().await { diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rs index d4a3869f31..e2df191020 100644 --- a/apps/photocraft/src/services.rs +++ b/apps/photocraft/src/services.rs @@ -42,6 +42,7 @@ fn open_filters() -> Vec<(&'static str, Vec)> { vec![ ("All Formats", open_filter_extensions(OPEN_EXTS)), ("PhotoCraft", open_filter_extensions(&["pcraft"])), + ("Photoshop", open_filter_extensions(&["psd", "psb", "psdt"])), ("OpenRaster", open_filter_extensions(&["ora"])), ("Krita", open_filter_extensions(&["kra", "krz"])), ("SVG", open_filter_extensions(SVG_EXTS)), @@ -628,6 +629,9 @@ mod tests { assert!(!globs.iter().any(|glob| matches(glob, "txt"))); let svg_filter = open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts).unwrap_or_default(); assert_eq!(svg_filter, open_filter_extensions(SVG_EXTS)); + let psd_filter = open_filters().into_iter().find(|(name, _)| *name == "Photoshop").map(|(_, exts)| exts).unwrap_or_default(); + assert_eq!(psd_filter, open_filter_extensions(&["psd", "psb", "psdt"])); + assert!(psd_filter.iter().any(|glob| matches(glob, "psd")) && psd_filter.iter().any(|glob| matches(glob, "PSD"))); assert!(svg_filter.iter().any(|glob| matches(glob, "svg")) && svg_filter.iter().any(|glob| matches(glob, "svgz"))); } @@ -637,6 +641,10 @@ mod tests { assert_eq!(open_filter_extensions(OPEN_EXTS), OPEN_EXTS); assert_eq!(open_filter_extensions(&["pcraft"]), ["pcraft"]); assert_eq!(open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts), Some(vec!["svg".to_string(), "svgz".to_string()])); + assert_eq!( + open_filters().into_iter().find(|(name, _)| *name == "Photoshop").map(|(_, exts)| exts), + Some(vec!["psd".to_string(), "psb".to_string(), "psdt".to_string()]) + ); } #[test] diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index 49e4bb60e2..c2c91a10b4 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -230,11 +230,20 @@ impl TgaBits { } } +/// Photoshop document extensions File › Open routes to the PSD reader. +const PSD_EXTS: &[&str] = &["psd", "psb", "psdt"]; + /// `true` if `bytes` start with the PSD/PSB signature. pub fn is_psd(bytes: &[u8]) -> bool { bytes.starts_with(b"8BPS") } +/// PSD/PSB/PSDT by magic or by a declared Photoshop extension so File › Open does not +/// send `.psd` through the generic image codecs (`codec: unrecognized image format`, #5258). +fn is_psd_input(name: &str, bytes: &[u8]) -> bool { + is_psd(bytes) || PSD_EXTS.iter().any(|e| has_extension(name, e)) +} + /// Imports a file. PSD/PSB, Affinity containers and camera raws are detected by magic; everything /// else is decoded with `photocraft-codecs`. pub fn import(name: &str, bytes: &[u8]) -> Result { @@ -272,7 +281,7 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, m if has_extension(name, photocraft_format::EXTENSION) || photocraft_format::is_pcraft(bytes) { return Ok(ImportResult { document: photocraft_format::load_from_bytes(bytes)?, warnings: Vec::new(), source_read_only: false, preview_only: false }); } - if is_psd(bytes) { + if is_psd_input(name, bytes) { let file = PsdFile::from_bytes(bytes)?; // Nesting past the document model's cap could never be saved; reject the file with the // actionable limit. @@ -516,7 +525,7 @@ pub fn merged_composite(file: &PsdFile) -> Result, IoError> { #[cfg(test)] mod tests { - use super::is_svg_input; + use super::{import, is_psd_input, is_svg_input}; #[test] fn svg_extension_does_not_override_another_format_signature() { @@ -526,4 +535,18 @@ mod tests { assert!(is_svg_input("drawing.svgz", &[0x1f, 0x8b, 0, 0])); assert!(!is_svg_input("drawing.svgz", webp_header)); } + + #[test] + fn photoshop_extension_reaches_the_psd_loader() { + // A declared .psd/.PSB/.psdt must not fall through to codecs (#5258). + for name in ["photo.psd", "dir/Photo.PSD", r"C:\w\a.PSB", "card.psdt"] { + assert!(is_psd_input(name, b"not a signature"), "{name}"); + } + assert!(!is_psd_input("photo.png", b"not a signature")); + assert!(is_psd_input("renamed.png", b"8BPS")); + let err = import("photo.psd", b"not a photoshop file").expect_err("named .psd is a PSD parse, not a codec miss"); + let msg = err.to_string(); + assert!(msg.starts_with("PSD:"), "{msg}"); + assert!(!msg.contains("unrecognized image format"), "{msg}"); + } } From f7842751803cf39432946c463ba0cf4b718f477b Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Wed, 14 Oct 2026 18:34:48 +1100 Subject: [PATCH 2/4] Open Affinity files renamed .psd with the Affinity loader. Magic 8BPS still goes to the PSD reader. A .psd suffix without that signature is PSD only after Affinity (and other native) magic is ruled out, so a renamed Affinity preview does not fail as an invalid PSD. --- crates/io/src/lib.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index c2c91a10b4..7c67ab4970 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -276,26 +276,35 @@ pub fn import_with_svg_group_depth_and_interrupt( Ok(r) } +fn import_psd(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt) -> Result { + let file = PsdFile::from_bytes(bytes)?; + // Nesting past the document model's cap could never be saved; reject the file with the + // actionable limit. + psd_import::check_group_depth(&file)?; + ctl.check().map_err(|_| IoError::Cancelled)?; + ctl.progress(0.05); + let (mut document, warnings) = psd_import::psd_to_document_with(&file, ctl).ok_or(IoError::Cancelled)?; + document.name = name.to_string(); + text_import::prepare(&mut document); + Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false }) +} + fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, max_group_depth: usize) -> Result { // A declared native extension must reach its loader so malformed bundles retain format errors. if has_extension(name, photocraft_format::EXTENSION) || photocraft_format::is_pcraft(bytes) { return Ok(ImportResult { document: photocraft_format::load_from_bytes(bytes)?, warnings: Vec::new(), source_read_only: false, preview_only: false }); } - if is_psd_input(name, bytes) { - let file = PsdFile::from_bytes(bytes)?; - // Nesting past the document model's cap could never be saved; reject the file with the - // actionable limit. - psd_import::check_group_depth(&file)?; - ctl.check().map_err(|_| IoError::Cancelled)?; - ctl.progress(0.05); - let (mut document, warnings) = psd_import::psd_to_document_with(&file, ctl).ok_or(IoError::Cancelled)?; - document.name = name.to_string(); - text_import::prepare(&mut document); - return Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false }); + // Magic first: an Affinity file renamed .psd is still Affinity (#5258 corpus + // preview_never_saves_back_to_its_native_or_renamed_source). + if is_psd(bytes) { + return import_psd(name, bytes, ctl); } if affinity::is_affinity(bytes) || affinity::has_extension(name) { return affinity::import(name, bytes); } + if PSD_EXTS.iter().any(|e| has_extension(name, e)) { + return import_psd(name, bytes, ctl); + } if ora::is_ora(bytes) || has_extension(name, "ora") { return ora::import(name, bytes, ctl); } @@ -548,5 +557,7 @@ mod tests { let msg = err.to_string(); assert!(msg.starts_with("PSD:"), "{msg}"); assert!(!msg.contains("unrecognized image format"), "{msg}"); + // A renamed Affinity file keeps the Affinity loader (magic before the .psd suffix). + assert!(!super::is_psd(b"\0\xffKA")); } } From 26cdfae307c3aed3d6464862bba50bdf0eac2515 Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Thu, 15 Oct 2026 22:55:05 +1100 Subject: [PATCH 3/4] IO: route declared Photoshop extensions through is_psd_input. The helpers-only use made cargo clippy -p photocraft-io --all-targets flag is_psd_input as never used; import_stages now calls it for the extension branch, keeping the magic-first order that lets an Affinity file renamed .psd stay with the Affinity loader. --- crates/io/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index 7c67ab4970..7050cffd77 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -302,7 +302,9 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, m if affinity::is_affinity(bytes) || affinity::has_extension(name) { return affinity::import(name, bytes); } - if PSD_EXTS.iter().any(|e| has_extension(name, e)) { + // Same predicate as above; the magic case already returned, so this routes the declared + // `.psd`/`.psb`/`.psdt` extensions that are not Affinity containers. + if is_psd_input(name, bytes) { return import_psd(name, bytes, ctl); } if ora::is_ora(bytes) || has_extension(name, "ora") { From ff61346024412fc74d302c97812daf2d75b4a768 Mon Sep 17 00:00:00 2001 From: Vikram Bhaskaran Date: Wed, 21 Oct 2026 08:57:47 +1100 Subject: [PATCH 4/4] Open mislabeled .psd files from their raster signature. PNG/JPEG/TIFF bytes named .psd still go through codecs, as on main and in Photoshop. Only an unrecognized .psd keeps the PSD parse error. --- apps/photocraft-web/src/web.rs | 5 +---- crates/io/src/lib.rs | 34 ++++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/apps/photocraft-web/src/web.rs b/apps/photocraft-web/src/web.rs index 3f5e0ee04d..a248ecbb13 100644 --- a/apps/photocraft-web/src/web.rs +++ b/apps/photocraft-web/src/web.rs @@ -397,10 +397,7 @@ fn services(inbox: Inbox, place_inbox: Inbox, cursor_pos: photocraft_ui_egui::Cu let dialog = if let Some(exts) = extensions { rfd::AsyncFileDialog::new().add_filter("Supported Files", &exts) } else { - rfd::AsyncFileDialog::new() - .add_filter("All Formats", OPEN_EXTS) - .add_filter("Photoshop", &["psd", "psb", "psdt"]) - .add_filter("SVG", SVG_EXTS) + rfd::AsyncFileDialog::new().add_filter("All Formats", OPEN_EXTS).add_filter("SVG", SVG_EXTS) }; let answer = if multiple { match dialog.pick_files().await { diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rs index 89dc11d706..3843bc54e4 100644 --- a/crates/io/src/lib.rs +++ b/crates/io/src/lib.rs @@ -273,10 +273,11 @@ pub fn is_psd(bytes: &[u8]) -> bool { bytes.starts_with(b"8BPS") } -/// PSD/PSB/PSDT by magic or by a declared Photoshop extension so File › Open does not -/// send `.psd` through the generic image codecs (`codec: unrecognized image format`, #5258). +/// PSD/PSB/PSDT by magic, or by a declared Photoshop extension when no other raster +/// signature is present. A PNG/JPEG/TIFF downloaded as `.psd` still opens from its bytes +/// (#5258 review). fn is_psd_input(name: &str, bytes: &[u8]) -> bool { - is_psd(bytes) || PSD_EXTS.iter().any(|e| has_extension(name, e)) + is_psd(bytes) || (PSD_EXTS.iter().any(|e| has_extension(name, e)) && photocraft_codecs::detect(bytes).is_none()) } /// Imports a file. PSD/PSB, Affinity containers and camera raws are detected by magic; everything @@ -343,11 +344,6 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, m if affinity::is_affinity(bytes) || affinity::has_extension(name) { return affinity::import(name, bytes); } - // Same predicate as above; the magic case already returned, so this routes the declared - // `.psd`/`.psb`/`.psdt` extensions that are not Affinity containers. - if is_psd_input(name, bytes) { - return import_psd(name, bytes, ctl); - } if ora::is_ora(bytes) || has_extension(name, "ora") { return ora::import(name, bytes, ctl); } @@ -365,6 +361,11 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, m if is_svg_input(name, bytes) { return svg::import_svg_with_group_depth(name, bytes, max_group_depth); } + // A `.psd`/`.psb`/`.psdt` with no raster signature keeps the PSD error instead of + // `codec: unrecognized image format` (#5258). Magic already returned above. + if is_psd_input(name, bytes) { + return import_psd(name, bytes, ctl); + } flat::import_flat(name, bytes) } @@ -611,4 +612,21 @@ mod tests { // A renamed Affinity file keeps the Affinity loader (magic before the .psd suffix). assert!(!super::is_psd(b"\0\xffKA")); } + + /// A common mislabeled download: PNG/JPEG bytes named `.psd` open from the signature, + /// as on main and in Photoshop. + #[test] + fn raster_bytes_named_psd_still_open() { + use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image}; + let n = 8 * 8 * 3; + let img = Image::from_u8(8, 8, ChannelLayout::Rgb, vec![180; n]).unwrap(); + let png = photocraft_codecs::encode(&img, Format::Png, &EncodeOptions::default()).unwrap(); + let jpeg = photocraft_codecs::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap(); + for (name, bytes) in [("download.psd", png.as_slice()), ("scan.PSB", jpeg.as_slice())] { + assert!(!is_psd_input(name, bytes), "{name}"); + let d = import(name, bytes).unwrap_or_else(|e| panic!("{name}: {e}")).document; + assert_eq!(d.bounds().width(), 8, "{name}"); + assert_eq!(d.bounds().height(), 8, "{name}"); + } + } }