Corpus: export round-trip step and pass floors; fill pixels in the mask frame

corpus.rs now re-imports every exported file and compares its flatten
with the first import (169/169 render the same). With PHOTOCRAFT_CORPUS
set (corpus dir, or 1 for the default location) it asserts the oracle
pass floor (113) and the round-trip floor (169).

The new step found gradient fills without Photoshop pixels exported in
the canvas frame instead of the frame their masks give them, so they
re-imported shifted (gradient-fill.psd/.psb, 0.26 max error). Export now
renders the fill like the compositor (render_fill_content).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Brandon ThomasandClaude Opus 5.5 committed 2026-10-04 13:41:48 -04:00
1 parent f225c290cc
commit 11b175e22b
3 files changed
+54 -10

No files matched your search

+6
View File
@@ -389,6 +389,12 @@ pub fn fill_frame(layer: &Layer, canvas: Rect) -> Rect {
frame
}
/// A fill layer's content over `canvas`, laid out in the frame its masks give it (as the
/// compositor does) but without applying the masks: the pixels a PSD fill layer stores.
pub fn render_fill_content(layer: &Layer, f: &Fill, canvas: Rect, patterns: &[Pattern]) -> Buffer {
render_fill(f, canvas, fill_frame(layer, canvas), patterns)
}
fn render_fill(f: &Fill, rect: Rect, canvas: Rect, patterns: &[Pattern]) -> Buffer {
match f {
Fill::Solid(c) => {
+2 -3
View File
@@ -333,9 +333,8 @@ impl Ex {
{
return c.surface.clone();
}
let mut plain = Layer::new("fill", LayerContent::Fill(f.clone()));
plain.fill_cache = None;
let buf = photocraft_compose::render_layer(&plain, self.canvas);
// 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, &[]);
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);
+46 -7
View File
@@ -3,9 +3,15 @@
//!
//! For each file: parse → document → flatten, compared with the file's own
//! merged composite (Photoshop's rendering) as the oracle; then document →
//! PSD → parse. Prints a per-file table. The oracle comparison is reported,
//! not asserted: differences are expected where features are not yet
//! rendered (effects, text engine, smart filters, some adjustments).
//! PSD → document → flatten, compared with the first flatten (our own export
//! must not change what the document looks like). Prints a per-file table.
//!
//! Without `PHOTOCRAFT_CORPUS` the comparisons are only reported: differences
//! are expected where features are not yet rendered (effects, text engine,
//! smart filters). With `PHOTOCRAFT_CORPUS` set (to the corpus directory, or
//! to `1` for the default location) the run asserts that the oracle pass
//! count and the export round-trip count do not fall below their floors.
//! Raise the floors when they improve; never lower them.
//! Set `PHOTOCRAFT_CORPUS_STRICT=1` to fail on import/export errors.
mod common;
@@ -28,17 +34,29 @@ fn collect(dir: &Path, out: &mut Vec<PathBuf>) {
}
const PASS_TOL: f32 = 2.0 / 255.0;
/// Export → re-import must render within one 8-bit step of the imported document.
const ROUNDTRIP_TOL: f32 = 1.0 / 255.0 + 1e-5;
/// Files whose flatten matches Photoshop's merged image (`corpus/psd`, 170 files).
const PASS_FLOOR: usize = 113;
/// Files whose export → re-import renders the same as the import.
const ROUNDTRIP_FLOOR: usize = 169;
#[test]
fn corpus_import_flatten_oracle() {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/psd");
let env = std::env::var_os("PHOTOCRAFT_CORPUS").filter(|v| !v.is_empty());
let root = match &env {
Some(v) if v != "1" => PathBuf::from(v),
_ => Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus/psd"),
};
if !root.is_dir() {
assert!(env.is_none(), "PHOTOCRAFT_CORPUS is set but {} is not a directory", root.display());
return;
}
let mut files = Vec::new();
collect(&root, &mut files);
files.sort();
let (mut pass, mut diff, mut skipped, mut errors) = (0, 0, 0, 0);
let (mut rt_same, mut rt_diff) = (0, Vec::new());
eprintln!("{:<60} {:>6} {:>9} {:>8} status", "file", "layers", "max_err", "bad_px%");
for p in &files {
let name = p.strip_prefix(&root).unwrap_or(p).display().to_string();
@@ -60,20 +78,37 @@ fn corpus_import_flatten_oracle() {
}
};
let doc = &imp.document;
// Export must always succeed and re-parse.
match export(doc, "x.psd", &ExportOptions::default()) {
// Export must always succeed, re-parse and re-import.
let reimported = match export(doc, "x.psd", &ExportOptions::default()) {
Ok(r) => {
if let Err(e) = PsdFile::from_bytes(&r.bytes) {
eprintln!("{name:<60} REEXPORT-PARSE-ERROR {e}");
errors += 1;
continue;
}
match import(&name, &r.bytes) {
Ok(i) => i.document,
Err(e) => {
eprintln!("{name:<60} REIMPORT-ERROR {e}");
errors += 1;
continue;
}
}
}
Err(e) => {
eprintln!("{name:<60} EXPORT-ERROR {e}");
errors += 1;
continue;
}
};
let ours = photocraft_compose::flatten(doc).px;
// Our own export must not change how the document renders.
let again = photocraft_compose::flatten(&reimported).px;
let rt = if again.len() == ours.len() { common::max_diff(&ours, &again) } else { f32::INFINITY };
if rt <= ROUNDTRIP_TOL {
rt_same += 1;
} else {
rt_diff.push(format!("{name} ({rt:.4})"));
}
let layers = doc.layer_count();
if file.has_real_merged_data() == Some(false) || file.layers().is_empty() {
@@ -86,7 +121,6 @@ fn corpus_import_flatten_oracle() {
skipped += 1;
continue;
};
let ours = photocraft_compose::flatten(doc).px;
let m = common::max_diff(&ours, &merged);
let bad = ours.iter().zip(&merged).filter(|(a, b)| (0..4).any(|c| (a[c] * a[3] - b[c] * b[3]).abs() > PASS_TOL)).count();
let pct = 100.0 * bad as f32 / ours.len().max(1) as f32;
@@ -101,7 +135,12 @@ fn corpus_import_flatten_oracle() {
eprintln!("{name:<60} {layers:>6} {:>9.4} {:>7.2}% {status} {}", m, pct, notes.join(" | "));
}
eprintln!("io corpus: {} files: {pass} pass (<= 2/255), {diff} differ, {skipped} skipped, {errors} errors", files.len());
eprintln!("io corpus: export -> re-import renders the same for {rt_same} files; differs for {}: {}", rt_diff.len(), rt_diff.join(", "));
if std::env::var_os("PHOTOCRAFT_CORPUS_STRICT").is_some() {
assert_eq!(errors, 0);
}
if env.is_some() {
assert!(pass >= PASS_FLOOR, "oracle pass count {pass} fell below the floor {PASS_FLOOR}");
assert!(rt_same >= ROUNDTRIP_FLOOR, "export round trip {rt_same} fell below the floor {ROUNDTRIP_FLOOR}: {rt_diff:?}");
}
}