From ea771f39e0582033c1500cbba69d236fe87072f0 Mon Sep 17 00:00:00 2001 From: Dan Date: Tue, 10 Nov 2026 17:29:12 +0000 Subject: [PATCH] fix(paint): match unaligned Pattern Stamp origins --- .../src/cmd/retouch_cmds/pattern_stamp.rs | 20 ++- .../pattern_stamp_origin_tests.rs | 119 ++++++++++++++++++ crates/engine/src/cmd/retouch_cmds/tests.rs | 7 +- 3 files changed, 137 insertions(+), 9 deletions(-) create mode 100644 crates/engine/src/cmd/retouch_cmds/pattern_stamp_origin_tests.rs diff --git a/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs b/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs index 06b5bd17e7..6842377974 100644 --- a/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs +++ b/crates/engine/src/cmd/retouch_cmds/pattern_stamp.rs @@ -1,8 +1,8 @@ //! Pattern Stamp: paint with the current pattern through a brush stroke. //! -//! Aligned strokes lock the tile origin in document space. Unaligned strokes restart the origin at -//! the first point of that stroke. Impressionist jitters each dab's phase with a seeded RNG so a -//! replay of the same points is identical. +//! Aligned strokes lock the tile origin in document space. Unaligned strokes derive a new origin +//! from the tile size and the first point of each stroke. Impressionist jitters each dab's phase +//! with a seeded RNG so a replay of the same points is identical. use photocraft_color::{BlendMode, PixelFormat}; use photocraft_compose::pattern::{Placement, Tile, render}; @@ -56,7 +56,7 @@ impl Setup { let Some(first) = stroke.points.first() else { return Err(bad(CMD, "`points` is empty")); }; - let phase = stamp_phase(p, CMD, aligned, first)?; + let phase = stamp_phase(p, CMD, aligned, first, (pat.width, pat.height))?; let tile = std::sync::Arc::new(Tile::new(&pat).ok_or_else(|| bad(CMD, "the pattern is empty or too large to paint"))?); reject_huge_stroke(CMD, &stroke)?; let seed = p.get("seed").and_then(Value::as_u64).unwrap_or_else(|| { @@ -152,13 +152,17 @@ fn stamp_angle(p: &Value, cmd: &str) -> Result { } } -fn stamp_phase(p: &Value, cmd: &str, aligned: bool, first: &StrokePoint) -> Result<(f32, f32)> { +fn stamp_phase(p: &Value, cmd: &str, aligned: bool, first: &StrokePoint, size: (u32, u32)) -> Result<(f32, f32)> { match p.get("phase") { None => { if aligned { Ok((0.0, 0.0)) } else { - Ok((first.x as f32, first.y as f32)) + // Each axis starts from half its tile plus one pixel, mirrored about the + // stroke's first point. Keep one phase for the complete stroke, independent + // of its brush tip. Empty patterns have already been rejected by Setup. + let origin = |first: f64, edge: u32| (f64::from(edge / 2 + 1) - first).rem_euclid(f64::from(edge)) as f32; + Ok((origin(first.x, size.0), origin(first.y, size.1))) } } Some(v) => { @@ -275,6 +279,10 @@ fn stamp_dab( } } +#[cfg(test)] +#[path = "pattern_stamp_origin_tests.rs"] +mod pattern_stamp_origin_tests; + #[cfg(test)] mod tests { use super::*; diff --git a/crates/engine/src/cmd/retouch_cmds/pattern_stamp_origin_tests.rs b/crates/engine/src/cmd/retouch_cmds/pattern_stamp_origin_tests.rs new file mode 100644 index 0000000000..dcd29975c7 --- /dev/null +++ b/crates/engine/src/cmd/retouch_cmds/pattern_stamp_origin_tests.rs @@ -0,0 +1,119 @@ +use super::*; + +fn fixture(width: i32, height: i32, depth: u64) -> (Session, String) { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 700, "height": 600, "depth": depth})).unwrap(); + let fmt = s.active().unwrap().doc.pixel_format(); + let mut surface = Surface::new(fmt); + for (rect, color) in [ + (Rect::new(0, 0, width / 2, height / 2), [1.0, 0.0, 0.0, 1.0]), + (Rect::new(width / 2, 0, width, height / 2), [0.0, 1.0, 0.0, 1.0]), + (Rect::new(0, height / 2, width / 2, height), [0.0, 0.0, 1.0, 1.0]), + (Rect::new(width / 2, height / 2, width, height), [1.0; 4]), + ] { + surface.fill_rect(rect, &color); + } + let pat = Pattern::new("Origin quadrants", surface, width as u32, height as u32); + let id = pat.id.clone(); + s.edit("Test pattern", |doc, _| { + doc.patterns.push(pat); + Ok(()) + }) + .unwrap(); + (s, id) +} + +fn stamp(s: &mut Session, id: &str, points: &[(i32, i32)], size: u32, extra: Value) -> Value { + let mut p = json!({"pattern": id, "points": points, "size": size, "hardness": 100, "spacing": 10, "aligned": false}); + for (key, value) in extra.as_object().unwrap() { + p[key] = value.clone(); + } + s.execute("paint.patternStamp", p).unwrap() +} + +/// Read a patch spanning tile seams and all four quadrants, rather than only its origin. +fn assert_patch(s: &Session, center: (i32, i32), size: (i32, i32), origin: (i32, i32)) { + let d = s.active().unwrap(); + let surf = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap(); + for dy in (-15..=15).step_by(5) { + for dx in (-15..=15).step_by(5) { + let (x, y) = (center.0 + dx, center.1 + dy); + let (u, v) = ((x - origin.0).rem_euclid(size.0), (y - origin.1).rem_euclid(size.1)); + let want = match (u < size.0 / 2, v < size.1 / 2) { + (true, true) => [1.0, 0.0, 0.0, 1.0], + (false, true) => [0.0, 1.0, 0.0, 1.0], + (true, false) => [0.0, 0.0, 1.0, 1.0], + (false, false) => [1.0; 4], + }; + assert_eq!(surf.rgba(x, y), want, "pixel ({x},{y}), tile {size:?}, origin {origin:?}"); + } + } +} + +#[test] +fn measured_origins_paint_reference_pixels_independent_of_tip_size() { + // Origins measured in the reference application, per axis modulo the pattern size. + for (edge, first, origin) in + [(20, (605, 455), (6, 16)), (20, (500, 500), (11, 11)), (20, (561, 503), (10, 8)), (20, (450, 100), (1, 11)), (30, (150, 500), (16, 26))] + { + for depth in [8, 16, 32] { + for tip in [64, 128] { + let (mut s, id) = fixture(edge, edge, depth); + let r = stamp(&mut s, &id, &[first], tip, json!({})); + assert_eq!(r["phase"], json!([origin.0 as f32, origin.1 as f32]), "{edge}px, {first:?}, tip {tip}, depth {depth}"); + assert_patch(&s, first, (edge, edge), origin); + } + } + } +} + +#[test] +fn rectangular_pattern_uses_each_axis_size() { + let (mut s, id) = fixture(20, 30, 16); + let first = (150, 500); + let r = stamp(&mut s, &id, &[first], 64, json!({})); + assert_eq!(r["phase"], json!([1.0, 26.0])); + assert_patch(&s, first, (20, 30), (1, 26)); +} + +#[test] +fn aligned_strokes_keep_document_origin() { + let (mut s, id) = fixture(20, 20, 8); + for first in [(605, 455), (500, 500)] { + let r = stamp(&mut s, &id, &[first], 64, json!({"aligned": true})); + assert_eq!(r["phase"], json!([0.0, 0.0])); + assert_patch(&s, first, (20, 20), (0, 0)); + } +} + +#[test] +fn explicit_phase_overrides_both_alignment_modes() { + for aligned in [false, true] { + let (mut s, id) = fixture(20, 30, 32); + let r = stamp(&mut s, &id, &[(561, 503)], 64, json!({"aligned": aligned, "phase": [-3, 7]})); + assert_eq!(r["phase"], json!([-3.0, 7.0])); + assert_patch(&s, (561, 503), (20, 30), (-3, 7)); + } +} + +#[test] +fn one_stroke_keeps_its_origin_and_new_stroke_resets_it() { + // Two points model a Shift-click line; intermediate points model a dragged stroke. + for points in [vec![(500, 500), (561, 503)], vec![(500, 500), (530, 490), (561, 503)]] { + let (mut s, id) = fixture(20, 20, 8); + let history = s.active().unwrap().history.past_len(); + let r = stamp(&mut s, &id, &points, 64, json!({})); + assert_eq!(r["phase"], json!([11.0, 11.0])); + for &point in &points { + assert_patch(&s, point, (20, 20), (11, 11)); + } + assert_eq!(s.active().unwrap().history.past_len(), history + 1); + assert!(s.undo()); + assert!(s.redo()); + assert_patch(&s, (561, 503), (20, 20), (11, 11)); + + let r = stamp(&mut s, &id, &[(561, 503)], 64, json!({})); + assert_eq!(r["phase"], json!([10.0, 8.0])); + assert_patch(&s, (561, 503), (20, 20), (10, 8)); + } +} diff --git a/crates/engine/src/cmd/retouch_cmds/tests.rs b/crates/engine/src/cmd/retouch_cmds/tests.rs index 60cabeeda2..c96b153fb7 100644 --- a/crates/engine/src/cmd/retouch_cmds/tests.rs +++ b/crates/engine/src/cmd/retouch_cmds/tests.rs @@ -1813,12 +1813,13 @@ fn pattern_stamp_aligned_keeps_document_phase_across_strokes() { fn pattern_stamp_unaligned_restarts_at_each_stroke() { let mut s = session(80, 40, 8, "rgb"); let r1 = stamp(&mut s, json!({"points": [[10, 16]], "size": 8, "hardness": 100, "aligned": false})).unwrap(); - assert_eq!(r1["phase"], json!([10.0, 16.0])); + assert_eq!(r1["phase"], json!([15.0, 9.0])); let a = rgba(&s, 10, 16); let r2 = stamp(&mut s, json!({"points": [[40, 16]], "size": 8, "hardness": 100, "aligned": false})).unwrap(); - assert_eq!(r2["phase"], json!([40.0, 16.0])); + assert_eq!(r2["phase"], json!([1.0, 9.0])); let b = rgba(&s, 40, 16); - assert!((a[0] - b[0]).abs() <= tol(8), "each stroke's first point is the tile origin: {a:?} vs {b:?}"); + assert!((a[0] - checker_at(10 - 15, 16 - 9)).abs() <= tol(8), "first stroke: {a:?}"); + assert!((b[0] - checker_at(40 - 1, 16 - 9)).abs() <= tol(8), "new stroke: {b:?}"); } #[test]