Skip to content

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

M12: merge origin/main into the SAM 3 masks branch (PR #162)

Brandon Thomasfefd61c96797
diff --cc crates/engine/src/tests.rs
index 65104127f,2837d6726..d005d5082
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@@ -728,36 -728,58 +728,92 @@@ fn curve_reset_by_channel_and_whole()
assert!(s.execute("curve.reset", &json!({"channel": "alpha"})).is_err());
}
+#[test]
+fn ai_masks_without_the_model_explain_themselves() {
+ let mut s = demo();
+ let id = s.active().unwrap();
+ // an Object mask that could never be computed is refused, not left empty
+ let e = s.execute("mask.add", &json!({"kind": "object"})).unwrap_err().to_string();
+ assert!(e.contains("SAM 3") || e.contains("not available"), "{e}");
+ assert!(!e.contains("invalid parameters"), "shown as is: {e}");
+ assert!(s.develop_of(id).unwrap().masks.is_empty());
+ // an empty Object component (as an older document may hold) still takes clicks
+ let shape = lightcraft_develop::MaskShape::Object { hint: vec![], exclude: vec![], seg: None, detail: vec![], edge: 0.0 };
+ let mut d = (*s.develop_of(id).unwrap()).clone();
+ d.masks.push(lightcraft_develop::Mask {
+ id: 1,
+ components: vec![lightcraft_develop::MaskComponent { name: None, op: lightcraft_develop::MaskOp::Add, invert: false, shape }],
+ ..Default::default()
+ });
+ s.set_develop(id, d, "test").unwrap();
+ s.active_mask = Some(1);
+ // clicks and descriptions need the model: an error that says what's missing, nothing changed
+ let e = s.execute("mask.objectPoint", &json!({"x": 0.5, "y": 0.5})).unwrap_err().to_string();
+ assert!(e.contains("SAM 3") || e.contains("not available"), "{e}");
+ let e = s.execute("mask.add", &json!({"kind": "prompt", "text": "sky"})).unwrap_err().to_string();
+ assert!(e.contains("SAM 3") || e.contains("not available"), "{e}");
+ let empty = std::env::temp_dir().join(format!("lc-no-sam3-{}", std::process::id()));
+ s.segmenter.dir = Some(empty.clone());
+ let e = s.execute("mask.objectPoint", &json!({"x": 0.5, "y": 0.5})).unwrap_err().to_string();
+ assert!(e.contains(&empty.display().to_string()) || e.contains("not available"), "names the folder: {e}");
+ assert_eq!(s.develop_of(id).unwrap().masks.len(), 1);
+ // bad input is an error, not a crash
+ assert!(s.execute("mask.objectPoint", &json!({"x": 1.5, "y": 0.5})).is_err());
+ assert!(s.execute("mask.add", &json!({"kind": "prompt", "text": " "})).is_err());
+}
++
+ /// A People card's close-up is a square (in pixels) inside the photo, around the face, whatever the
+ /// face rectangle says: corners, oversized and degenerate rectangles never leave the frame or panic.
+ #[test]
+ fn face_job_crops_a_square_inside_the_photo() {
+ use lightcraft_geom::Rect;
+ let mut s = demo();
+ let id = s.active().unwrap();
+ let (w, h) = {
+ let p = s.catalog.photo(id).unwrap();
+ (f64::from(p.width), f64::from(p.height))
+ };
+ let faces = [
+ Rect { x0: 0.40, y0: 0.30, x1: 0.50, y1: 0.45 },
+ Rect { x0: 0.0, y0: 0.0, x1: 0.05, y1: 0.05 },
+ Rect { x0: 0.95, y0: 0.95, x1: 1.0, y1: 1.0 },
+ Rect { x0: -5.0, y0: -5.0, x1: 5.0, y1: 5.0 },
+ Rect { x0: 0.5, y0: 0.5, x1: 0.5, y1: 0.5 },
+ Rect { x0: f64::NAN, y0: 0.5, x1: 0.6, y1: f64::INFINITY },
+ ];
+ for face in faces {
+ let job = s.face_job(id, face, 256).expect("a job for a photo in the library");
+ let r = job.settings.crop.geometry.rect;
+ assert!((0.0..=1.0).contains(&r.x0) && (0.0..=1.0).contains(&r.y0) && r.x1 <= 1.0 + 1e-9 && r.y1 <= 1.0 + 1e-9, "{face:?} → {r:?}");
+ assert!(r.x1 > r.x0 && r.y1 > r.y0, "{face:?} → {r:?}");
+ let (pw, ph) = ((r.x1 - r.x0) * w, (r.y1 - r.y0) * h);
+ assert!((pw - ph).abs() < 1e-6 * w.max(h), "square in pixels: {face:?} → {pw} × {ph}");
+ assert_eq!(job.settings.crop.geometry.angle, 0.0);
+ }
+ // a face around (0.45, 0.375) is centred in its crop (room to spare on every side)
+ let r = s.face_job(id, faces[0], 256).unwrap().settings.crop.geometry.rect;
+ assert!((((r.x0 + r.x1) / 2.0) - 0.45).abs() < 1e-9 && (((r.y0 + r.y1) / 2.0) - 0.375).abs() < 1e-9, "{r:?}");
+ assert!(s.face_job(lightcraft_catalog::PhotoId(u64::MAX), faces[0], 256).is_none());
+ }
+
+ /// Regions are on the upright photo; after Rotate Right the close-up still frames the face: the box
+ /// is carried into the rotated frame the crop lives in, and the crop stays square in rotated pixels.
+ #[test]
+ fn face_job_follows_rotate_right() {
+ use lightcraft_geom::Rect;
+ let mut s = demo();
+ let id = s.active().unwrap();
+ let (w, h) = {
+ let p = s.catalog.photo(id).unwrap();
+ (f64::from(p.width), f64::from(p.height))
+ };
+ // a small face left of centre on the upright photo
+ let face = Rect { x0: 0.30, y0: 0.45, x1: 0.35, y1: 0.50 };
+ s.execute("photo.rotateRight", &json!({})).unwrap();
+ let r = s.face_job(id, face, 256).unwrap().settings.crop.geometry.rect;
+ // a quarter turn clockwise puts the left of the photo at the top: centre (0.325, 0.475) → (0.525, 0.325)
+ assert!((((r.x0 + r.x1) / 2.0) - 0.525).abs() < 1e-9 && (((r.y0 + r.y1) / 2.0) - 0.325).abs() < 1e-9, "{r:?}");
+ // the rotated photo is h × w pixels
+ let (pw, ph) = ((r.x1 - r.x0) * h, (r.y1 - r.y0) * w);
+ assert!((pw - ph).abs() < 1e-6 * w.max(h), "square in rotated pixels: {pw} × {ph}");
+ }
diff --cc crates/ui-egui/src/lib.rs
index 0afdd915d,bdf6f4f40..bdc2780f3
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@@ -345,38 -348,15 +349,43 @@@ impl LightcraftApp
}
pub fn toast(&mut self, ctx: &egui::Context, text: impl Into<String>) {
+ self.toast_for(ctx, text, 1.4);
+ }
+
+ /// A toast that stays `secs` seconds (messages that say where to look or what to do next).
+ pub fn toast_for(&mut self, ctx: &egui::Context, text: impl Into<String>, secs: f64) {
let t = ctx.input(|i| i.time);
- self.ui.toast = Some((text.into(), t + 1.4));
+ self.ui.toast = Some((text.into(), t + secs));
}
+ /// AI masks: start a zoomed-in detail pass once the clicking stops (`ui.detail_due`), and
+ /// apply finished passes.
+ fn ai_mask_detail(&mut self, ctx: &egui::Context) {
+ if self.session.segment_poll() {
+ ctx.request_repaint();
+ }
+ let now = ctx.input(|i| i.time);
+ if let Some((due, mask)) = self.ui.detail_due {
+ if now >= due && !self.session.segmenter.busy() && !self.session.segmenter.detail_busy() {
+ self.ui.detail_due = None;
+ if let Err(e) = self.run("mask.refineDetail", serde_json::json!({"id": mask})) {
+ log::warn!("detail pass: {e}");
+ }
+ } else {
+ ctx.request_repaint_after(std::time::Duration::from_millis(150));
+ }
+ }
+ if self.session.segmenter.detail_busy() {
+ ctx.request_repaint_after(std::time::Duration::from_millis(200));
+ }
+ }
+
+ /// A toast for an error the user has to read and act on (stays 6 s).
+ pub fn toast_error(&mut self, ctx: &egui::Context, text: impl Into<String>) {
+ let t = ctx.input(|i| i.time);
+ self.ui.toast = Some((text.into(), t + 6.0));
+ }
+
fn drain_control(&mut self, ctx: &egui::Context) {
let Some(rx) = self.control_rx.take() else { return };
while let Ok(req) = rx.try_recv() {
diff --cc docs/parity.md
index 2967d3115,38d768af5..c15d1c098
--- a/docs/parity.md
+++ b/docs/parity.md
@@@ -31,7 -31,7 +31,7 @@@ and prints the summary; `cargo xtask pa
| H. Crop & rotate (CROP) | 9 | 0 | 0 | 1 | 6/6 (100%) | 3/3 (100%) |
| I. Remove / healing (REM) | 7 | 1 | 2 | 2 | 4/4 (100%) | 2/3 (67%) |
| J. Red eye (EYE) | 2 | 0 | 0 | 0 | — | 1/1 (100%) |
--| K. Masking (MASK) | 14 | 3 | 6 | 0 | 8/8 (100%) | 5/5 (100%) |
++| K. Masking (MASK) | 14 | 4 | 5 | 0 | 8/8 (100%) | 5/5 (100%) |
| L. Presets (PRE) | 6 | 0 | 1 | 1 | 2/2 (100%) | 2/2 (100%) |
| M. Versions & history (VER) | 5 | 0 | 0 | 0 | 1/1 (100%) | 3/3 (100%) |
| N. Copy / paste / sync (SYNC) | 5 | 0 | 0 | 0 | 3/3 (100%) | 1/1 (100%) |
@@@ -44,13 -44,13 +44,13 @@@
| U. Map & location (MAP) | 0 | 1 | 1 | 0 | — | 0/1 (0%) |
| V. Preferences (PREF) | 5 | 0 | 3 | 3 | 1/1 (100%) | 4/4 (100%) |
| W. Cloud & AI infrastructure (CLOUD / AI) | 1 | 0 | 1 | 2 | — | — |
- | X. Cross-cutting behaviours (BEHAV) | 15 | 3 | 2 | 1 | 8/8 (100%) | 6/8 (75%) |
+ | X. Cross-cutting behaviours (BEHAV) | 16 | 4 | 1 | 1 | 8/8 (100%) | 6/8 (75%) |
| Y. Menus | 81 | 0 | 4 | 8 | 47/47 (100%) | 23/23 (100%) |
| Z. Keyboard shortcuts (desktop) | 74 | 3 | 3 | 1 | 49/52 (94%) | 22/23 (96%) |
- | Lightroom Classic extras | 31 | 9 | 40 | 9 | — | 21/22 (95%) |
- | **Total** | 387 | 29 | 91 | 37 | 194/200 (97%) | 139/149 (93%) |
+ | Lightroom Classic extras | 31 | 10 | 39 | 9 | — | 21/22 (95%) |
-| **Total** | 388 | 33 | 87 | 37 | 194/200 (97%) | 139/149 (93%) |
++| **Total** | 388 | 34 | 86 | 37 | 194/200 (97%) | 139/149 (93%) |
- Weighted completion (✅ = 1, 🟡 = ½, 🚫 left out): **79.2%** of 507 in-scope rows — P0 98.5% of 200 · P1 95.6% of 149 · P2 39.2% of 158.
-Weighted completion (✅ = 1, 🟡 = ½, 🚫 left out): **79.6%** of 508 in-scope rows — P0 98.5% of 200 · P1 95.6% of 149 · P2 40.9% of 159.
++Weighted completion (✅ = 1, 🟡 = ½, 🚫 left out): **79.7%** of 508 in-scope rows — P0 98.5% of 200 · P1 95.6% of 149 · P2 41.2% of 159.
<!-- /parity:summary -->
## Top gaps