Skip to content

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

Merge PR #527

Brandon Thomasc5551783d250
diff --cc crates/ui-egui/src/tests_masking.rs
index 6a5f59e17,f0c5a762c..905b5e294
--- a/crates/ui-egui/src/tests_masking.rs
+++ b/crates/ui-egui/src/tests_masking.rs
@@@ -39,78 -39,99 +39,171 @@@ fn develop(h: &Headless) -> lightcraft_
(*h.app.session.develop_of(id).unwrap_or_default()).clone()
}
+ fn click(h: &mut Headless, id: &str) {
+ let r = h.request("ui.clickWidget", json!({"id": id}), T);
+ assert_eq!(r["ok"], true, "{id}: {r}");
+ }
+
+ fn brush_at(h: &mut Headless, x: f64, y: f64) {
+ pointer(h, json!([{"kind": "down", "x": x, "y": y}, {"kind": "up", "x": x, "y": y}]));
+ }
+
+ /// Evaluate the real composite, with a deterministic embedded sky matte (no AI model required).
+ fn mask_coverage(h: &Headless, mask: usize, x: usize, y: usize) -> f32 {
+ use lightcraft_pipeline::{
+ geometry::Frame,
+ masks::{MatteKind, Mattes, evaluate_one},
+ };
+ use lightcraft_raster::{Image, Plane, Rgb32f};
+ let d = develop(h);
+ let mut sky = Image::<u8>::new(100, 100);
+ sky.data.fill(255);
+ let mut mattes = Mattes::default();
+ mattes.push(MatteKind::Sky, sky);
+ let a = evaluate_one(
+ &d.masks[mask],
+ &Frame::new(100, 100, &Default::default(), true),
+ 100,
+ 100,
+ &Rgb32f::new(100, 100),
+ &Plane::new(100, 100),
+ 0.0,
+ Some(&mattes),
+ );
+ a.get(x, y)
+ }
+
+ #[test]
+ fn subtract_brush_removes_sky_coverage_and_add_restores_it() {
+ let mut h = detail("panel.masking");
+ exec(&mut h, "mask.add", json!({"kind": "sky"}));
+ let r = h.request("ui.set", json!({"brushSize": 0.15, "brushFeather": 0.0, "brushFlow": 100.0}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ click(&mut h, "button:maskMinus:1");
+ click(&mut h, "maskComp:subtract:brush");
+ brush_at(&mut h, 0.5, 0.5);
+ assert!(mask_coverage(&h, 0, 50, 50) < 0.01, "Subtract > Brush must remove sky coverage");
+ assert!(mask_coverage(&h, 0, 10, 10) > 0.99, "unpainted sky stays selected");
+ let subtracted = develop(&h);
+ exec(&mut h, "edit.undo", json!({}));
+ assert!(mask_coverage(&h, 0, 50, 50) > 0.99);
+ exec(&mut h, "edit.redo", json!({}));
+ assert_eq!(develop(&h).masks, subtracted.masks);
+
+ // Renaming must not detach subsequent strokes from the subtract component.
+ exec(&mut h, "mask.component", json!({"component": 1, "action": "rename", "name": "Sky cleanup"}));
+ brush_at(&mut h, 0.25, 0.5);
+ assert!(mask_coverage(&h, 0, 25, 50) < 0.01);
+ assert_eq!(develop(&h).masks[0].components.len(), 2);
+
+ // Erase removes paint from the subtract component, restoring the underlying sky.
+ h.app.ui.brush_erase = true;
+ brush_at(&mut h, 0.5, 0.5);
+ assert!(mask_coverage(&h, 0, 50, 50) > 0.99);
+ click(&mut h, "button:maskPlus:1");
+ click(&mut h, "maskComp:add:brush");
+ brush_at(&mut h, 0.25, 0.5);
+ assert!(mask_coverage(&h, 0, 25, 50) > 0.99, "Add must be composed after Subtract");
+ assert_eq!(develop(&h).masks[0].components.len(), 3);
+ h.settle(SETTLE);
+ }
+
+ #[test]
+ fn new_brush_starts_a_separate_mask_in_paint_mode() {
+ let mut h = detail("panel.masking");
+ exec(&mut h, "mask.add", json!({"kind": "sky"}));
+ let original = develop(&h).masks[0].clone();
+ h.app.ui.brush_erase = true;
+ h.app.ui.mask_overlay = false;
+ click(&mut h, "maskNew:brush");
+ assert_eq!(h.app.session.active_mask, Some(2));
+ assert!(!h.app.ui.brush_erase);
+ assert!(h.app.ui.mask_overlay);
+ let r = h.request("ui.set", json!({"brushSize": 0.15, "brushFeather": 0.0, "brushFlow": 100.0}), T);
+ assert_eq!(r["ok"], true, "{r}");
+ brush_at(&mut h, 0.5, 0.5);
+ assert!(mask_coverage(&h, 1, 50, 50) > 0.99);
+ h.app.ui.brush_erase = true;
+ brush_at(&mut h, 0.5, 0.5);
+ assert!(mask_coverage(&h, 1, 50, 50) < 0.01);
+ exec(&mut h, "edit.undo", json!({}));
+ assert!(mask_coverage(&h, 1, 50, 50) > 0.99);
+ assert_eq!(develop(&h).masks[0], original);
+ h.settle(SETTLE);
+ }
+
+#[test]
+fn radial_body_drag_moves_rotated_components_and_undoes_once() {
+ let mut h = detail("panel.masking");
+ exec(&mut h, "mask.add", json!({"kind": "luminanceRange"}));
+ exec(&mut h, "mask.addComponent", json!({"kind": "radial", "op": "add", "center": [0.45, 0.5], "rx": 0.22, "ry": 0.1, "angle": 40}));
+ let before = develop(&h);
+ // Well away from the pin and edge/rotation grips, inside a rotated ellipse.
+ pointer(
+ &mut h,
+ json!([{"kind": "down", "x": 0.51, "y": 0.56}, {"kind": "drag", "x": 0.54, "y": 0.58},
+ {"kind": "drag", "x": 0.61, "y": 0.66}, {"kind": "up", "x": 0.61, "y": 0.66}]),
+ );
+ let after = develop(&h);
+ let MaskShape::Radial { center, rx, ry, angle, feather, invert } = &after.masks[0].components[1].shape else { panic!("radial") };
+ assert!((center.x - 0.55).abs() < 0.005 && (center.y - 0.6).abs() < 0.005, "{center:?}");
+ let MaskShape::Radial { rx: old_rx, ry: old_ry, angle: old_angle, feather: old_feather, invert: old_invert, .. } =
+ &before.masks[0].components[1].shape
+ else {
+ panic!("radial")
+ };
+ assert_eq!((rx, ry, angle, feather, invert), (old_rx, old_ry, old_angle, old_feather, old_invert));
+ assert_eq!(after.masks[0].components[0], before.masks[0].components[0], "only the hit component moves");
+ exec(&mut h, "edit.undo", json!({}));
+ assert_eq!(develop(&h), before, "one undo reverses the entire drag");
+ exec(&mut h, "edit.redo", json!({}));
+ assert_eq!(develop(&h), after);
+ // The bounding-box corner is outside the ellipse, even for an inverted radial mask.
+ exec(&mut h, "mask.component", json!({"component": 1, "action": "invert"}));
+ let outside_before = develop(&h);
+ pointer(
+ &mut h,
+ json!([{"kind": "down", "x": 0.35, "y": 0.75}, {"kind": "drag", "x": 0.4, "y": 0.8},
+ {"kind": "up", "x": 0.4, "y": 0.8}]),
+ );
+ assert_eq!(develop(&h), outside_before, "outside the ellipse must not move it");
+ // Hiding pins does not disable dragging the selected ellipse body.
+ exec(&mut h, "view.maskPins", json!({"show": false}));
+ pointer(
+ &mut h,
+ json!([{"kind": "down", "x": 0.61, "y": 0.66}, {"kind": "drag", "x": 0.66, "y": 0.71},
+ {"kind": "up", "x": 0.66, "y": 0.71}]),
+ );
+ let MaskShape::Radial { center, .. } = develop(&h).masks[0].components[1].shape.clone() else { panic!("radial") };
+ assert!((center.x - 0.6).abs() < 0.005 && (center.y - 0.65).abs() < 0.005, "{center:?}");
+}
+
+#[test]
+fn radial_body_drag_keeps_resize_and_rotation_handles() {
+ let mut h = detail("panel.masking");
+ exec(&mut h, "mask.add", json!({"kind": "radial", "center": [0.5, 0.5], "rx": 0.18, "ry": 0.1}));
+ for handle in [1, 5] {
+ let widget = format!("maskHandle:1:0:{handle}");
+ let rect = h.app.widgets.iter().find(|(id, _)| *id == widget).unwrap().1;
+ let image = h.app.image_rect.unwrap();
+ let q = rect.center();
+ let x = (q.x - image.left()) / image.width();
+ let y = (q.y - image.top()) / image.height();
+ pointer(
+ &mut h,
+ json!([{"kind": "down", "x": x, "y": y}, {"kind": "drag", "x": x + 0.03, "y": y + 0.03},
+ {"kind": "up", "x": x + 0.03, "y": y + 0.03}]),
+ );
+ let MaskShape::Radial { center, rx, angle, .. } = develop(&h).masks[0].components[0].shape.clone() else { panic!("radial") };
+ assert!((center.x - 0.5).abs() < 0.005 && (center.y - 0.5).abs() < 0.005, "a handle must not translate the mask");
+ if handle == 1 {
+ assert!(rx > 0.19, "resize grip changes radius");
+ } else {
+ assert!(angle.abs() > 1.0, "rotation grip changes angle");
+ }
+ }
+}
+
#[test]
fn mask_overlay_keys_and_pins() {
use lightcraft_pipeline::{MaskView, Overlay};
diff --cc docs/parity.md
index b8c71608e,df0d667a3..ded0a4745
--- a/docs/parity.md
+++ b/docs/parity.md
@@@ -352,9 -352,9 +352,9 @@@ under the UI command that wraps it (a n
| 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; 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; 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-LINEAR | Linear gradient | P0 | βœ… | `cmd:tool.linear`, `cmd:mask.update` | |
-| LR-MASK-RADIAL | Radial gradient | P0 | βœ… | `cmd:tool.radial`, `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 |
| LR-MASK-LUMRANGE | Luminance range | P1 | βœ… | `cmd:mask.add` (`luminanceRange`), `cmd:mask.update`, `crates/ui-egui/src/panels/masking.rs` (`range_controls`) | range bar with two handles (one undo step per drag), Smoothness, Show Luminance Map (B&W photo with the range tinted) |
| LR-MASK-DEPTHRANGE | Depth range | P2 | ⬜ | | shape exists, needs depth data |