From 2819f370332c7ca8a5df72e93c3076aa16aa052a Mon Sep 17 00:00:00 2001 From: kayrockscreenprinting Date: Fri, 9 Oct 2026 12:26:56 -0700 Subject: [PATCH] PSD export: pattern fill layers keep their pattern in CMYK and Lab (#1917) The PSD (and layered TIFF) exporter rendered fill layers with no patterns, so pattern fills were written as transparent pixels; in CMYK and Lab those tiles survive and the reopened layer drew empty. Pattern fills now render from the document's patterns. Round-trip test over 4 modes x 3 depths. Fixes #1907. Co-Authored-By: Claude Opus 5.5 --- crates/io/src/psd_export.rs | 7 +++- crates/io/tests/pattern_fill_psd.rs | 61 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 crates/io/tests/pattern_fill_psd.rs diff --git a/crates/io/src/psd_export.rs b/crates/io/src/psd_export.rs index 137a1a3ab7..8cdb79a74a 100644 --- a/crates/io/src/psd_export.rs +++ b/crates/io/src/psd_export.rs @@ -64,6 +64,8 @@ struct Ex { comps: Option<(Vec, Option)>, /// Smart objects: embedded files and filter caches for the global blocks. smart: SmartOut, + /// The document's patterns, which Pattern Fill layers are rendered from. + patterns: Vec, } /// How deep embedded documents are exported inside each other before a smart object is written @@ -776,7 +778,9 @@ impl Ex { return c.surface.clone(); } // In the frame the layer's masks give it, like the compositor (masks are stored apart). - let buf = photocraft_compose::render_fill_content(l, f, self.canvas, &[]); + // Readers composite these pixels (ours keeps them as the fill's rendering), so a pattern + // fill must be rendered from the document's patterns, not left transparent (#1907). + let buf = photocraft_compose::render_fill_content(l, f, self.canvas, &self.patterns); let mut s = Surface::new(self.fmt); let vals: Vec = buf.px.iter().flat_map(|p| photocraft_raster::from_rgba(&self.fmt, *p)).collect(); s.write_region(self.canvas, &vals); @@ -1145,6 +1149,7 @@ fn document_to_psd_nested(doc: &Document, opts: &PsdExportOptions, depth: u32) - guides: doc.guides.clone(), comps: (!crate::comps_map::comps_unchanged(doc)).then(|| (doc.layer_comps.clone(), doc.last_document_state.clone())), smart: SmartOut::new(doc, depth), + patterns: doc.patterns.clone(), }; if big && !opts.force_psb { ex.warnings.push("document exceeds 30000 px; written as PSB".into()); diff --git a/crates/io/tests/pattern_fill_psd.rs b/crates/io/tests/pattern_fill_psd.rs new file mode 100644 index 0000000000..5ca00152b6 --- /dev/null +++ b/crates/io/tests/pattern_fill_psd.rs @@ -0,0 +1,61 @@ +//! A Pattern Fill layer looks the same after a PSD or layered-TIFF save and reopen in every +//! colour mode and depth (#1907): the pixels written for it are the pattern, not transparency. + +use photocraft_color::{ColorMode, PixelFormat, SampleType}; +use photocraft_doc::{Document, Fill, Layer, LayerContent, Pattern}; +use photocraft_geom::{Rect, Size}; +use photocraft_io::*; +use photocraft_raster::Surface; + +/// An RGB checkerboard (as the default patterns are), whatever the document's mode. +fn checker() -> Pattern { + let mut s = Surface::new(PixelFormat::RGBA8); + s.fill_rect(Rect::new(0, 0, 8, 8), &[0.8, 0.8, 0.8, 1.0]); + s.fill_rect(Rect::new(0, 0, 4, 4), &[0.2, 0.4, 0.9, 1.0]); + s.fill_rect(Rect::new(4, 4, 8, 8), &[0.2, 0.4, 0.9, 1.0]); + Pattern { id: "059289fb-0000-4000-8000-000000000001".into(), name: "Checker".into(), width: 8, height: 8, surface: s } +} + +fn doc(mode: ColorMode, depth: SampleType) -> Document { + let mut d = Document::new("p", Size::new(40, 24), mode, depth); + d.patterns.push(checker()); + let fill = Fill::Pattern { name: "Checker".into(), scale: 1.0, id: checker().id, angle: 0.0, link: true, phase: (0.0, 0.0) }; + d.layers.push(Layer::new("Pattern Fill 1", LayerContent::Fill(fill))); + d +} + +/// How far `back`'s composite is from `d`'s pattern stored in `d`'s pixel format (the pattern +/// as Edit › Fill would paint it: an RGB pattern is grey in a Grayscale document). +fn max_diff(d: &Document, back: &Document) -> f32 { + let fmt = d.pixel_format(); + let want = photocraft_compose::flatten(d).px.iter().map(|p| photocraft_raster::to_rgba(&fmt, &photocraft_raster::from_rgba(&fmt, *p))).collect::>(); + let got = photocraft_compose::flatten(back); + want.iter().zip(&got.px).flat_map(|(p, q)| p.iter().zip(q).map(|(u, v)| (u - v).abs())).fold(0.0, f32::max) +} + +#[test] +fn pattern_fill_survives_psd_and_layered_tiff_in_every_mode() { + for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Cmyk, ColorMode::Lab] { + for depth in [SampleType::U8, SampleType::U16, SampleType::F32] { + let d = doc(mode, depth); + let before = photocraft_compose::flatten(&d); + assert!(before.px.iter().all(|p| p[3] > 0.99), "{mode:?}/{depth:?}: the pattern renders before saving"); + for (path, opts) in [("x.psd", ExportOptions::default()), ("x.tif", ExportOptions { tiff_layers: true, ..Default::default() })] { + if mode == ColorMode::Lab && path == "x.tif" { + continue; // Lab documents are saved as a flat TIFF (with a warning). + } + let r = export(&d, path, &opts).expect("export"); + let back = import(path, &r.bytes).expect("import").document; + let names: Vec<&str> = back.layers.iter().map(|l| l.name.as_str()).collect(); + let l = back + .layers + .iter() + .find(|l| l.name == "Pattern Fill 1") + .unwrap_or_else(|| panic!("{mode:?}/{depth:?} {path}: layers {names:?}, warnings {:?}", r.warnings)); + assert!(matches!(l.content, LayerContent::Fill(Fill::Pattern { .. })), "{mode:?}/{depth:?} {path}"); + let diff = max_diff(&d, &back); + assert!(diff < 0.02, "{mode:?}/{depth:?} {path}: the reopened pattern fill differs by {diff}"); + } + } + } +}