From 85eec7afaaaf99cd2b9f02c3c03be8e3cb4f0236 Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 25 Oct 2026 02:04:24 +0200 Subject: [PATCH] Address CodeRabbit review: size_limit_psb off keeps Multichannel, Indexed and Bitmap as PSD The layerless exporters took a plain limit and always ran fit_psd_size, so an export with size_limit_psb off could still be promoted to PSB by size. They now take an optional limit, None when the option is off. Co-Authored-By: Claude Opus 5.5 --- crates/io/src/multichannel_map.rs | 13 ++++++++----- crates/io/src/palette_map.rs | 13 +++++++------ crates/io/src/psd_export.rs | 11 +++++++++-- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/crates/io/src/multichannel_map.rs b/crates/io/src/multichannel_map.rs index 7513559b4e..e877276198 100644 --- a/crates/io/src/multichannel_map.rs +++ b/crates/io/src/multichannel_map.rs @@ -73,8 +73,9 @@ pub(crate) fn import(file: &PsdFile, all: &[u8], doc: &mut Document, names: &[St } /// Writes a Multichannel document as a mode-7 PSD (no layer section; every channel a plane). -/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`). -pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: u64) -> (PsdFile, Vec) { +/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps +/// the version whatever the size. +pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: Option) -> (PsdFile, Vec) { let mut warnings = Vec::new(); // Photoshop's Multichannel mode is 8 or 16 bits per channel. let sample = match doc.depth { @@ -139,7 +140,7 @@ pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: u64) /// EXIF and every other imported resource `keep` accepts (except the ones that only describe /// layers and any already in `resources`), and the document-level tagged blocks. Shared by the /// modes that have no layers in Photoshop: Multichannel, Indexed Color and Bitmap. A file past -/// `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`). +/// `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps the version. #[allow(clippy::too_many_arguments)] pub(crate) fn layerless_file( doc: &Document, @@ -148,7 +149,7 @@ pub(crate) fn layerless_file( color_mode_data: Vec, mut resources: Vec, keep: impl Fn(u16, &[u8]) -> bool, - size_limit: u64, + size_limit: Option, warnings: &mut Vec, ) -> PsdFile { resources.extend(crate::psd_export::path_resources(doc, warnings)); @@ -195,7 +196,9 @@ pub(crate) fn layerless_file( image_data, write_as_psb: false, }; - warnings.extend(crate::psd_export::fit_psd_size(&mut file, size_limit)); + if let Some(limit) = size_limit { + warnings.extend(crate::psd_export::fit_psd_size(&mut file, limit)); + } crate::psd_export::sign_global_blocks(&mut file); file } diff --git a/crates/io/src/palette_map.rs b/crates/io/src/palette_map.rs index baccb48fb5..8a20d7477b 100644 --- a/crates/io/src/palette_map.rs +++ b/crates/io/src/palette_map.rs @@ -21,8 +21,9 @@ const CHANNEL_RESOURCES: [u16; 4] = [1006, 1045, 1053, crate::channel_map::DISPL /// opaque, or a Bitmap document; None for any other document (it is written like the others). A /// document with alpha channels or a Quick Mask is written like the others too: the importer does /// not read the extra channels of these modes back, so they would be lost. -/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`). -pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: u64) -> Option<(PsdFile, Vec)> { +/// A file past `size_limit` bytes is written as PSB (see `psd_export::fit_psd_size`); `None` keeps +/// the version whatever the size. +pub(crate) fn document_to_psd(doc: &Document, force_psb: bool, size_limit: Option) -> Option<(PsdFile, Vec)> { if !doc.channels.is_empty() || doc.quick_mask.is_some() { return None; } @@ -161,7 +162,7 @@ mod tests { } fn round_trip(doc: &Document) -> (PsdFile, Document) { - let (file, _) = document_to_psd(doc, false, u64::MAX).unwrap(); + let (file, _) = document_to_psd(doc, false, None).unwrap(); let bytes = file.to_bytes().unwrap(); let file = PsdFile::from_bytes(&bytes).unwrap(); let back = crate::import("test.psd", &bytes).unwrap().document; @@ -213,17 +214,17 @@ mod tests { assert_eq!(back.color_table, doc.color_table); for count in [0, 257] { doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0]; count], transparent: None }); - assert!(document_to_psd(&doc, false, u64::MAX).is_none(), "{count}"); + assert!(document_to_psd(&doc, false, None).is_none(), "{count}"); } doc.color_table = None; - assert!(document_to_psd(&doc, false, u64::MAX).is_none()); + assert!(document_to_psd(&doc, false, None).is_none()); } #[test] fn indexed_import_bounds_malformed_count_and_transparency_resources() { let mut doc = document(1, 1, SampleType::U8, ColorMode::Indexed, &[0.0, 0.0, 0.0, 1.0]); doc.color_table = Some(ColorTable { colors: vec![[0, 0, 0], [255, 255, 255]], transparent: None }); - let (original, _) = document_to_psd(&doc, false, u64::MAX).unwrap(); + let (original, _) = document_to_psd(&doc, false, None).unwrap(); for (count, expected) in [(vec![0, 0], 1), (vec![1, 1], 256), (vec![255, 255], 256), (vec![0], 256)] { let mut file = original.clone(); file.resources.retain(|r| r.id != COLOR_TABLE_COUNT); diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index 682bbdc11b..8bfe50038c 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -1598,10 +1598,12 @@ fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32, c /// [`document_to_psd_nested`] with the largest file kept as PSD (lowered by tests). fn document_to_psd_sized(doc: &Document, opts: &PsdExportOptions, depth: u32, ctl: &Interrupt, size_limit: u64) -> (PsdFile, Vec) { + // Without `size_limit_psb` the encoded size never changes the version, in every mode. + let layerless_limit = opts.size_limit_psb.then_some(size_limit); if doc.mode == ColorMode::Multichannel { - return crate::multichannel_map::document_to_psd(doc, opts.force_psb, size_limit); + return crate::multichannel_map::document_to_psd(doc, opts.force_psb, layerless_limit); } - if let Some(file) = crate::palette_map::document_to_psd(doc, opts.force_psb, size_limit) { + if let Some(file) = crate::palette_map::document_to_psd(doc, opts.force_psb, layerless_limit) { return file; } let fmt = doc.pixel_format(); @@ -2316,6 +2318,11 @@ mod tests { assert!(warnings.iter().any(|w| w == "encoded size exceeds 2 GB; written as PSB"), "{psd_mode:?}: {warnings:?}"); assert_eq!(blocks, vec![(*b"8B64", *b"cinf"), (*b"8B64", *b"pths"), (*b"8B64", *b"FMsk"), (*b"8BIM", *b"Patt")], "{psd_mode:?}: a PSB signs 8B64"); assert_eq!(big.decode_merged().unwrap(), small.decode_merged().unwrap(), "{psd_mode:?}"); + // Without `size_limit_psb` no mode is promoted by its size, the layerless ones included. + let opts = PsdExportOptions { size_limit_psb: false, ..PsdExportOptions::default() }; + let (kept, warnings) = document_to_psd_sized(&doc, &opts, 0, &Interrupt::NONE, len + PSD_SAVE_HEADROOM - 1); + assert_eq!((kept.written_version(), kept.header.color_mode), (Version::Psd, psd_mode), "{psd_mode:?}: {warnings:?}"); + assert!(!warnings.iter().any(|w| w.contains("written as PSB")), "{psd_mode:?}: {warnings:?}"); } }