Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-MASK-BRUSH: holding the brush still adds no coverage; flow builds up by passes (#518)

Jason Baldwin61df5d304f15
diff --git a/crates/pipeline/src/masks.rs b/crates/pipeline/src/masks.rs
index 4777c8782..e6508c084 100644
--- a/crates/pipeline/src/masks.rs
+++ b/crates/pipeline/src/masks.rs
@@ -440,25 +440,35 @@ pub struct BrushDabs {
pub auto: bool,
}
+/// Output pixels a brush sample must move from the last dab to count as movement: below this the
+/// pointer is held still (the same screen position mapped again, up to float rounding).
+const STATIONARY_PX: f64 = 1e-6;
+
/// The dabs of stroke `s` in a `w × h` output.
pub fn brush_dabs(s: &BrushStroke, frame: &Frame, w: usize, h: usize) -> BrushDabs {
let to_out = frame.norm_to_out(w, h);
let ppl = frame.px_per_long(w);
let r = (s.size * ppl).max(0.5);
let hard = r * (1.0 - (s.feather / 100.0).clamp(0.0, 1.0));
- // Densify the path so dabs overlap (spacing r/4).
+ // Densify the path so dabs overlap (spacing r/4). Coverage builds up by passes, not by how
+ // long the button is held: a sample where the pointer hasn't moved (a stationary hold records
+ // one per frame) lays no dab, so waiting in place adds nothing (#518).
let mut dabs: Vec<Point> = Vec::new();
- for (i, p) in s.points.iter().enumerate() {
+ let mut prev: Option<Point> = None;
+ for p in &s.points {
let q = to_out.apply(*p);
- if i > 0 {
- let prev = to_out.apply(s.points[i - 1]);
+ if let Some(prev) = prev {
let dist = prev.dist(q);
+ if dist.is_nan() || dist <= STATIONARY_PX {
+ continue;
+ }
let n = (dist / (r / 4.0).max(0.5)).ceil() as usize;
for k in 1..n {
dabs.push(prev.lerp(q, k as f64 / n as f64));
}
}
dabs.push(q);
+ prev = Some(q);
}
BrushDabs {
dabs,
@@ -714,6 +724,58 @@ mod tests {
assert!(a.get(40, 5) < 0.01);
}
+ /// #518: holding the brush still adds no coverage. A press plus any number of stationary
+ /// pointer samples paints exactly the single dab of the press, whatever the frame rate.
+ #[test]
+ fn holding_the_brush_still_adds_no_coverage() {
+ let (w, h) = (200, 100);
+ let f = frame(w, h);
+ let img = Rgb32f::new(w, h);
+ let l = Plane::new(w, h);
+ let p = Point::new(0.5, 0.5);
+ let spot = |samples: usize, flow: f64| {
+ let s = BrushStroke { points: vec![p; samples], size: 0.1, feather: 80.0, flow, ..Default::default() };
+ shape_alpha(&MaskShape::Brush { strokes: vec![s] }, &f, w, h, &img, &l, 0.0, None)
+ };
+ for flow in [100.0, 40.0] {
+ let press = spot(1, flow);
+ // the feathered edge is soft to begin with
+ let edge = press.get(100 + 14, 50);
+ assert!(edge > 0.05 && edge < 0.6, "flow {flow}: edge {edge}");
+ for n in [2, 5, 60, 600] {
+ let held = spot(n, flow);
+ assert!(held.data == press.data, "flow {flow}: {n} stationary samples hardened the edge {edge} → {}", held.get(100 + 14, 50));
+ }
+ }
+ let s = BrushStroke { points: vec![p; 60], size: 0.1, ..Default::default() };
+ assert_eq!(brush_dabs(&s, &f, w, h).dabs.len(), 1, "one dab for the press");
+ // a stroke that pauses partway lays the same dabs as one that doesn't
+ let (a, b) = (Point::new(0.2, 0.5), Point::new(0.8, 0.5));
+ let paused = BrushStroke { points: vec![a, p, p, p, p, b], flow: 30.0, ..Default::default() };
+ let smooth = BrushStroke { points: vec![a, p, b], flow: 30.0, ..Default::default() };
+ assert_eq!(brush_dabs(&paused, &f, w, h).dabs, brush_dabs(&smooth, &f, w, h).dabs);
+ }
+
+ /// Flow builds up by passes: going back over an area in the same stroke adds coverage.
+ #[test]
+ fn a_second_pass_builds_up_with_low_flow() {
+ let (w, h) = (200, 100);
+ let f = frame(w, h);
+ let img = Rgb32f::new(w, h);
+ let l = Plane::new(w, h);
+ let (a, b) = (Point::new(0.2, 0.5), Point::new(0.8, 0.5));
+ let paint = |points: Vec<Point>| {
+ let s = BrushStroke { points, size: 0.05, feather: 50.0, flow: 20.0, ..Default::default() };
+ shape_alpha(&MaskShape::Brush { strokes: vec![s] }, &f, w, h, &img, &l, 0.0, None)
+ };
+ let once = paint(vec![a, b]);
+ let twice = paint(vec![a, b, a]);
+ let thrice = paint(vec![a, b, a, b]);
+ let (o, t, th) = (once.get(100, 50), twice.get(100, 50), thrice.get(100, 50));
+ assert!(o > 0.05 && o < 0.99, "one pass {o}");
+ assert!(t > o + 0.02 && th > t + 0.01, "passes build up: {o} → {t} → {th}");
+ }
+
#[test]
fn auto_mask_stops_at_edges() {
// dark left half, bright right half; a stroke along the boundary, centred on the dark side
diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs
index c3678ae20..7a6a0379a 100644
--- a/crates/ui-egui/src/panels/detail.rs
+++ b/crates/ui-egui/src/panels/detail.rs
@@ -1672,6 +1672,8 @@ fn mask_overlay(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::Respons
{
let n = map.norm(q);
match &mut app.gesture {
+ // a held, unmoving pointer records nothing: coverage builds by passes, not time (#518)
+ Some(Gesture::Brush { points }) if points.last() == Some(&n) => {}
Some(Gesture::Brush { points }) => points.push(n),
_ => app.gesture = Some(Gesture::Brush { points: vec![n] }),
}
diff --git a/crates/ui-egui/src/tests_masking.rs b/crates/ui-egui/src/tests_masking.rs
index c748ecf14..bb03aa564 100644
--- a/crates/ui-egui/src/tests_masking.rs
+++ b/crates/ui-egui/src/tests_masking.rs
@@ -298,6 +298,32 @@ fn mask_overlay_keys_and_pins() {
h.settle(SETTLE);
}
+/// #518: holding the brush still, however long, paints the same feathered spot as a quick press.
+#[test]
+fn holding_the_brush_still_does_not_harden_the_spot() {
+ let mut h = detail("panel.masking");
+ let r = h.request("ui.set", json!({"brushSize": 0.1, "brushFeather": 80.0, "brushFlow": 100.0}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ exec(&mut h, "tool.brush", json!({"new": true}));
+ // a quick press at the left, a long stationary hold (120 frames, ~2 s) at the right
+ brush_at(&mut h, 0.3, 0.5);
+ let mut hold = vec![json!({"kind": "down", "x": 0.7, "y": 0.5})];
+ hold.extend((0..120).map(|_| json!({"kind": "move", "x": 0.7, "y": 0.5})));
+ hold.push(json!({"kind": "up", "x": 0.7, "y": 0.5}));
+ pointer(&mut h, json!(hold));
+ let d = develop(&h);
+ let MaskShape::Brush { strokes } = &d.masks[0].components[0].shape else { panic!("brush") };
+ assert_eq!(strokes.len(), 2, "{strokes:?}");
+ assert_eq!(strokes[1].points.len(), 1, "a stationary hold records one sample: {:?}", strokes[1].points);
+ // the same edge profile around both spots
+ for dx in [0usize, 4, 6, 8] {
+ let (quick, held) = (mask_coverage(&h, 0, 30 + dx, 50), mask_coverage(&h, 0, 70 + dx, 50));
+ assert!((quick - held).abs() < 0.01, "{dx} px from the centre: quick {quick}, held {held}");
+ }
+ assert!(mask_coverage(&h, 0, 70 + 6, 50) < 0.9, "the edge stays feathered");
+ h.settle(SETTLE);
+}
+
#[test]
fn brush_strokes_carry_auto_mask() {
let mut h = detail("panel.masking");
diff --git a/docs/parity.md b/docs/parity.md
index d0bdc35c2..4250b3940 100644
--- a/docs/parity.md
+++ b/docs/parity.md
@@ -356,7 +356,7 @@ under the UI command that wraps it (a no-op error); the UI command now wins.
| LR-MASK-OBJECTS | Object selection | P2 | 🟡 | `cmd:mask.add` (`object`, `prompt`), `cmd:mask.objectPoint`, `cmd:mask.refineDetail`, `cmd:segment.prepare`, `cmd:segment.model.status`, `cmd:segment.model.download`, `cmd:segment.model.cancel`, `crates/segment`, `crates/engine/src/segment/mod.rs` | SAM 3 in pure Rust (candle; Metal on macOS, CPU elsewhere): Object tile → click to include, ⌥-click to leave out; Describe tile → a text prompt selects every instance ("sky", "the red car"); both also as Add/Subtract/Intersect components; + / − under the selected mask; comma lists (`car, road`); a zoomed-in detail pass for 5–10× finer edges on small objects; per-selection Edge (hard ↔ soft). The model runs on its own worker thread (the UI never waits; panics become errors), is unloaded after 10 min idle, and is optional: the segmentation is stored with the mask (288² logits), so renders and exports never need it. Weights are never bundled (SAM License): without them the app offers a consented background download (mirrors, resume, timeouts, SHA-256) — **but no default download location is configured yet** (users set `LIGHTCRAFT_SAM3_MIRRORS` or install by hand: the dialog names the three files, shows the model folder and links docs/ai-masks.md). Not verified against Lightroom's Select Object; no brush/box object mode; the first click on a photo waits for its analysis (~4 s on an M4 Pro, much longer on CPU) |
| LR-MASK-PEOPLE | People parts | P2 | ⬜ | | shape exists (no UI): uses the photo's person / skin / hair / teeth / glasses mattes when it carries them (DNG semantic masks), else the subject heuristic |
| LR-MASK-LANDSCAPE | Landscape classes | P2 | ⬜ | | shape exists, evaluates empty |
-| LR-MASK-BRUSH | Brush mask | P0 | 🟡 | `cmd:tool.brush`, `cmd:mask.brushStroke` (`autoMask`), `crates/pipeline/src/masks.rs` | size/feather/flow/density/erase; Create New Mask starts a separate brush in paint mode; live-preview gaps (#517) and stationary accumulation (#518) remain; Auto Mask: dabs weighted by similarity to the colour under the dab centre, refined by a guided filter on luminance (CPU + GPU); no A/B brushes, no pressure |
+| LR-MASK-BRUSH | Brush mask | P0 | 🟡 | `cmd:tool.brush`, `cmd:mask.brushStroke` (`autoMask`), `crates/pipeline/src/masks.rs` | size/feather/flow/density/erase; Create New Mask starts a separate brush in paint mode; coverage builds up by passes, not hold time (holding the brush still adds nothing, #518); live-preview gaps (#517) remain; Auto Mask: dabs weighted by similarity to the colour under the dab centre, refined by a guided filter on luminance (CPU + GPU); no A/B brushes, no pressure |
| LR-MASK-LINEAR | Linear gradient | P0 | ✅ | `cmd:tool.linear`, `cmd:mask.update` | |
| LR-MASK-RADIAL | Radial gradient | P0 | ✅ | `cmd:tool.radial`, `cmd:mask.update`, `crates/ui-egui/src/panels/detail.rs` | drag the selected ellipse's body or centre pin to move; edge grips resize, outer grip rotates; rotated and compound radial components, hidden pins, outside hit rejection and one-step undo tested |
| LR-MASK-COLORRANGE | Colour range | P1 | ✅ | `cmd:mask.add` (`colorRange`), `cmd:mask.sampleColor`, `cmd:mask.update`, `crates/pipeline/src/lib.rs` (`color_range_sample`) | Color tile → click the photo to sample (⇧-click adds, up to 5; samples taken in the space the mask compares in), Pick button, Refine slider |