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 <[email protected]>
This commit is contained in:
kayrockscreenprintingandClaude Opus 5.5 authored and GitHub committed 2026-10-09 15:26:56 -04:00
1 parent 19256a29f5
commit 2819f37033
2 files changed
+67 -1

No files matched your search

+6 -1
View File
@@ -64,6 +64,8 @@ struct Ex {
comps: Option<(Vec<photocraft_doc::LayerComp>, Option<photocraft_doc::LayerComp>)>,
/// 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<photocraft_doc::Pattern>,
}
/// 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<f32> = 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());
+61
View File
@@ -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::<Vec<_>>();
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}");
}
}
}
}