Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Pencil: a 1 px line paints the same pixels whichever end it starts from, at any Smoothing (#6336) (#6341)

Michael Higginsc408c83a9f52
diff --git a/crates/paint/src/brush_tests.rs b/crates/paint/src/brush_tests.rs
index d0d7de411..9ba17fcf1 100644
--- a/crates/paint/src/brush_tests.rs
+++ b/crates/paint/src/brush_tests.rs
@@ -189,28 +189,61 @@ fn pencil_dabs_sit_on_the_pixel_grid() {
#[test]
fn pencil_1px_line_is_a_clean_8_connected_staircase() {
- // #2139: a 1 px Pencil line is one pixel per column (or row, when steep) on the ideal line
- // between the end pixels: no doubled pixels at the steps.
+ // #2139: a 1 px Pencil line is one pixel per column (or row, when steep) on the line: no
+ // doubled pixels at the steps.
let b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() };
let cases = [((2.5, 2.5), (22.5, 9.5)), ((3.5, 30.5), (10.5, 5.5)), ((1.2, 1.7), (25.8, 4.1)), ((5.5, 5.5), (17.5, 17.5)), ((20.3, 3.6), (4.9, 12.2))];
for ((ax, ay), (bx, by)) in cases {
let s = paint(&b, &[StrokePoint::new(ax, ay, 1.0), StrokePoint::new(bx, by, 1.0)], 40, 40);
let painted: Vec<(i32, i32)> = (0..40).flat_map(|y| (0..40).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect();
let (a, e) = ((ax.floor(), ay.floor()), (bx.floor(), by.floor()));
- let steep = (e.1 - a.1).abs() > (e.0 - a.0).abs();
+ let steep = (by - ay).abs() > (bx - ax).abs();
let major = |p: (f64, f64)| if steep { (p.1, p.0) } else { (p.0, p.1) };
- let ((m0, n0), (m1, n1)) = (major(a), major(e));
+ let ((m0, _), (m1, _)) = (major(a), major(e));
+ let ((am, an), (bm, bn)) = (major((ax, ay)), major((bx, by)));
assert_eq!(painted.len() as f64, (m1 - m0).abs() + 1.0, "{a:?}->{e:?}: one pixel per step, got {painted:?}");
for &(x, y) in &painted {
- let (m, n) = major((f64::from(x), f64::from(y)));
- let ideal = n0 + (n1 - n0) * (m - m0) / (m1 - m0);
- assert!((n - ideal).abs() <= 0.5 + 1e-9, "{a:?}->{e:?}: ({x},{y}) is off the line");
+ if (f64::from(x), f64::from(y)) == a {
+ continue; // The pixel the stroke starts in.
+ }
+ // The pixel's centre is within half a pixel of the line where it crosses its column.
+ let (m, n) = major((f64::from(x) + 0.5, f64::from(y) + 0.5));
+ let on_line = an + (bn - an) * (m - am) / (bm - am);
+ assert!((n - on_line).abs() <= 0.5 + 1e-9, "{a:?}->{e:?}: ({x},{y}) is off the line");
}
let columns: std::collections::BTreeSet<i64> = painted.iter().map(|&(x, y)| major((f64::from(x), f64::from(y))).0 as i64).collect();
assert_eq!(columns.len(), painted.len(), "{a:?}->{e:?}: a column holds two pixels");
}
}
+#[test]
+fn pencil_1px_line_is_the_same_whichever_way_and_however_smoothed() {
+ // #6336: a ⇧-click line between pixel centres painted different pixels depending on which end
+ // it started from, and Smoothing (which splits it into pieces along the line) bent it.
+ let pixels = |a: (f64, f64), e: (f64, f64), amount: f32| {
+ let mut b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() };
+ b.smoothing.amount = amount;
+ let s = paint(&b, &[StrokePoint::new(a.0, a.1, 1.0), StrokePoint::new(e.0, e.1, 1.0)], 24, 24);
+ (0..24).flat_map(|y| (0..24).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect::<Vec<_>>()
+ };
+ for (a, e) in [((3.5, 3.5), (4.5, 5.5)), ((3.5, 3.5), (8.5, 5.5)), ((2.5, 20.5), (9.5, 17.5)), ((1.5, 1.5), (20.5, 9.5))] {
+ let line = pixels(a, e, 0.0);
+ assert_eq!(line, pixels(e, a, 0.0), "{a:?}->{e:?} differs from {e:?}->{a:?}");
+ for amount in [0.1, 0.5, 0.9] {
+ assert_eq!(pixels(a, e, amount), line, "{a:?}->{e:?} at Smoothing {amount}");
+ assert_eq!(pixels(e, a, amount), line, "{e:?}->{a:?} at Smoothing {amount}");
+ }
+ }
+ // One right and two down: three pixels, one per row.
+ assert_eq!(pixels((3.5, 3.5), (4.5, 5.5), 0.1), vec![(3, 3), (4, 4), (4, 5)]);
+ // A straight line fed as many pointer samples along it paints the line's pixels.
+ let b = BrushSettings { aliased: true, hardness: 1.0, size: 1.0, ..brush() };
+ let samples: Vec<StrokePoint> = (0..=37).map(|i| StrokePoint::new(1.5 + 19.0 * f64::from(i) / 37.0, 1.5 + 8.0 * f64::from(i) / 37.0, 1.0)).collect();
+ let s = paint(&b, &samples, 24, 24);
+ let sampled: Vec<(i32, i32)> = (0..24).flat_map(|y| (0..24).map(move |x| (x, y))).filter(|&(x, y)| s.rgba(x, y)[3] > 0.0).collect();
+ assert_eq!(sampled, pixels((1.5, 1.5), (20.5, 9.5), 0.0));
+}
+
// ---------- spacing ----------
#[test]
diff --git a/crates/paint/src/dynamics.rs b/crates/paint/src/dynamics.rs
index ab846f195..05a638a53 100644
--- a/crates/paint/src/dynamics.rs
+++ b/crates/paint/src/dynamics.rs
@@ -27,6 +27,15 @@ const MIN_SPEED_STEP: f64 = 0.5;
/// Maximum airbrush catch-up dabs for one input segment.
const MAX_AIRBRUSH_DABS_PER_SEGMENT: usize = 512;
+/// The dab centre of the pixel-grid cell holding `v` (see [`PathWalker::pixel_grid`]): the
+/// pixel's centre for `offset` 0.5, the nearest pixel corner for 0. A value within rounding
+/// noise of a cell boundary counts as on it, so a line lands on the same pixels both ways.
+fn grid_cell(v: f64, offset: f64) -> f64 {
+ let w = v - offset + 0.5;
+ let w = if (w - w.round()).abs() < 1e-7 { w.round() } else { w };
+ w.floor() + offset
+}
+
#[inline]
fn lerp_pt(a: &StrokePoint, b: &StrokePoint, f: f64) -> StrokePoint {
let ff = f as f32;
@@ -216,10 +225,14 @@ impl PathWalker {
}
/// Step pixel by pixel instead of by distance: one step per pixel along the segment's major
- /// axis, on the line between the pixels holding its ends, each at the pixel's dab centre
- /// (`offset` 0.5 for odd tip diameters, 0 for even ones, as `render::grid_center` snaps). A
- /// small aliased tip (the Pencil) then draws a clean 8-connected staircase: stepping by
- /// distance put two dabs in a row wherever the line crossed a pixel corner (#2139).
+ /// axis, each at the dab centre (`offset` 0.5 for odd tip diameters, 0 for even ones, as
+ /// `render::grid_center` snaps) of the pixel the segment's line crosses at that column (or
+ /// row, when steep). A small aliased tip (the Pencil) then draws a clean 8-connected
+ /// staircase: stepping by distance put two dabs in a row wherever the line crossed a pixel
+ /// corner (#2139). The line is sampled where it really runs, not between the pixels holding
+ /// the segment's ends, so a straight line split into pieces (by Smoothing, or the pointer
+ /// samples of a ⇧-drag) paints the pixels of the whole line, whichever end it starts from
+ /// (#6336).
pub fn pixel_grid(mut self, offset: f64) -> Self {
self.grid = Some(offset);
self
@@ -228,19 +241,24 @@ impl PathWalker {
/// Emits the pixel steps from `a` to `p` (see [`PathWalker::pixel_grid`]). False, emitting
/// nothing, when the span is not finite or implausibly long: distance spacing takes over.
fn grid_steps(&mut self, a: &StrokePoint, p: &StrokePoint, offset: f64, out: &mut impl FnMut(StepInput)) -> bool {
- let cell = |v: f64| if offset > 0.0 { v.floor() } else { v.round() };
- let (x0, y0) = (cell(a.x), cell(a.y));
- let (dx, dy) = (cell(p.x) - x0, cell(p.y) - y0);
- let n = dx.abs().max(dy.abs());
- if !n.is_finite() || n > MAX_GRID_STEPS {
+ // The pieces of a 45° line differ in rounding noise: keep them all on one major axis.
+ let steep = (p.y - a.y).abs() > (p.x - a.x).abs() * (1.0 + 1e-9);
+ let (am, an, pm, pn) = if steep { (a.y, a.x, p.y, p.x) } else { (a.x, a.y, p.x, p.y) };
+ let (m0, m1) = (grid_cell(am, offset), grid_cell(pm, offset));
+ let (dm, dn) = (pm - am, pn - an);
+ let n = (m1 - m0).abs();
+ if !(n.is_finite() && dn.is_finite()) || n > MAX_GRID_STEPS || dm == 0.0 {
return false;
}
- let steps = n as u64;
- for i in 1..=steps {
- let i = i as f64;
- let mut q = lerp_pt(a, p, i / n);
- q.x = x0 + (dx * i / n).round() + offset;
- q.y = y0 + (dy * i / n).round() + offset;
+ let dir = (m1 - m0).signum();
+ for i in 1..=n as u64 {
+ let c = m0 + dir * i as f64;
+ // The last column's centre can sit up to half a pixel past `p`: the line goes on to
+ // it, so the next piece of a straight line picks up where this one left off.
+ let t = (c - am) / dm;
+ let mut q = lerp_pt(a, p, t.clamp(0.0, 1.0));
+ let cn = grid_cell(an + dn * t, offset);
+ (q.x, q.y) = if steep { (cn, c) } else { (c, cn) };
self.emit(q, out);
}
true