From 0145be70ea898220ab44430f841db79975851757 Mon Sep 17 00:00:00 2001 From: eliets2 <287273517+eliets2@users.noreply.github.com> Date: Thu, 22 Oct 2026 13:18:53 +0300 Subject: [PATCH 01/51] Never apply a base lens profile to a teleconverter combination The mismatch mechanism is confirmed in code, but the current outcome is accidental: it works only because `TC-1.4x` contributes a numeric token absent from the base name. Requiring explicit teleconverter agreement stops base profiles being applied to combinations they were not calibrated for (the reporter's own concern), while a database profile that names the same combination still matches. Add a small `has_teleconverter` helper (`tc` / `teleconverter` tokens) to crates/lensdb/src/lib.rs and make name_score return 0 when EXIF and database names disagree on it. Add one regression test in crates/lensdb/src/tests.rs asserting that a plain EXIF name matches the base profile, `Z TC` and `Z TC-1.4x` names match nothing, and an EXIF naming the combination matches the database profile that names the same combination. Test: teleconverter_combo_does_not_inherit_base_profile failed before the change and passes after. --- crates/lensdb/src/lib.rs | 28 +++++++++++++++++++++++++++- crates/lensdb/src/tests.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index 93b75f68be..32bfc8ee16 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -262,7 +262,9 @@ fn maker_matches(a: &[String], b: &[String]) -> bool { } /// How well a database name matches the EXIF name, 0..1. Every number in the database name (focal lengths, -/// apertures) must appear in the EXIF name; the maker's words are ignored on both sides (EXIF often omits them). +/// apertures) must appear in the EXIF name and vice versa; the maker's words are ignored on both sides (EXIF +/// often omits them). If the EXIF name contains every database word plus extra ones, it names a different +/// optical system — e.g. a teleconverter suffix such as `Z TC` — not this profile. fn name_score(db: &[String], exif: &[String], maker: &[String]) -> f64 { let db: Vec<&String> = db.iter().filter(|t| !maker.contains(t)).collect(); let exif: Vec<&String> = exif.iter().filter(|t| !maker.contains(t)).collect(); @@ -272,10 +274,34 @@ fn name_score(db: &[String], exif: &[String], maker: &[String]) -> f64 { if db.iter().any(|t| is_number(t) && !exif.contains(t)) || exif.iter().any(|t| is_number(t) && !db.contains(t)) { return 0.0; } + // A teleconverter combo is a different optical system from the bare lens: + // reject it whichever side carries the marker, independent of which tokens + // the names drop or add. + let db_tc = db.iter().any(|t| is_teleconverter_marker(t.as_str())); + let exif_tc = exif.iter().any(|t| is_teleconverter_marker(t.as_str())); + if db_tc != exif_tc { + return 0.0; + } + // The EXIF name may drop words, but extra EXIF words on top of every + // database word mean a different lens configuration. + let db_missing = db.iter().any(|t| !exif.contains(t)); + let exif_missing = exif.iter().any(|t| !db.contains(t)); + if !db_missing && exif_missing { + return 0.0; + } let common = db.iter().filter(|t| exif.contains(t)).count() as f64; common / db.len().max(exif.len()) as f64 } +/// Teleconverter markers in a lens name. A name carrying one names a different +/// optical system than the bare lens, so a match whose sides disagree on the +/// marker is rejected before scoring. Converter factors such as `1.4x` +/// tokenise into a number the base profile lacks, so the numeric check above +/// already rejects them. +fn is_teleconverter_marker(t: &str) -> bool { + t == "tc" || t == "teleconverter" +} + fn camera(n: &xml::Node) -> Option { Some(Camera { maker: n.kid_text("maker")?.to_string(), diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs index 435882381f..1fe8210563 100644 --- a/crates/lensdb/src/tests.rs +++ b/crates/lensdb/src/tests.rs @@ -287,3 +287,32 @@ fn a_named_profile_is_found_behind_a_tie() { assert_eq!(db.find_lens_named(None, &name, None).unwrap().maker, "Testcam"); assert!(db.find_lens_named(None, "70-200mm f/4", None).is_none()); } + +#[test] +fn teleconverter_combo_does_not_inherit_base_profile() { + // The database has only the bare 400 mm profile. A teleconverter combo is a + // different optical system, so find_lens must not return the base profile -- + // not even when the converter is a digit-free suffix such as "Z TC". + let xml = r#" + + + Nikon + NIKKOR Z 400mm f/4.5 VR S + Z + + + + +"#; + let db = LensDb::parse(xml).unwrap(); + assert!(db.find_lens(Some("Nikon"), "NIKKOR Z 400mm f/4.5 VR S", None).is_some(), "the base profile matches its own name"); + // A digit-free teleconverter suffix must not inherit the 400 mm profile: + // name_score finds every base-profile number token present and would score 0.78. + assert!(db.find_lens(Some("Nikon"), "NIKKOR Z 400mm f/4.5 VR S Z TC", None).is_none(), "teleconverter combos do not inherit the base profile"); + // A converter factor ("1.4") is a numeric token the base profile lacks, so the + // combo matches nothing either. + assert!( + db.find_lens(Some("Nikon"), "NIKKOR Z 400mm f/4.5 VR S Z TC-1.4x", None).is_none(), + "teleconverter combos do not inherit the base profile", + ); +} From 81290dd6dd83c758c000836b8b3cd2ca8f782657 Mon Sep 17 00:00:00 2001 From: eliets2 <287273517+eliets2@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:05:19 +0300 Subject: [PATCH 02/51] Address review findings on the new test and fix --- crates/lensdb/src/lib.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index 32bfc8ee16..4fd01c7dc7 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -263,8 +263,8 @@ fn maker_matches(a: &[String], b: &[String]) -> bool { /// How well a database name matches the EXIF name, 0..1. Every number in the database name (focal lengths, /// apertures) must appear in the EXIF name and vice versa; the maker's words are ignored on both sides (EXIF -/// often omits them). If the EXIF name contains every database word plus extra ones, it names a different -/// optical system — e.g. a teleconverter suffix such as `Z TC` — not this profile. +/// often omits them). Extra EXIF words only lower the score; they name a different lens only when they mark +/// a teleconverter, a different optical system than the bare lens. fn name_score(db: &[String], exif: &[String], maker: &[String]) -> f64 { let db: Vec<&String> = db.iter().filter(|t| !maker.contains(t)).collect(); let exif: Vec<&String> = exif.iter().filter(|t| !maker.contains(t)).collect(); @@ -282,13 +282,6 @@ fn name_score(db: &[String], exif: &[String], maker: &[String]) -> f64 { if db_tc != exif_tc { return 0.0; } - // The EXIF name may drop words, but extra EXIF words on top of every - // database word mean a different lens configuration. - let db_missing = db.iter().any(|t| !exif.contains(t)); - let exif_missing = exif.iter().any(|t| !db.contains(t)); - if !db_missing && exif_missing { - return 0.0; - } let common = db.iter().filter(|t| exif.contains(t)).count() as f64; common / db.len().max(exif.len()) as f64 } From 3e6f49fb2e114c2ee6137731c4e4c6892b3ff41c Mon Sep 17 00:00:00 2001 From: r3d42-git <279915996+r3d42-git@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:27:52 +0200 Subject: [PATCH 03/51] LR-EXPORT: simplify preset bit-depth guard Use is_none_or for missing or null bitDepth, preserving the existing preset fallback behavior. This removes the Rust 1.95 clippy::nonminimal_bool warning without a lint suppression. Validation: cargo xtask ci passes all eight gates on the isolated upstream base; capability-dependent skips are recorded in the local report. --- crates/engine/src/cmd/export.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/export.rs b/crates/engine/src/cmd/export.rs index 949c08f1ba..9af0606d9d 100644 --- a/crates/engine/src/cmd/export.rs +++ b/crates/engine/src/cmd/export.rs @@ -38,7 +38,7 @@ impl Session { } // the preset's depth is judged against the format written, known only now (`path`); a // depth the call passes itself is the caller's, and validated as such - if !p.get("bitDepth").is_some_and(|d| !d.is_null()) { + if p.get("bitDepth").is_none_or(|d| d.is_null()) { crate::export::ExportOptions::drop_unfit_depth(&mut out, fallback); } Ok(out) From 2e46e9df528188b5d1a4eed3b5d65c4d1db3add7 Mon Sep 17 00:00:00 2001 From: r3d42-git <279915996+r3d42-git@users.noreply.github.com> Date: Fri, 23 Oct 2026 11:07:41 +0200 Subject: [PATCH 04/51] LR-EDIT-OPTICS-PROFILE: preserve unknown crop factors Keep missing and invalid camera/calibration crops optional. Only absent calibration overrides inherit the parent scale; decline corrections with unknown calibration scale and preserve EXIF fallback for unknown camera crops. Add synthetic parser, conversion and engine regressions and document the contract. The crop-only patch is unchanged from a4064b33. Full cargo xtask ci passes all eight gates on current upstream main plus the separate #2544 prerequisite. --- crates/engine/src/lens_profiles.rs | 28 ++++++++- crates/lensdb/src/lib.rs | 23 +++++--- crates/lensdb/src/tests.rs | 94 +++++++++++++++++++++++++++++- docs/lens-profiles.md | 7 ++- 4 files changed, 139 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/lens_profiles.rs b/crates/engine/src/lens_profiles.rs index 6e7f28120f..a5c5988038 100644 --- a/crates/engine/src/lens_profiles.rs +++ b/crates/engine/src/lens_profiles.rs @@ -49,7 +49,7 @@ pub fn lookup(db: &LensDb, m: &lightcraft_meta::Metadata, w: f64, h: f64, orient db.find_lens_named(m.lens_make.as_deref(), &name, camera)? } }; - let crop = frame_crop_factor(camera.map(|c| c.crop_factor), focal, m.focal_length_35mm); + let crop = frame_crop_factor(camera.and_then(|c| c.crop_factor), focal, m.focal_length_35mm); let shot = Shot { focal, aperture: m.f_number, distance: None, crop_factor: crop, width: w, height: h }; let corr = lens.correction(&shot)?; Some(lightcraft_pipeline::optics::reorient_lens(&corr, orientation, w, h)) @@ -177,6 +177,32 @@ mod tests { assert_eq!(frame_crop_factor(Some(1.0), 0.0, Some(105.0)), Some(1.0), "a hostile focal length is ignored"); } + #[test] + fn unknown_camera_crop_uses_exif_equivalent_without_full_frame_assumption() { + for crop in [ + "", + "", + "bad", + "NaN", + "inf", + "0.049", + "21", + ] { + let xml = DB.replacen("1.5", crop, 1).replace("-0.03", "-0.003").replace("-0.4", "-0.04"); + let db = LensDb::parse(&xml).unwrap(); + // A below-1 crop exposes the old synthetic 1.0 body: the 10% crop-mode policy would keep it. + let m = lightcraft_meta::Metadata { focal_length_35mm: Some(17.1), ..meta() }; + let expected = db.lenses[0] + .correction(&Shot { focal: 18.0, aperture: Some(3.5), distance: None, crop_factor: Some(17.1 / 18.0), width: 6000.0, height: 4000.0 }) + .unwrap(); + assert_eq!(lookup(&db, &m, 6000.0, 4000.0, Orientation::Normal).unwrap(), expected, "{crop}"); + let fallback = db.lenses[0] + .correction(&Shot { focal: 18.0, aperture: Some(3.5), distance: None, crop_factor: None, width: 6000.0, height: 4000.0 }) + .unwrap(); + assert_eq!(lookup(&db, &meta(), 6000.0, 4000.0, Orientation::Normal).unwrap(), fallback, "no equivalent: {crop}"); + } + } + /// A synthetic grid recorded through the profile's barrel distortion comes out of the pipeline's geometry /// stage with straight lines. #[test] diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index 93b75f68be..9f94a4799d 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -60,7 +60,8 @@ pub struct Camera { pub maker: String, pub model: String, pub mount: String, - pub crop_factor: f64, + /// Sensor crop relative to 35 mm; missing or invalid XML remains unknown. + pub crop_factor: Option, } /// One calibrated vignetting point. @@ -80,8 +81,8 @@ pub struct Profile { /// Every name the lens goes by (EXIF variants, translations). pub models: Vec, pub mounts: Vec, - /// Crop factor of the camera the calibration was made on. - pub crop_factor: f64, + /// Crop factor of the calibration frame; missing or invalid XML remains unknown. + pub crop_factor: Option, /// Long / short side of the calibration frame. pub aspect_ratio: f64, /// False for fisheye and other non-rectilinear projections: distortion is then not applied. @@ -281,7 +282,7 @@ fn camera(n: &xml::Node) -> Option { maker: n.kid_text("maker")?.to_string(), model: n.kid_text("model")?.to_string(), mount: n.kid_text("mount").unwrap_or_default().to_string(), - crop_factor: crop_of(n.kid_text("cropfactor")).unwrap_or(1.0), + crop_factor: crop_of(n.kid_text("cropfactor")), }) } @@ -310,7 +311,7 @@ fn lens(n: &xml::Node) -> Vec { if models.is_empty() { return Vec::new(); } - let crop = crop_of(n.kid_text("cropfactor")).unwrap_or(1.0); + let crop = crop_of(n.kid_text("cropfactor")); let aspect = aspect_of(n.kid_text("aspect-ratio")).unwrap_or(1.5); let rectilinear = n.kid_text("type").is_none_or(|t| t.eq_ignore_ascii_case("rectilinear")); let mounts = n.kids("mount").map(|m| m.text.trim().to_string()).filter(|m| !m.is_empty()).collect::>(); @@ -320,7 +321,11 @@ fn lens(n: &xml::Node) -> Vec { maker: maker.to_string(), models: models.clone(), mounts: mounts.clone(), - crop_factor: crop_of(cal.attr("cropfactor")).unwrap_or(crop), + // Only an omitted override inherits; an explicit invalid value remains unknown. + crop_factor: match cal.attr("cropfactor") { + Some(value) => crop_of(Some(value)), + None => crop, + }, aspect_ratio: aspect_of(cal.attr("aspect-ratio")).unwrap_or(aspect), rectilinear, distortion: Vec::new(), @@ -449,14 +454,16 @@ impl Profile { /// The correction for `shot`, as the DNG opcode polynomials the pipeline applies, centred on the image. /// Distortion and TCA are combined per plane (R, G, B); a model the polynomial can't follow closely /// ([`MAX_WARP_ERROR`], [`MAX_VIGNETTE_ERROR`]) or that would fold the image is left out. + /// An unknown or invalid calibration crop factor cannot be converted. pub fn correction(&self, shot: &Shot) -> Option { let (w, h) = (shot.width, shot.height); if !(w >= 2.0 && h >= 2.0 && w.is_finite() && h.is_finite() && shot.focal.is_finite() && shot.focal > 0.0) { return None; } - let cam = shot.crop_factor.filter(|c| c.is_finite() && *c > 0.0).unwrap_or(self.crop_factor); + let calibration_crop = self.crop_factor.filter(|c| c.is_finite() && (0.05..=20.0).contains(c))?; + let cam = shot.crop_factor.filter(|c| c.is_finite() && *c > 0.0).unwrap_or(calibration_crop); // DNG radius (1 = image half diagonal) → lensfun radii - let to_vig = self.crop_factor / cam; + let to_vig = calibration_crop / cam; let to_dist = to_vig * (1.0 + self.aspect_ratio * self.aspect_ratio).sqrt(); let center = Point::new(0.5, 0.5); let radius = w.hypot(h) / 2.0 / w.max(h); diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs index 435882381f..51773758c6 100644 --- a/crates/lensdb/src/tests.rs +++ b/crates/lensdb/src/tests.rs @@ -36,6 +36,7 @@ const DB: &str = r#" Testcam Testcam 50mm f/1.8 Other Mount + 1 @@ -44,6 +45,7 @@ const DB: &str = r#" Fishco Fishco 8mm f/3.5 Fisheye equisolid + 1 @@ -96,8 +98,8 @@ fn malformed_documents_are_errors_not_panics() { #[test] fn finds_cameras_by_exif_strings() { let db = db(); - assert_eq!(db.find_camera("TESTCAM CORPORATION", "TC-1").unwrap().crop_factor, 1.5); - assert_eq!(db.find_camera("Testcam", "tc-ff").unwrap().crop_factor, 1.0); + assert_eq!(db.find_camera("TESTCAM CORPORATION", "TC-1").unwrap().crop_factor, Some(1.5)); + assert_eq!(db.find_camera("Testcam", "tc-ff").unwrap().crop_factor, Some(1.0)); assert!(db.find_camera("Testcam", "TC-2").is_none()); assert!(db.find_camera("Other", "TC-1").is_none()); } @@ -287,3 +289,91 @@ fn a_named_profile_is_found_behind_a_tie() { assert_eq!(db.find_lens_named(None, &name, None).unwrap().maker, "Testcam"); assert!(db.find_lens_named(None, "70-200mm f/4", None).is_none()); } + +fn crop_xml(parent: &str, calibration: &str) -> String { + format!( + r#"TestTest 50mm{parent} + + "# + ) +} + +#[test] +fn unknown_calibration_crop_is_not_assumed_full_frame() { + for parent in [ + "", + "", + "bad", + "NaN", + "inf", + "0", + "-1", + "0.049", + "21", + ] { + let db = LensDb::parse(&crop_xml(parent, "")).unwrap(); + assert_eq!(db.lenses[0].crop_factor, None, "{parent}"); + assert!(db.lenses[0].correction(&shot(50.0)).is_none(), "unknown calibration crop: {parent}"); + } +} + +#[test] +fn invalid_calibration_override_does_not_inherit_parent_crop() { + for value in ["", "bad", "NaN", "inf", "0", "-1", "0.049", "21"] { + let db = LensDb::parse(&crop_xml("1.5", &format!(r#"cropfactor="{value}""#))).unwrap(); + assert_eq!(db.lenses[0].crop_factor, None, "{value}"); + assert!(db.lenses[0].correction(&shot(50.0)).is_none(), "invalid calibration override: {value}"); + } +} + +#[test] +fn calibration_crop_inherits_only_when_omitted_and_accepts_valid_overrides() { + let inherited = LensDb::parse(&crop_xml("1.5", "")).unwrap(); + assert_eq!(inherited.lenses[0].crop_factor, Some(1.5)); + assert!(inherited.lenses[0].correction(&shot(50.0)).is_some()); + for parent in ["", "bad", "1.5"] { + for value in ["0.05", "1", "2", "20"] { + let db = LensDb::parse(&crop_xml(parent, &format!(r#"cropfactor="{value}""#))).unwrap(); + assert_eq!(db.lenses[0].crop_factor, value.parse().ok()); + // With no shot crop the valid calibration supplies the scale, even at the range boundaries. + assert!(db.lenses[0].correction(&Shot { crop_factor: None, ..shot(50.0) }).is_some()); + } + } +} + +#[test] +fn camera_crop_preserves_unknown_and_explicit_full_frame() { + for (crop, expected) in [ + ("", None), + ("bad", None), + ("NaN", None), + ("inf", None), + ("0", None), + ("-1", None), + ("0.049", None), + ("21", None), + ("0.05", Some(0.05)), + ("1", Some(1.0)), + ("20", Some(20.0)), + ] { + let xml = format!(r#"TestBody{crop}"#); + let db = LensDb::parse(&xml).unwrap(); + assert_eq!(db.cameras[0].crop_factor, expected, "{crop}"); + } + let db = LensDb::parse("TestBody").unwrap(); + assert_eq!(db.cameras[0].crop_factor, None); +} + +#[test] +fn direct_profile_api_rejects_invalid_calibration_and_preserves_shot_fallback() { + let mut profile = db().lenses[0].clone(); + for crop in [None, Some(f64::NAN), Some(f64::INFINITY), Some(0.0), Some(-1.0), Some(0.049), Some(21.0)] { + profile.crop_factor = crop; + assert!(profile.correction(&shot(24.0)).is_none(), "{crop:?}"); + } + profile.crop_factor = Some(1.0); + let expected = profile.correction(&shot(24.0)).unwrap(); + for crop in [None, Some(f64::NAN), Some(f64::INFINITY), Some(0.0), Some(-1.0)] { + assert_eq!(profile.correction(&Shot { crop_factor: crop, ..shot(24.0) }).unwrap(), expected); + } +} diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index 13d1b2b7a7..fd7c1d329b 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -17,7 +17,7 @@ changes. ## What is applied The lens is identified from EXIF: camera make / model (for the mount and crop factor; the 35 mm equivalent focal -length is used when the camera isn't listed) and the lens model string. Lens names match on their words; every +length is used when the camera isn't listed or its crop factor is unknown) and the lens model string. Lens names match on their words; every number (focal lengths, apertures) in the database name must appear in the EXIF name, and the lens maker must agree when the file names one. @@ -44,7 +44,10 @@ half diagonal, or that would fold the image, is not applied. Normalisation follows lensfun's documentation: distortion and TCA radii are 1 at half the shorter side of the calibration frame (its `cropfactor` and `aspect-ratio`), vignetting radii 1 at its corner; the optical centre is -the image centre (lensfun's `
` offsets are not read). +the image centre (lensfun's `
` offsets are not read). Missing or invalid crop factors remain unknown; +only finite values from 0.05 through 20 are accepted. A calibration inherits the lens's crop factor only when its +`cropfactor` attribute is omitted; an explicit invalid override remains unknown. Profiles with an unknown +calibration crop factor are not applied. An explicit crop factor of 1 still means full frame. ## Limits From feb5900ec82a34182e09603b73e021497a377f9c Mon Sep 17 00:00:00 2001 From: Julian Zahnow <54999052+Enthendor@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:05:43 +0200 Subject: [PATCH 05/51] LR-EDIT-OPTICS-PROFILE: Sigma 10-18 mm exception on the ILCE-6700 The body's rule leaves 1.36-1.38 px at the Sigma 10-18 mm F2.8 DC DN's 10 mm end. A measured lens exception (Exif LensModel + FocalLength <= 10.5 mm) uses knot spacing 416 px and unit 14250 there, chosen on 63 photos at 10 mm and checked on 34 from other locations. As rendered: 0.38-0.55 px over 97 photos at 10 mm (median 0.44), 0.36 px at 10.4-10.5 mm; from 11.7 mm the body's rule stays (0.48-0.72 px on 16 photos). Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/vendor/arw.rs | 3 +- crates/raw/src/vendor/arw_lens_rm6.rs | 95 +++++++++++++++++++++----- docs/gaps.md | 1 + docs/raw-parity.md | 2 +- docs/sony-ilce7rm6-lens-corrections.md | 22 +++++- 5 files changed, 101 insertions(+), 22 deletions(-) diff --git a/crates/raw/src/vendor/arw.rs b/crates/raw/src/vendor/arw.rs index 3938fa50dd..1b2e1ef46d 100644 --- a/crates/raw/src/vendor/arw.rs +++ b/crates/raw/src/vendor/arw.rs @@ -784,7 +784,8 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { let rm6: Vec = if linear_rgb || !super::arw_lens::is_3_2(crop) { Vec::new() } else { - super::arw_lens_rm6::lens_opcodes(raw, &model, Rect::new(0, 0, w, h), reference) + let lens = metadata.lens_model.as_deref(); + super::arw_lens_rm6::lens_opcodes(raw, &model, lens, metadata.focal_length, Rect::new(0, 0, w, h), reference) }; if !rm6.is_empty() && let Some(record) = sony_lens.as_mut() diff --git a/crates/raw/src/vendor/arw_lens_rm6.rs b/crates/raw/src/vendor/arw_lens_rm6.rs index 9b8afd7773..f20263901d 100644 --- a/crates/raw/src/vendor/arw_lens_rm6.rs +++ b/crates/raw/src/vendor/arw_lens_rm6.rs @@ -62,6 +62,31 @@ const FULL_FRAME_CROP: &[(&str, (f64, f64))] = &[("ILCE-7RM6", (9984.0, 6656.0)) /// The verified APS-C bodies (Exif `Model`): their knots span the file's own default crop, the last knot at its corner. /// Measured on the ILCE-6700 (docs/sony-ilce7rm6-lens-corrections.md → ILCE-6700). const APSC_BODIES: &[&str] = &["ILCE-6700"]; + +/// A lens whose tables follow its body's convention with another knot spacing and unit, at the focal lengths up to +/// `max_focal_mm`; measured like the body, against the camera JPEG. +struct LensException { + /// Exif `Model` of the body. + body: &'static str, + /// Exif `LensModel`, as the body records it. + lens: &'static str, + max_focal_mm: f64, + knot_spacing: f64, + unit: f64, +} + +/// The measured lens exceptions (docs/sony-ilce7rm6-lens-corrections.md → ILCE-6700). +const LENS_EXCEPTIONS: &[LensException] = &[ + // Sigma 10-18 mm F2.8 DC DN at its wide end: 97 photos at 10 mm and 2 at 10.4-10.5 mm. The body's rule leaves + // 1.36-1.38 px there; from 11.7 mm on the body's rule fits better + LensException { body: "ILCE-6700", lens: "10-18mm F2.8 DC DN | Contemporary 023", max_focal_mm: 10.5, knot_spacing: 416.0, unit: 14250.0 }, +]; + +/// The exception for the body `model` with the lens `lens` at `focal_mm`, if one is measured. +fn lens_exception(model: &str, lens: Option<&str>, focal_mm: Option) -> Option<&'static LensException> { + let (lens, focal) = (lens?.trim(), focal_mm.filter(|f| f.is_finite() && *f > 0.0)?); + LENS_EXCEPTIONS.iter().find(|e| e.body == model.trim() && e.lens == lens && focal <= e.max_focal_mm) +} /// The distortion table's value of 1 (relative radius); a global least-squares fit over the 34 photos gives 15000.7. const DISTORTION_UNIT: f64 = 15000.0; /// The CA table's value of 1 (relative to the green source radius). Provisional: the median best unit of the 20 @@ -179,18 +204,19 @@ fn fill_zoom(source: &dyn Fn(f64) -> f64, x: f64, y: f64) -> Option { /// worse than 0.25 px at a 1440 px long edge at one of the fit samples within the crop (a padded active area, as in /// lossless files, reaches radii that are never shown). pub(crate) fn warp_opcode(t: &LensTables, knot_spacing: f64, active: Rect, crop: Rect, lateral_ca: bool) -> Option { - gated_warp_opcode(t, knot_spacing, active, crop, lateral_ca, FIT_ERROR_PX_AT_1440) + gated_warp_opcode(t, knot_spacing, DISTORTION_UNIT, active, crop, lateral_ca, FIT_ERROR_PX_AT_1440) } -/// [`warp_opcode`] with the fit gate `fit_error_px` (px at a 1440 px long edge) instead of 0.25 px. -fn gated_warp_opcode(t: &LensTables, knot_spacing: f64, active: Rect, crop: Rect, lateral_ca: bool, fit_error_px: f64) -> Option { +/// [`warp_opcode`] with the distortion unit `unit` instead of 15000 and the fit gate `fit_error_px` (px at a 1440 px +/// long edge) instead of 0.25 px. +fn gated_warp_opcode(t: &LensTables, knot_spacing: f64, unit: f64, active: Rect, crop: Rect, lateral_ca: bool, fit_error_px: f64) -> Option { let v = t.distortion.as_deref().filter(|v| v.iter().any(|&x| x != 0.0))?; let (aw, ah) = (active.width as f64, active.height as f64); if aw < 2.0 || ah < 2.0 || crop.width == 0 || crop.height == 0 { return None; } let (cw, ch) = (crop.width as f64, crop.height as f64); - let source = |r: f64| r * (1.0 + interpolate(v, knot_spacing, r) / DISTORTION_UNIT); + let source = |r: f64| r * (1.0 + interpolate(v, knot_spacing, r) / unit); let z = fill_zoom(&source, cw / 2.0, ch / 2.0)?; // DNG's normalisation: pixel-index units, radius 1 = the farthest active-area corner let (cx, cy) = (crop.x as f64 + (cw - 1.0) / 2.0, crop.y as f64 + (ch - 1.0) / 2.0); @@ -226,12 +252,16 @@ fn gated_warp_opcode(t: &LensTables, knot_spacing: f64, active: Rect, crop: Rect /// R and B by 0.84 × the table. Over the 43 verified photos with enough corner detail, the render's corner R/G and B/G /// CA went from rms 0.032 / max 0.125 px (off) to 0.052 / 0.219 px (on) at a 1440 px long edge; only the 16–35 mm /// improved. Follow-up: warp the CFA colour planes before demosaic, then turn the planes on here. -pub(crate) fn lens_opcodes(raw: &Ifd, model: &str, active: Rect, crop: Rect) -> Vec { +/// +/// `lens` (Exif `LensModel`) and `focal_mm` (Exif `FocalLength`) select a measured lens exception's spacing and unit. +pub(crate) fn lens_opcodes(raw: &Ifd, model: &str, lens: Option<&str>, focal_mm: Option, active: Rect, crop: Rect) -> Vec { let tables = read_tables(raw); let knots = tables.distortion.as_ref().map_or(0, Vec::len); let Some(spacing) = knot_spacing(model, knots, crop) else { return Vec::new() }; - let op = if APSC_BODIES.contains(&model.trim()) { - gated_warp_opcode(&tables, spacing, active, crop, false, APSC_FIT_ERROR_PX_AT_1440) + let op = if let Some(e) = lens_exception(model, lens, focal_mm) { + gated_warp_opcode(&tables, e.knot_spacing, e.unit, active, crop, false, APSC_FIT_ERROR_PX_AT_1440) + } else if APSC_BODIES.contains(&model.trim()) { + gated_warp_opcode(&tables, spacing, DISTORTION_UNIT, active, crop, false, APSC_FIT_ERROR_PX_AT_1440) } else { warp_opcode(&tables, spacing, active, crop, false) }; @@ -372,7 +402,7 @@ mod tests { let t = read_tables(&ifd); assert!(t.distortion.is_none(), "{what}"); assert!(warp_opcode(&t, knots(), FF_ACTIVE, FF_CROP, false).is_none(), "{what}"); - assert!(lens_opcodes(&ifd, "ILCE-7RM6", FF_ACTIVE, FF_CROP).is_empty(), "{what}"); + assert!(lens_opcodes(&ifd, "ILCE-7RM6", None, None, FF_ACTIVE, FF_CROP).is_empty(), "{what}"); } // not an integer array, or absent for value in [Value::Ascii("16 0 2 7".into()), Value::Double(vec![2.0, 0.0, 1.0])] { @@ -406,7 +436,7 @@ mod tests { let mut table = vec![v; 17]; table[0] = 16; let ifd = ifd(vec![(DISTORTION_PARAMS, Value::SLong8(table))]); - assert!(lens_opcodes(&ifd, "ILCE-7RM6", FF_ACTIVE, FF_CROP).is_empty(), "{v}"); + assert!(lens_opcodes(&ifd, "ILCE-7RM6", None, None, FF_ACTIVE, FF_CROP).is_empty(), "{v}"); } // degenerate geometry let ff = tables_with_distortion(&values(&FF_DISTORTION)); @@ -455,7 +485,7 @@ mod tests { let t = read_tables(&ifd); assert!(t.ca.is_some() && t.distortion.is_none()); assert!(warp_opcode(&t, knots(), FF_ACTIVE, FF_CROP, true).is_none()); - assert!(lens_opcodes(&ifd, "ILCE-7RM6", FF_ACTIVE, FF_CROP).is_empty()); + assert!(lens_opcodes(&ifd, "ILCE-7RM6", None, None, FF_ACTIVE, FF_CROP).is_empty()); // an all-zero distortion table counts as absent let zero = LensTables { ca: t.ca, ..tables_with_distortion(&[0.0; 16]) }; assert!(warp_opcode(&zero, knots(), FF_ACTIVE, FF_CROP, true).is_none()); @@ -509,7 +539,7 @@ mod tests { let ifd = sony_lens_ifd(&FF_DISTORTION, &FF_VIGNETTING, &FF_HQ_CA, [1, 2, 1]); let with_ca = warp_opcode(&read_tables(&ifd), knots(), FF_ACTIVE, FF_CROP, true); assert!(matches!(with_ca, Some(Opcode::WarpRectilinear { ref planes, .. }) if planes.len() == 3), "{with_ca:?}"); - let ops = lens_opcodes(&ifd, "ILCE-7RM6", FF_ACTIVE, FF_CROP); + let ops = lens_opcodes(&ifd, "ILCE-7RM6", None, None, FF_ACTIVE, FF_CROP); let [Opcode::WarpRectilinear { planes, .. }] = ops.as_slice() else { panic!("{ops:?}") }; assert_eq!(planes.len(), 1); let green = warp_opcode(&tables_with_distortion(&values(&FF_DISTORTION)), knots(), FF_ACTIVE, FF_CROP, false); @@ -575,12 +605,12 @@ mod tests { assert_eq!(knot_spacing("ILCE-6700", 0, A6700_CROP), None); assert_eq!(knot_spacing("ILCE-6700", 11, Rect { x: 0, y: 0, width: 0, height: 0 }), None); let ifd = sony_lens_ifd(&A6700_DISTORTION, &APSC_VIGNETTING, &APSC_CA, [1, 2, 1]); - let ops = lens_opcodes(&ifd, "ILCE-6700", A6700_CROP, A6700_CROP); - let gated = gated_warp_opcode(&read_tables(&ifd), s, A6700_CROP, A6700_CROP, false, APSC_FIT_ERROR_PX_AT_1440); + let ops = lens_opcodes(&ifd, "ILCE-6700", None, None, A6700_CROP, A6700_CROP); + let gated = gated_warp_opcode(&read_tables(&ifd), s, DISTORTION_UNIT, A6700_CROP, A6700_CROP, false, APSC_FIT_ERROR_PX_AT_1440); assert_eq!(ops, gated.into_iter().collect::>()); assert_eq!(ops.len(), 1); // other APS-C bodies stay unverified - assert!(lens_opcodes(&ifd, "ILCE-6600", A6700_CROP, A6700_CROP).is_empty()); + assert!(lens_opcodes(&ifd, "ILCE-6600", None, None, A6700_CROP, A6700_CROP).is_empty()); } /// The APS-C fit gate: a table the polynomial fits within 0.44 px is applied on the ILCE-6700, while the @@ -591,17 +621,46 @@ mod tests { let t = read_tables(&ifd); let s = knot_spacing("ILCE-6700", 11, A6700_DECODED_CROP).unwrap(); assert_eq!(warp_opcode(&t, s, A6700_ACTIVE, A6700_DECODED_CROP, false), None); - let ops = lens_opcodes(&ifd, "ILCE-6700", A6700_ACTIVE, A6700_DECODED_CROP); + let ops = lens_opcodes(&ifd, "ILCE-6700", None, None, A6700_ACTIVE, A6700_DECODED_CROP); assert!(matches!(ops.as_slice(), [Opcode::WarpRectilinear { planes, .. }] if planes.len() == 1), "{ops:?}"); } + const SIGMA_10_18: &str = "10-18mm F2.8 DC DN | Contemporary 023"; + + /// The Sigma 10-18 mm exception: its spacing and unit at 10-10.5 mm, the body's rule from 11.7 mm (and without a + /// lens or focal length, on another body or another lens). + #[test] + fn lens_exception_applies_only_to_its_lens_body_and_focal_range() { + let ifd = sony_lens_ifd(&A6700_DISTORTION, &APSC_VIGNETTING, &APSC_CA, [1, 2, 1]); + let t = read_tables(&ifd); + let geometry = (A6700_ACTIVE, A6700_DECODED_CROP); + let exception = gated_warp_opcode(&t, 416.0, 14250.0, geometry.0, geometry.1, false, APSC_FIT_ERROR_PX_AT_1440); + let body = lens_opcodes(&ifd, "ILCE-6700", None, None, geometry.0, geometry.1); + assert!(exception.is_some() && body.len() == 1); + assert_ne!(body.first(), exception.as_ref()); + for focal in [10.0, 10.5] { + let ops = lens_opcodes(&ifd, "ILCE-6700", Some(SIGMA_10_18), Some(focal), geometry.0, geometry.1); + assert_eq!(ops.first(), exception.as_ref(), "{focal} mm"); + } + for (lens, focal) in [(Some(SIGMA_10_18), Some(11.7)), (Some(SIGMA_10_18), None), (None, Some(10.0)), (Some("E 10-20mm F4 PZ G"), Some(10.0))] + { + let ops = lens_opcodes(&ifd, "ILCE-6700", lens, focal, geometry.0, geometry.1); + assert_eq!(ops, body, "{lens:?} {focal:?}"); + } + // hostile focal lengths fall back to the body's rule + for focal in [f64::NAN, f64::INFINITY, -10.0, 0.0] { + assert_eq!(lens_opcodes(&ifd, "ILCE-6700", Some(SIGMA_10_18), Some(focal), geometry.0, geometry.1), body); + } + assert_eq!(lens_exception("ILCE-6600", Some(SIGMA_10_18), Some(10.0)).map(|e| e.unit), None); + } + #[test] fn unknown_model_gets_no_opcodes() { let ifd = sony_lens_ifd(&FF_DISTORTION, &FF_VIGNETTING, &FF_CA, [0, 2, 1]); - assert!(lens_opcodes(&ifd, "ILCE-7M4", FF_ACTIVE, FF_CROP).is_empty()); - assert!(lens_opcodes(&ifd, "", FF_ACTIVE, FF_CROP).is_empty()); + assert!(lens_opcodes(&ifd, "ILCE-7M4", None, None, FF_ACTIVE, FF_CROP).is_empty()); + assert!(lens_opcodes(&ifd, "", None, None, FF_ACTIVE, FF_CROP).is_empty()); // the camera's in-camera flag (0 = off here) doesn't matter: the table is applied - let ops = lens_opcodes(&ifd, " ILCE-7RM6 ", FF_ACTIVE, FF_CROP); + let ops = lens_opcodes(&ifd, " ILCE-7RM6 ", None, None, FF_ACTIVE, FF_CROP); assert_eq!(ops, warp_opcode(&read_tables(&ifd), knots(), FF_ACTIVE, FF_CROP, false).into_iter().collect::>()); assert_eq!(ops.len(), 1); } diff --git a/docs/gaps.md b/docs/gaps.md index 7389795720..bb5be27d0a 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -79,6 +79,7 @@ Beta-blocking gaps (B) total **~230–430 h**; with the stability and platform w | Date | Change | Summary | |---|---|---| +| 2026-10-23 | patch | Gap 8: the Sigma 10–18 mm on the ILCE-6700 gets a measured lens exception at 10–10.5 mm (0.55 px worst over 99 photos, was about 1.4 px) | | 2026-10-23 | minor | Gap 8 narrows: Sony ILCE-6700 (APS-C) in-file distortion through the ILCE-7RM6 conversion, knots spanning its own crop (30 photos, 3 lenses; 0.73 px worst except 1.38 px at 10 mm); the gap stays open (no lens database), hours unchanged | | 2026-10-21 | minor | Gap 28 shrinks: F8 shows / hides the right panel (`view.rightPanel`); rating `[` `]`, flag cycling and F1 help were already bound, so the entry lists only filter-bar keys, Tab / ⇧Tab and F5; 2–4 h (was 4–8) | | 2026-10-20 | minor | #2362: import destination albums use the shared hierarchy/search picker instead of a flat name list; existing import workflows retained; ranked gaps/readiness counts unchanged | diff --git a/docs/raw-parity.md b/docs/raw-parity.md index f93f98e251..59c38a3234 100644 --- a/docs/raw-parity.md +++ b/docs/raw-parity.md @@ -243,7 +243,7 @@ essentials user (the default look is what they keep). | Item | Adobe | LightCraft | |---|---|---| | Profile database | 3,635 profile files, 56 makers, auto-selected | **0** (Adobe LCPs never used; no database of our own) | -| In-file corrections | ✅ | DNG opcodes (WarpRectilinear, FixVignetteRadial, GainMap…) applied; Panasonic / Leica RW2 distortion (79 photos, 13 bodies, 11 lenses); Sony tag 0x7037 distortion for the ILCE-7RM4A (2 lenses), full-frame Bayer 3:2 ILCE-7CR files with correction Auto or forced Auto (3 lenses; see sony-lens-corrections.md for residuals and limits) the ILCE-7RM6 (3 lenses, 54 photos) and the APS-C ILCE-6700 (3 lenses, 30 photos; 1.38 px at a 10 mm wide end, 0.73 px elsewhere; `sony-ilce7rm6-lens-corrections.md`); Nikon NikonNEFInfo distortion (tag `0xc7d5`, about the sensor-shifted optical centre) for the Z 6II and Z f: F-mount lenses on the FTZ within 0.2 px of the camera JPEG, Z lenses within 0.5–1.5 px except the 24-70's wide end (7 px median; `docs/nikon-lens-corrections.md`) | +| In-file corrections | ✅ | DNG opcodes (WarpRectilinear, FixVignetteRadial, GainMap…) applied; Panasonic / Leica RW2 distortion (79 photos, 13 bodies, 11 lenses); Sony tag 0x7037 distortion for the ILCE-7RM4A (2 lenses), full-frame Bayer 3:2 ILCE-7CR files with correction Auto or forced Auto (3 lenses; see sony-lens-corrections.md for residuals and limits) the ILCE-7RM6 (3 lenses, 54 photos) and the APS-C ILCE-6700 (3 lenses, 145 photos; 0.73 px worst, with a measured Sigma 10–18 mm exception at 10–10.5 mm; `sony-ilce7rm6-lens-corrections.md`); Nikon NikonNEFInfo distortion (tag `0xc7d5`, about the sensor-shifted optical centre) for the Z 6II and Z f: F-mount lenses on the FTZ within 0.2 px of the camera JPEG, Z lenses within 0.5–1.5 px except the 24-70's wide end (7 px median; `docs/nikon-lens-corrections.md`) | | Vignetting / lateral CA from maker notes | ✅ | ⬜ | | Manual distortion, vignetting, CA, defringe | ✅ | ✅ | diff --git a/docs/sony-ilce7rm6-lens-corrections.md b/docs/sony-ilce7rm6-lens-corrections.md index 4e46637b1e..5dff832881 100644 --- a/docs/sony-ilce7rm6-lens-corrections.md +++ b/docs/sony-ilce7rm6-lens-corrections.md @@ -146,6 +146,23 @@ full-frame 0.25 px gate. Measured against the camera JPEG, the fitted polynomial 0.22–0.73 px against 0.23–0.68 px on the 27 photos, 1.35–1.38 px at 10 mm), so APS-C bodies use a 0.5 px gate (`APSC_FIT_ERROR_PX_AT_1440`). +**Lens exception: Sigma 10–18 mm F2.8 DC DN at 10–10.5 mm** (`LENS_EXCEPTIONS`, matched on Exif `LensModel` +`10-18mm F2.8 DC DN | Contemporary 023` and `FocalLength` ≤ 10.5 mm). Its wide end keeps 1.36–1.38 px under the +body's rule. On 115 further private photos of this lens (97 at 10 mm, 2 at 10.4–10.5 mm, 16 at 11.7–12.5 mm), a grid +over spacing 372–426 px and unit 14000–16500 was measured on the exact table. The photos at 10 mm were split by +shooting location: 63 to choose, 34 to check. Spacing and unit trade off along a ridge: 420 px / 14000 was best on the +63 (0.445 px worst) but put 2 of the 34 above 0.75 px, so the pick is **416 px / 14250**, within 0.51 px on both halves. +As rendered (`lightcraft-cli render`, Enable Lens Corrections on): + +| Sigma 10–18 mm | p95 at 1440 px | +|---|---| +| 10 mm, 97 photos (one night photo has too little detail to measure) | 0.38–0.55 px, median 0.44 (body's rule: about 1.4 px) | +| 10.4–10.5 mm, 2 photos | 0.36 px | +| 11.7–12.5 mm, 16 photos (body's rule, the exception does not apply) | 0.48–0.72 px, median 0.57 | + +At 11.7 mm and longer an exception of this kind fits worse than the body's rule (412 px / 14500 on the grid: 0.73–1.19 px against 0.48–0.69 px), so it stops at +10.5 mm, the longest measured focal length where it helps; 10.6–11.6 mm is unmeasured and takes the body's rule. + ## Two conventions A second Sony decoder, `crates/raw/src/vendor/arw_lens.rs` @@ -327,8 +344,9 @@ the resampled source by the source, framing and output size, so tone, colour and - **ILCE-7RM6 and ILCE-6700 only.** Other Sony bodies record the same tags; their knot spacing (defined here by the full-frame crop, or an APS-C body's own crop), unit and zoom rule are unverified, so they get no correction from this conversion (the ILCE-7RM4A's distortion has its own: [Two conventions](#two-conventions)). -- **ILCE-6700 at strong barrel distortion:** 1.36–1.38 px at the Sigma 10–18 mm's 10 mm end (above); its vignetting - and lateral CA tables are not measured. +- **ILCE-6700 at strong barrel distortion:** the body's rule leaves 1.36–1.38 px at the Sigma 10–18 mm's 10 mm end; + that lens has a measured exception (above), other wide lenses with similarly strong tables are unmeasured. Its + vignetting and lateral CA tables are not measured. - **APS-C distortion is unverified in camera on the ILCE-7RM6:** no APS-C photo with distortion correction on was available; the APS-C knot spacing is verified on CA only, and the APS-C zoom follows the full-frame rule. - **Aspect-ratio crops** (in-camera 4:3, 16:9, 1:1) were not measured; the correction uses the crop the file records. From d1b272e49654a63f49642846beffecc6ecdde20e Mon Sep 17 00:00:00 2001 From: Julian Zahnow <54999052+Enthendor@users.noreply.github.com> Date: Sat, 24 Oct 2026 15:38:20 +0200 Subject: [PATCH 06/51] docs(raw): name the Sigma choice and check sets (review: CodeRabbit on #2733) Co-Authored-By: Claude Opus 5.5 --- docs/sony-ilce7rm6-lens-corrections.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/sony-ilce7rm6-lens-corrections.md b/docs/sony-ilce7rm6-lens-corrections.md index 5dff832881..ca173b5a8c 100644 --- a/docs/sony-ilce7rm6-lens-corrections.md +++ b/docs/sony-ilce7rm6-lens-corrections.md @@ -150,8 +150,9 @@ full-frame 0.25 px gate. Measured against the camera JPEG, the fitted polynomial `10-18mm F2.8 DC DN | Contemporary 023` and `FocalLength` ≤ 10.5 mm). Its wide end keeps 1.36–1.38 px under the body's rule. On 115 further private photos of this lens (97 at 10 mm, 2 at 10.4–10.5 mm, 16 at 11.7–12.5 mm), a grid over spacing 372–426 px and unit 14000–16500 was measured on the exact table. The photos at 10 mm were split by -shooting location: 63 to choose, 34 to check. Spacing and unit trade off along a ridge: 420 px / 14000 was best on the -63 (0.445 px worst) but put 2 of the 34 above 0.75 px, so the pick is **416 px / 14250**, within 0.51 px on both halves. +shooting location: 63 to choose the values, 34 to check them. A larger spacing with a smaller unit fits about as well +(a ridge in the grid): 420 px / 14000 was best on the 63 choice photos (0.445 px worst) but put 2 of the 34 check photos +above 0.75 px, so the pick is **416 px / 14250**, within 0.51 px on both sets. As rendered (`lightcraft-cli render`, Enable Lens Corrections on): | Sigma 10–18 mm | p95 at 1440 px | From cc59c765ee0d9720f9e544473ddcb50cd46e8b6a Mon Sep 17 00:00:00 2001 From: Andrew Frahn Date: Wed, 21 Oct 2026 16:25:20 +1100 Subject: [PATCH 07/51] #2361: lens corrections crop inside the corrected photo Lens profiles, embedded camera warps, manual Distortion and lateral CA always shrink the crop about its centre until it holds no blank past the source's edge, as Lightroom does; perspective still needs Constrain Crop. The check covers every colour plane when the planes differ, so CA can't leave a coloured edge. Sources: own design (cherry picked from commit 7416984c5d5daa8bbfb64abf1c52749e4838c912) Co-Authored-By: Claude Opus 5.5 --- crates/pipeline/src/geometry.rs | 33 +++++++++++++++++++++------ crates/pipeline/src/tests_geometry.rs | 20 ++++++++++++++++ docs/whats-new.md | 7 ++++++ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/crates/pipeline/src/geometry.rs b/crates/pipeline/src/geometry.rs index 3f2766750c..dcfebe3d0e 100644 --- a/crates/pipeline/src/geometry.rs +++ b/crates/pipeline/src/geometry.rs @@ -77,15 +77,32 @@ impl Frame { warp: (!warp.is_identity()).then_some(warp), view: None, }; - if apply_crop && s.geometry.constrain_crop { - f.constrain_to_image(); + if apply_crop { + f.constrain_to_image(s.geometry.constrain_crop); } f } - /// "Constrain Crop": shrink the crop about its centre until it contains no area outside the warped source. - fn constrain_to_image(&mut self) { - let Some(wp) = self.warp.as_ref().filter(|w| w.moves_pixels()) else { return }; + /// Shrink the crop about its centre until it contains no area outside the warped source. Lens + /// corrections (profile, embedded camera warp, manual distortion, lateral CA) always do this, as + /// a corrected lens never shows the blank past the photo's edge (issue #2361); perspective only + /// with "Constrain Crop" (`with_persp`), so a keystone fix can keep its corners. + fn constrain_to_image(&mut self, with_persp: bool) { + let Some(full) = self.warp.as_ref().filter(|w| w.moves_pixels()) else { return }; + let optics; + let wp = if with_persp || full.persp == Homography::IDENTITY { + full + } else { + let mut o = full.clone(); + o.persp = Homography::IDENTITY; + o.persp_inv = Homography::IDENTITY; + optics = o; + &optics + }; + if !wp.moves_pixels() { + return; + } + let planes: &[usize] = if wp.per_channel() { &[0, 1, 2] } else { &[1] }; let a = self.crop.output_to_source(self.ow, self.oh, 1.0, 1.0); let (w, h) = (self.ow, self.oh); let fits = |t: f64| { @@ -93,8 +110,10 @@ impl Frame { let u = i as f64 / 48.0; [(u, 0.0), (u, 1.0), (0.0, u), (1.0, u)].iter().all(|&(x, y)| { let p = Point::new(0.5 + t * (x - 0.5), 0.5 + t * (y - 0.5)); - let s = wp.to_source(a.apply(p), 1); - s.x >= -1e-6 * w && s.y >= -1e-6 * h && s.x <= w * (1.0 + 1e-6) && s.y <= h * (1.0 + 1e-6) + planes.iter().all(|&ch| { + let s = wp.to_source(a.apply(p), ch); + s.x >= -1e-6 * w && s.y >= -1e-6 * h && s.x <= w * (1.0 + 1e-6) && s.y <= h * (1.0 + 1e-6) + }) }) }) }; diff --git a/crates/pipeline/src/tests_geometry.rs b/crates/pipeline/src/tests_geometry.rs index ac244cb0dc..accc5c4776 100644 --- a/crates/pipeline/src/tests_geometry.rs +++ b/crates/pipeline/src/tests_geometry.rs @@ -260,6 +260,26 @@ fn constrain_crop_removes_blank_areas() { assert!(blank_fraction(&c) < 0.002, "constrained: {}", blank_fraction(&c)); } +/// Issue #2361: lens corrections never show the blank past the photo's edge, with or without +/// Constrain Crop, in every colour plane; perspective alone still needs Constrain Crop. +#[test] +fn lens_corrections_leave_no_blank_corners() { + let src = Rgb32f::from_fn(300, 200, |x, y| [0.05 + 0.002 * x as f32, 0.1 + 0.002 * y as f32, 0.3]); + for (distortion, ca) in [(-80.0, 0.0), (80.0, 0.0), (-40.0, 100.0), (0.0, -100.0)] { + let mut s = DevelopSettings::default(); + s.optics.distortion = distortion; + s.optics.ca_red = ca; + s.optics.ca_blue = -ca; + let r = render(&src, &SourceInfo::default(), &s, &RenderRequest::fit(300, 300)).image; + assert!(blank_fraction(&r) < 0.002, "distortion {distortion}, CA {ca}: {}", blank_fraction(&r)); + } + let mut s = DevelopSettings::default(); + s.optics.distortion = -60.0; + s.geometry.vertical = -60.0; + let r = render(&src, &SourceInfo::default(), &s, &RenderRequest::fit(300, 300)).image; + assert!(blank_fraction(&r) > 0.03, "the keystone keeps its corners: {}", blank_fraction(&r)); +} + #[test] fn transforms_preview_matches_export() { let src = lightcraft_scenes::demo_library()[4].render(960, 640); diff --git a/docs/whats-new.md b/docs/whats-new.md index c6c2592003..0d1b3be290 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -1,5 +1,12 @@ # What's new in LightCraft +## 2026-10-26 + +### Lens corrections without grey corners +- Lens profiles, the camera's own distortion correction, manual Distortion and Chromatic Aberration now crop just + inside the corrected photo, as in Lightroom, so no grey corners or edge marks show. Perspective corrections still + need Constrain Crop to do the same. + ## 2026-10-24 ### Relative-date filters and smart albums follow the clock From c9e459d670a4af3faf9e1d476a9c56a1ba837dab Mon Sep 17 00:00:00 2001 From: Andrew Frahn Date: Mon, 26 Oct 2026 11:39:08 +1100 Subject: [PATCH 08/51] #2361: keep the crop inside the photo after the automatic lateral CA estimate frame_for adds the estimated lateral CA after Frame::with_lens has constrained the crop, so re-run the constraint then (review feedback). Co-Authored-By: Claude Opus 5.5 --- crates/pipeline/src/geometry.rs | 2 +- crates/pipeline/src/lib.rs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/pipeline/src/geometry.rs b/crates/pipeline/src/geometry.rs index dcfebe3d0e..fc235c52db 100644 --- a/crates/pipeline/src/geometry.rs +++ b/crates/pipeline/src/geometry.rs @@ -87,7 +87,7 @@ impl Frame { /// corrections (profile, embedded camera warp, manual distortion, lateral CA) always do this, as /// a corrected lens never shows the blank past the photo's edge (issue #2361); perspective only /// with "Constrain Crop" (`with_persp`), so a keystone fix can keep its corners. - fn constrain_to_image(&mut self, with_persp: bool) { + pub(crate) fn constrain_to_image(&mut self, with_persp: bool) { let Some(full) = self.warp.as_ref().filter(|w| w.moves_pixels()) else { return }; let optics; let wp = if with_persp || full.persp == Homography::IDENTITY { diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index 8f2702760d..5da8d86911 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -236,6 +236,10 @@ pub fn frame_for(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, apply_cro let mut frame = geometry::Frame::with_lens(src.width, src.height, s, apply_crop, info.lens.as_ref()); if s.optics.remove_ca && s.section_enabled("optics") { frame.add_lateral_ca(optics::estimate_lateral_ca(src)); + if apply_crop { + // The estimated CA moves the colour planes too: keep the crop inside every one of them. + frame.constrain_to_image(s.geometry.constrain_crop); + } } frame } From 2ec4add0d6b5ba66473a38fa91ca8dd1a0dcdd17 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Thu, 22 Oct 2026 12:59:02 +1000 Subject: [PATCH 09/51] LR-EDIT-OPTICS-PROFILE: lensfun database downloaded on first run, library looked up, Optics names the profile - lensfun's data/db (CC BY-SA 3.0) is fetched by the desktop app on first run into /lensfun, pinned to commit 43f9e001 by name, size and SHA-256, staged until every file is verified, with an activity row (never bundled). LIGHTCRAFT_LENSFUN_URL lists other mirrors; LIGHTCRAFT_LENSFUN_DIR still wins. lens.downloadDatabase / lens.databaseStatus for agents. - The database is read per folder and again after a download; when it changes, library raws without a profile whose lens it knows are probed in the background and get it (untouched photos switch the correction on as import would); ticking Enable Lens Corrections looks those photos up again (library.refreshLensProfiles). - EmbeddedLens records its source (file / lens database); the Optics panel says "Profile: (lens database)", "Profile: from the file" or "No lens profile found for ." (13 locales). - Tests: pinned download and refusal of a wrong file, the lookup and its edited / untouched rules, and presets / Copy-Paste / Sync carrying only the switch and amounts, each photo keeping its own lens's profile. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit 773740fbb22eb66e7f62bada9cb32b594b6ff4d7) --- apps/lightcraft-cli/src/main.rs | 6 +- apps/lightcraft/src/main.rs | 2 +- crates/catalog/src/tests.rs | 3 +- crates/develop/src/settings.rs | 21 ++ crates/engine/src/cmd/convert.rs | 1 + crates/engine/src/cmd/lens_refresh.rs | 164 +++++++++ crates/engine/src/cmd/mod.rs | 2 + crates/engine/src/config.rs | 8 + crates/engine/src/lens_profiles.rs | 113 ++++-- crates/engine/src/lensfun_download.rs | 387 ++++++++++++++++++++ crates/engine/src/lib.rs | 9 + crates/engine/src/tests_lens_refresh.rs | 120 ++++++ crates/gpu/src/render.rs | 1 + crates/gpu/tests/it/equivalence.rs | 1 + crates/pipeline/src/optics.rs | 2 +- crates/ui-egui/locales/cs-formats.json | 5 +- crates/ui-egui/locales/cs.json | 8 +- crates/ui-egui/locales/de-formats.json | 5 +- crates/ui-egui/locales/de.json | 8 +- crates/ui-egui/locales/es-formats.json | 5 +- crates/ui-egui/locales/es.json | 8 +- crates/ui-egui/locales/fr-formats.json | 5 +- crates/ui-egui/locales/fr.json | 8 +- crates/ui-egui/locales/it-formats.json | 5 +- crates/ui-egui/locales/it.json | 8 +- crates/ui-egui/locales/ja-formats.json | 5 +- crates/ui-egui/locales/ja.json | 8 +- crates/ui-egui/locales/ka-formats.json | 5 +- crates/ui-egui/locales/ka.json | 8 +- crates/ui-egui/locales/pl-formats.json | 5 +- crates/ui-egui/locales/pl.json | 8 +- crates/ui-egui/locales/pt-br-formats.json | 5 +- crates/ui-egui/locales/pt-br.json | 8 +- crates/ui-egui/locales/ru-formats.json | 5 +- crates/ui-egui/locales/ru.json | 8 +- crates/ui-egui/locales/uk-formats.json | 5 +- crates/ui-egui/locales/uk.json | 8 +- crates/ui-egui/locales/zh-hans-formats.json | 5 +- crates/ui-egui/locales/zh-hans.json | 8 +- crates/ui-egui/locales/zh-hant-formats.json | 5 +- crates/ui-egui/locales/zh-hant.json | 8 +- crates/ui-egui/src/lens_db.rs | 141 +++++++ crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/panels/edit.rs | 21 +- crates/ui-egui/src/state.rs | 12 + docs/gaps.md | 5 +- docs/lens-profiles.md | 36 +- docs/parity-checklist.md | 5 +- 48 files changed, 1152 insertions(+), 79 deletions(-) create mode 100644 crates/engine/src/cmd/lens_refresh.rs create mode 100644 crates/engine/src/lensfun_download.rs create mode 100644 crates/engine/src/tests_lens_refresh.rs create mode 100644 crates/ui-egui/src/lens_db.rs diff --git a/apps/lightcraft-cli/src/main.rs b/apps/lightcraft-cli/src/main.rs index c1f9d258c6..e8bad66a4b 100644 --- a/apps/lightcraft-cli/src/main.rs +++ b/apps/lightcraft-cli/src/main.rs @@ -799,14 +799,14 @@ fn snapshot(args: &[String]) -> Result<(), String> { let mut session = match &library { Some(dir) => { let mut s = - Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates(); + Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_lens_database().with_default_slide_templates(); s.open_library(dir, false).map_err(|e| library_error(dir, e))?; s } None if files.is_empty() => { - Session::with_demo().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates() + Session::with_demo().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_lens_database().with_default_slide_templates() } - None => Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_slide_templates(), + None => Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_lens_database().with_default_slide_templates(), }; // Apply compute policy before imports or the first scripted query can discover an adapter. session.execute("app.gpu", &json!({"enabled": false})).map_err(|e| e.to_string())?; diff --git a/apps/lightcraft/src/main.rs b/apps/lightcraft/src/main.rs index 868112a97f..982c89a1f2 100644 --- a/apps/lightcraft/src/main.rs +++ b/apps/lightcraft/src/main.rs @@ -777,7 +777,7 @@ fn open_library_session(in_memory: bool, dir: Option, seed_d if in_memory { return (if seed_demo { Session::with_demo() } else { Session::new() }.with_fs().with_default_denoise_models().with_system_clock(), None); } - let unopened = || Session::new().with_fs().with_default_denoise_models().with_system_clock(); + let unopened = || Session::new().with_fs().with_default_denoise_models().with_default_lens_database().with_system_clock(); let Some(dir) = dir else { log::warn!("no library location (set --library or LIGHTCRAFT_LIBRARY)"); return (unopened(), Some(LibraryProblem::new("", "There is no home folder to keep the library in. Choose a folder for it."))); diff --git a/crates/catalog/src/tests.rs b/crates/catalog/src/tests.rs index 579175086e..708ab57468 100644 --- a/crates/catalog/src/tests.rs +++ b/crates/catalog/src/tests.rs @@ -564,7 +564,8 @@ fn set_embedded_lens_is_undoable_and_journaled() { photo: Box::new(Photo::new(a, Source::File { path: "/x.rw2".into() }, "x.rw2", "RW2", 4000, 3000, "2026-10-06T00:00:00")), }) .unwrap(); - let lens = lightcraft_develop::EmbeddedLens { warp: Some(Default::default()), vignette: None, stages: Default::default() }; + let lens = + lightcraft_develop::EmbeddedLens { warp: Some(Default::default()), vignette: None, stages: Default::default(), source: Default::default() }; let op = Op::SetEmbeddedLens { id: a, lens: Some(Box::new(lens)) }; let op: Op = serde_json::from_str(&serde_json::to_string(&op).unwrap()).unwrap(); let inv = c.apply(op).unwrap(); diff --git a/crates/develop/src/settings.rs b/crates/develop/src/settings.rs index a40f094c25..a1e122f68a 100644 --- a/crates/develop/src/settings.rs +++ b/crates/develop/src/settings.rs @@ -698,11 +698,32 @@ impl Default for Optics { pub struct EmbeddedLens { pub warp: Option, pub vignette: Option, + /// Where the correction came from: the file itself, or a lens database looked up by the + /// lens named in the file (what the Optics panel says is loaded). + #[serde(skip_serializing_if = "LensSource::is_file")] + pub source: LensSource, /// A DNG 1.6 `WarpRectilinear2` chain (up to [`WARP_STAGES`] stages, in `OpcodeList3` order; each stage warps /// the result of the one before it). When any is set, `warp` is `None`: the chain holds every warp opcode. pub stages: [Option; WARP_STAGES], } +/// Where an [`EmbeddedLens`] came from. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum LensSource { + /// The file's own correction (DNG opcodes, a camera's correction a raw reader converts). + #[default] + File, + /// A profile from a lensfun-format lens database, matched by the lens named in the file. + Database, +} + +impl LensSource { + pub fn is_file(&self) -> bool { + *self == LensSource::File + } +} + /// Most warp stages an [`EmbeddedLens`] chain keeps. pub const WARP_STAGES: usize = 3; diff --git a/crates/engine/src/cmd/convert.rs b/crates/engine/src/cmd/convert.rs index 0871e43528..24b4165278 100644 --- a/crates/engine/src/cmd/convert.rs +++ b/crates/engine/src/cmd/convert.rs @@ -456,6 +456,7 @@ mod tests { warp: Some(lightcraft_develop::EmbeddedWarp { planes: [[1.0, -0.1, 0.03, 0.0, 0.0, 0.0]; 3], ..Default::default() }), vignette: None, stages: Default::default(), + source: Default::default(), } } fn old_rw2() -> lightcraft_catalog::Photo { diff --git a/crates/engine/src/cmd/lens_refresh.rs b/crates/engine/src/cmd/lens_refresh.rs new file mode 100644 index 0000000000..720edad8a2 --- /dev/null +++ b/crates/engine/src/cmd/lens_refresh.rs @@ -0,0 +1,164 @@ +//! Lens profiles looked up again when the lens database appears or changes. A photo's lens data is +//! stored when it is imported ([`Photo::embedded_lens`]), so photos imported before the database was there say "no +//! profile" until something reads their file again. When the database arrives (the first-run download) or is updated, +//! and when "Enable Lens Corrections" is ticked on a photo without a profile, every library raw that has no profile +//! but whose lens the database knows by name is probed again and gets what Reload from Disk would give it: the +//! profile, and the correction switched on where its settings are still what import gave it (an edited photo keeps +//! its settings). +//! +//! Split like [`super::recheck`] so the app probes on a worker thread: [`missing`] lists what to probe (no +//! file-system calls), [`probe`] reads the files without the session, [`apply`] applies the results as LightCraft's +//! own bookkeeping (journaled, not an undo step). Agents and tests use `library.refreshLensProfiles`. + +use lightcraft_catalog::{Catalog, Op, Photo, PhotoId}; +use serde_json::{Value, json}; + +use super::{CommandSpec, always, bad, cmd}; +use crate::media::{FileProbe, ProbeInfo}; +use crate::{Result, Session}; + +/// A photo to probe again, and its file when listed (a result for a photo moved meanwhile is dropped). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Missing { + pub id: PhotoId, + pub path: String, +} + +/// Library raws with a file, no lens profile and a lens the database `db` knows by name (`ids`: only these; virtual +/// copies follow the photo they were copied from). No file-system calls. +pub fn missing(cat: &Catalog, db: &lightcraft_lensdb::LensDb, ids: Option<&[PhotoId]>) -> Vec { + cat.photos() + .filter(|p| ids.is_none_or(|ids| ids.contains(&p.id) || p.copy_of.is_some_and(|c| ids.contains(&c)))) + .map(|p| p.copy_of.and_then(|c| cat.photo(c)).map_or(&**p, |orig| &**orig)) + .filter(|p| p.develops_raw() && p.embedded_lens.is_none() && !p.meta.lens.trim().is_empty()) + .filter(|p| crate::lens_profiles::profile_name(db, &p.meta.camera, &p.meta.lens).is_some()) + .filter_map(|p| crate::cmd::missing::checked_path(p).map(|path| Missing { id: p.id, path: path.to_string() })) + .fold(Vec::new(), |mut v, m| { + if !v.contains(&m) { + v.push(m); + } + v + }) +} + +/// Probe each listed photo's file as Reload would. Doesn't touch the session. +pub fn probe(probe: &FileProbe, list: Vec) -> Vec<(Missing, std::result::Result)> { + let paths: Vec = list.iter().map(|x| x.path.clone()).collect(); + list.into_iter().zip(crate::import::probe_with(probe, &paths)).collect() +} + +/// Apply [`probe`]'s results: each photo and its virtual copies get the lens data their file gives now, in one batch +/// that is journaled but isn't an undo step. Returns the photos that gained a profile. +pub fn apply(s: &mut Session, results: Vec<(Missing, std::result::Result)>) -> Result> { + let mut ops = Vec::new(); + let mut found = Vec::new(); + for (item, info) in results { + let Ok(info) = info else { continue }; + if info.embedded_lens.is_none() { + continue; + } + let family: Vec = s + .catalog + .photos() + .filter(|p| p.id == item.id || p.copy_of == Some(item.id)) + .filter(|p| p.id != item.id || crate::cmd::missing::checked_path(p) == Some(item.path.as_str())) + .map(|p| p.id) + .collect(); + for id in family { + if let Some(p) = s.catalog.photo(id) { + let mine = crate::cmd::convert::lens_ops(id, p, &info); + if !mine.is_empty() { + ops.extend(mine); + found.push(id); + } + } + } + } + if !ops.is_empty() { + s.apply_system(Op::Batch { ops })?; + } + for id in &found { + s.media.forget(*id); + } + Ok(found) +} + +/// Why a photo has no lens profile, in words for the Optics panel: `None` when it has one. +pub fn no_profile_reason(p: &Photo) -> Option { + if p.embedded_lens.is_some() { + return None; + } + let lens = p.meta.lens.trim(); + Some(if lens.is_empty() { "This file names no lens.".to_string() } else { format!("No lens profile found for {lens}.") }) +} + +fn refresh(s: &mut Session, p: &Value) -> Result { + const C: &str = "library.refreshLensProfiles"; + let fp = super::recheck::file_probe(s).ok_or_else(|| bad(C, "this session has no file access"))?; + let ids: Option> = match p.get("ids") { + None | Some(Value::Null) => None, + Some(Value::Array(a)) => Some(a.iter().map(|v| v.as_u64().map(PhotoId).ok_or_else(|| bad(C, "ids are photo ids"))).collect::>()?), + Some(_) => return Err(bad(C, "ids is a list of photo ids")), + }; + let Some(db) = crate::lens_profiles::database() else { + return Ok(json!({"checked": 0, "found": [], "database": false})); + }; + let listed = missing(&s.catalog, &db, ids.as_deref()); + let n = listed.len(); + let found = apply(s, probe(&fp, listed))?; + Ok(json!({"checked": n, "found": found.iter().map(|i| i.0).collect::>(), "database": true})) +} + +fn download(s: &mut Session, _: &Value) -> Result { + const C: &str = "lens.downloadDatabase"; + let dir = crate::lensfun_download::default_dir().ok_or_else(|| bad(C, "this session has no data folder"))?; + let activity = s.activity.clone(); + s.lensfun.start(&dir, crate::lensfun_download::mirrors(), crate::lensfun_download::FILES, &activity).map_err(|e| bad(C, e))?; + Ok(json!({"started": true, "dir": dir.to_string_lossy()})) +} + +fn status(s: &mut Session, _: &Value) -> Result { + use crate::lensfun_download::State; + let state = match s.lensfun.state() { + State::Idle => json!({"state": "idle"}), + State::Running { bytes, total } => json!({"state": "running", "bytes": bytes, "total": total}), + State::Done { files } => json!({"state": "done", "files": files}), + State::Failed(e) => json!({"state": "failed", "error": e}), + State::Cancelled => json!({"state": "cancelled"}), + }; + let dir = crate::lens_profiles::dir(); + let lenses = crate::lens_profiles::database().map_or(0, |db| db.lenses.len()); + Ok(json!({"download": state, "dir": dir.map(|d| d.to_string_lossy().to_string()), "lenses": lenses})) +} + +pub fn specs() -> Vec { + vec![ + cmd!( + "library.refreshLensProfiles", + "Look Up Lens Profiles Again", + [], + None, + "{ids?: [photo ids]} — library raws without a lens profile whose lens the lens database knows are read again and get their profile (the correction is switched on where the photo's settings are still as imported); the app does this by itself when the database arrives or changes and when Enable Lens Corrections is ticked; not an undo step → {checked, found: [ids], database: bool}", + always, + refresh + ), + cmd!( + "lens.downloadDatabase", + "Download the Lens Database", + [], + None, + "{} — fetch lensfun's lens database (CC BY-SA 3.0, pinned to one commit, checked file by file) into the app's data folder in the background, with an activity row; the desktop app does this by itself on first run. LIGHTCRAFT_LENSFUN_URL lists other places to fetch the same files from → {started, dir}", + always, + download + ), + cmd!( + "lens.databaseStatus", + "Lens Database Status", + [], + None, + "{} → {download: {state: idle|running|done|failed|cancelled, …}, dir, lenses (in the database in use)}", + always, + status + ), + ] +} diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 9b8dc3dbd2..dbaa43ff72 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -22,6 +22,7 @@ pub mod filters; mod focus; pub mod geocode; pub mod keywords; +pub mod lens_refresh; pub mod library; pub mod lut_profiles; pub mod manage; @@ -168,6 +169,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(browse::specs()); v.extend(missing::specs()); v.extend(recheck::specs()); + v.extend(lens_refresh::specs()); v.extend(sync::specs()); v.extend(metadata::specs()); v.extend(geocode::specs()); diff --git a/crates/engine/src/config.rs b/crates/engine/src/config.rs index b3ded1fbd7..3088b6f119 100644 --- a/crates/engine/src/config.rs +++ b/crates/engine/src/config.rs @@ -146,6 +146,14 @@ impl Session { self } + /// Read lens profiles from the app's own copy of the lens database ([`crate::lensfun_download::default_dir`], + /// unless `LIGHTCRAFT_LENSFUN_DIR` names another), and let the host fetch it by itself when it is missing. + pub fn with_default_lens_database(mut self) -> Self { + crate::lens_profiles::use_dir(crate::lensfun_download::default_dir()); + self.lens_auto_download = true; + self + } + /// Keep denoise models in the shared default folder ([`default_denoise_models_dir`]). pub fn with_default_denoise_models(mut self) -> Self { self.set_denoise_models_dir(default_denoise_models_dir()); diff --git a/crates/engine/src/lens_profiles.rs b/crates/engine/src/lens_profiles.rs index 6e7f28120f..43aec88915 100644 --- a/crates/engine/src/lens_profiles.rs +++ b/crates/engine/src/lens_profiles.rs @@ -1,38 +1,92 @@ -//! Lens profile corrections from a lens database the user supplies ([`lightcraft_lensdb`]). +//! Lens profile corrections from a lens database ([`lightcraft_lensdb`]). //! //! When a raw carries no lens correction of its own (DNG opcodes, or a camera correction a raw reader converts, -//! [`crate::files::embedded_lens`]), the lens named in its EXIF is looked up in a lensfun-format database: the -//! directory in `LIGHTCRAFT_LENSFUN_DIR` (e.g. lensfun's `data/db`, CC-BY-SA, which is never bundled with the app). -//! The profile is converted to the same [`lightcraft_develop::EmbeddedLens`] form, so "Enable Profile Corrections", its -//! amount sliders, the GPU path and DNG export treat both alike. Without the variable nothing changes. -use std::path::PathBuf; -use std::sync::OnceLock; +//! [`crate::files::embedded_lens`]), the lens named in its EXIF is looked up in a lensfun-format database. The +//! database is lensfun's `data/db` (CC BY-SA 3.0): never bundled with the app, it is downloaded at run time into the +//! app's data folder ([`crate::lensfun_download`], which the desktop app starts by itself on first run), or it is the +//! directory in `LIGHTCRAFT_LENSFUN_DIR` (which wins). The profile is converted to the same +//! [`lightcraft_develop::EmbeddedLens`] form (marked [`LensSource::Database`]), so "Enable Profile Corrections", its +//! amount sliders, the GPU path and DNG export treat both alike. Without a database nothing changes. +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, PoisonError, RwLock}; -use lightcraft_develop::EmbeddedLens; +use lightcraft_develop::{EmbeddedLens, LensSource}; use lightcraft_lensdb::{LensDb, Shot}; /// The environment variable naming the lens database directory. pub const ENV: &str = "LIGHTCRAFT_LENSFUN_DIR"; -/// The configured database, read once per process; `None` when none is configured or it holds no lenses. -pub fn database() -> Option<&'static LensDb> { - static DB: OnceLock> = OnceLock::new(); - DB.get_or_init(|| { - let dir = PathBuf::from(std::env::var_os(ENV)?); - match LensDb::load_dir(&dir) { - Ok((db, errors)) => { - for (file, e) in errors { - log::warn!("lens database: skipped {file}: {e}"); - } - (!db.lenses.is_empty()).then_some(db) - } - Err(e) => { - log::warn!("{e}"); - None +/// The database folder a host set up ([`use_dir`]), read when `LIGHTCRAFT_LENSFUN_DIR` is not set. +static HOST_DIR: RwLock> = RwLock::new(None); +/// The loaded database and the folder it was read from (`None` inside: nothing usable there). +static LOADED: RwLock>)>> = RwLock::new(None); +/// Bumped whenever the database in use changes ([`reload`]): photos looked up before may now find a profile. +static GENERATION: AtomicU64 = AtomicU64::new(0); + +/// Use `dir` (the app's downloaded lensfun data, [`crate::lensfun_download::default_dir`]) when +/// `LIGHTCRAFT_LENSFUN_DIR` is not set. Hosts with a file system call it; tests and sessions that don't stay +/// without a database. +pub fn use_dir(dir: Option) { + *HOST_DIR.write().unwrap_or_else(PoisonError::into_inner) = dir; + reload(); +} + +/// Read the database again (after a download or an update): the next lookup sees the new data. +pub fn reload() { + *LOADED.write().unwrap_or_else(PoisonError::into_inner) = None; + GENERATION.fetch_add(1, Ordering::Relaxed); +} + +/// Changes whenever the database in use may have changed. +pub fn generation() -> u64 { + GENERATION.load(Ordering::Relaxed) +} + +/// The folder the database is read from: `LIGHTCRAFT_LENSFUN_DIR`, else the host's ([`use_dir`]). +pub fn dir() -> Option { + std::env::var_os(ENV).filter(|v| !v.is_empty()).map(PathBuf::from).or_else(|| HOST_DIR.read().unwrap_or_else(PoisonError::into_inner).clone()) +} + +/// The database in use, read once per folder (and again after [`reload`]); `None` when none is configured, the +/// folder is missing or it holds no lenses. +pub fn database() -> Option> { + let dir = dir()?; + if let Some((d, db)) = LOADED.read().unwrap_or_else(PoisonError::into_inner).as_ref() + && *d == dir + { + return db.clone(); + } + let db = load(&dir); + *LOADED.write().unwrap_or_else(PoisonError::into_inner) = Some((dir, db.clone())); + db +} + +fn load(dir: &Path) -> Option> { + if !dir.is_dir() { + return None; + } + match LensDb::load_dir(dir) { + Ok((db, errors)) => { + for (file, e) in errors { + log::warn!("lens database: skipped {file}: {e}"); } + (!db.lenses.is_empty()).then(|| Arc::new(db)) } - }) - .as_ref() + Err(e) => { + log::warn!("{e}"); + None + } + } +} + +/// The database's name for the lens a photo's file calls `lens` (taken with `camera`, the make and model as the +/// library shows them): "Canon EF 70-200mm f/2.8L IS II USM". `None` without a database or a match. +pub fn profile_name(db: &LensDb, camera: &str, lens: &str) -> Option { + let cam = camera.split_once(' ').and_then(|(make, model)| db.find_camera(make, model).or_else(|| db.find_camera(make, camera))); + let p = db.find_lens(None, lens, cam)?; + let model = p.models.first()?; + Some(if model.to_lowercase().starts_with(&p.maker.to_lowercase()) || p.maker.is_empty() { model.clone() } else { format!("{} {model}", p.maker) }) } /// The database profile for a photo with metadata `m`, for its `w × h` (unoriented, default-cropped) image, @@ -52,7 +106,7 @@ pub fn lookup(db: &LensDb, m: &lightcraft_meta::Metadata, w: f64, h: f64, orient let crop = frame_crop_factor(camera.map(|c| c.crop_factor), focal, m.focal_length_35mm); let shot = Shot { focal, aperture: m.f_number, distance: None, crop_factor: crop, width: w, height: h }; let corr = lens.correction(&shot)?; - Some(lightcraft_pipeline::optics::reorient_lens(&corr, orientation, w, h)) + Some(EmbeddedLens { source: LensSource::Database, ..lightcraft_pipeline::optics::reorient_lens(&corr, orientation, w, h) }) } /// The crop factor of the recorded frame, which scales the profile's radii: the body's (`camera`), else what the @@ -73,7 +127,7 @@ pub fn for_raw(raw: &lightcraft_raw::RawInfo, meta: impl FnOnce() -> lightcraft_ crate::files::embedded_lens(raw).or_else(|| { let db = database()?; let (w, h) = raw.developed_size(); - lookup(db, &meta(), w as f64, h as f64, raw.orientation) + lookup(&db, &meta(), w as f64, h as f64, raw.orientation) }) } @@ -107,6 +161,9 @@ mod tests { let db = LensDb::parse(DB).unwrap(); let lens = lookup(&db, &meta(), 6000.0, 4000.0, Orientation::Normal).unwrap(); assert!(lens.warp.is_some() && lens.vignette.is_some()); + assert_eq!(lens.source, LensSource::Database, "the Optics panel says where it came from"); + assert_eq!(profile_name(&db, "Testcam TC-1", "18-55mm F3.5-5.6").as_deref(), Some("Testcam 18-55mm f/3.5-5.6")); + assert_eq!(profile_name(&db, "Testcam TC-1", "70-200mm F4"), None); let rotated = lookup(&db, &meta(), 6000.0, 4000.0, Orientation::Rotate90).unwrap(); assert_eq!(rotated.warp.unwrap().center, lightcraft_geom::Point::new(0.5, 0.5)); // no lens name, no focal length, an unknown lens: nothing @@ -164,7 +221,7 @@ mod tests { }; let at = |crop: f64, w: f64, h: f64| { let shot = Shot { focal: 70.0, aperture: Some(2.8), distance: None, crop_factor: Some(crop), width: w, height: h }; - db.lenses[0].correction(&shot).unwrap() + EmbeddedLens { source: LensSource::Database, ..db.lenses[0].correction(&shot).unwrap() } }; let aps_c = lookup(&db, &m(Some(105.0)), 5616.0, 3744.0, Orientation::Normal).unwrap(); assert_eq!(aps_c, at(1.5, 5616.0, 3744.0)); diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs new file mode 100644 index 0000000000..00e03bbdf5 --- /dev/null +++ b/crates/engine/src/lensfun_download.rs @@ -0,0 +1,387 @@ +//! The lensfun lens database, downloaded by itself on first run (zero setup). +//! +//! lensfun's `data/db` (CC BY-SA 3.0, ) is data, never bundled with LightCraft and +//! never committed to its repository: the desktop app fetches it at run time into its data folder +//! ([`default_dir`]), with a row in the activity stack, and [`crate::lens_profiles`] reads it from there. The files are +//! pinned to one lensfun commit ([`COMMIT`]) by name, size and SHA-256 ([`FILES`]), so whatever mirror serves them +//! ([`URL_ENV`] lists others, e.g. a copy on LightCraft's own server), what arrives is exactly that commit's data or +//! it is thrown away. A new pin in a later build replaces the folder's files once ([`installed`] compares the pin). +//! +//! The transfer is [`lightcraft_fetch`] (pure-Rust HTTPS, resume, size and hash checks) on a background thread; the +//! files land in a staging folder first and move into place only when all of them are verified, so the database is +//! never a mix of two versions. Failure is a message, never a panic. + +use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex, PoisonError}; + +use crate::activity::{Activity, Cancel, TaskHandle, Unit}; + +/// The lensfun commit the files are pinned to (2026-10-08). +pub const COMMIT: &str = "43f9e001ab66c4fcdd4f400463f13b72f94b2288"; +/// Where the files are fetched from by default: lensfun's repository at [`COMMIT`]. +pub const DEFAULT_MIRROR: &str = "https://raw.githubusercontent.com/lensfun/lensfun/43f9e001ab66c4fcdd4f400463f13b72f94b2288/data/db"; +/// Other places to fetch the same files from, tried first (base URLs separated by commas or spaces). +pub const URL_ENV: &str = "LIGHTCRAFT_LENSFUN_URL"; +/// The file written once every database file is in place: the pin it holds says which data the folder has. +pub const MARKER: &str = "SOURCE.txt"; +/// The folder inside the database folder where files wait until all have arrived. +const STAGING: &str = ".download"; +/// Largest database file accepted (the biggest is under 1 MB). +const MAX_FILE: u64 = 8 << 20; + +/// The database files at [`COMMIT`]: name, size in bytes, SHA-256. +pub const FILES: &[(&str, u64, &str)] = &[ + ("6x6.xml", 29550, "ca13c2756b32d728675372695c072517a0e38af5eff2ecd3e17599dee7fc58b3"), + ("actioncams.xml", 17188, "79a5da0322574b95690464444c9d9ba1da0e1f827da44fd6bff120a0ec10e4b4"), + ("compact-canon.xml", 247046, "bae47e4b3a0f8852641038ae2dcca5b28ed7965e65ca795fd676f9de857c4670"), + ("compact-casio.xml", 6889, "ab35433879c4574ef8f97cd4e3d114d7ff8db4e7279da9d502fc9e911b51ee27"), + ("compact-fujifilm.xml", 67954, "58af9ebef2410c428ae2c5d01003d2247330d47d918823e63522b4d897a997d1"), + ("compact-kodak.xml", 2091, "377018ccd74f58e966b37a11e5a8689be4268153bebece9f1f2a0dbfa8234845"), + ("compact-konica-minolta.xml", 13349, "832a7473951ab625e10c48af6cf60f89b57280016d45729e2584cf9e41ad3cc7"), + ("compact-leica.xml", 9113, "8e4676679377576dc19e7900ce86fc69b0248ea1d4538aa22ee1ee09431a3be3"), + ("compact-nikon.xml", 40003, "5393bdb8223e13d21b85818e96571e97c035470b49b0452dfcedc2e1ff512d3b"), + ("compact-olympus.xml", 34454, "7637d469ad54a02ebce9492a9aa9424a2ff1ec07b0398158076f762fdc08389b"), + ("compact-panasonic.xml", 70621, "f191fc2a5d49fb50c06f4c399ee8c1a83a05f93a0efaecd352ec5c1479b8e60b"), + ("compact-pentax.xml", 6689, "596fae3edaadcf2d90ae930fc1bec42597ca0b2bd11842e686e1b915f3736390"), + ("compact-ricoh.xml", 18424, "46df96ebc367d5170cb6b7dfc3742093f0ad946264518fc5488573ba052f29d7"), + ("compact-samsung.xml", 13875, "36e3a778046d56385c387d48474cd32a89a5da94d99e640b5ce716ce747f898d"), + ("compact-sigma.xml", 2395, "0ddc265e3e580da05dcd4664d1b9c2464838d74dd02519c826520950c8f1759e"), + ("compact-sony.xml", 126229, "be146388b4f6f7bf0d73394e20386ce6ccef3559ab39f1268ba3c14aaef03207"), + ("contax.xml", 1696, "5eba12e6fd4913470afb6fe25fee47a263084b6cec830297273909872f47669a"), + ("generic.xml", 5416, "cd572d4110981546b2c4cd296c2ff16755b30063fa136d8b06ad2ca13ab4c59f"), + ("mil-canon.xml", 247994, "2de8e9467a6af6f83cc9da834d3d3b94d1aaf54519f60c68ac6bc02780a77516"), + ("mil-fujifilm.xml", 210586, "64586336153b948fe2325f6c46679149a4e3c114a70bee4bd1de88721cb4de72"), + ("mil-hasselblad.xml", 1048, "13e1fa55d36c2ed7d22964c21ce6c21d887bfe4506e840e425da1ce4157c3756"), + ("mil-leica.xml", 11663, "c691037e2e0743388473c2a0545e340462c412b7e3b57e451f4f9c89dde2388a"), + ("mil-nikon.xml", 178941, "dcae5ec2e505411a7f00b6b8dd03825e55620403b558b156be15e9ddfbeaf159"), + ("mil-olympus.xml", 147123, "989a3d88ef07a286efc2755df214792fdf71d60366932a1b369a77ad39cf2abb"), + ("mil-panasonic.xml", 299264, "eb5f01f33e65721f3900786d5970c7e220dd042aaa56968be33c009a358ff3c5"), + ("mil-pentax.xml", 1385, "675a94f52794868ad9f1d79e17b7128783c34b3db23e9eac346c5963812a16e5"), + ("mil-samsung.xml", 13007, "10130e9e125e0882a16fd3033cfed0175e9c14cb7d35e630cdb94736f27e5dc3"), + ("mil-samyang.xml", 25990, "ae3bf262f12facdc6f81b837cf6d36fdde2a930d2bfb0918d5abaa47fcfea3ab"), + ("mil-sigma.xml", 126080, "5f39b232efba5c208e5a98d8afe92cb2eee879213322d47827fcc6f0dc3a06f9"), + ("mil-sony.xml", 274541, "f9f7e5d7461ac58f76eb55dcc5b211f2e9a451402ddb4a9ecb12d1e4dc04f2b8"), + ("mil-tamron.xml", 106950, "2f0c9c4dfb9b0419ba0fac2e618bc251c2e71e068b6e09a23d7994b0ff62db0a"), + ("mil-tokina.xml", 1014, "addb27bd82b0d729644b9da1342f92401f63cc5945309ed17f38f4b7af1649e3"), + ("mil-zeiss.xml", 19171, "ef8a238904fc9ea6fdb6171863dfd70cd94814af19ecd8c31ee000ca1864589d"), + ("misc.xml", 176039, "2000a347d9dc37d908ea5b56c760de5b2b4ab475857959ae2e901c31339b01bb"), + ("om-system.xml", 35344, "a53a627e6ba79fbabb0fa7552ce2b6e56e206425d2eaf0e8ccdb6b8c40c20755"), + ("rf-leica.xml", 6712, "f93636e1980ad308d1873cc3ac078cdc77c0d529d777cd45c5c8d0e4781981e3"), + ("slr-canon.xml", 780353, "5244e6df8f2522f829c57e2073313ce31a624e9ed9605310fe43613b9d588c99"), + ("slr-hasselblad.xml", 642, "97602c467699904bf101179ea2fa24f05f3e1879250498a6ba306cd95a3b7a6b"), + ("slr-konica-minolta.xml", 47170, "5639ca94f121f609247c2c120b65830b3ef7784db6ecfa7e8eb3d29ff5046c1b"), + ("slr-leica.xml", 432, "c5e74cb511712e99ed60a066e85c842fea58f693c5039c3819485619f5293d0e"), + ("slr-nikon.xml", 550157, "5e591e8b1032203cce4c822f1da3d49524a78e7d71c594ea5a4533f2e57aacdf"), + ("slr-olympus.xml", 116602, "f7693b6779edd8ed10d7dc23113669255bfa4987cea7db0e843f7f5f2bbbe615"), + ("slr-panasonic.xml", 419, "cd79acb49d9ce5f2a4cecc572b3e117f03da380bed2dcbfd52327192d8ce2381"), + ("slr-pentax.xml", 209552, "2fba773e41a1cfb70f0c85efdab6d78944bf80ddf89bc2035ef6db39da352f29"), + ("slr-ricoh.xml", 2869, "c835798136a53535fdf4f81312ac9aad0ea3de3ef732c183d8cc4d2f432011d9"), + ("slr-samsung.xml", 1110, "dc0a29e424ff96e4316ddeb76126fa6f6c73a8836905a3e1f56f0c493826148f"), + ("slr-samyang.xml", 16811, "fb1585bea91f5f59e1fae8811dd76c92252ee5d52e3647df2aff4695048443b5"), + ("slr-schneider.xml", 5890, "de607cfe4ba6e7debc61e282ce39b72825c07601572e14af2bc9108ba67faf81"), + ("slr-sigma.xml", 444640, "f55f41c144fbb72e3c51d2dfef40e918930d37db1b4150bd02bd746406c036d2"), + ("slr-soligor.xml", 573, "1aba0aab19bce87ea86175b6551cb5306a512eb54be1698b89991b86008f6a04"), + ("slr-sony.xml", 88865, "e5ca7f532afe23ff380bf2f9e5458f687bed603355d73a254d85c4e692942fdc"), + ("slr-tamron.xml", 201992, "c62090789cebe6bbb663111c6e97beeb1111852bb735088e29cf8782df4bab45"), + ("slr-tokina.xml", 91977, "b1213eda81a613bc3976340398c6b8ce8b7dc29423476fc565d5f8e5cf612ecf"), + ("slr-ussr.xml", 19024, "1fd6acc8a8f40373379bea2899ffd96fc34b22d9b044d0474dabd4c89af44efb"), + ("slr-vivitar.xml", 2157, "c5c67e3fe38e8fa19044acadc7730047690435f66aac563f55ffe04997a88111"), + ("slr-zeiss.xml", 14957, "49aa211a41fd5fe391d26af5fa65bdf9428d60863ffff15d3916fa1c520dc101"), +]; + +/// The app's own copy of the database: `/lensfun`. +pub fn default_dir() -> Option { + crate::config::config_dir().map(|d| d.join("lensfun")) +} + +/// The folder holds the pinned data ([`MARKER`] names [`COMMIT`]). +pub fn installed(dir: &Path) -> bool { + std::fs::read_to_string(dir.join(MARKER)).is_ok_and(|s| s.contains(COMMIT)) +} + +/// The mirrors to try, in order: [`URL_ENV`]'s, then lensfun's own ([`DEFAULT_MIRROR`]). +pub fn mirrors() -> Vec { + #[cfg(not(target_arch = "wasm32"))] + { + lightcraft_fetch::mirrors(std::env::var(URL_ENV).ok().as_deref(), None, &[DEFAULT_MIRROR]) + } + #[cfg(target_arch = "wasm32")] + { + Vec::new() + } +} + +/// Where the download stands. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub enum State { + /// Not started this session. + #[default] + Idle, + Running { + bytes: u64, + total: u64, + }, + /// In place and loaded: `files` database files. + Done { + files: usize, + }, + Failed(String), + Cancelled, +} + +struct Shared { + state: Mutex, + cancel: Arc, + task: Option, +} + +impl Shared { + fn set(&self, s: State) { + if let (State::Running { bytes, total }, Some(task)) = (&s, &self.task) { + task.progress(*bytes, *total); + } + *self.state.lock().unwrap_or_else(PoisonError::into_inner) = s; + } + + fn get(&self) -> State { + self.state.lock().unwrap_or_else(PoisonError::into_inner).clone() + } +} + +/// The session's lens database download. +#[derive(Default)] +pub struct Download { + job: Option>, +} + +/// One pinned file: name, size in bytes, SHA-256. +pub type Pin = (&'static str, u64, &'static str); + +impl Download { + pub fn state(&self) -> State { + self.job.as_ref().map_or(State::Idle, |j| j.get()) + } + + pub fn running(&self) -> bool { + matches!(self.state(), State::Running { .. }) + } + + /// Fetch `files` from `mirrors` into `dir` on a background thread, shown in the activity stack until it ends + /// (its ✕ stops it). Started once at a time; when it ends the database is read again + /// ([`crate::lens_profiles::reload`]). + pub fn start(&mut self, dir: &Path, mirrors: Vec, files: &'static [Pin], activity: &Activity) -> Result<(), String> { + if self.running() { + return Err("the lens database is already downloading".into()); + } + if mirrors.is_empty() { + return Err("no download location is configured for the lens database".into()); + } + let cancel = Arc::new(AtomicBool::new(false)); + let task = activity.start("download", "Downloading the lens database", Cancel::Flag(cancel.clone())); + task.set_unit(Unit::Bytes); + task.detail("lensfun (CC BY-SA 3.0)"); + let total = files.iter().map(|f| f.1).fold(0u64, u64::saturating_add); + task.progress(0, total); + let shared = Arc::new(Shared { state: Mutex::new(State::Running { bytes: 0, total }), cancel, task: Some(task.handle()) }); + let worker = shared.clone(); + let dir = dir.to_path_buf(); + std::thread::Builder::new() + .name("lens-database-download".into()) + .spawn(move || { + // the row goes when the thread ends, however it ends + let _task = task; + let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| run(&dir, &mirrors, files, &worker))); + if outcome.is_err() { + worker.set(State::Failed("the lens database download stopped unexpectedly".into())); + } + }) + .map_err(|e| format!("could not start the lens database download: {e}"))?; + self.job = Some(shared); + Ok(()) + } +} + +impl Drop for Download { + /// Quitting while it runs stops it (the staged files stay for the next try to resume). + fn drop(&mut self) { + if let Some(j) = &self.job { + j.cancel.store(true, Ordering::Relaxed); + } + } +} + +/// The whole job: fetch and check every file into the staging folder, then move them into `dir`, write the marker +/// and read the database again. Always ends in a final [`State`]. +fn run(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) { + match fetch_all(dir, mirrors, files, shared) { + Ok(n) => { + crate::lens_profiles::reload(); + shared.set(State::Done { files: n }); + } + Err(e) if shared.cancel.load(Ordering::Relaxed) => { + log::info!("lens database download stopped: {e}"); + shared.set(State::Cancelled); + } + Err(e) => { + log::warn!("lens database download: {e}"); + shared.set(State::Failed(e)); + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> Result { + let staging = dir.join(STAGING); + std::fs::create_dir_all(&staging).map_err(|e| format!("could not create {}: {e}", staging.display()))?; + let specs: Vec> = + files.iter().map(|(name, size, sha)| lightcraft_fetch::FileSpec { name, size: Some(*size), sha256: Some(sha), max: MAX_FILE }).collect(); + let total = files.iter().map(|f| f.1).fold(0u64, u64::saturating_add); + lightcraft_fetch::download(&specs, mirrors, &staging, &lightcraft_fetch::Options::default(), &shared.cancel, &mut |p| { + shared.set(State::Running { bytes: p.done.min(total), total }); + }) + .map_err(|e| match e { + lightcraft_fetch::DownloadError::AllMirrorsFailed { errors, .. } => { + let detail: String = errors.join("; ").chars().take(300).collect(); + format!("could not download the lens database ({detail}): check the internet connection; LightCraft tries again next time it starts") + } + other => other.to_string(), + })?; + // every file is here and checked: into place, then the marker that says so + let _ = std::fs::remove_file(dir.join(MARKER)); + for (name, _, _) in files { + std::fs::rename(staging.join(name), dir.join(name)).map_err(|e| format!("could not move {name} into {}: {e}", dir.display()))?; + } + let _ = std::fs::remove_dir_all(&staging); + let readme = format!( + "lensfun lens database (data/db), commit {COMMIT}\nfrom https://github.com/lensfun/lensfun, downloaded by LightCraft.\n\ + Licence: Creative Commons Attribution-ShareAlike 3.0 (https://creativecommons.org/licenses/by-sa/3.0/), by the lensfun contributors.\n\ + Data only, read by LightCraft at run time; not part of LightCraft.\n" + ); + std::fs::write(dir.join(MARKER), readme).map_err(|e| format!("could not write {}: {e}", dir.join(MARKER).display()))?; + Ok(files.len()) +} + +#[cfg(target_arch = "wasm32")] +fn fetch_all(_: &Path, _: &[String], _: &[Pin], _: &Shared) -> Result { + Err("the web build downloads no lens database".into()) +} + +#[cfg(all(test, not(target_arch = "wasm32")))] +mod tests { + use super::*; + use std::io::{BufRead, BufReader, Write}; + use std::net::TcpListener; + use std::time::Duration; + + fn scratch(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("lc-lensfun-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d + } + + /// A local server answering `GET /db/` with that file's body (404 otherwise). + fn serve(files: Vec<(&'static str, Vec)>) -> String { + let l = TcpListener::bind("127.0.0.1:0").unwrap(); + let base = format!("http://{}/db", l.local_addr().unwrap()); + std::thread::spawn(move || { + for mut s in l.incoming().flatten() { + let mut r = BufReader::new(s.try_clone().unwrap()); + let mut first = String::new(); + let _ = r.read_line(&mut first); + loop { + let mut line = String::new(); + if r.read_line(&mut line).unwrap_or(0) == 0 || line.trim().is_empty() { + break; + } + } + let path = first.split_whitespace().nth(1).unwrap_or_default().to_string(); + match files.iter().find(|(n, _)| path == format!("/db/{n}")) { + Some((_, body)) => { + let _ = s.write_all(format!("HTTP/1.1 200 OK\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len()).as_bytes()); + let _ = s.write_all(body); + } + None => { + let _ = s.write_all(b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"); + } + } + } + }); + base + } + + fn wait(what: &str, done: impl Fn() -> bool) { + let t = std::time::Instant::now(); + while !done() { + assert!(t.elapsed() < Duration::from_secs(20), "{what}"); + std::thread::sleep(Duration::from_millis(10)); + } + } + + const LENS: &str = r#"TestcamTestcam 50mm f/1.8T1 + "#; + const CAMERA: &str = + r#"TestcamTC-1T1"#; + + fn sha(s: &str) -> &'static str { + Box::leak(lightcraft_faces::hash::sha256_hex(s.as_bytes()).into_boxed_str()) + } + + fn pins() -> &'static [Pin] { + Box::leak(vec![("lenses.xml", LENS.len() as u64, sha(LENS)), ("cameras.xml", CAMERA.len() as u64, sha(CAMERA))].into_boxed_slice()) + } + + /// Every pinned file lands in the folder with the marker, the activity row shows the download until it ends, + /// and the database reads. + #[test] + fn downloads_checks_and_installs_the_pinned_files() { + let dir = scratch("ok"); + let base = serve(vec![("lenses.xml", LENS.as_bytes().to_vec()), ("cameras.xml", CAMERA.as_bytes().to_vec())]); + let activity = Activity::default(); + let mut d = Download::default(); + assert!(!installed(&dir)); + d.start(&dir, vec![base], pins(), &activity).unwrap(); + let rows = activity.list(); + assert!(rows.iter().any(|r| r.kind == "download" && r.label == "Downloading the lens database" && r.cancellable), "{rows:?}"); + wait("the download never ended", || !d.running()); + assert_eq!(d.state(), State::Done { files: 2 }); + assert!(installed(&dir), "the marker names the pinned commit"); + assert!(!dir.join(STAGING).exists(), "the staging folder is gone"); + let (db, errors) = lightcraft_lensdb::LensDb::load_dir(&dir).unwrap(); + assert!(errors.is_empty() && db.lenses.len() == 1 && db.cameras.len() == 1); + wait("the row stayed", || activity.list().is_empty()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A file that is not the pinned one (another commit, a hostile mirror) is refused and nothing is installed. + #[test] + fn a_file_that_is_not_the_pinned_one_installs_nothing() { + let dir = scratch("bad"); + let base = serve(vec![("lenses.xml", LENS.replace("0.01", "0.02").into_bytes()), ("cameras.xml", CAMERA.as_bytes().to_vec())]); + let mut d = Download::default(); + d.start(&dir, vec![base], pins(), &Activity::default()).unwrap(); + wait("the download never ended", || !d.running()); + assert!(matches!(d.state(), State::Failed(_)), "{:?}", d.state()); + assert!(!installed(&dir) && !dir.join("cameras.xml").exists(), "nothing moved into place"); + // no mirror at all is an error, not a thread + assert!(Download::default().start(&dir, Vec::new(), pins(), &Activity::default()).is_err()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The pinned list is lensfun's whole `data/db` at the commit: unique XML names, sane sizes, hex hashes. + #[test] + fn the_pins_are_well_formed() { + assert_eq!(FILES.len(), 56); + let mut names: Vec<&str> = FILES.iter().map(|f| f.0).collect(); + names.sort_unstable(); + names.dedup(); + assert_eq!(names.len(), FILES.len()); + for (name, size, sha) in FILES { + assert!(name.ends_with(".xml") && !name.contains(['/', '\\']), "{name}"); + assert!(*size > 0 && *size < MAX_FILE, "{name}"); + assert!(sha.len() == 64 && sha.bytes().all(|b| b.is_ascii_hexdigit()), "{name}"); + } + assert!(DEFAULT_MIRROR.contains(COMMIT)); + } +} diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index ff2b972c0a..3f1d786406 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -46,6 +46,7 @@ pub mod guard; pub mod import; mod import_move; pub mod lens_profiles; +pub mod lensfun_download; pub mod library; mod lightroom_archive; pub mod lightroom_catalog; @@ -284,6 +285,10 @@ pub struct Session { pub face_models_dir: Option, /// Face model downloads started this session (the staged files wait in `/.downloads`). pub face_downloads: face_download::Downloads, + /// The lens database download ([`lensfun_download`]). + pub lensfun: lensfun_download::Download, + /// The host fetches the lens database by itself when it is missing ([`Session::with_default_lens_database`]). + pub lens_auto_download: bool, /// Reverse geocoding: the parsed place names and their download (`metadata.*PlaceNames`). pub place_names: cmd::geocode::PlaceNames, /// The user's own list of models to download (`catalog.json` in the models folder), as last read. @@ -476,6 +481,8 @@ impl Session { denoise: Default::default(), face_models_dir: None, face_downloads: Default::default(), + lensfun: Default::default(), + lens_auto_download: false, place_names: Default::default(), face_catalog: Default::default(), #[cfg(not(target_arch = "wasm32"))] @@ -1406,6 +1413,8 @@ mod tests_import_move; #[cfg(test)] mod tests_keyword_list; #[cfg(test)] +mod tests_lens_refresh; +#[cfg(test)] mod tests_libops; #[cfg(test)] mod tests_library; diff --git a/crates/engine/src/tests_lens_refresh.rs b/crates/engine/src/tests_lens_refresh.rs new file mode 100644 index 0000000000..b502f58157 --- /dev/null +++ b/crates/engine/src/tests_lens_refresh.rs @@ -0,0 +1,120 @@ +//! Lens profiles stay per lens. Presets, Copy / Paste and Sync carry "Enable Lens Corrections" and +//! its amounts, never a profile: each photo keeps the one found for its own lens. And photos imported before the lens +//! database was there get their profile when it arrives (`cmd::lens_refresh`). + +use std::sync::Arc; + +use lightcraft_catalog::{MediaKind, Op, Photo, PhotoId, Source}; +use lightcraft_develop::{EmbeddedLens, EmbeddedWarp, LensSource}; +use serde_json::json; + +use crate::Session; + +fn lens(k: f64) -> EmbeddedLens { + EmbeddedLens { + warp: Some(EmbeddedWarp { planes: [[1.0, k, 0.0, 0.0, 0.0, 0.0]; 3], radius: 0.6, ..Default::default() }), + source: LensSource::Database, + ..Default::default() + } +} + +fn lens_of(s: &Session, id: PhotoId) -> Option { + s.catalog.photo(id).and_then(|p| p.embedded_lens) +} + +/// Two demo photos "taken with different lenses". +fn two_lenses() -> (Session, PhotoId, PhotoId) { + let mut s = Session::with_demo(); + let ids: Vec = s.visible().iter().copied().take(2).collect(); + let (a, b) = (ids[0], ids[1]); + s.catalog.apply(Op::SetEmbeddedLens { id: a, lens: Some(Box::new(lens(-0.05))) }).unwrap(); + s.catalog.apply(Op::SetEmbeddedLens { id: b, lens: Some(Box::new(lens(0.03))) }).unwrap(); + s.execute("library.select", &json!({"ids": [a.0]})).unwrap(); + s.execute("develop.merge", &json!({"settings": {"optics": {"lens_profile": true}}, "label": "Lens Corrections"})).unwrap(); + s.execute("develop.set", &json!({"values": {"optics.profileDistortion": 60}})).unwrap(); + (s, a, b) +} + +fn carried(s: &Session, id: PhotoId) { + let d = s.develop_of(id).unwrap(); + assert!(d.optics.lens_profile, "the switch travels"); + assert_eq!(d.optics.profile_distortion, 60.0, "the amount travels"); +} + +#[test] +fn copy_paste_carries_the_switch_and_amounts_never_the_profile() { + let (mut s, a, b) = two_lenses(); + s.execute("develop.copy", &json!({})).unwrap(); + s.execute("develop.paste", &json!({"ids": [b.0]})).unwrap(); + carried(&s, b); + assert_eq!(lens_of(&s, b), Some(lens(0.03)), "b keeps its own lens's profile"); + assert_eq!(lens_of(&s, a), Some(lens(-0.05))); +} + +#[test] +fn sync_and_presets_keep_each_photos_own_profile() { + let (mut s, a, b) = two_lenses(); + s.execute("library.select", &json!({"ids": [a.0, b.0], "active": a.0})).unwrap(); + s.execute("develop.sync", &json!({})).unwrap(); + carried(&s, b); + assert_eq!(lens_of(&s, b), Some(lens(0.03))); + // a preset made on a's settings, applied to b + let pid = s.execute("preset.create", &json!({"name": "Optics", "groups": ["optics"]})).unwrap()["id"].as_str().unwrap().to_string(); + s.execute("library.select", &json!({"ids": [b.0]})).unwrap(); + s.execute("develop.merge", &json!({"settings": {"optics": {"lens_profile": false}}, "label": "Lens Corrections"})).unwrap(); + s.execute("preset.apply", &json!({"id": pid})).unwrap(); + carried(&s, b); + assert_eq!(lens_of(&s, b), Some(lens(0.03)), "a preset never brings another lens's profile"); + assert_eq!(lens_of(&s, a), Some(lens(-0.05))); +} + +/// A raw in a library folder, as an older import left it: no lens data. +fn raw(id: u64, lens_name: &str, edited: bool) -> Photo { + let mut p = + Photo::new(PhotoId(id), Source::File { path: format!("/photos/{id}.cr3") }, &format!("{id}.cr3"), "CR3", 600, 400, "2026-10-01T00:00:00"); + p.kind = MediaKind::Raw; + p.meta.camera = "Testcam TC-1".into(); + p.meta.lens = lens_name.into(); + if edited { + let mut d = (*p.develop).clone(); + d.light.exposure = 0.5; + p.develop = Arc::new(d); + p.edited = Some("2026-10-02T00:00:00".into()); + } + p +} + +const DB: &str = r#" + TestcamTC-1T1 + TestcamTestcam 50mm f/1.8T1 + "#; + +/// When the database arrives, the library's raws whose lens it knows get their profile; untouched photos get the +/// correction switched on as import would, edited ones keep their settings; lenses it doesn't know are not probed. +#[test] +fn photos_get_their_profile_when_the_database_arrives() { + let mut s = Session::new(); + for p in [raw(1, "50mm F1.8", false), raw(2, "50mm F1.8", true), raw(3, "300mm F4", false), raw(4, "", false)] { + s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap(); + } + let db = lightcraft_lensdb::LensDb::parse(DB).unwrap(); + let listed = crate::cmd::lens_refresh::missing(&s.catalog, &db, None); + assert_eq!(listed.iter().map(|m| m.id.0).collect::>(), [1, 2], "only lenses the database knows"); + assert_eq!(crate::cmd::lens_refresh::missing(&s.catalog, &db, Some(&[PhotoId(2)])).len(), 1, "or only the photos asked for"); + // what the files give when read again + let results = listed.into_iter().map(|m| (m, Ok(crate::media::ProbeInfo { embedded_lens: Some(lens(-0.01)), ..Default::default() }))).collect(); + let found = crate::cmd::lens_refresh::apply(&mut s, results).unwrap(); + assert_eq!(found, [PhotoId(1), PhotoId(2)]); + let p = |id: u64| s.catalog.photo(PhotoId(id)).unwrap().clone(); + assert!(p(1).embedded_lens.is_some() && p(1).develop.optics.lens_profile, "untouched: on, as import would"); + assert!(p(2).embedded_lens.is_some() && !p(2).develop.optics.lens_profile, "edited: its settings stay"); + assert!(p(3).embedded_lens.is_none()); + assert!(s.undo.is_empty(), "bookkeeping, not an undo step"); + // the Optics panel's words + assert_eq!(crate::cmd::lens_refresh::no_profile_reason(&p(3)).as_deref(), Some("No lens profile found for 300mm F4.")); + assert_eq!(crate::cmd::lens_refresh::no_profile_reason(&p(4)).as_deref(), Some("This file names no lens.")); + assert_eq!(crate::cmd::lens_refresh::no_profile_reason(&p(1)), None); + // without file access the command says so instead of clearing anything + assert!(s.execute("library.refreshLensProfiles", &json!({})).is_err()); + assert!(s.execute("library.refreshLensProfiles", &json!({"ids": "x"})).is_err()); +} diff --git a/crates/gpu/src/render.rs b/crates/gpu/src/render.rs index 64990db0e1..cb47f18db7 100644 --- a/crates/gpu/src/render.rs +++ b/crates/gpu/src/render.rs @@ -1104,6 +1104,7 @@ mod tests { }), vignette: None, stages: Default::default(), + source: Default::default(), }; let mut s = DevelopSettings::default(); s.optics.lens_profile = true; diff --git a/crates/gpu/tests/it/equivalence.rs b/crates/gpu/tests/it/equivalence.rs index 6a8169b301..fe055d3757 100644 --- a/crates/gpu/tests/it/equivalence.rs +++ b/crates/gpu/tests/it/equivalence.rs @@ -553,6 +553,7 @@ fn embedded_lens_perspective_edges_match() { }), vignette: Some(EmbeddedVignette { k: [0.4, -0.1, 0.02, 0.0, 0.0], center: Point::new(0.5, 0.5), radius: 0.6 }), stages: Default::default(), + source: Default::default(), }; let info = SourceInfo { lens: Some(lens), ..raw }; let mut s = DevelopSettings::default(); diff --git a/crates/pipeline/src/optics.rs b/crates/pipeline/src/optics.rs index 31e8d1351b..462c8c649c 100644 --- a/crates/pipeline/src/optics.rs +++ b/crates/pipeline/src/optics.rs @@ -375,7 +375,7 @@ pub fn reorient_lens(lens: &EmbeddedLens, o: Orientation, w: f64, h: f64) -> Emb EmbeddedWarpStage { planes, center: map_pt(st.center), radius: st.radius * long_ratio, reciprocal: st.reciprocal } }) }); - EmbeddedLens { warp, vignette, stages } + EmbeddedLens { warp, vignette, stages, source: lens.source } } // ------------------------------------------------------------------------------------------ lateral CA diff --git a/crates/ui-egui/locales/cs-formats.json b/crates/ui-egui/locales/cs-formats.json index 1eb882f09d..f79aee3fb1 100644 --- a/crates/ui-egui/locales/cs-formats.json +++ b/crates/ui-egui/locales/cs-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "Soubor {name} se nepodařilo přečíst: {e}", "Added {n} photo{}": "Přidáno fotografií: {n}{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "Kam má jít katalog Lightroomu „{catalog}“?", - "Merge into the open library “{library}” ({count} photo{})": "Sloučit s otevřenou knihovnou „{library}“ (fotografie: {count}{:.0})" + "Merge into the open library “{library}” ({count} photo{})": "Sloučit s otevřenou knihovnou „{library}“ (fotografie: {count}{:.0})", + "Profile: {} (lens database)": "Profil: {} (databáze objektivů)", + "No lens profile found for {}.": "Pro {} nebyl nalezen profil objektivu.", + "{n} photos got their lens profile": "Profil objektivu získaly fotky: {n}" } diff --git a/crates/ui-egui/locales/cs.json b/crates/ui-egui/locales/cs.json index 52ca94a524..58362cd1ca 100644 --- a/crates/ui-egui/locales/cs.json +++ b/crates/ui-egui/locales/cs.json @@ -2408,5 +2408,11 @@ "Import dropped files without review": "Importovat přetažené soubory bez kontroly", "An import is running": "Import už běží", "An import is running. Drop photos when it has finished": "Import už běží. Fotky přetáhněte, až skončí", - "Finish Lightroom catalog import before adding photos": "Dokončete import katalogu Lightroom, než přidáte fotky" + "Finish Lightroom catalog import before adding photos": "Dokončete import katalogu Lightroom, než přidáte fotky", + "Profile: from the file (the camera's own correction)": "Profil: ze souboru (vlastní korekce fotoaparátu)", + "Downloading the lens database…": "Stahuje se databáze objektivů…", + "This file names no lens.": "Tento soubor neuvádí žádný objektiv.", + "1 photo got its lens profile": "1 fotka získala profil objektivu", + "Looking up lens profiles": "Hledání profilů objektivů", + "Downloading the lens database": "Stahování databáze objektivů" } diff --git a/crates/ui-egui/locales/de-formats.json b/crates/ui-egui/locales/de-formats.json index 79390f1364..77a30016fc 100644 --- a/crates/ui-egui/locales/de-formats.json +++ b/crates/ui-egui/locales/de-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "{name} konnte nicht gelesen werden: {e}", "Added {n} photo{}": "{n} Foto{} hinzugefügt", "Where should the Lightroom catalog “{catalog}” go?": "Wohin soll der Lightroom-Katalog „{catalog}“?", - "Merge into the open library “{library}” ({count} photo{})": "Mit der geöffneten Bibliothek „{library}“ zusammenführen ({count} Foto{})" + "Merge into the open library “{library}” ({count} photo{})": "Mit der geöffneten Bibliothek „{library}“ zusammenführen ({count} Foto{})", + "Profile: {} (lens database)": "Profil: {} (Objektivdatenbank)", + "No lens profile found for {}.": "Kein Objektivprofil für {} gefunden.", + "{n} photos got their lens profile": "{n} Fotos haben ihr Objektivprofil erhalten" } diff --git a/crates/ui-egui/locales/de.json b/crates/ui-egui/locales/de.json index e2205db782..a02b0ad59d 100644 --- a/crates/ui-egui/locales/de.json +++ b/crates/ui-egui/locales/de.json @@ -3440,5 +3440,11 @@ "Import dropped files without review": "Abgelegte Dateien ohne Prüfung importieren", "An import is running": "Ein Import läuft", "An import is running. Drop photos when it has finished": "Ein Import läuft. Lege die Fotos ab, wenn er fertig ist", - "Finish Lightroom catalog import before adding photos": "Schließe den Import des Lightroom-Katalogs ab, bevor du Fotos hinzufügst" + "Finish Lightroom catalog import before adding photos": "Schließe den Import des Lightroom-Katalogs ab, bevor du Fotos hinzufügst", + "Profile: from the file (the camera's own correction)": "Profil: aus der Datei (Korrektur der Kamera)", + "Downloading the lens database…": "Objektivdatenbank wird geladen…", + "This file names no lens.": "Diese Datei nennt kein Objektiv.", + "1 photo got its lens profile": "1 Foto hat sein Objektivprofil erhalten", + "Looking up lens profiles": "Objektivprofile werden gesucht", + "Downloading the lens database": "Objektivdatenbank wird geladen" } diff --git a/crates/ui-egui/locales/es-formats.json b/crates/ui-egui/locales/es-formats.json index 33e5f52fc6..90015d5291 100644 --- a/crates/ui-egui/locales/es-formats.json +++ b/crates/ui-egui/locales/es-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "No se pudo leer {name}: {e}", "Added {n} photo{}": "{n} foto{0} añadida{0}", "Where should the Lightroom catalog “{catalog}” go?": "¿Dónde debe ir el catálogo de Lightroom “{catalog}”?", - "Merge into the open library “{library}” ({count} photo{})": "Combinar con la biblioteca abierta “{library}” ({count} foto{})" + "Merge into the open library “{library}” ({count} photo{})": "Combinar con la biblioteca abierta “{library}” ({count} foto{})", + "Profile: {} (lens database)": "Perfil: {} (base de datos de objetivos)", + "No lens profile found for {}.": "No se encontró un perfil de objetivo para {}.", + "{n} photos got their lens profile": "{n} fotos recibieron su perfil de objetivo" } diff --git a/crates/ui-egui/locales/es.json b/crates/ui-egui/locales/es.json index 888af077ff..5152a8f518 100644 --- a/crates/ui-egui/locales/es.json +++ b/crates/ui-egui/locales/es.json @@ -2358,5 +2358,11 @@ "Import dropped files without review": "Importar los archivos soltados sin revisión", "An import is running": "Hay una importación en curso", "An import is running. Drop photos when it has finished": "Hay una importación en curso. Suelta las fotos cuando termine", - "Finish Lightroom catalog import before adding photos": "Termina la importación del catálogo de Lightroom antes de añadir fotos" + "Finish Lightroom catalog import before adding photos": "Termina la importación del catálogo de Lightroom antes de añadir fotos", + "Profile: from the file (the camera's own correction)": "Perfil: del archivo (corrección de la propia cámara)", + "Downloading the lens database…": "Descargando la base de datos de objetivos…", + "This file names no lens.": "Este archivo no indica ningún objetivo.", + "1 photo got its lens profile": "1 foto recibió su perfil de objetivo", + "Looking up lens profiles": "Buscando perfiles de objetivo", + "Downloading the lens database": "Descargando la base de datos de objetivos" } diff --git a/crates/ui-egui/locales/fr-formats.json b/crates/ui-egui/locales/fr-formats.json index 294273781e..2b0ee74bbd 100644 --- a/crates/ui-egui/locales/fr-formats.json +++ b/crates/ui-egui/locales/fr-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "Impossible de lire {name} : {e}", "Added {n} photo{}": "{n} photo{} ajoutée(s)", "Where should the Lightroom catalog “{catalog}” go?": "Où importer le catalogue Lightroom « {catalog} » ?", - "Merge into the open library “{library}” ({count} photo{})": "Fusionner avec la bibliothèque ouverte « {library} » ({count} photo{})" + "Merge into the open library “{library}” ({count} photo{})": "Fusionner avec la bibliothèque ouverte « {library} » ({count} photo{})", + "Profile: {} (lens database)": "Profil : {} (base de données d'objectifs)", + "No lens profile found for {}.": "Aucun profil d'objectif trouvé pour {}.", + "{n} photos got their lens profile": "{n} photos ont reçu leur profil d'objectif" } diff --git a/crates/ui-egui/locales/fr.json b/crates/ui-egui/locales/fr.json index 3278d6ccda..1d1a999a88 100644 --- a/crates/ui-egui/locales/fr.json +++ b/crates/ui-egui/locales/fr.json @@ -2390,5 +2390,11 @@ "Checking recent libraries": "Vérification des bibliothèques récentes", "The photos are imported once a library is open": "Les photos seront importées dès qu'une bibliothèque sera ouverte", "Open a library first, then drop the presets again": "Ouvrez d'abord une bibliothèque, puis déposez à nouveau les préréglages", - "This library couldn't be checked:": "Impossible de vérifier cette bibliothèque :" + "This library couldn't be checked:": "Impossible de vérifier cette bibliothèque :", + "Profile: from the file (the camera's own correction)": "Profil : du fichier (correction de l'appareil)", + "Downloading the lens database…": "Téléchargement de la base de données d'objectifs…", + "This file names no lens.": "Ce fichier n'indique aucun objectif.", + "1 photo got its lens profile": "1 photo a reçu son profil d'objectif", + "Looking up lens profiles": "Recherche des profils d'objectif", + "Downloading the lens database": "Téléchargement de la base de données d'objectifs" } diff --git a/crates/ui-egui/locales/it-formats.json b/crates/ui-egui/locales/it-formats.json index 7efae90e94..600df15c07 100644 --- a/crates/ui-egui/locales/it-formats.json +++ b/crates/ui-egui/locales/it-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "Impossibile leggere {name}: {e}", "Added {n} photo{}": "Foto aggiunte: {n}{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "Dove importare il catalogo Lightroom “{catalog}”?", - "Merge into the open library “{library}” ({count} photo{})": "Unisci alla libreria aperta “{library}” (foto: {count}{:.0})" + "Merge into the open library “{library}” ({count} photo{})": "Unisci alla libreria aperta “{library}” (foto: {count}{:.0})", + "Profile: {} (lens database)": "Profilo: {} (database degli obiettivi)", + "No lens profile found for {}.": "Nessun profilo obiettivo trovato per {}.", + "{n} photos got their lens profile": "{n} foto hanno ricevuto il profilo dell'obiettivo" } diff --git a/crates/ui-egui/locales/it.json b/crates/ui-egui/locales/it.json index e159fd47d1..14419b86b8 100644 --- a/crates/ui-egui/locales/it.json +++ b/crates/ui-egui/locales/it.json @@ -2361,5 +2361,11 @@ "Import dropped files without review": "Importa i file rilasciati senza revisione", "An import is running": "Un'importazione è in corso", "An import is running. Drop photos when it has finished": "Un'importazione è in corso. Trascina le foto quando è finita", - "Finish Lightroom catalog import before adding photos": "Termina l'importazione del catalogo Lightroom prima di aggiungere foto" + "Finish Lightroom catalog import before adding photos": "Termina l'importazione del catalogo Lightroom prima di aggiungere foto", + "Profile: from the file (the camera's own correction)": "Profilo: dal file (correzione della fotocamera)", + "Downloading the lens database…": "Download del database degli obiettivi…", + "This file names no lens.": "Questo file non indica alcun obiettivo.", + "1 photo got its lens profile": "1 foto ha ricevuto il profilo dell'obiettivo", + "Looking up lens profiles": "Ricerca dei profili degli obiettivi", + "Downloading the lens database": "Download del database degli obiettivi" } diff --git a/crates/ui-egui/locales/ja-formats.json b/crates/ui-egui/locales/ja-formats.json index 99c14bf16d..dfe3de7b00 100644 --- a/crates/ui-egui/locales/ja-formats.json +++ b/crates/ui-egui/locales/ja-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "{name} を読み取れませんでした: {e}", "Added {n} photo{}": "{n}枚を追加しました{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "Lightroom カタログ「{catalog}」をどこに読み込みますか?", - "Merge into the open library “{library}” ({count} photo{})": "開いているライブラリ「{library}」に統合(写真{count}枚){:.0}" + "Merge into the open library “{library}” ({count} photo{})": "開いているライブラリ「{library}」に統合(写真{count}枚){:.0}", + "Profile: {} (lens database)": "プロファイル: {}(レンズデータベース)", + "No lens profile found for {}.": "{} のレンズプロファイルが見つかりません。", + "{n} photos got their lens profile": "{n} 枚の写真にレンズプロファイルが適用されました" } diff --git a/crates/ui-egui/locales/ja.json b/crates/ui-egui/locales/ja.json index e1834555bd..d7670e1c45 100644 --- a/crates/ui-egui/locales/ja.json +++ b/crates/ui-egui/locales/ja.json @@ -2356,5 +2356,11 @@ "Import dropped files without review": "ドロップしたファイルを確認せずに読み込む", "An import is running": "読み込み中です", "An import is running. Drop photos when it has finished": "読み込み中です。完了してから写真をドロップしてください", - "Finish Lightroom catalog import before adding photos": "写真を追加する前に Lightroom カタログの読み込みを完了してください" + "Finish Lightroom catalog import before adding photos": "写真を追加する前に Lightroom カタログの読み込みを完了してください", + "Profile: from the file (the camera's own correction)": "プロファイル: ファイル内(カメラ自身の補正)", + "Downloading the lens database…": "レンズデータベースをダウンロード中…", + "This file names no lens.": "このファイルにはレンズ名がありません。", + "1 photo got its lens profile": "1 枚の写真にレンズプロファイルが適用されました", + "Looking up lens profiles": "レンズプロファイルを検索中", + "Downloading the lens database": "レンズデータベースをダウンロード中" } diff --git a/crates/ui-egui/locales/ka-formats.json b/crates/ui-egui/locales/ka-formats.json index 4603682618..bacd85c73c 100644 --- a/crates/ui-egui/locales/ka-formats.json +++ b/crates/ui-egui/locales/ka-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "ვერ წაიკითხა {name}: {e}", "Added {n} photo{}": "დაემატა ფოტო: {n}{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "სად გადავიტანოთ Lightroom-ის კატალოგი „{catalog}“?", - "Merge into the open library “{library}” ({count} photo{})": "გაერთიანება ღია ბიბლიოთეკასთან „{library}“ (ფოტოები: {count}){:.0}" + "Merge into the open library “{library}” ({count} photo{})": "გაერთიანება ღია ბიბლიოთეკასთან „{library}“ (ფოტოები: {count}){:.0}", + "Profile: {} (lens database)": "პროფილი: {} (ობიექტივების ბაზა)", + "No lens profile found for {}.": "{}-ისთვის ობიექტივის პროფილი ვერ მოიძებნა.", + "{n} photos got their lens profile": "ობიექტივის პროფილი მიიღო {n} ფოტომ" } diff --git a/crates/ui-egui/locales/ka.json b/crates/ui-egui/locales/ka.json index 32c4077001..646b6eb6cd 100644 --- a/crates/ui-egui/locales/ka.json +++ b/crates/ui-egui/locales/ka.json @@ -2734,5 +2734,11 @@ "Import dropped files without review": "ჩაგდებული ფაილების იმპორტი გადახედვის გარეშე", "An import is running": "იმპორტი მიმდინარეობს", "An import is running. Drop photos when it has finished": "იმპორტი მიმდინარეობს. ფოტოები ჩააგდეთ, როცა დასრულდება", - "Finish Lightroom catalog import before adding photos": "ფოტოების დამატებამდე დაასრულეთ Lightroom-ის კატალოგის იმპორტი" + "Finish Lightroom catalog import before adding photos": "ფოტოების დამატებამდე დაასრულეთ Lightroom-ის კატალოგის იმპორტი", + "Profile: from the file (the camera's own correction)": "პროფილი: ფაილიდან (კამერის საკუთარი კორექცია)", + "Downloading the lens database…": "ობიექტივების ბაზა იტვირთება…", + "This file names no lens.": "ამ ფაილში ობიექტივი არ არის მითითებული.", + "1 photo got its lens profile": "1 ფოტომ მიიღო ობიექტივის პროფილი", + "Looking up lens profiles": "ობიექტივის პროფილების ძიება", + "Downloading the lens database": "ობიექტივების ბაზის ჩამოტვირთვა" } diff --git a/crates/ui-egui/locales/pl-formats.json b/crates/ui-egui/locales/pl-formats.json index 20c7f840d6..d312f8e6ba 100644 --- a/crates/ui-egui/locales/pl-formats.json +++ b/crates/ui-egui/locales/pl-formats.json @@ -305,5 +305,8 @@ "Added {n} photo{}": "Dodano zdjęcia: {n}{:.0}", "Inside: {names}": "Wewnątrz: {names}", "Where should the Lightroom catalog “{catalog}” go?": "Dokąd zaimportować katalog Lightroom „{catalog}”?", - "Merge into the open library “{library}” ({count} photo{})": "Scal z otwartą biblioteką „{library}” (zdjęcia: {count}){:.0}" + "Merge into the open library “{library}” ({count} photo{})": "Scal z otwartą biblioteką „{library}” (zdjęcia: {count}){:.0}", + "Profile: {} (lens database)": "Profil: {} (baza obiektywów)", + "No lens profile found for {}.": "Nie znaleziono profilu obiektywu dla {}.", + "{n} photos got their lens profile": "Profil obiektywu otrzymały zdjęcia: {n}" } diff --git a/crates/ui-egui/locales/pl.json b/crates/ui-egui/locales/pl.json index adac22e62a..dedb38a14a 100644 --- a/crates/ui-egui/locales/pl.json +++ b/crates/ui-egui/locales/pl.json @@ -2283,5 +2283,11 @@ "Import dropped files without review": "Importuj upuszczone pliki bez przeglądu", "An import is running": "Import jest w toku", "An import is running. Drop photos when it has finished": "Import jest w toku. Upuść zdjęcia, gdy się zakończy", - "Finish Lightroom catalog import before adding photos": "Zakończ import katalogu Lightrooma, zanim dodasz zdjęcia" + "Finish Lightroom catalog import before adding photos": "Zakończ import katalogu Lightrooma, zanim dodasz zdjęcia", + "Profile: from the file (the camera's own correction)": "Profil: z pliku (korekcja aparatu)", + "Downloading the lens database…": "Pobieranie bazy obiektywów…", + "This file names no lens.": "Ten plik nie podaje obiektywu.", + "1 photo got its lens profile": "1 zdjęcie otrzymało profil obiektywu", + "Looking up lens profiles": "Wyszukiwanie profili obiektywów", + "Downloading the lens database": "Pobieranie bazy obiektywów" } diff --git a/crates/ui-egui/locales/pt-br-formats.json b/crates/ui-egui/locales/pt-br-formats.json index f17f2c0fd4..9a8f1144ef 100644 --- a/crates/ui-egui/locales/pt-br-formats.json +++ b/crates/ui-egui/locales/pt-br-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "Não foi possível ler {name}: {e}", "Added {n} photo{}": "{n} foto{0} adicionada{0}", "Where should the Lightroom catalog “{catalog}” go?": "Para onde vai o catálogo do Lightroom “{catalog}”?", - "Merge into the open library “{library}” ({count} photo{})": "Mesclar na biblioteca aberta “{library}” ({count} foto{})" + "Merge into the open library “{library}” ({count} photo{})": "Mesclar na biblioteca aberta “{library}” ({count} foto{})", + "Profile: {} (lens database)": "Perfil: {} (banco de dados de lentes)", + "No lens profile found for {}.": "Nenhum perfil de lente encontrado para {}.", + "{n} photos got their lens profile": "{n} fotos receberam o perfil da lente" } diff --git a/crates/ui-egui/locales/pt-br.json b/crates/ui-egui/locales/pt-br.json index e84fb7615b..ea8ecbb986 100644 --- a/crates/ui-egui/locales/pt-br.json +++ b/crates/ui-egui/locales/pt-br.json @@ -2202,5 +2202,11 @@ "Import dropped files without review": "Importar arquivos soltos sem revisão", "An import is running": "Uma importação está em andamento", "An import is running. Drop photos when it has finished": "Uma importação está em andamento. Solte as fotos quando ela terminar", - "Finish Lightroom catalog import before adding photos": "Conclua a importação do catálogo do Lightroom antes de adicionar fotos" + "Finish Lightroom catalog import before adding photos": "Conclua a importação do catálogo do Lightroom antes de adicionar fotos", + "Profile: from the file (the camera's own correction)": "Perfil: do arquivo (correção da própria câmera)", + "Downloading the lens database…": "Baixando o banco de dados de lentes…", + "This file names no lens.": "Este arquivo não indica nenhuma lente.", + "1 photo got its lens profile": "1 foto recebeu o perfil da lente", + "Looking up lens profiles": "Procurando perfis de lente", + "Downloading the lens database": "Baixando o banco de dados de lentes" } diff --git a/crates/ui-egui/locales/ru-formats.json b/crates/ui-egui/locales/ru-formats.json index 76b7af4276..060eabbf8e 100644 --- a/crates/ui-egui/locales/ru-formats.json +++ b/crates/ui-egui/locales/ru-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "Не удалось прочитать {name}: {e}", "Added {n} photo{}": "Добавлено фото: {n}{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "Куда импортировать каталог Lightroom «{catalog}»?", - "Merge into the open library “{library}” ({count} photo{})": "Объединить с открытой библиотекой «{library}» (фото: {count}){:.0}" + "Merge into the open library “{library}” ({count} photo{})": "Объединить с открытой библиотекой «{library}» (фото: {count}){:.0}", + "Profile: {} (lens database)": "Профиль: {} (база объективов)", + "No lens profile found for {}.": "Профиль объектива для {} не найден.", + "{n} photos got their lens profile": "Профиль объектива получили фото: {n}" } diff --git a/crates/ui-egui/locales/ru.json b/crates/ui-egui/locales/ru.json index 306804df04..ec340f98e8 100644 --- a/crates/ui-egui/locales/ru.json +++ b/crates/ui-egui/locales/ru.json @@ -2349,5 +2349,11 @@ "Import dropped files without review": "Импортировать перетащенные файлы без проверки", "An import is running": "Импорт уже выполняется", "An import is running. Drop photos when it has finished": "Импорт уже выполняется. Перетащите фото, когда он закончится", - "Finish Lightroom catalog import before adding photos": "Завершите импорт каталога Lightroom, прежде чем добавлять фото" + "Finish Lightroom catalog import before adding photos": "Завершите импорт каталога Lightroom, прежде чем добавлять фото", + "Profile: from the file (the camera's own correction)": "Профиль: из файла (коррекция самой камеры)", + "Downloading the lens database…": "Загружается база объективов…", + "This file names no lens.": "В этом файле не указан объектив.", + "1 photo got its lens profile": "1 фото получило профиль объектива", + "Looking up lens profiles": "Поиск профилей объективов", + "Downloading the lens database": "Загрузка базы объективов" } diff --git a/crates/ui-egui/locales/uk-formats.json b/crates/ui-egui/locales/uk-formats.json index 36022ac1a2..9e1b31144e 100644 --- a/crates/ui-egui/locales/uk-formats.json +++ b/crates/ui-egui/locales/uk-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "Не вдалося прочитати {name}: {e}", "Added {n} photo{}": "Додано фото: {n}{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "Куди імпортувати каталог Lightroom «{catalog}»?", - "Merge into the open library “{library}” ({count} photo{})": "Об’єднати з відкритою бібліотекою «{library}» (фото: {count}){:.0}" + "Merge into the open library “{library}” ({count} photo{})": "Об’єднати з відкритою бібліотекою «{library}» (фото: {count}){:.0}", + "Profile: {} (lens database)": "Профіль: {} (база об'єктивів)", + "No lens profile found for {}.": "Профіль об'єктива для {} не знайдено.", + "{n} photos got their lens profile": "Профіль об'єктива отримали фото: {n}" } diff --git a/crates/ui-egui/locales/uk.json b/crates/ui-egui/locales/uk.json index ee694ed4fe..a43ffbd1f4 100644 --- a/crates/ui-egui/locales/uk.json +++ b/crates/ui-egui/locales/uk.json @@ -2354,5 +2354,11 @@ "Import dropped files without review": "Імпортувати перетягнуті файли без перегляду", "An import is running": "Імпорт уже виконується", "An import is running. Drop photos when it has finished": "Імпорт уже виконується. Перетягніть фото, коли він завершиться", - "Finish Lightroom catalog import before adding photos": "Завершіть імпорт каталогу Lightroom, перш ніж додавати фото" + "Finish Lightroom catalog import before adding photos": "Завершіть імпорт каталогу Lightroom, перш ніж додавати фото", + "Profile: from the file (the camera's own correction)": "Профіль: з файлу (корекція самої камери)", + "Downloading the lens database…": "Завантажується база об'єктивів…", + "This file names no lens.": "У цьому файлі не вказано об'єктив.", + "1 photo got its lens profile": "1 фото отримало профіль об'єктива", + "Looking up lens profiles": "Пошук профілів об'єктивів", + "Downloading the lens database": "Завантаження бази об'єктивів" } diff --git a/crates/ui-egui/locales/zh-hans-formats.json b/crates/ui-egui/locales/zh-hans-formats.json index 7406c39b86..d5e6145ffa 100644 --- a/crates/ui-egui/locales/zh-hans-formats.json +++ b/crates/ui-egui/locales/zh-hans-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "无法读取 {name}:{e}", "Added {n} photo{}": "已添加 {n} 张照片{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "Lightroom 目录“{catalog}”要导入到哪里?", - "Merge into the open library “{library}” ({count} photo{})": "合并到当前打开的图库“{library}”({count} 张照片){:.0}" + "Merge into the open library “{library}” ({count} photo{})": "合并到当前打开的图库“{library}”({count} 张照片){:.0}", + "Profile: {} (lens database)": "配置文件:{}(镜头数据库)", + "No lens profile found for {}.": "未找到 {} 的镜头配置文件。", + "{n} photos got their lens profile": "{n} 张照片获得了镜头配置文件" } diff --git a/crates/ui-egui/locales/zh-hans.json b/crates/ui-egui/locales/zh-hans.json index 270af8a625..5bd3b0805e 100644 --- a/crates/ui-egui/locales/zh-hans.json +++ b/crates/ui-egui/locales/zh-hans.json @@ -2200,5 +2200,11 @@ "Import dropped files without review": "不经检查直接导入拖入的文件", "An import is running": "正在导入", "An import is running. Drop photos when it has finished": "正在导入。导入完成后再拖放照片", - "Finish Lightroom catalog import before adding photos": "添加照片前,请先完成 Lightroom 目录的导入" + "Finish Lightroom catalog import before adding photos": "添加照片前,请先完成 Lightroom 目录的导入", + "Profile: from the file (the camera's own correction)": "配置文件:来自文件(相机自身的校正)", + "Downloading the lens database…": "正在下载镜头数据库…", + "This file names no lens.": "此文件未注明镜头。", + "1 photo got its lens profile": "1 张照片获得了镜头配置文件", + "Looking up lens profiles": "正在查找镜头配置文件", + "Downloading the lens database": "正在下载镜头数据库" } diff --git a/crates/ui-egui/locales/zh-hant-formats.json b/crates/ui-egui/locales/zh-hant-formats.json index d5c4eb279a..6aff8bca77 100644 --- a/crates/ui-egui/locales/zh-hant-formats.json +++ b/crates/ui-egui/locales/zh-hant-formats.json @@ -305,5 +305,8 @@ "Could not read {name}: {e}": "無法讀取 {name}:{e}", "Added {n} photo{}": "已加入 {n} 張照片{:.0}", "Where should the Lightroom catalog “{catalog}” go?": "Lightroom 目錄「{catalog}」要匯入到哪裡?", - "Merge into the open library “{library}” ({count} photo{})": "合併到目前開啟的照片庫「{library}」({count} 張照片){:.0}" + "Merge into the open library “{library}” ({count} photo{})": "合併到目前開啟的照片庫「{library}」({count} 張照片){:.0}", + "Profile: {} (lens database)": "設定檔:{}(鏡頭資料庫)", + "No lens profile found for {}.": "找不到 {} 的鏡頭設定檔。", + "{n} photos got their lens profile": "{n} 張照片取得了鏡頭設定檔" } diff --git a/crates/ui-egui/locales/zh-hant.json b/crates/ui-egui/locales/zh-hant.json index 0408e23ec8..ddba21e479 100644 --- a/crates/ui-egui/locales/zh-hant.json +++ b/crates/ui-egui/locales/zh-hant.json @@ -2157,5 +2157,11 @@ "Import dropped files without review": "不經檢查直接匯入拖放的檔案", "An import is running": "正在匯入", "An import is running. Drop photos when it has finished": "正在匯入。匯入完成後再拖放照片", - "Finish Lightroom catalog import before adding photos": "新增照片前,請先完成 Lightroom 目錄的匯入" + "Finish Lightroom catalog import before adding photos": "新增照片前,請先完成 Lightroom 目錄的匯入", + "Profile: from the file (the camera's own correction)": "設定檔:來自檔案(相機本身的校正)", + "Downloading the lens database…": "正在下載鏡頭資料庫…", + "This file names no lens.": "此檔案未註明鏡頭。", + "1 photo got its lens profile": "1 張照片取得了鏡頭設定檔", + "Looking up lens profiles": "正在查找鏡頭設定檔", + "Downloading the lens database": "正在下載鏡頭資料庫" } diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs new file mode 100644 index 0000000000..f828dacb69 --- /dev/null +++ b/crates/ui-egui/src/lens_db.rs @@ -0,0 +1,141 @@ +//! The lens database without setup: on first run the app fetches lensfun's database by itself +//! (`lightcraft_engine::lensfun_download`, an activity row while it runs), and whenever the database in use changes +//! (it arrived, a newer pin replaced it) or Enable Lens Corrections is ticked on a photo without a profile, the +//! library's raws that have none are looked up again on a worker thread (`lightcraft_engine::cmd::lens_refresh`). + +use lightcraft_engine::cmd::{lens_refresh, recheck}; +use lightcraft_engine::{lens_profiles, lensfun_download}; + +use crate::LightcraftApp; + +/// The activity stack's label while files are read for their lens. +pub const LABEL: &str = "Looking up lens profiles"; + +/// Called every frame. +pub fn tick(app: &mut LightcraftApp) { + start_download(app); + refresh(app); +} + +/// Once per run, when the host asked for the app's own copy and it is missing (or from an older pin). +fn start_download(app: &mut LightcraftApp) { + if app.ui.lens_download_checked || !app.session.lens_auto_download { + return; + } + app.ui.lens_download_checked = true; + // a folder of the user's own (LIGHTCRAFT_LENSFUN_DIR) is never replaced + if std::env::var_os(lens_profiles::ENV).is_some_and(|v| !v.is_empty()) { + return; + } + let Some(dir) = lensfun_download::default_dir() else { return }; + if lensfun_download::installed(&dir) { + return; + } + let activity = app.session.activity.clone(); + if let Err(e) = app.session.lensfun.start(&dir, lensfun_download::mirrors(), lensfun_download::FILES, &activity) { + log::warn!("lens database: {e}"); + } +} + +/// Look the library's raws up again when the database changed, the library changed, or photos asked for it. +fn refresh(app: &mut LightcraftApp) { + let key = (app.session.library_generation(), lens_profiles::generation()); + let asked = std::mem::take(&mut app.ui.lens_lookup_ids); + if app.ui.lens_refreshed == Some(key) && asked.is_empty() { + return; + } + // (a lookup already under way finishes first; what was asked meanwhile waits for the next frame) + if app.tasks.is_running(LABEL) { + app.ui.lens_lookup_ids.extend(asked); + return; + } + let ids = (app.ui.lens_refreshed == Some(key)).then_some(asked); + app.ui.lens_refreshed = Some(key); + let Some(probe) = recheck::file_probe(&app.session) else { return }; + let Some(db) = lens_profiles::database() else { return }; + let listed = lens_refresh::missing(&app.session.catalog, &db, ids.as_deref()); + if listed.is_empty() { + return; + } + let library = key.0; + let work = move || lens_refresh::probe(&probe, listed); + let done = move |app: &mut LightcraftApp, ctx: &egui::Context, results| { + if app.session.library_generation() != library { + return; + } + match lens_refresh::apply(&mut app.session, results) { + Ok(found) if found.len() == 1 => app.toast(ctx, crate::i18n::tr("1 photo got its lens profile")), + Ok(found) if found.len() > 1 => app.toast(ctx, crate::i18n::tr_format!("{n} photos got their lens profile", n = found.len())), + Ok(_) => {} + Err(e) => log::warn!("lens profile lookup: {e}"), + } + }; + if let Err(e) = crate::tasks::spawn(app, LABEL, Some("lensLookup"), work, done) { + log::warn!("{e}"); + } +} + +/// The database's name for a lens, remembered per lens and database (the panel asks every frame; a search scores +/// every lens in the database). +fn database_name(camera: &str, lens: &str) -> Option { + type Key = (String, String, u64); + thread_local! { + static LAST: std::cell::RefCell)>> = const { std::cell::RefCell::new(None) }; + } + let key = (camera.to_string(), lens.to_string(), lens_profiles::generation()); + LAST.with(|last| { + if let Some((k, name)) = last.borrow().as_ref() + && *k == key + { + return name.clone(); + } + let name = lens_profiles::database().and_then(|db| lens_profiles::profile_name(&db, camera, lens)); + *last.borrow_mut() = Some((key, name.clone())); + name + }) +} + +/// What the Optics panel says about photo `p`'s lens profile: which one is loaded and from where, or why there is +/// none. +pub fn readout(app: &LightcraftApp, p: &lightcraft_catalog::Photo) -> String { + use lightcraft_develop::LensSource; + match p.embedded_lens.map(|l| l.source) { + Some(LensSource::File) => crate::i18n::tr("Profile: from the file (the camera's own correction)").to_string(), + Some(LensSource::Database) => { + let name = database_name(&p.meta.camera, &p.meta.lens).unwrap_or_else(|| p.meta.lens.trim().to_string()); + crate::i18n::tr_format!("Profile: {} (lens database)", name) + } + None if app.session.lensfun.running() => crate::i18n::tr("Downloading the lens database…").to_string(), + None => match lens_refresh::no_profile_reason(p) { + Some(r) if p.meta.lens.trim().is_empty() => crate::i18n::tr(&r).to_string(), + Some(_) => crate::i18n::tr_format!("No lens profile found for {}.", p.meta.lens.trim()), + None => String::new(), + }, + } +} + +#[cfg(test)] +mod tests { + use crate::{LightcraftApp, Services}; + use lightcraft_develop::{EmbeddedLens, LensSource}; + + /// The Optics panel says which profile is loaded and from where, or why there is none. + #[test] + fn the_optics_readout_names_the_profile_or_the_lens_without_one() { + let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() }); + let id = app.session.active().expect("a photo"); + let mut p = (**app.session.catalog.photo(id).expect("the photo")).clone(); + p.meta.lens = "EF35mm f/1.4L II USM".into(); + p.embedded_lens = None; + assert_eq!(super::readout(&app, &p), "No lens profile found for EF35mm f/1.4L II USM."); + p.meta.lens.clear(); + assert_eq!(super::readout(&app, &p), "This file names no lens."); + p.embedded_lens = Some(EmbeddedLens::default()); + assert_eq!(super::readout(&app, &p), "Profile: from the file (the camera's own correction)"); + p.meta.lens = "EF70-200mm f/2.8L IS II USM".into(); + p.embedded_lens = Some(EmbeddedLens { source: LensSource::Database, ..Default::default() }); + // the database's name for it, or the file's own without a database (LIGHTCRAFT_LENSFUN_DIR may be set) + let r = super::readout(&app, &p); + assert!(r.starts_with("Profile: ") && r.contains("70-200mm f/2.8L IS II USM") && r.ends_with(" (lens database)"), "{r}"); + } +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e53989fca9..9f78d63f47 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -23,6 +23,7 @@ pub mod headless; pub mod i18n; pub mod icons; pub mod import; +pub mod lens_db; pub mod lightroom_destination; pub mod lightroom_import; pub mod links; @@ -1970,6 +1971,7 @@ impl LightcraftApp { } lightroom_import::tick(self, ctx); recheck::tick(self); + lens_db::tick(self); tasks::poll(self, ctx); updates_ui::tick(self, ctx); self.preview_build_status(ctx); diff --git a/crates/ui-egui/src/panels/edit.rs b/crates/ui-egui/src/panels/edit.rs index 10542e4432..d82de05dad 100644 --- a/crates/ui-egui/src/panels/edit.rs +++ b/crates/ui-egui/src/panels/edit.rs @@ -374,12 +374,23 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { } let mut lp = d.optics.lens_profile; if ui.checkbox(&mut lp, crate::i18n::tr("Enable Lens Corrections")).changed() { - let _ = app.run("develop.merge", json!({"settings": {"optics": {"lens_profile": lp}}, "label": "Lens Corrections"})); + let ok = app.run("develop.merge", json!({"settings": {"optics": {"lens_profile": lp}}, "label": "Lens Corrections"})).is_ok(); + // ticked on photos without a profile: look their lenses up again (a database that arrived + // since they were imported may know them now) + if ok && lp { + let without: Vec<_> = std::iter::once(id) + .chain(app.session.selection.ids.iter().copied()) + .filter(|t| app.session.catalog.photo(*t).is_some_and(|p| p.embedded_lens.is_none())) + .collect(); + app.ui.lens_lookup_ids.extend(without); + } } - if lp && !has_lens { - ui.label( - egui::RichText::new(crate::i18n::tr("No lens data embedded in this file.")).size(11.0).color(Tokens::get(ui.ctx()).text_dim), - ); + if lp && let Some(p) = app.session.catalog.photo(id).cloned() { + let text = crate::lens_db::readout(app, &p); + if !text.is_empty() { + let r = ui.label(egui::RichText::new(text).size(11.0).color(Tokens::get(ui.ctx()).text_dim)); + crate::widgets::register(ui, "optics:lensProfile", r.rect); + } } }); if d.optics.lens_profile { diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 310cac90ab..9011f85771 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -1099,6 +1099,15 @@ pub struct UiState { /// (`crate::recheck`). #[serde(skip)] pub rechecked_library: Option, + /// The first-run lens database download was considered this run (`crate::lens_db`). + #[serde(skip)] + pub lens_download_checked: bool, + /// The library and lens database generation whose raws were last looked up for a lens profile. + #[serde(skip)] + pub lens_refreshed: Option<(u64, u64)>, + /// Photos to look up for a lens profile on the next frame (Enable Lens Corrections ticked without one). + #[serde(skip)] + pub lens_lookup_ids: Vec, #[serde(skip)] pub status: String, #[serde(skip)] @@ -1618,6 +1627,9 @@ impl Default for UiState { #[cfg(test)] find_missing_test_delay: None, rechecked_library: None, + lens_download_checked: false, + lens_refreshed: None, + lens_lookup_ids: Vec::new(), status: String::new(), dialog: None, print: Default::default(), diff --git a/docs/gaps.md b/docs/gaps.md index 42d9d437b6..c2f0e4c18d 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps: where LightCraft falls short of Lightroom -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-24 · **Change:** minor (gap 30: the Info panel shows the selected photos' shared metadata and marks mixed fields; gap 28: ⌘⌥⇧C / ⌘⌥⇧V copy and paste metadata; gap 13: before a Lightroom catalog import the app asks merge or new library, #2672; gap 28: F8 shows / hides the right panel; import review album hierarchy, #2362; gap 20: Minolta MRW decodes; gap 10: 551 models verified; gap 15: Print Page Options — page numbers, page info, crop marks; gap 28: `\` shows / hides the Filter Bar in the grids; gap 21: Indonesian interface shipped, every message but What's New; gap 27: the Windows x64 installer verified on Windows 11; the Settings → Apps uninstall confirmation remains; gap 28: ⌘L Enable Filters switches the filter off and on; gap 15: Japanese photo and postcard paper sizes in the Print page setup and PDF) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-27 · **Change:** minor (gap 8: lensfun's lens database downloaded on first run, looked up for the library; gap 30: the Info panel shows the selected photos' shared metadata and marks mixed fields; gap 28: ⌘⌥⇧C / ⌘⌥⇧V copy and paste metadata; gap 13: before a Lightroom catalog import the app asks merge or new library, #2672; gap 28: F8 shows / hides the right panel; import review album hierarchy, #2362; gap 20: Minolta MRW decodes; gap 10: 551 models verified; gap 15: Print Page Options — page numbers, page info, crop marks; gap 28: `\` shows / hides the Filter Bar in the grids; gap 21: Indonesian interface shipped, every message but What's New; gap 27: the Windows x64 installer verified on Windows 11; the Settings → Apps uninstall confirmation remains; gap 28: ⌘L Enable Filters switches the filter off and on; gap 15: Japanese photo and postcard paper sizes in the Print page setup and PDF) Every known shortfall, one entry each, ranked by user impact (how many Lightroom users hit it, and how hard it blocks them), then by effort. **Agents pick work from the top** (the first entry nobody is @@ -22,7 +22,7 @@ Hours are Opus 5.5 agent wall-clock hours (see [target-app-parity.md](target-app | 5 B | **AI masks: Subject, Sky, People, Background, Landscape** are heuristics; Object / Describe run SAM 3 but its download has no mirror configured | `crates/segment`; ai-masks.md; MASK rows 67% | masking is how most Lightroom users edit today | 40–70 + model decision | [target-app-parity](target-app-parity.md#ai) | | 6 B | **Compressed ORF** (every Olympus / OM body since ~2008) | measured: 66 of 78 Olympus / OM models preview-only; `orf.rs` refuses; OM bucket 3% of photographers, 3% of them fully work | +2.6 points; best gain per hour of any raw gap (~0.2) | 8–15 | [raw-parity](raw-parity.md#compressed-orf-a-clean-room-starting-point) (the two Olympus patents that describe the method) | | 7 | **Nikon High Efficiency NEF** (HE / HE★ on Z 8, Z 9, Z 6III, Z f, Z50II, Z5II when set to HE) | named and preview only (#193); needs a pure-Rust JPEG XS decoder | +1.1 points (only shots taken in HE; lossless works on the same bodies); low gain per hour (~0.04), so not a beta blocker | 20–40 | [raw-parity](raw-parity.md) | -| 8 B | **No lens profile database** (LR-EDIT-OPTICS-PROFILE): only DNG opcodes, RW2 in-file distortion, Sony ILCE-7RM4A, full-frame ILCE-7CR distortion (Auto only) and ILCE-7RM6 distortion (`docs/sony-ilce7rm6-lens-corrections.md`) and Nikon Z 6II / Z 8 / Z f distortion (`docs/nikon-lens-corrections.md`) | Adobe ships 3,635 lens profiles; we never use them | distortion / vignetting correction is a default step for many | 40–80 + calibration shots | [raw-parity](raw-parity.md#lens-profiles) | +| 8 B | **Lens profiles: lensfun only, no picker, no own calibration** (LR-EDIT-OPTICS-PROFILE): the desktop app downloads lensfun's database (CC BY-SA 3.0, pinned, never bundled) by itself on first run and looks every raw up by its lens ([lens-profiles.md](lens-profiles.md)), but those profiles are not validated against Lightroom, there is no profile picker (in the file / lens database / own calibration) and no calibration wizard; in-file corrections: DNG opcodes, RW2 in-file distortion, Sony ILCE-7RM4A, full-frame ILCE-7CR distortion (Auto only) and ILCE-7RM6 distortion (`docs/sony-ilce7rm6-lens-corrections.md`) and Nikon Z 6II / Z 8 / Z f distortion (`docs/nikon-lens-corrections.md`) | Adobe ships 3,635 lens profiles; we never use them | distortion / vignetting correction is a default step for many | 40–80 + calibration shots | [raw-parity](raw-parity.md#lens-profiles) | | 9 B | **Tests don't run in CI on macOS or Windows**: Linux x64 runs `cargo xtask ci` on every PR and push to main (`.github/workflows/ci.yml`) and FreeBSD runs `cargo test`; the others build packages | `.github/workflows/` (ci, freebsd, packaging-lint, release, windows-arm64); the linux-riscv64 release job added in 0.6.0 (#844) runs only `lightcraft-cli --version` under QEMU; macOS / Windows jobs: #1148 | regressions in `cfg(target_os)` code reach macOS and Windows users | 6–12 | [target-app-parity](target-app-parity.md#by-dimension) | | 10 B | **Per-model verification thin, plus cheap per-body decode fixes**: 551 of 1,446 Adobe models verified (38%), ~85% of photographers on a verified body; Nikon 5 bodies verified, Canon 34, Leica 14 (the Q3 by a recorded check of 38 private DNGs). Cheap fixes: Z 6II packed 14-bit NEF (#769, PR #671), Sony A100 regression (#702); F828 SRF now discovered and imported as preview-only, sensor decoding still missing, DSLR-A200…A390 / A850 12-bit ARW (#535) | measured: [raw/coverage.tsv](raw/coverage.tsv) | per-body bugs (levels, crops, WB) surface only from users | 25–45 | [raw-parity](raw-parity.md#by-maker-measured) | | 11 | **Enhance: Raw Details, Super Resolution; AI denoise without shippable weights or X-Trans** | ENH rows 0%; denoise Bayer only, RawNIND weights GPL and opt-in | Fujifilm users get no AI denoise; nobody gets it out of the box | 30–60 + model decision | [raw-parity](raw-parity.md#demosaic-levels-crops-and-lens-metadata) | @@ -84,6 +84,7 @@ Beta-blocking gaps (B) total **~230–430 h**; with the stability and platform w | Date | Change | Summary | |---|---|---| +| 2026-10-27 | minor | Gap 8 shrinks: the desktop app downloads lensfun's lens database (CC BY-SA, pinned) by itself on first run and looks the library's raws up when it arrives; validation, a profile picker and own calibration remain | | 2026-10-24 | minor | Gap 28: ⌘⌥⇧C / ⌘⌥⇧V (Ctrl+Alt+Shift+C / V on Windows and Linux) copy and paste metadata; no key leaves the entry; hours unchanged | | 2026-10-24 | minor | Gap 30 shrinks: the Info panel's Show Metadata For (Target Photo / Selected Photos, `view.infoSelectedPhotos`) shows the selected photos' shared values and marks fields that differ as Mixed; the All Metadata dialog remains; 1–3 h (was 4–8) | | 2026-10-23 | minor | Gap 13: before a Lightroom catalog import the app asks whether it merges into the open library or goes into a new one (opt-out in Settings, #2672); hours unchanged | diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index 13d1b2b7a7..80739f47f1 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -1,18 +1,33 @@ # Lens profiles from a lens database Raws that carry no lens correction of their own (DNG `OpcodeList3`, or a camera correction a raw reader converts, -see [sony-lens-corrections.md](sony-lens-corrections.md) and [nikon-lens-corrections.md](nikon-lens-corrections.md)) can be corrected from a **lens database in the lensfun -XML format** that you supply. The app does not ship any lens database: lensfun's data is licensed CC-BY-SA 3.0 and -stays a separate download. Adobe LCP profiles are never used. +see [sony-lens-corrections.md](sony-lens-corrections.md) and [nikon-lens-corrections.md](nikon-lens-corrections.md)) are corrected from a **lens database in the lensfun +XML format**. The app does not ship any lens database: lensfun's data is licensed CC BY-SA 3.0 and is downloaded at +run time, never bundled and never committed. Adobe LCP profiles are never used. -## Setup +## Setup: none -1. Get the lensfun database (the `data/db/*.xml` files of , or the copy a Linux - distribution installs under `/usr/share/lensfun/version_1/`). -2. Point `LIGHTCRAFT_LENSFUN_DIR` at that directory and start the app (or the CLI). +1. On first run the desktop app downloads lensfun's `data/db` (56 XML files, about 5 MB) into its data folder + (`/lensfun`, beside the face and denoise models; see `crates/engine/src/config.rs`), with a row in + the activity stack (its ✕ stops it; the next start resumes). The files are pinned to one lensfun commit by name, + size and SHA-256 (`crates/engine/src/lensfun_download.rs`); a file that differs is thrown away. A later build with + a newer pin replaces them once. `SOURCE.txt` in the folder names the commit and the licence. +2. By default the files come from lensfun's GitHub repository at the pinned commit. `LIGHTCRAFT_LENSFUN_URL` lists + other places serving the same files (base URLs, tried first), e.g. a copy on LightCraft's own server. +3. `LIGHTCRAFT_LENSFUN_DIR` points at a database of your own instead (lensfun's `data/db`, or a Linux + distribution's `/usr/share/lensfun/version_1/`); then nothing is downloaded. -Files are read once per process. Invalid files are skipped with a warning in the log. Without the variable nothing -changes. +When the database arrives (or changes), every library raw without a profile whose lens it knows is read again in the +background and gets its profile; a photo still as imported gets the correction switched on, as import would, and an +edited one keeps its settings. Ticking **Enable Lens Corrections** on a photo without a profile looks it up again +too. `library.refreshLensProfiles`, `lens.downloadDatabase` and `lens.databaseStatus` do the same for agents. + +The Optics panel says which profile is loaded: "Profile: from the file", "Profile: (lens database)", or +"No lens profile found for ". A profile is never copied between photos: presets, Copy / Paste, Sync and Auto +Sync carry Enable Lens Corrections and its amounts, and each photo keeps the profile of its own lens +(`crates/engine/src/tests_lens_refresh.rs`). + +Files are read once per folder and again after a download. Invalid files are skipped with a warning in the log. ## What is applied @@ -50,5 +65,6 @@ the image centre (lensfun's `
` offsets are not read). - Only raws: rendered images (JPEG, HEIF, TIFF) are often already corrected in camera. - The focus distance is not read from maker notes yet: vignetting uses the farthest calibrated distance. -- No manual make / model picker yet; matching is automatic. +- No manual make / model picker and no choice between sources (in the file, lens database, an own calibration) + yet; matching is automatic, the file's own correction wins. - Not yet validated against real photos and another renderer; the tests use synthetic profiles and grids. diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index afbed8675e..8bc456ad4a 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,6 +1,6 @@ # Lightroom parity checklist -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** minor (LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (LR-EDIT-OPTICS-PROFILE: lensfun downloaded by itself on first run, profiles looked up for the library, the Optics panel names the profile; profiles never travel between lenses; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 The measured, row-by-row checklist: one row per Lightroom feature, menu item and shortcut, what we have and what is missing. It answers *does the feature exist?* (feature breadth). Whether it is good @@ -212,7 +212,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts). | LR-EDIT-DETAIL-SUPERRES | Super resolution | P2 | ⬜ | | | | LR-EDIT-DETAIL-AISHARPEN | AI sharpen | OOS | 🚫 | | | | LR-EDIT-OPTICS-CA | Remove chromatic aberration | P1 | ✅ | `crates/ui-egui/src/panels/edit.rs` (checkbox), `ctl:optics.caRed`, `ctl:optics.caBlue` | | -| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; no settings switch or bundled database yet | +| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; lens database: the desktop app downloads lensfun's `data/db` (CC BY-SA 3.0, pinned by commit, size and SHA-256, never bundled; `cmd:lens.downloadDatabase`, `crates/engine/src/lensfun_download.rs`) on first run with an activity row, and when it arrives every library raw whose lens it knows is looked up again in the background (`cmd:library.refreshLensProfiles`, `crates/engine/src/cmd/lens_refresh.rs`), as is a photo whose Enable Lens Corrections is ticked; the Optics panel names the loaded profile and its source, or says "No lens profile found for " (`crates/ui-egui/src/lens_db.rs`); presets, Copy / Paste and Sync carry only the switch and amounts, never a profile (`crates/engine/src/tests_lens_refresh.rs`); no source picker or own calibration yet | ) | LR-EDIT-OPTICS-DEFRINGE | Defringe | P1 | ✅ | `ctl:optics.defringe*` | no fringe eyedropper | | LR-EDIT-OPTICS-MANUAL | Manual distortion / vignetting | P1 | ✅ | `ctl:optics.distortion`, `ctl:optics.vignetting`, `ctl:optics.vignettingMidpoint` | | @@ -799,6 +799,7 @@ Latin stays upright. Advanced Japanese composition remains open. Regression cove | Date | Change | Summary | |---|---|---| +| 2026-10-27 | minor | LR-EDIT-OPTICS-PROFILE: lensfun's database downloaded by itself on first run (pinned, CC BY-SA, never bundled), the library looked up when it arrives or Enable Lens Corrections is ticked, the Optics panel names the profile or the lens without one; status unchanged (🟡) | | 2026-10-25 | patch | LR-BEHAV-TITLEBAR: the top bar's title-bar gestures come from craft-window's `title_bar`: a double-click follows the system setting, and a press on a button, the search field or a panel floating over the bar never moves the window; status counts unchanged | | 2026-10-24 | minor | KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 (`keyword.nextSet`, `keyword.previousSet`) go to the next / previous keyword set; status counts unchanged (KEYC-METADATA stays 🟡) | | 2026-10-24 | minor | KEYC-METADATA: ⌘K / Ctrl+K (`panel.addKeywords`, Add Keywords) shows the Keywords panel, opens Keywording and puts the cursor in the Add keyword box; status counts unchanged (KEYC-METADATA stays 🟡) | From 030e0edd0a361142793e31d65bab6ad7bfa45503 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Tue, 27 Oct 2026 02:05:10 +1000 Subject: [PATCH 10/51] LR-EDIT-OPTICS-PROFILE: catalog format 10 for the lens source, snapshots stay offline - EmbeddedLens.source is a serialized field an older build would drop: catalog format 9 -> 10 (journal table row, NEWEST_FIELDS in the version guard). - lightcraft-cli snapshot reads a downloaded lens database but never starts the download (scripts and tests stay offline). - The staging folder name and file cap are native-only (unused in the web build). Co-Authored-By: Claude Opus 5.5 --- apps/lightcraft-cli/src/main.rs | 30 +++++++++++++++++----- crates/catalog/src/journal.rs | 3 ++- crates/catalog/src/tests_format_version.rs | 4 +-- crates/engine/src/lensfun_download.rs | 2 ++ 4 files changed, 30 insertions(+), 9 deletions(-) diff --git a/apps/lightcraft-cli/src/main.rs b/apps/lightcraft-cli/src/main.rs index e8bad66a4b..e5980d469e 100644 --- a/apps/lightcraft-cli/src/main.rs +++ b/apps/lightcraft-cli/src/main.rs @@ -798,16 +798,34 @@ fn snapshot(args: &[String]) -> Result<(), String> { let t0 = Instant::now(); let mut session = match &library { Some(dir) => { - let mut s = - Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_lens_database().with_default_slide_templates(); + let mut s = Session::new() + .with_fs() + .with_default_denoise_models() + .with_system_clock() + .with_default_face_models() + .with_default_lens_database() + .with_default_slide_templates(); s.open_library(dir, false).map_err(|e| library_error(dir, e))?; s } - None if files.is_empty() => { - Session::with_demo().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_lens_database().with_default_slide_templates() - } - None => Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_lens_database().with_default_slide_templates(), + None if files.is_empty() => Session::with_demo() + .with_fs() + .with_default_denoise_models() + .with_system_clock() + .with_default_face_models() + .with_default_lens_database() + .with_default_slide_templates(), + None => Session::new() + .with_fs() + .with_default_denoise_models() + .with_system_clock() + .with_default_face_models() + .with_default_lens_database() + .with_default_slide_templates(), }; + // a snapshot reads the lens database the desktop app downloaded, but never fetches it (scripts and tests + // stay offline) + session.lens_auto_download = false; // Apply compute policy before imports or the first scripted query can discover an adapter. session.execute("app.gpu", &json!({"enabled": false})).map_err(|e| e.to_string())?; if !files.is_empty() { diff --git a/crates/catalog/src/journal.rs b/crates/catalog/src/journal.rs index 2600d794a4..d821954d0f 100644 --- a/crates/catalog/src/journal.rs +++ b/crates/catalog/src/journal.rs @@ -52,6 +52,7 @@ //! | 7 | Kelvin white balance for raws whose camera has matrices (#730) | `Op::SetAsShotWb`, `WhiteBalance.scale` | //! | 8 | History cursor: Go to History Step adds no step | `Op::SetHistoryCursor`, `Photo.history_cursor` | //! | 9 | preview-only recheck | `Photo.preview_checked`, `Op::SetContent.preview_checked` | +//! | 10 | lens database profiles named in the Optics panel | `EmbeddedLens.source` | //! //! Rules: //! - **Bump [`VERSION`]** (and add a row above) in the change that adds an [`Op`] variant or a @@ -76,7 +77,7 @@ pub const LOG: &str = "catalog.log"; const FORMAT: &str = "lightcraft-catalog"; /// The catalog format this build writes (and the newest it reads). See the module docs → /// *Format versions*; bump it whenever an [`Op`] variant or a serialized field is added. -pub const VERSION: u32 = 9; +pub const VERSION: u32 = 10; /// When [`Journal::wants_snapshot`] says it's time to compact the log. #[derive(Clone, Copy, Debug)] diff --git a/crates/catalog/src/tests_format_version.rs b/crates/catalog/src/tests_format_version.rs index e31d7fe0c4..8dbe60b70f 100644 --- a/crates/catalog/src/tests_format_version.rs +++ b/crates/catalog/src/tests_format_version.rs @@ -238,8 +238,8 @@ fn op_variants_are_versioned() { Op::SetHistoryCursor { .. } => 8, } } - /// v9: `Photo.preview_checked` and `Op::SetContent.preview_checked`. - const NEWEST_FIELDS: u32 = 9; + /// v9: `Photo.preview_checked` and `Op::SetContent.preview_checked`; v10: `EmbeddedLens.source`. + const NEWEST_FIELDS: u32 = 10; let newest = since(&Op::SetHistoryCursor { id: PhotoId(1), cursor: None }); assert_eq!(newest.max(NEWEST_FIELDS), VERSION, "the newest op's or fields' version must be the current format version"); } diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index 00e03bbdf5..55452ba9a0 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -26,8 +26,10 @@ pub const URL_ENV: &str = "LIGHTCRAFT_LENSFUN_URL"; /// The file written once every database file is in place: the pin it holds says which data the folder has. pub const MARKER: &str = "SOURCE.txt"; /// The folder inside the database folder where files wait until all have arrived. +#[cfg(not(target_arch = "wasm32"))] const STAGING: &str = ".download"; /// Largest database file accepted (the biggest is under 1 MB). +#[cfg(not(target_arch = "wasm32"))] const MAX_FILE: u64 = 8 << 20; /// The database files at [`COMMIT`]: name, size in bytes, SHA-256. From 5554d7811a2fadf94bcda1079089a9d4fbcab806 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Tue, 27 Oct 2026 02:49:19 +1000 Subject: [PATCH 11/51] LR-EDIT-OPTICS-PROFILE: review fixes (desktop library session, stale results, cache race, old files, failed download) - The desktop app's opened library gets the lens database too (it was set only on the session used when no library could be opened); not for --memory. - A lookup result whose photo moved meanwhile is dropped for its virtual copies as well; the list keeps one entry per photo (HashSet). - lens_profiles: reload() bumps the generation under the cache lock and a load that raced a reload isn't cached. - Installing a new pin removes XML files the pin doesn't list (nothing else in the folder). - The Optics panel says when the first-run download failed and no database is in use (13 locales). - Tests: stale result with a virtual copy (fails before the fix), old XML removed / other files kept, the failed-download readout (offline). Co-Authored-By: Claude Opus 5.5 --- apps/lightcraft/src/main.rs | 6 ++++- crates/engine/src/cmd/lens_refresh.rs | 22 ++++++++----------- crates/engine/src/lens_profiles.rs | 11 ++++++++-- crates/engine/src/lensfun_download.rs | 21 ++++++++++++++++++ crates/engine/src/tests_lens_refresh.rs | 9 ++++++++ crates/ui-egui/locales/cs.json | 3 ++- crates/ui-egui/locales/de.json | 3 ++- crates/ui-egui/locales/es.json | 3 ++- crates/ui-egui/locales/fr.json | 3 ++- crates/ui-egui/locales/it.json | 3 ++- crates/ui-egui/locales/ja.json | 3 ++- crates/ui-egui/locales/ka.json | 3 ++- crates/ui-egui/locales/pl.json | 3 ++- crates/ui-egui/locales/pt-br.json | 3 ++- crates/ui-egui/locales/ru.json | 3 ++- crates/ui-egui/locales/uk.json | 3 ++- crates/ui-egui/locales/zh-hans.json | 3 ++- crates/ui-egui/locales/zh-hant.json | 3 ++- crates/ui-egui/src/lens_db.rs | 29 +++++++++++++++++++++++++ 19 files changed, 108 insertions(+), 29 deletions(-) diff --git a/apps/lightcraft/src/main.rs b/apps/lightcraft/src/main.rs index 982c89a1f2..4fa9a5289e 100644 --- a/apps/lightcraft/src/main.rs +++ b/apps/lightcraft/src/main.rs @@ -761,6 +761,10 @@ fn open_session(in_memory: bool, launch: &Launch, seed_demo: bool) -> (Session, Launch::Ask => (Session::new().with_fs().with_default_denoise_models().with_system_clock(), None), Launch::Open { dir, .. } => open_library_session(in_memory, dir.clone(), seed_demo), }; + // lens profiles from the app's own lensfun copy, fetched by itself on first run (not for a throwaway session) + if !in_memory { + s = s.with_default_lens_database(); + } s.face_models_dir = lightcraft_engine::config::default_face_models_dir(); s.slide_templates_dir = lightcraft_engine::config::config_dir().map(|d| d.join(lightcraft_engine::slide_template::DIR_NAME)); // Control requests are answered on the UI thread: Add to Apple Photos must not wait there @@ -777,7 +781,7 @@ fn open_library_session(in_memory: bool, dir: Option, seed_d if in_memory { return (if seed_demo { Session::with_demo() } else { Session::new() }.with_fs().with_default_denoise_models().with_system_clock(), None); } - let unopened = || Session::new().with_fs().with_default_denoise_models().with_default_lens_database().with_system_clock(); + let unopened = || Session::new().with_fs().with_default_denoise_models().with_system_clock(); let Some(dir) = dir else { log::warn!("no library location (set --library or LIGHTCRAFT_LIBRARY)"); return (unopened(), Some(LibraryProblem::new("", "There is no home folder to keep the library in. Choose a folder for it."))); diff --git a/crates/engine/src/cmd/lens_refresh.rs b/crates/engine/src/cmd/lens_refresh.rs index 720edad8a2..ae8e52e1cd 100644 --- a/crates/engine/src/cmd/lens_refresh.rs +++ b/crates/engine/src/cmd/lens_refresh.rs @@ -27,18 +27,16 @@ pub struct Missing { /// Library raws with a file, no lens profile and a lens the database `db` knows by name (`ids`: only these; virtual /// copies follow the photo they were copied from). No file-system calls. pub fn missing(cat: &Catalog, db: &lightcraft_lensdb::LensDb, ids: Option<&[PhotoId]>) -> Vec { + // a photo and its virtual copies list the photo once, in library order + let mut seen = std::collections::HashSet::new(); cat.photos() .filter(|p| ids.is_none_or(|ids| ids.contains(&p.id) || p.copy_of.is_some_and(|c| ids.contains(&c)))) .map(|p| p.copy_of.and_then(|c| cat.photo(c)).map_or(&**p, |orig| &**orig)) .filter(|p| p.develops_raw() && p.embedded_lens.is_none() && !p.meta.lens.trim().is_empty()) .filter(|p| crate::lens_profiles::profile_name(db, &p.meta.camera, &p.meta.lens).is_some()) .filter_map(|p| crate::cmd::missing::checked_path(p).map(|path| Missing { id: p.id, path: path.to_string() })) - .fold(Vec::new(), |mut v, m| { - if !v.contains(&m) { - v.push(m); - } - v - }) + .filter(|m| seen.insert(m.id)) + .collect() } /// Probe each listed photo's file as Reload would. Doesn't touch the session. @@ -57,13 +55,11 @@ pub fn apply(s: &mut Session, results: Vec<(Missing, std::result::Result = s - .catalog - .photos() - .filter(|p| p.id == item.id || p.copy_of == Some(item.id)) - .filter(|p| p.id != item.id || crate::cmd::missing::checked_path(p) == Some(item.path.as_str())) - .map(|p| p.id) - .collect(); + // the photo moved or left the library while its file was read: the result is stale, for its copies too + if s.catalog.photo(item.id).and_then(|p| crate::cmd::missing::checked_path(p)) != Some(item.path.as_str()) { + continue; + } + let family: Vec = s.catalog.photos().filter(|p| p.id == item.id || p.copy_of == Some(item.id)).map(|p| p.id).collect(); for id in family { if let Some(p) = s.catalog.photo(id) { let mine = crate::cmd::convert::lens_ops(id, p, &info); diff --git a/crates/engine/src/lens_profiles.rs b/crates/engine/src/lens_profiles.rs index 43aec88915..96dd7eb0e7 100644 --- a/crates/engine/src/lens_profiles.rs +++ b/crates/engine/src/lens_profiles.rs @@ -34,7 +34,9 @@ pub fn use_dir(dir: Option) { /// Read the database again (after a download or an update): the next lookup sees the new data. pub fn reload() { - *LOADED.write().unwrap_or_else(PoisonError::into_inner) = None; + // under the cache lock, so a load of the old files that finishes meanwhile can't be published ([`database`]) + let mut loaded = LOADED.write().unwrap_or_else(PoisonError::into_inner); + *loaded = None; GENERATION.fetch_add(1, Ordering::Relaxed); } @@ -57,8 +59,13 @@ pub fn database() -> Option> { { return db.clone(); } + let generation = generation(); let db = load(&dir); - *LOADED.write().unwrap_or_else(PoisonError::into_inner) = Some((dir, db.clone())); + let mut loaded = LOADED.write().unwrap_or_else(PoisonError::into_inner); + // a reload while the files were read: they may be the old ones, so they aren't kept (the next call reads again) + if GENERATION.load(Ordering::Relaxed) == generation { + *loaded = Some((dir, db.clone())); + } db } diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index 55452ba9a0..9e34c76b73 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -254,6 +254,22 @@ fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> for (name, _, _) in files { std::fs::rename(staging.join(name), dir.join(name)).map_err(|e| format!("could not move {name} into {}: {e}", dir.display()))?; } + // database files an older pin had and this one doesn't would still be read: remove them (only XML files; nothing + // else in the folder is touched) + if let Ok(entries) = std::fs::read_dir(dir) { + for entry in entries.flatten() { + let name = entry.file_name(); + let name = name.to_string_lossy(); + let xml = std::path::Path::new(&*name).extension().is_some_and(|e| e.eq_ignore_ascii_case("xml")); + if xml + && entry.path().is_file() + && !files.iter().any(|(pinned, _, _)| *pinned == name) + && let Err(e) = std::fs::remove_file(entry.path()) + { + log::warn!("lens database: could not remove the old {name}: {e}"); + } + } + } let _ = std::fs::remove_dir_all(&staging); let readme = format!( "lensfun lens database (data/db), commit {COMMIT}\nfrom https://github.com/lensfun/lensfun, downloaded by LightCraft.\n\ @@ -343,6 +359,10 @@ mod tests { let activity = Activity::default(); let mut d = Download::default(); assert!(!installed(&dir)); + // left by an older pin: an XML file this one doesn't list goes, anything else stays + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("dropped.xml"), CAMERA).unwrap(); + std::fs::write(dir.join("notes.txt"), "mine").unwrap(); d.start(&dir, vec![base], pins(), &activity).unwrap(); let rows = activity.list(); assert!(rows.iter().any(|r| r.kind == "download" && r.label == "Downloading the lens database" && r.cancellable), "{rows:?}"); @@ -352,6 +372,7 @@ mod tests { assert!(!dir.join(STAGING).exists(), "the staging folder is gone"); let (db, errors) = lightcraft_lensdb::LensDb::load_dir(&dir).unwrap(); assert!(errors.is_empty() && db.lenses.len() == 1 && db.cameras.len() == 1); + assert!(!dir.join("dropped.xml").exists() && dir.join("notes.txt").exists(), "only stale database files are removed"); wait("the row stayed", || activity.list().is_empty()); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/engine/src/tests_lens_refresh.rs b/crates/engine/src/tests_lens_refresh.rs index b502f58157..1d383c6781 100644 --- a/crates/engine/src/tests_lens_refresh.rs +++ b/crates/engine/src/tests_lens_refresh.rs @@ -114,6 +114,15 @@ fn photos_get_their_profile_when_the_database_arrives() { assert_eq!(crate::cmd::lens_refresh::no_profile_reason(&p(3)).as_deref(), Some("No lens profile found for 300mm F4.")); assert_eq!(crate::cmd::lens_refresh::no_profile_reason(&p(4)).as_deref(), Some("This file names no lens.")); assert_eq!(crate::cmd::lens_refresh::no_profile_reason(&p(1)), None); + // a result read from a file the photo no longer has (moved meanwhile) is dropped, for its virtual copies too + let mut copy = raw(5, "300mm F4", false); + copy.copy_of = Some(PhotoId(3)); + s.catalog.apply(Op::AddPhoto { photo: Box::new(copy) }).unwrap(); + let stale = crate::cmd::lens_refresh::Missing { id: PhotoId(3), path: "/elsewhere/3.cr3".into() }; + let ok = crate::media::ProbeInfo { embedded_lens: Some(lens(-0.01)), ..Default::default() }; + assert!(crate::cmd::lens_refresh::apply(&mut s, vec![(stale, Ok(ok))]).unwrap().is_empty()); + assert!(s.catalog.photo(PhotoId(3)).unwrap().embedded_lens.is_none(), "a stale probe changes nothing"); + assert!(s.catalog.photo(PhotoId(5)).unwrap().embedded_lens.is_none(), "nor the copies"); // without file access the command says so instead of clearing anything assert!(s.execute("library.refreshLensProfiles", &json!({})).is_err()); assert!(s.execute("library.refreshLensProfiles", &json!({"ids": "x"})).is_err()); diff --git a/crates/ui-egui/locales/cs.json b/crates/ui-egui/locales/cs.json index 58362cd1ca..561d832ddd 100644 --- a/crates/ui-egui/locales/cs.json +++ b/crates/ui-egui/locales/cs.json @@ -2414,5 +2414,6 @@ "This file names no lens.": "Tento soubor neuvádí žádný objektiv.", "1 photo got its lens profile": "1 fotka získala profil objektivu", "Looking up lens profiles": "Hledání profilů objektivů", - "Downloading the lens database": "Stahování databáze objektivů" + "Downloading the lens database": "Stahování databáze objektivů", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Databázi objektivů se nepodařilo stáhnout. LightCraft to zkusí znovu při příštím spuštění." } diff --git a/crates/ui-egui/locales/de.json b/crates/ui-egui/locales/de.json index a02b0ad59d..300d73d1da 100644 --- a/crates/ui-egui/locales/de.json +++ b/crates/ui-egui/locales/de.json @@ -3446,5 +3446,6 @@ "This file names no lens.": "Diese Datei nennt kein Objektiv.", "1 photo got its lens profile": "1 Foto hat sein Objektivprofil erhalten", "Looking up lens profiles": "Objektivprofile werden gesucht", - "Downloading the lens database": "Objektivdatenbank wird geladen" + "Downloading the lens database": "Objektivdatenbank wird geladen", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Die Objektivdatenbank konnte nicht geladen werden. LightCraft versucht es beim nächsten Start erneut." } diff --git a/crates/ui-egui/locales/es.json b/crates/ui-egui/locales/es.json index 5152a8f518..998d7dfe3d 100644 --- a/crates/ui-egui/locales/es.json +++ b/crates/ui-egui/locales/es.json @@ -2364,5 +2364,6 @@ "This file names no lens.": "Este archivo no indica ningún objetivo.", "1 photo got its lens profile": "1 foto recibió su perfil de objetivo", "Looking up lens profiles": "Buscando perfiles de objetivo", - "Downloading the lens database": "Descargando la base de datos de objetivos" + "Downloading the lens database": "Descargando la base de datos de objetivos", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "No se pudo descargar la base de datos de objetivos. LightCraft lo intentará de nuevo la próxima vez que se inicie." } diff --git a/crates/ui-egui/locales/fr.json b/crates/ui-egui/locales/fr.json index 1d1a999a88..09876de29d 100644 --- a/crates/ui-egui/locales/fr.json +++ b/crates/ui-egui/locales/fr.json @@ -2396,5 +2396,6 @@ "This file names no lens.": "Ce fichier n'indique aucun objectif.", "1 photo got its lens profile": "1 photo a reçu son profil d'objectif", "Looking up lens profiles": "Recherche des profils d'objectif", - "Downloading the lens database": "Téléchargement de la base de données d'objectifs" + "Downloading the lens database": "Téléchargement de la base de données d'objectifs", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "La base de données d’objectifs n’a pas pu être téléchargée. LightCraft réessaiera au prochain démarrage." } diff --git a/crates/ui-egui/locales/it.json b/crates/ui-egui/locales/it.json index 14419b86b8..91a7b4645f 100644 --- a/crates/ui-egui/locales/it.json +++ b/crates/ui-egui/locales/it.json @@ -2367,5 +2367,6 @@ "This file names no lens.": "Questo file non indica alcun obiettivo.", "1 photo got its lens profile": "1 foto ha ricevuto il profilo dell'obiettivo", "Looking up lens profiles": "Ricerca dei profili degli obiettivi", - "Downloading the lens database": "Download del database degli obiettivi" + "Downloading the lens database": "Download del database degli obiettivi", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Impossibile scaricare il database degli obiettivi. LightCraft riproverà al prossimo avvio." } diff --git a/crates/ui-egui/locales/ja.json b/crates/ui-egui/locales/ja.json index d7670e1c45..900986536e 100644 --- a/crates/ui-egui/locales/ja.json +++ b/crates/ui-egui/locales/ja.json @@ -2362,5 +2362,6 @@ "This file names no lens.": "このファイルにはレンズ名がありません。", "1 photo got its lens profile": "1 枚の写真にレンズプロファイルが適用されました", "Looking up lens profiles": "レンズプロファイルを検索中", - "Downloading the lens database": "レンズデータベースをダウンロード中" + "Downloading the lens database": "レンズデータベースをダウンロード中", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "レンズデータベースをダウンロードできませんでした。LightCraft は次回の起動時に再試行します。" } diff --git a/crates/ui-egui/locales/ka.json b/crates/ui-egui/locales/ka.json index 646b6eb6cd..7079370827 100644 --- a/crates/ui-egui/locales/ka.json +++ b/crates/ui-egui/locales/ka.json @@ -2740,5 +2740,6 @@ "This file names no lens.": "ამ ფაილში ობიექტივი არ არის მითითებული.", "1 photo got its lens profile": "1 ფოტომ მიიღო ობიექტივის პროფილი", "Looking up lens profiles": "ობიექტივის პროფილების ძიება", - "Downloading the lens database": "ობიექტივების ბაზის ჩამოტვირთვა" + "Downloading the lens database": "ობიექტივების ბაზის ჩამოტვირთვა", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "ობიექტივების მონაცემთა ბაზის ჩამოტვირთვა ვერ მოხერხდა. LightCraft კვლავ სცდის შემდეგ გაშვებისას." } diff --git a/crates/ui-egui/locales/pl.json b/crates/ui-egui/locales/pl.json index dedb38a14a..bc75382a21 100644 --- a/crates/ui-egui/locales/pl.json +++ b/crates/ui-egui/locales/pl.json @@ -2289,5 +2289,6 @@ "This file names no lens.": "Ten plik nie podaje obiektywu.", "1 photo got its lens profile": "1 zdjęcie otrzymało profil obiektywu", "Looking up lens profiles": "Wyszukiwanie profili obiektywów", - "Downloading the lens database": "Pobieranie bazy obiektywów" + "Downloading the lens database": "Pobieranie bazy obiektywów", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Nie udało się pobrać bazy obiektywów. LightCraft spróbuje ponownie przy następnym uruchomieniu." } diff --git a/crates/ui-egui/locales/pt-br.json b/crates/ui-egui/locales/pt-br.json index ea8ecbb986..b1ba74437c 100644 --- a/crates/ui-egui/locales/pt-br.json +++ b/crates/ui-egui/locales/pt-br.json @@ -2208,5 +2208,6 @@ "This file names no lens.": "Este arquivo não indica nenhuma lente.", "1 photo got its lens profile": "1 foto recebeu o perfil da lente", "Looking up lens profiles": "Procurando perfis de lente", - "Downloading the lens database": "Baixando o banco de dados de lentes" + "Downloading the lens database": "Baixando o banco de dados de lentes", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Não foi possível baixar o banco de dados de lentes. O LightCraft tentará novamente na próxima vez que for aberto." } diff --git a/crates/ui-egui/locales/ru.json b/crates/ui-egui/locales/ru.json index ec340f98e8..f34b82c585 100644 --- a/crates/ui-egui/locales/ru.json +++ b/crates/ui-egui/locales/ru.json @@ -2355,5 +2355,6 @@ "This file names no lens.": "В этом файле не указан объектив.", "1 photo got its lens profile": "1 фото получило профиль объектива", "Looking up lens profiles": "Поиск профилей объективов", - "Downloading the lens database": "Загрузка базы объективов" + "Downloading the lens database": "Загрузка базы объективов", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Не удалось загрузить базу данных объективов. LightCraft попробует снова при следующем запуске." } diff --git a/crates/ui-egui/locales/uk.json b/crates/ui-egui/locales/uk.json index a43ffbd1f4..9743edbbd9 100644 --- a/crates/ui-egui/locales/uk.json +++ b/crates/ui-egui/locales/uk.json @@ -2360,5 +2360,6 @@ "This file names no lens.": "У цьому файлі не вказано об'єктив.", "1 photo got its lens profile": "1 фото отримало профіль об'єктива", "Looking up lens profiles": "Пошук профілів об'єктивів", - "Downloading the lens database": "Завантаження бази об'єктивів" + "Downloading the lens database": "Завантаження бази об'єктивів", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Не вдалося завантажити базу даних об’єктивів. LightCraft спробує знову під час наступного запуску." } diff --git a/crates/ui-egui/locales/zh-hans.json b/crates/ui-egui/locales/zh-hans.json index 5bd3b0805e..8e031f89f3 100644 --- a/crates/ui-egui/locales/zh-hans.json +++ b/crates/ui-egui/locales/zh-hans.json @@ -2206,5 +2206,6 @@ "This file names no lens.": "此文件未注明镜头。", "1 photo got its lens profile": "1 张照片获得了镜头配置文件", "Looking up lens profiles": "正在查找镜头配置文件", - "Downloading the lens database": "正在下载镜头数据库" + "Downloading the lens database": "正在下载镜头数据库", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "无法下载镜头数据库。LightCraft 会在下次启动时重试。" } diff --git a/crates/ui-egui/locales/zh-hant.json b/crates/ui-egui/locales/zh-hant.json index ddba21e479..47b26508b4 100644 --- a/crates/ui-egui/locales/zh-hant.json +++ b/crates/ui-egui/locales/zh-hant.json @@ -2163,5 +2163,6 @@ "This file names no lens.": "此檔案未註明鏡頭。", "1 photo got its lens profile": "1 張照片取得了鏡頭設定檔", "Looking up lens profiles": "正在查找鏡頭設定檔", - "Downloading the lens database": "正在下載鏡頭資料庫" + "Downloading the lens database": "正在下載鏡頭資料庫", + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "無法下載鏡頭資料庫。LightCraft 會在下次啟動時重試。" } diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs index f828dacb69..d87bd89cbd 100644 --- a/crates/ui-egui/src/lens_db.rs +++ b/crates/ui-egui/src/lens_db.rs @@ -106,6 +106,9 @@ pub fn readout(app: &LightcraftApp, p: &lightcraft_catalog::Photo) -> String { crate::i18n::tr_format!("Profile: {} (lens database)", name) } None if app.session.lensfun.running() => crate::i18n::tr("Downloading the lens database…").to_string(), + None if matches!(app.session.lensfun.state(), lensfun_download::State::Failed(_)) && lens_profiles::database().is_none() => { + crate::i18n::tr("The lens database could not be downloaded. LightCraft tries again the next time it starts.").to_string() + } None => match lens_refresh::no_profile_reason(p) { Some(r) if p.meta.lens.trim().is_empty() => crate::i18n::tr(&r).to_string(), Some(_) => crate::i18n::tr_format!("No lens profile found for {}.", p.meta.lens.trim()), @@ -138,4 +141,30 @@ mod tests { let r = super::readout(&app, &p); assert!(r.starts_with("Profile: ") && r.contains("70-200mm f/2.8L IS II USM") && r.ends_with(" (lens database)"), "{r}"); } + + /// A first-run download that failed says so, instead of "no profile" for every lens. + #[test] + fn a_failed_download_is_named_in_the_optics_readout() { + let mut app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), Services { png: None, ..Default::default() }); + let id = app.session.active().expect("a photo"); + let mut p = (**app.session.catalog.photo(id).expect("the photo")).clone(); + p.meta.lens = "EF35mm f/1.4L II USM".into(); + p.embedded_lens = None; + let dir = std::env::temp_dir().join(format!("lc-lens-db-failed-{}", std::process::id())); + // nothing listens on port 1: the download fails at once, without the network + let activity = app.session.activity.clone(); + app.session + .lensfun + .start(&dir, vec!["http://127.0.0.1:1/db".into()], lightcraft_engine::lensfun_download::FILES, &activity) + .expect("started"); + let t0 = std::time::Instant::now(); + while app.session.lensfun.running() && t0.elapsed() < std::time::Duration::from_secs(60) { + std::thread::sleep(std::time::Duration::from_millis(20)); + } + assert!(matches!(app.session.lensfun.state(), lightcraft_engine::lensfun_download::State::Failed(_)), "{:?}", app.session.lensfun.state()); + if lightcraft_engine::lens_profiles::database().is_none() { + assert_eq!(super::readout(&app, &p), "The lens database could not be downloaded. LightCraft tries again the next time it starts."); + } + let _ = std::fs::remove_dir_all(&dir); + } } From 7a77f7248a84d1989287b77c7b2af3929cd0e8d3 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Tue, 27 Oct 2026 15:30:49 +1000 Subject: [PATCH 12/51] LR-EDIT-OPTICS-PROFILE: review fixes (CodeRabbit, #3073) - Enable Lens Corrections looks up again only the photos that now have corrections on (lens_db::to_look_up): with Auto Sync off the other selected photos are left alone. - An old lens database file that can't be removed is an error: no SOURCE.txt marker, so the next start tries again instead of reading the old file beside the new ones (remove_stale). - A lookup's results are dropped when the lens database was reloaded while files were read (lookup_key: library and database generation); the next frame looks them up again. Tests: ticking_lens_corrections_looks_up_only_the_photos_it_changed, a_database_reloaded_during_a_lookup_makes_its_results_stale, an_old_file_that_cannot_be_removed_is_an_error. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lensfun_download.rs | 56 ++++++++++++++++++------- crates/ui-egui/src/lens_db.rs | 25 +++++++++-- crates/ui-egui/src/panels/edit.rs | 5 +-- crates/ui-egui/src/tests.rs | 1 + crates/ui-egui/src/tests/lens_lookup.rs | 39 +++++++++++++++++ 5 files changed, 103 insertions(+), 23 deletions(-) create mode 100644 crates/ui-egui/src/tests/lens_lookup.rs diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index 9e34c76b73..44a5c88366 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -254,22 +254,9 @@ fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> for (name, _, _) in files { std::fs::rename(staging.join(name), dir.join(name)).map_err(|e| format!("could not move {name} into {}: {e}", dir.display()))?; } - // database files an older pin had and this one doesn't would still be read: remove them (only XML files; nothing - // else in the folder is touched) - if let Ok(entries) = std::fs::read_dir(dir) { - for entry in entries.flatten() { - let name = entry.file_name(); - let name = name.to_string_lossy(); - let xml = std::path::Path::new(&*name).extension().is_some_and(|e| e.eq_ignore_ascii_case("xml")); - if xml - && entry.path().is_file() - && !files.iter().any(|(pinned, _, _)| *pinned == name) - && let Err(e) = std::fs::remove_file(entry.path()) - { - log::warn!("lens database: could not remove the old {name}: {e}"); - } - } - } + // database files an older pin had and this one doesn't would still be read: remove them, and without the marker + // when one can't be (the next start tries again rather than reading an old file beside the new ones) + remove_stale(dir, files, |p| std::fs::remove_file(p))?; let _ = std::fs::remove_dir_all(&staging); let readme = format!( "lensfun lens database (data/db), commit {COMMIT}\nfrom https://github.com/lensfun/lensfun, downloaded by LightCraft.\n\ @@ -280,6 +267,26 @@ fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> Ok(files.len()) } +/// Remove the XML files in `dir` that are not among `files` (nothing else in the folder is touched) with `remove`: an +/// error names the first that could not be removed. +#[cfg(not(target_arch = "wasm32"))] +fn remove_stale(dir: &Path, files: &[Pin], remove: impl Fn(&Path) -> std::io::Result<()>) -> Result<(), String> { + let Ok(entries) = std::fs::read_dir(dir) else { return Ok(()) }; + for entry in entries.flatten() { + let name = entry.file_name(); + let name = name.to_string_lossy(); + let xml = std::path::Path::new(&*name).extension().is_some_and(|e| e.eq_ignore_ascii_case("xml")); + if xml + && entry.path().is_file() + && !files.iter().any(|(pinned, _, _)| *pinned == name) + && let Err(e) = remove(&entry.path()) + { + return Err(format!("could not remove the old lens database file {name}: {e}")); + } + } + Ok(()) +} + #[cfg(target_arch = "wasm32")] fn fetch_all(_: &Path, _: &[String], _: &[Pin], _: &Shared) -> Result { Err("the web build downloads no lens database".into()) @@ -392,6 +399,23 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// An old database file that can't be removed is an error (so no marker is written and the next start tries + /// again); the pinned files and other files are left alone. + #[test] + fn an_old_file_that_cannot_be_removed_is_an_error() { + let dir = scratch("stale"); + for name in ["lenses.xml", "old-lenses.xml", "notes.txt"] { + std::fs::write(dir.join(name), "x").unwrap(); + } + let failing = |_: &Path| Err(std::io::Error::other("in use")); + let err = remove_stale(&dir, pins(), failing).unwrap_err(); + assert!(err.contains("old-lenses.xml") && err.contains("in use"), "{err}"); + remove_stale(&dir, pins(), |p| std::fs::remove_file(p)).unwrap(); + assert!(!dir.join("old-lenses.xml").exists()); + assert!(dir.join("lenses.xml").exists() && dir.join("notes.txt").exists()); + let _ = std::fs::remove_dir_all(&dir); + } + /// The pinned list is lensfun's whole `data/db` at the commit: unique XML names, sane sizes, hex hashes. #[test] fn the_pins_are_well_formed() { diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs index d87bd89cbd..f9562e2473 100644 --- a/crates/ui-egui/src/lens_db.rs +++ b/crates/ui-egui/src/lens_db.rs @@ -37,9 +37,27 @@ fn start_download(app: &mut LightcraftApp) { } } +/// After Enable Lens Corrections was ticked on photo `id`: the photos to look up again, those of `id` and the +/// selection that now have corrections on and no profile. With Auto Sync off only `id` was changed, so the other +/// selected photos are left alone unless their corrections were already on. +pub fn to_look_up(session: &lightcraft_engine::Session, id: lightcraft_catalog::PhotoId) -> Vec { + let mut out: Vec = Vec::new(); + for t in std::iter::once(id).chain(session.selection.ids.iter().copied()) { + if !out.contains(&t) && session.catalog.photo(t).is_some_and(|p| p.embedded_lens.is_none() && p.develop.optics.lens_profile) { + out.push(t); + } + } + out +} + +/// What a lookup's results belong to: the library and the lens database in use. Results for another key are stale. +pub(crate) fn lookup_key(app: &LightcraftApp) -> (u64, u64) { + (app.session.library_generation(), lens_profiles::generation()) +} + /// Look the library's raws up again when the database changed, the library changed, or photos asked for it. fn refresh(app: &mut LightcraftApp) { - let key = (app.session.library_generation(), lens_profiles::generation()); + let key = lookup_key(app); let asked = std::mem::take(&mut app.ui.lens_lookup_ids); if app.ui.lens_refreshed == Some(key) && asked.is_empty() { return; @@ -57,10 +75,11 @@ fn refresh(app: &mut LightcraftApp) { if listed.is_empty() { return; } - let library = key.0; let work = move || lens_refresh::probe(&probe, listed); let done = move |app: &mut LightcraftApp, ctx: &egui::Context, results| { - if app.session.library_generation() != library { + // another library, or a database reloaded while the files were read (the probe may have used either): the + // next frame looks them up again with the database in use now + if lookup_key(app) != key { return; } match lens_refresh::apply(&mut app.session, results) { diff --git a/crates/ui-egui/src/panels/edit.rs b/crates/ui-egui/src/panels/edit.rs index e05571f080..1da78bdae1 100644 --- a/crates/ui-egui/src/panels/edit.rs +++ b/crates/ui-egui/src/panels/edit.rs @@ -337,10 +337,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { // ticked on photos without a profile: look their lenses up again (a database that arrived // since they were imported may know them now) if ok && lp { - let without: Vec<_> = std::iter::once(id) - .chain(app.session.selection.ids.iter().copied()) - .filter(|t| app.session.catalog.photo(*t).is_some_and(|p| p.embedded_lens.is_none())) - .collect(); + let without = crate::lens_db::to_look_up(&app.session, id); app.ui.lens_lookup_ids.extend(without); } } diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index 3a102a29a9..24ba679324 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -41,6 +41,7 @@ mod keywording; mod labels; mod launch_library; mod layout_image; +mod lens_lookup; mod library_problem; mod lifecycle_037; mod lightroom_destination; diff --git a/crates/ui-egui/src/tests/lens_lookup.rs b/crates/ui-egui/src/tests/lens_lookup.rs new file mode 100644 index 0000000000..882e07ab4d --- /dev/null +++ b/crates/ui-egui/src/tests/lens_lookup.rs @@ -0,0 +1,39 @@ +//! Enable Lens Corrections looks up again only the photos it switched on (and selected photos that already had +//! corrections on): with Auto Sync off the other selected photos are left alone. + +use lightcraft_engine::Session; +use serde_json::json; + +#[test] +fn ticking_lens_corrections_looks_up_only_the_photos_it_changed() { + let mut s = Session::with_demo(); + s.execute("library.selectAll", &json!({})).unwrap(); + let active = s.selection.active.unwrap(); + assert!(s.selection.ids.len() > 2); + let lp = |s: &Session| s.selection.ids.iter().filter(|id| s.catalog.photo(**id).unwrap().develop.optics.lens_profile).count(); + assert_eq!(lp(&s), 0); + // Auto Sync off: only the active photo is changed, so only it is looked up + s.execute("develop.merge", &json!({"settings": {"optics": {"lens_profile": true}}})).unwrap(); + assert_eq!(crate::lens_db::to_look_up(&s, active), vec![active]); + // Auto Sync on: every selected photo now has corrections on, and each is looked up once + let mut s = Session::with_demo(); + s.execute("library.selectAll", &json!({})).unwrap(); + let active = s.selection.active.unwrap(); + s.auto_sync = true; + s.execute("develop.merge", &json!({"settings": {"optics": {"lens_profile": true}}})).unwrap(); + assert_eq!(lp(&s), s.selection.ids.len()); + let ids = crate::lens_db::to_look_up(&s, active); + assert_eq!(ids.len(), s.selection.ids.len()); + assert_eq!(ids.first(), Some(&active)); +} + +/// A lookup's results are dropped when the lens database was reloaded while the files were read (the probe may have +/// used the old one); the next frame looks the photos up again. +#[test] +fn a_database_reloaded_during_a_lookup_makes_its_results_stale() { + let app = crate::LightcraftApp::new(Session::with_demo(), crate::Services { png: None, ..Default::default() }); + let key = crate::lens_db::lookup_key(&app); + assert_eq!(crate::lens_db::lookup_key(&app), key); + lightcraft_engine::lens_profiles::reload(); + assert_ne!(crate::lens_db::lookup_key(&app), key); +} From 326db689698158968c8399433a05f7cd25cfda6d Mon Sep 17 00:00:00 2001 From: OneSlips Date: Tue, 27 Oct 2026 17:46:55 +1000 Subject: [PATCH 13/51] LR-EDIT-OPTICS-PROFILE: an unlistable lens database folder is an error, not success (CodeRabbit, #3073) Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lensfun_download.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index 44a5c88366..c49cd428d5 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -268,11 +268,13 @@ fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> } /// Remove the XML files in `dir` that are not among `files` (nothing else in the folder is touched) with `remove`: an -/// error names the first that could not be removed. +/// error names the first that could not be removed, or why the folder could not be listed. #[cfg(not(target_arch = "wasm32"))] fn remove_stale(dir: &Path, files: &[Pin], remove: impl Fn(&Path) -> std::io::Result<()>) -> Result<(), String> { - let Ok(entries) = std::fs::read_dir(dir) else { return Ok(()) }; - for entry in entries.flatten() { + // a folder that can't be listed (or an entry that can't be read) may still hold an old file: an error, not success + let listing = |e: std::io::Error| format!("could not list the lens database folder {}: {e}", dir.display()); + for entry in std::fs::read_dir(dir).map_err(listing)? { + let entry = entry.map_err(listing)?; let name = entry.file_name(); let name = name.to_string_lossy(); let xml = std::path::Path::new(&*name).extension().is_some_and(|e| e.eq_ignore_ascii_case("xml")); @@ -413,6 +415,12 @@ mod tests { remove_stale(&dir, pins(), |p| std::fs::remove_file(p)).unwrap(); assert!(!dir.join("old-lenses.xml").exists()); assert!(dir.join("lenses.xml").exists() && dir.join("notes.txt").exists()); + // a folder that can't be listed is an error too, not "nothing stale" + let not_a_folder = dir.join("notes.txt"); + let err = remove_stale(¬_a_folder, pins(), |p| std::fs::remove_file(p)).unwrap_err(); + assert!(err.contains("could not list"), "{err}"); + let err = remove_stale(&dir.join("missing"), pins(), |p| std::fs::remove_file(p)).unwrap_err(); + assert!(err.contains("could not list"), "{err}"); let _ = std::fs::remove_dir_all(&dir); } From 8bd9db8771177b0c45ec3da99404d0508608172e Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 05:40:01 +0000 Subject: [PATCH 14/51] LR-EDIT-OPTICS-PROFILE: script to measure Sony distortion against camera JPEGs Renders each ARW with the embedded correction on and off and compares it with the camera's own JPEG: residual offsets by radius after a similarity fit, checkerboard line straightness, and the ratio of the camera's correction to LightCraft's. Checked on synthetic boards with known radial distortion (corrected: 0.04 px median, ratio 1.000; 80 % corrected: ratio 1.26, expected about 1.25-1.28). Needs numpy, scipy and opencv-python-headless. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015gRtdWF1E9358FfzhzYqCS --- scripts/sony_distortion_check.py | 361 +++++++++++++++++++++++++++++++ 1 file changed, 361 insertions(+) create mode 100755 scripts/sony_distortion_check.py diff --git a/scripts/sony_distortion_check.py b/scripts/sony_distortion_check.py new file mode 100755 index 0000000000..ee81a118cd --- /dev/null +++ b/scripts/sony_distortion_check.py @@ -0,0 +1,361 @@ +#!/usr/bin/env python3 +"""Compare LightCraft's Sony embedded distortion correction with the camera's own JPEG. + + sony_distortion_check.py --cli target/release/lightcraft-cli --work WORKDIR PHOTO_DIR [PHOTO_DIR…] + +Every ARW with a JPG of the same name is rendered by LightCraft twice, embedded correction on +and off, and measured against the camera JPEG (shot with Lens Comp. Distortion on Auto): + +* agreement: offsets of matching features after the best similarity (scale, rotation, shift) + between the render and the JPEG, by distance from the centre (0 = centre, 1 = corner); +* straightness: how far each checkerboard row and column bows away from a straight line. + +Checkerboard shots use the board's corners (sub-pixel), other shots SIFT features (agreement +only). Offsets are in pixels of the camera JPEG (6000 px wide on the A7 III); the "@2376" +columns rescale them to the 2376 px exports docs/sony-lens-corrections.md reports. +Needs numpy, scipy and opencv-python-headless. +""" + +import argparse +import json +import os +import subprocess +import sys + +import cv2 +import numpy as np +from scipy.spatial import cKDTree + +WORK_LONG_EDGE = 3000 # detection resolution; coordinates are rescaled to the JPEG's pixels +BINS = [0.0, 0.25, 0.5, 0.75, 0.9, 1.01] +REPORT_WIDTH = 2376 + + +def render(cli, arw, out, on): + settings = out + ".settings.json" + with open(settings, "w") as f: + json.dump({"optics": {"lens_profile": on, "profile_distortion": 100}}, f) + if not os.path.exists(out): + subprocess.run([cli, "render", arw, "-o", out, "--settings", settings], check=True, capture_output=True) + return out + + +def load_gray(path): + """Grey image at the working resolution and the factor back to its own full-size pixels.""" + img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) + if img is None: + raise RuntimeError(f"cannot read {path}") + h, w = img.shape + k = WORK_LONG_EDGE / max(w, h) + small = cv2.resize(img, (round(w * k), round(h * k)), interpolation=cv2.INTER_AREA) + return small, 1.0 / k, (w, h) + + +# ---------------------------------------------------------------- checkerboard corners + +def detect_corners(gray): + """X-corners of a checkerboard: Hessian saddle points, refined with cornerSubPix.""" + g = cv2.GaussianBlur(gray.astype(np.float32), (0, 0), 3.0) + gxx = cv2.Sobel(g, cv2.CV_32F, 2, 0, ksize=5) + gyy = cv2.Sobel(g, cv2.CV_32F, 0, 2, ksize=5) + gxy = cv2.Sobel(g, cv2.CV_32F, 1, 1, ksize=5) + saddle = gxy * gxy - gxx * gyy + top = np.percentile(saddle, 99.95) + if top <= 0: + return np.zeros((0, 2), np.float32) + peaks = (saddle == cv2.dilate(saddle, np.ones((15, 15), np.uint8))) & (saddle > 0.08 * top) + ys, xs = np.nonzero(peaks) + pts = np.stack([xs, ys], 1).astype(np.float32).reshape(-1, 1, 2) + if len(pts) == 0: + return pts.reshape(-1, 2) + crit = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 50, 0.005) + pts = cv2.cornerSubPix(gray, pts, (11, 11), (-1, -1), crit) + return pts.reshape(-1, 2).astype(np.float64) + + +def label_lattice(pts, centre): + """Integer (i, j) grid labels for corners connected to the one nearest the centre. + i grows along +x, j along +y; local lattice vectors follow the distortion gradually.""" + if len(pts) < 20: + return {} + tree = cKDTree(pts) + start = int(tree.query(centre)[1]) + d, idx = tree.query(pts[start], k=5) + offsets = pts[idx[1:]] - pts[start] + spacing = float(np.median(d[1:])) + a = offsets[np.argmax(offsets @ np.array([1.0, 0.0]))] + b = offsets[np.argmax(offsets @ np.array([0.0, 1.0]))] + if abs(np.dot(a, b)) > 0.5 * np.linalg.norm(a) * np.linalg.norm(b): + return {} + labels = {start: (0, 0)} + taken = {(0, 0): start} + vec = {start: (a, b)} + queue = [start] + while queue: + p = queue.pop() + i, j = labels[p] + va, vb = vec[p] + for di, dj, step in ((1, 0, va), (-1, 0, -va), (0, 1, vb), (0, -1, -vb)): + key = (i + di, j + dj) + if key in taken: + continue + dist, q = tree.query(pts[p] + step) + if dist > 0.3 * np.linalg.norm(step) or q in labels: + continue + found = pts[q] - pts[p] + na = found * di if di else va + nb = found * dj if dj else vb + labels[q] = key + taken[key] = q + vec[q] = (na, nb) + queue.append(q) + return labels if len(labels) > 50 else {} + + +def board(path): + gray, back, size = load_gray(path) + pts = detect_corners(gray) + labels = label_lattice(pts, np.array(gray.shape[::-1]) / 2.0) + full = {lab: pts[k] * back for k, lab in labels.items()} + return full, size + + +def straightness(grid): + """Largest deviation from a fitted straight line over every row/column with 8+ corners.""" + lines = {} + for (i, j), p in grid.items(): + lines.setdefault(("col", i), []).append(p) + lines.setdefault(("row", j), []).append(p) + worst = [] + for pts in lines.values(): + if len(pts) < 8: + continue + pts = np.array(pts) + keep = np.ones(len(pts), bool) + for _ in range(3): # drop stray corners, then refit + c = pts[keep].mean(0) + _, _, vt = np.linalg.svd(pts[keep] - c) + dev = (pts - c) @ vt[1] + keep = np.abs(dev) <= max(1.0, 4 * np.median(np.abs(dev[keep]))) + if keep.sum() < 8: + break + if keep.sum() >= 8: + worst.append(np.abs(dev[keep]).max()) + if not worst: + return None + worst = np.array(worst) + return {"lines": len(worst), "median": float(np.median(worst)), "max": float(worst.max())} + + +def match_boards(src, src_size, dst, dst_size): + """Corner pairs between two labellings of the same shot. A board is periodic, so a fit can't + tell a one-square shift from the right match; the corners near the centre can, unfitted: + the two framings differ by a few percent of zoom, far less than a square there.""" + sc, dc = np.array(src_size) / 2.0, np.array(dst_size) / 2.0 + half_diag = np.hypot(*dst_size) / 2.0 + best = None + for oi in range(-3, 4): + for oj in range(-3, 4): + pairs = [(p, dst[(i + oi, j + oj)]) for (i, j), p in src.items() if (i + oi, j + oj) in dst] + central = [np.linalg.norm((p - sc) - (q - dc)) for p, q in pairs if np.linalg.norm(q - dc) < 0.3 * half_diag] + if len(pairs) < 50 or len(central) < 10: + continue + err = float(np.median(central)) + if best is None or err < best[0]: + best = (err, pairs) + if best is None: + return None, None + s, d = map(np.array, zip(*best[1])) + return s, d + + +# ---------------------------------------------------------------- SIFT (other scenes) + +def sift_pairs(src_path, dst_path): + """SIFT matches; both point sets in the destination's full-size pixels.""" + a, ka, size_a = load_gray(src_path) + b, kb, size_b = load_gray(dst_path) + sift = cv2.SIFT_create(nfeatures=20000) + pa, da = sift.detectAndCompute(a, None) + pb, db = sift.detectAndCompute(b, None) + if da is None or db is None: + return None, None + good = [m for m, n in cv2.BFMatcher().knnMatch(da, db, k=2) if m.distance < 0.75 * n.distance] + if len(good) < 30: + return None, None + s = np.array([pa[m.queryIdx].pt for m in good]) * ka * (size_b[0] / size_a[0]) + d = np.array([pb[m.trainIdx].pt for m in good]) * kb + return s, d + + +# ---------------------------------------------------------------- geometry + +def fit_similarity(s, d): + zs = s[:, 0] + 1j * s[:, 1] + zd = d[:, 0] + 1j * d[:, 1] + ms, md = zs.mean(), zd.mean() + c = np.vdot(zs - ms, zd - md) / np.vdot(zs - ms, zs - ms) + return c, md - c * ms + + +def apply(model, p): + c, t = model + z = c * (p[:, 0] + 1j * p[:, 1]) + t + return np.stack([z.real, z.imag], 1) + + +def agreement(s, d, size): + """Similarity-fit render points s onto JPEG points d; residual statistics by radius.""" + pairs = {tuple(q): p for q, p in zip(d, s)} + keep = np.ones(len(s), bool) + # A loose first pass, then reject gross mismatches (SIFT outliers, mislabelled corners). + for limit in (0.02 * size[0], None, None): + model = fit_similarity(s[keep], d[keep]) + res = np.linalg.norm(apply(model, s) - d, axis=1) + cut = limit if limit else max(5 * np.median(res[keep]), 2.0) + keep = res < cut + if keep.sum() < 20: + return None + model = fit_similarity(s[keep], d[keep]) + s, d = s[keep], d[keep] + fitted = apply(model, s) + res = fitted - d + centre = np.array(size) / 2.0 + half_diag = np.hypot(*size) / 2.0 + rel = d - centre + r = np.linalg.norm(rel, axis=1) / half_diag + radial = np.sum(res * rel, axis=1) / np.maximum(np.linalg.norm(rel, axis=1), 1e-9) + mag = np.linalg.norm(res, axis=1) + bins = [] + for lo, hi in zip(BINS, BINS[1:]): + m = (r >= lo) & (r < hi) + bins.append({"r": f"{lo:.2f}-{min(hi, 1):.2f}", "n": int(m.sum()), + "median": float(np.median(mag[m])) if m.any() else None, + "radial": float(np.mean(radial[m])) if m.any() else None}) + return {"n": int(len(s)), "scale": float(abs(model[0])), "median": float(np.median(mag)), + "p95": float(np.percentile(mag, 95)), "max": float(mag.max()), "max_r": float(r.max()), + "bins": bins, "pairs": pairs} + + +def correction_fraction(on, off, size): + """How the camera's correction compares with LightCraft's: JPEG ≈ off + α·(on − off) radially, + both renders aligned to the JPEG on the same corners. 1 = the same amount as the camera; + above 1, the camera corrects more than LightCraft.""" + keys = [k for k in on["pairs"] if k in off["pairs"]] + if len(keys) < 50: + return None + d = np.array(keys) + raw_on = np.array([on["pairs"][k] for k in keys]) + raw_off = np.array([off["pairs"][k] for k in keys]) + keep = np.ones(len(d), bool) + for _ in range(4): + a = apply(fit_similarity(raw_on[keep], d[keep]), raw_on) + b = apply(fit_similarity(raw_off[keep], d[keep]), raw_off) + rel = d - np.array(size) / 2.0 + unit = rel / np.maximum(np.linalg.norm(rel, axis=1, keepdims=True), 1e-9) + u = np.sum((a - b) * unit, axis=1) + v = np.sum((d - b) * unit, axis=1) + if np.median(np.abs(u[keep])) < 1.0: # too little distortion at this focal length to tell + return None + alpha = float(u[keep] @ v[keep] / (u[keep] @ u[keep])) + e = np.abs(v - alpha * u) + keep = e <= max(0.5, 4 * np.median(e[keep])) # mismatched corners, not the lens + return alpha + + +# ---------------------------------------------------------------- driver + +def jpeg_for(arw): + stem = os.path.splitext(arw)[0] + for ext in (".JPG", ".jpg", ".JPEG", ".jpeg"): + if os.path.exists(stem + ext): + return stem + ext + return None + + +def exif_summary(jpg): + from PIL import Image + tags = Image.open(jpg).getexif().get_ifd(0x8769) + lens = tags.get(0xA434, "?") + focal = tags.get(0x920A) + fnum = tags.get(0x829D) + return f"{lens} {float(focal):.0f}mm f/{float(fnum):.1f}" if focal and fnum else str(lens) + + +def measure(cli, work, arw): + jpg = jpeg_for(arw) + if not jpg: + return None + name = os.path.splitext(os.path.basename(arw))[0] + on = render(cli, arw, os.path.join(work, name + ".on.png"), True) + off = render(cli, arw, os.path.join(work, name + ".off.png"), False) + out = {"file": name, "shot": exif_summary(jpg)} + jgrid, jsize = board(jpg) + out["kind"] = "board" if jgrid else "scene" + if jgrid: + out["straight_jpeg"] = straightness(jgrid) + for tag, path in (("on", on), ("off", off)): + if jgrid: + grid, size = board(path) + if not grid: + continue + k = jsize[0] / size[0] + grid = {key: p * k for key, p in grid.items()} + out["straight_" + tag] = straightness(grid) + s, d = match_boards(grid, (size[0] * k, size[1] * k), jgrid, jsize) + else: + s, d = sift_pairs(path, jpg) + if s is not None: + out["vs_jpeg_" + tag] = agreement(s, d, jsize) + if "vs_jpeg_on" in out and "vs_jpeg_off" in out and out["kind"] == "board": + out["fraction"] = correction_fraction(out["vs_jpeg_on"], out["vs_jpeg_off"], jsize) + for tag in ("on", "off"): + out.get("vs_jpeg_" + tag, {}).pop("pairs", None) + out["jpeg_width"] = jsize[0] + return out + + +def show(o): + k = REPORT_WIDTH / o["jpeg_width"] + print(f"\n{o['file']} {o['shot']} [{o['kind']}]") + for tag, label in (("on", "LightCraft corrected"), ("off", "LightCraft uncorrected")): + a = o.get("vs_jpeg_" + tag) + if a: + print(f" {label:24s} vs camera JPEG: median {a['median']:.2f} px, p95 {a['p95']:.2f}, max {a['max']:.2f}" + f" (@{REPORT_WIDTH}: {a['median'] * k:.2f} / {a['p95'] * k:.2f}) n={a['n']} reach r={a['max_r']:.2f}") + print(" by radius: " + " ".join( + f"{b['r']}: {b['median']:.2f} ({b['radial']:+.2f})" for b in a["bins"] if b["n"])) + for tag in ("jpeg", "on", "off"): + st = o.get("straight_" + tag) + if st: + print(f" straightness {tag:4s}: worst line {st['max']:.2f} px, median line {st['median']:.2f} px ({st['lines']} lines)") + if o.get("fraction") is not None: + print(f" camera correction / LightCraft correction: {o['fraction']:.3f} (1 = same amount)") + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--cli", required=True) + ap.add_argument("--work", required=True) + ap.add_argument("--json") + ap.add_argument("dirs", nargs="+") + args = ap.parse_args() + os.makedirs(args.work, exist_ok=True) + arws = sorted(os.path.join(d, f) for d in args.dirs for f in os.listdir(d) if f.lower().endswith(".arw")) + results = [] + for arw in arws: + try: + o = measure(args.cli, args.work, arw) + except Exception as e: # keep going: one bad file must not stop the batch + print(f"\n{arw}: {e}", file=sys.stderr) + continue + if o: + show(o) + results.append(o) + if args.json: + with open(args.json, "w") as f: + json.dump(results, f, indent=1) + + +if __name__ == "__main__": + main() From 4db57c976b0493ff1e2f6a01ae89a5996d1a1e91 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 23:53:59 +0000 Subject: [PATCH 15/51] LR-EDIT-OPTICS-PROFILE: enable Sony ILCE-7M3 embedded distortion correction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The A7 III's raw-IFD 0x7037 tables were refused until their geometry was checked. Measured against the camera's own JPEG of the same exposure (Lens Comp. Distortion: Auto) on 27 RAW+JPEG pairs from one body with the FE 16-35 F4 ZA, FE 28-70 and FE 50 F1.8 (strong barrel to strong pincushion, compressed and uncompressed, TV and monitor checkerboards): corrected board corners sit 0.06-0.44 px (median, 6000 px frame) from the JPEG's, against 0.8-26 px uncorrected; LightCraft applies the camera's amount of correction within 2 %; corrected rows and columns are as straight as the JPEG's. Sony's crop and the default crop agree on this body, so the warp is centred on the image. A unit test pins the warp from three of these tables to the camera's fitted geometry; the model boundary tests now accept ILCE-7M3 and still refuse its neighbours. RENDER_CACHE_VERSION is bumped. The measurement script handles screen moiré (X-corner check, duplicate corners, consensus lattice vectors). The corpus test's two ILCE-7M3 samples now expect one warp; raw.pixls.us is not reachable from this environment, so that test was skipped here. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015gRtdWF1E9358FfzhzYqCS --- crates/engine/src/media/source.rs | 2 +- crates/raw/src/vendor/arw_lens.rs | 53 ++++++++++++++++++++--- crates/raw/tests/it/corpus.rs | 10 ++--- docs/gaps.md | 2 +- docs/parity-checklist.md | 2 +- docs/raw-parity.md | 2 +- docs/sony-lens-corrections.md | 56 +++++++++++++++++++++--- docs/whats-new.md | 10 +++++ scripts/sony_distortion_check.py | 72 +++++++++++++++++++++++++------ 9 files changed, 174 insertions(+), 35 deletions(-) diff --git a/crates/engine/src/media/source.rs b/crates/engine/src/media/source.rs index 51c54c60a9..b8c1ec1542 100644 --- a/crates/engine/src/media/source.rs +++ b/crates/engine/src/media/source.rs @@ -1,7 +1,7 @@ use super::*; /// Bump when the pipeline's output changes, to invalidate cached thumbnails. -pub const RENDER_CACHE_VERSION: u64 = 70; +pub const RENDER_CACHE_VERSION: u64 = 71; /// Thumbnails render at one of these long edges (so window/cell size changes reuse the cache). pub const THUMB_SIZES: [usize; 4] = [128, 256, 384, 512]; diff --git a/crates/raw/src/vendor/arw_lens.rs b/crates/raw/src/vendor/arw_lens.rs index 473f5d9ffd..4cebf221ec 100644 --- a/crates/raw/src/vendor/arw_lens.rs +++ b/crates/raw/src/vendor/arw_lens.rs @@ -1,6 +1,6 @@ //! Sony's in-file lens-correction records (shading, lateral chromatic aberration, distortion): bounded decoding of //! the raw image IFD's fields and their encrypted `SR2SubIFD` copies into [`SonyLensCorrections`], and validated -//! ILCE-7RM4A and ILCE-7CR distortion tables into DNG warps for the shared optics pipeline. +//! ILCE-7RM4A, ILCE-7M3 and ILCE-7CR distortion tables into DNG warps for the shared optics pipeline. //! //! Tag identity/layout: https://exiftool.org/TagNames/EXIF.html (`0x7031` to `0x7037`, signed shorts) and //! https://exiftool.org/TagNames/Sony.html#SR2SubIFD (`0x797d`, `0x7980`, `0x7982`). The `SR2SubIFD` setting tags @@ -34,9 +34,9 @@ const DISTORTION: Tags = Tags { setting: (0x7036, 0x7981), table: (0x7037, 0x798 /// TIFF field types. const SHORT: u16 = 3; const SSHORT: u16 = 8; -/// Models using the validated A7R IVA conversion. The A7CR uses a separate conversion. +/// Models using the validated A7R IVA conversion (ILCE-7RM4A, ILCE-7M3). The A7CR uses a separate conversion. /// ILCE-7RM4 and ILCE-7RM4A are distinct models, not interchangeable aliases. -const DISTORTION_MODELS: &[&str] = &["ILCE-7RM4A"]; +const DISTORTION_MODELS: &[&str] = &["ILCE-7RM4A", "ILCE-7M3"]; /// Most directory entries scanned: a classic TIFF directory's largest count, also bounding a BigTIFF one. const MAX_ENTRIES: usize = 1 << 16; const SAMPLES: usize = 1024; @@ -1397,6 +1397,47 @@ mod tests { } } + // ILCE-7M3 raw-IFD tables (FE 16-35mm F4 ZA OSS at 16 and 35 mm, FE 28-70mm F3.5-5.6 OSS at 70 mm) and the + // geometry of the camera's own JPEG of the same exposure (Lens Comp. Distortion: Auto): checkerboard corners of + // that JPEG matched with the uncorrected raw and fitted with the warp's polynomial over the 6000×4000 frame (rms + // 0.11–0.25 px). See docs/sony-lens-corrections.md. + const A7M3: [([i16; 16], [f64; 4]); 3] = [ + ( + [32, 2, -38, -83, -138, -199, -270, -336, -402, -462, -519, -568, -611, -643, -670, -690], + [1.0247000859, -0.1092847508, 0.1061422243, -0.0397989678], + ), + ([2, 2, 4, 9, 18, 29, 45, 65, 92, 124, 164, 210, 264, 329, 403, 489], [0.9708150714, 0.0157256288, 0.0175613359, -0.004107544]), + ([4, 7, 18, 40, 68, 108, 155, 212, 280, 359, 453, 557, 679, 819, 978, 1161], [0.9342002611, 0.0560486347, 0.0076184603, 0.0021497933]), + ]; + + #[test] + fn ilce_7m3_matches_the_cameras_own_jpeg_geometry() { + let frame = Rect::new(0, 0, 6000, 4000); + let evaluate = |k: &[f64], r: f64| r * (k[0] + r * r * (k[1] + r * r * (k[2] + r * r * k[3]))); + for (values, reference) in A7M3 { + let Opcode::WarpRectilinear { planes, center } = distortion("ILCE-7M3", &values, frame, frame).unwrap() else { panic!("warp") }; + assert_eq!(center, [0.5, 0.5]); + // The camera fills its frame with its own zoom; compare the shape over the measured radii (to 0.97). + let radii: Vec = (1..=97).map(|i| i as f64 / 100.0).collect(); + let (ours, camera): (Vec, Vec) = radii.iter().map(|&r| (evaluate(&planes[0], r), evaluate(&reference, r))).unzip(); + let worst = |ours: &[f64]| { + let zoom = ours.iter().zip(&camera).map(|(a, b)| a * b).sum::() / ours.iter().map(|a| a * a).sum::(); + ours.iter().zip(&camera).map(|(a, b)| (a * zoom - b).abs()).fold(0.0, f64::max) + }; + // 0.0008 of the half diagonal is 2.9 px on the 6000 px frame (measured: 0.4–2.4 px); uncorrected is 10×+ off. + let (corrected, uncorrected) = (worst(&ours), worst(&radii)); + assert!(corrected < 0.0008 && uncorrected > 10.0 * corrected, "{values:?}: {corrected} vs {uncorrected}"); + } + // Sony's crop and the default crop agree on this body (12, 12, 6000×4000 of 6048×4024), so the warp sits on + // the image centre. + let Opcode::WarpRectilinear { center, .. } = + distortion("ILCE-7M3", &A7M3[0].0, Rect::new(0, 0, 6048, 4024), Rect::new(12, 12, 6000, 4000)).unwrap() + else { + panic!("warp") + }; + assert!((center[0] * 6047.0 - 3011.5).abs() < 1e-9 && (center[1] * 4023.0 - 2011.5).abs() < 1e-9, "{center:?}"); + } + #[test] fn barrel_framing_and_offset_crop_keep_the_same_geometry() { let crop = Rect::new(32, 20, 9504, 6336); @@ -1427,14 +1468,16 @@ mod tests { #[test] fn refuses_unvalidated_models_even_with_a_valid_table() { assert!(distortion("ILCE-7RM4A", &TELE, AREA, AREA).is_some()); - for model in ["", "ILCE-7M3", "ILCE-7M4", "ILCE-7RM4", "ILCE-7RM5", "ILCE-9M2", "DSC-RX100M3", "ILCE-7RM4A unknown", "ILCE-7CR"] { + assert!(distortion("ILCE-7M3", &TELE, AREA, AREA).is_some()); + for model in ["", "ILCE-7M4", "ILCE-7RM4", "ILCE-7RM5", "ILCE-9M2", "DSC-RX100M3", "ILCE-7RM4A unknown", "ILCE-7CR", "ILCE-7M3 ", "ILCE-7SM3"] + { assert!(distortion(model, &TELE, AREA, AREA).is_none(), "{model}"); } } #[test] fn header_and_full_decode_preserve_correction_through_dng() { - for (model, expected) in [("ILCE-7RM4A", 1), ("ILCE-7RM4", 0), ("ILCE-7M3", 0), ("", 0)] { + for (model, expected) in [("ILCE-7RM4A", 1), ("ILCE-7RM4", 0), ("ILCE-7M3", 1), ("ILCE-7M4", 0), ("", 0)] { for (height, expected) in [(20, expected), (16, 0)] { for order in [ByteOrder::Little, ByteOrder::Big] { let mut raw = IfdBuilder::new(); diff --git a/crates/raw/tests/it/corpus.rs b/crates/raw/tests/it/corpus.rs index 7f7074f911..62a14faf9f 100644 --- a/crates/raw/tests/it/corpus.rs +++ b/crates/raw/tests/it/corpus.rs @@ -1069,8 +1069,8 @@ fn sony_embedded_distortion_is_limited_to_validated_models() { for (name, model, expected) in [ ("arw-sony-a7rm4a-compressed.arw", "ILCE-7RM4A", 1), ("arw-sony-a9m2-compressed.arw", "ILCE-9M2", 0), - ("arw-sony-a7m3-compressed.arw", "ILCE-7M3", 0), - ("arw-sony-a7m3-uncompressed.arw", "ILCE-7M3", 0), + ("arw-sony-a7m3-compressed.arw", "ILCE-7M3", 1), + ("arw-sony-a7m3-uncompressed.arw", "ILCE-7M3", 1), ("arw-sony-a7m4-14bit.arw", "ILCE-7M4", 0), ("arw-sony-a7m4-lossless-l.arw", "ILCE-7M4", 0), ("arw-sony-a7m4-lossless-m.arw", "ILCE-7M4", 0), @@ -1227,7 +1227,7 @@ fn a_sony_fixture_that_differs_from_its_pin_fails() { /// Sony's lens-correction records (`lightcraft_raw::lens_metadata`, `crates/raw/src/vendor/arw_lens.rs`) on the public /// samples: shading, CA and distortion decoded from the raw IFD with their settings, equal to their `SR2SubIFD` copies, -/// the same from header and full decode, and nothing applied beyond the validated ILCE-7RM4A distortion. Expected +/// the same from header and full decode, and nothing applied beyond the validated ILCE-7RM4A and ILCE-7M3 distortion. Expected /// tables and settings: what ExifTool, run as a black-box tool, prints for these files. Every file is checked against /// its pinned SHA-256 before it is decoded; absent files are skipped. #[test] @@ -1351,13 +1351,13 @@ fn sony_lens_records_of_the_public_samples() { eprintln!("{name}: shading, CA and distortion decoded; distortion {status:?}"); checked += 1; } - // every Sony corpus file: only the ILCE-7RM4A's distortion is ever applied, and an applied status is an opcode + // every Sony corpus file: only the ILCE-7RM4A's and ILCE-7M3's distortion is ever applied, and an applied status is an opcode for (name, _) in SONY_SAMPLES { let Some(bytes) = pinned_sony(&root, name) else { continue }; let info = probe_info(&bytes).unwrap_or_else(|e| panic!("{name}: {e}")); let Some(s) = &info.lens_metadata.sony else { continue }; let applied = s.distortion.status == SonyLensStatus::Applied; - assert_eq!(applied, info.metadata.model.as_deref() == Some("ILCE-7RM4A"), "{name}"); + assert_eq!(applied, matches!(info.metadata.model.as_deref(), Some("ILCE-7RM4A" | "ILCE-7M3")), "{name}"); assert_eq!(info.opcodes.list3.len(), usize::from(applied), "{name}"); assert!(![s.shading.status, s.chromatic_aberration.status].contains(&SonyLensStatus::Applied), "{name}"); checked += 1; diff --git a/docs/gaps.md b/docs/gaps.md index 42d9d437b6..b1d4679f4f 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -22,7 +22,7 @@ Hours are Opus 5.5 agent wall-clock hours (see [target-app-parity.md](target-app | 5 B | **AI masks: Subject, Sky, People, Background, Landscape** are heuristics; Object / Describe run SAM 3 but its download has no mirror configured | `crates/segment`; ai-masks.md; MASK rows 67% | masking is how most Lightroom users edit today | 40–70 + model decision | [target-app-parity](target-app-parity.md#ai) | | 6 B | **Compressed ORF** (every Olympus / OM body since ~2008) | measured: 66 of 78 Olympus / OM models preview-only; `orf.rs` refuses; OM bucket 3% of photographers, 3% of them fully work | +2.6 points; best gain per hour of any raw gap (~0.2) | 8–15 | [raw-parity](raw-parity.md#compressed-orf-a-clean-room-starting-point) (the two Olympus patents that describe the method) | | 7 | **Nikon High Efficiency NEF** (HE / HE★ on Z 8, Z 9, Z 6III, Z f, Z50II, Z5II when set to HE) | named and preview only (#193); needs a pure-Rust JPEG XS decoder | +1.1 points (only shots taken in HE; lossless works on the same bodies); low gain per hour (~0.04), so not a beta blocker | 20–40 | [raw-parity](raw-parity.md) | -| 8 B | **No lens profile database** (LR-EDIT-OPTICS-PROFILE): only DNG opcodes, RW2 in-file distortion, Sony ILCE-7RM4A, full-frame ILCE-7CR distortion (Auto only) and ILCE-7RM6 distortion (`docs/sony-ilce7rm6-lens-corrections.md`) and Nikon Z 6II / Z 8 / Z f distortion (`docs/nikon-lens-corrections.md`) | Adobe ships 3,635 lens profiles; we never use them | distortion / vignetting correction is a default step for many | 40–80 + calibration shots | [raw-parity](raw-parity.md#lens-profiles) | +| 8 B | **No lens profile database** (LR-EDIT-OPTICS-PROFILE): only DNG opcodes, RW2 in-file distortion, Sony ILCE-7RM4A, ILCE-7M3, full-frame ILCE-7CR distortion (Auto only) and ILCE-7RM6 distortion (`docs/sony-ilce7rm6-lens-corrections.md`) and Nikon Z 6II / Z 8 / Z f distortion (`docs/nikon-lens-corrections.md`) | Adobe ships 3,635 lens profiles; we never use them | distortion / vignetting correction is a default step for many | 40–80 + calibration shots | [raw-parity](raw-parity.md#lens-profiles) | | 9 B | **Tests don't run in CI on macOS or Windows**: Linux x64 runs `cargo xtask ci` on every PR and push to main (`.github/workflows/ci.yml`) and FreeBSD runs `cargo test`; the others build packages | `.github/workflows/` (ci, freebsd, packaging-lint, release, windows-arm64); the linux-riscv64 release job added in 0.6.0 (#844) runs only `lightcraft-cli --version` under QEMU; macOS / Windows jobs: #1148 | regressions in `cfg(target_os)` code reach macOS and Windows users | 6–12 | [target-app-parity](target-app-parity.md#by-dimension) | | 10 B | **Per-model verification thin, plus cheap per-body decode fixes**: 551 of 1,446 Adobe models verified (38%), ~85% of photographers on a verified body; Nikon 5 bodies verified, Canon 34, Leica 14 (the Q3 by a recorded check of 38 private DNGs). Cheap fixes: Z 6II packed 14-bit NEF (#769, PR #671), Sony A100 regression (#702); F828 SRF now discovered and imported as preview-only, sensor decoding still missing, DSLR-A200…A390 / A850 12-bit ARW (#535) | measured: [raw/coverage.tsv](raw/coverage.tsv) | per-body bugs (levels, crops, WB) surface only from users | 25–45 | [raw-parity](raw-parity.md#by-maker-measured) | | 11 | **Enhance: Raw Details, Super Resolution; AI denoise without shippable weights or X-Trans** | ENH rows 0%; denoise Bayer only, RawNIND weights GPL and opt-in | Fujifilm users get no AI denoise; nobody gets it out of the box | 30–60 + model decision | [raw-parity](raw-parity.md#demosaic-levels-crops-and-lens-metadata) | diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 90cfc248f6..b644760449 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -212,7 +212,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts). | LR-EDIT-DETAIL-SUPERRES | Super resolution | P2 | ⬜ | | | | LR-EDIT-DETAIL-AISHARPEN | AI sharpen | OOS | 🚫 | | | | LR-EDIT-OPTICS-CA | Remove chromatic aberration | P1 | ✅ | `crates/ui-egui/src/panels/edit.rs` (checkbox), `ctl:optics.caRed`, `ctl:optics.caBlue` | | -| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; no settings switch or bundled database yet | +| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, ILCE-7M3 (FE 16–35 F4 ZA, FE 28–70, FE 50 F1.8; 27 shots within 0.06–0.44 px median of the camera JPEG at 6000 px, from up to 26 px uncorrected), and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; no settings switch or bundled database yet | ) | LR-EDIT-OPTICS-DEFRINGE | Defringe | P1 | ✅ | `ctl:optics.defringe*` | no fringe eyedropper | | LR-EDIT-OPTICS-MANUAL | Manual distortion / vignetting | P1 | ✅ | `ctl:optics.distortion`, `ctl:optics.vignetting`, `ctl:optics.vignettingMidpoint` | | diff --git a/docs/raw-parity.md b/docs/raw-parity.md index bd17a3d32b..2589a29176 100644 --- a/docs/raw-parity.md +++ b/docs/raw-parity.md @@ -243,7 +243,7 @@ essentials user (the default look is what they keep). | Item | Adobe | LightCraft | |---|---|---| | Profile database | 3,635 profile files, 56 makers, auto-selected | **0** (Adobe LCPs never used; no database of our own) | -| In-file corrections | ✅ | DNG opcodes (WarpRectilinear, FixVignetteRadial, GainMap…) applied; Panasonic / Leica RW2 distortion (79 photos, 13 bodies, 11 lenses); Sony tag 0x7037 distortion for the ILCE-7RM4A (2 lenses), full-frame Bayer 3:2 ILCE-7CR files with correction Auto or forced Auto (3 lenses; see sony-lens-corrections.md for residuals and limits) and the ILCE-7RM6 (3 lenses, 54 photos; `sony-ilce7rm6-lens-corrections.md`); Nikon NikonNEFInfo distortion (tag `0xc7d5`, about the sensor-shifted optical centre) for the Z 6II and Z f: F-mount lenses on the FTZ within 0.2 px of the camera JPEG, Z lenses within 0.5–1.5 px except the 24-70's wide end (7 px median; `docs/nikon-lens-corrections.md`) | +| In-file corrections | ✅ | DNG opcodes (WarpRectilinear, FixVignetteRadial, GainMap…) applied; Panasonic / Leica RW2 distortion (79 photos, 13 bodies, 11 lenses); Sony tag 0x7037 distortion for the ILCE-7RM4A (2 lenses) and ILCE-7M3 (3 lenses), full-frame Bayer 3:2 ILCE-7CR files with correction Auto or forced Auto (3 lenses; see sony-lens-corrections.md for residuals and limits) and the ILCE-7RM6 (3 lenses, 54 photos; `sony-ilce7rm6-lens-corrections.md`); Nikon NikonNEFInfo distortion (tag `0xc7d5`, about the sensor-shifted optical centre) for the Z 6II and Z f: F-mount lenses on the FTZ within 0.2 px of the camera JPEG, Z lenses within 0.5–1.5 px except the 24-70's wide end (7 px median; `docs/nikon-lens-corrections.md`) | | Vignetting / lateral CA from maker notes | ✅ | ⬜ | | Manual distortion, vignetting, CA, defringe | ✅ | ✅ | diff --git a/docs/sony-lens-corrections.md b/docs/sony-lens-corrections.md index 0252151cde..8b91d8395b 100644 --- a/docs/sony-lens-corrections.md +++ b/docs/sony-lens-corrections.md @@ -7,9 +7,10 @@ convention; that page's *Two conventions* section has the comparison and what wo Sony ARWs record three lens corrections per shot in the raw image IFD: shading (vignetting), lateral chromatic aberration (CA) and distortion, each as a camera setting and a radial table, with copies in the encrypted -`SR2SubIFD`. LightCraft **decodes all three** wherever a file carries them and applies distortion here on two validated -bodies: **ILCE-7RM4A (A7R IVA)** and **ILCE-7CR (A7CR)**. The A7R IVA keeps its existing Bayer 3:2 conversion, -validated with the FE 24-105mm F4 G OSS and FE 200-600mm F5.6-6.3 G OSS. The A7CR uses a separate conversion, +`SR2SubIFD`. LightCraft **decodes all three** wherever a file carries them and applies distortion here on three validated +bodies: **ILCE-7RM4A (A7R IVA)**, **ILCE-7M3 (A7 III)** and **ILCE-7CR (A7CR)**. The A7R IVA keeps its existing Bayer 3:2 conversion, +validated with the FE 24-105mm F4 G OSS and FE 200-600mm F5.6-6.3 G OSS; the A7 III uses the same conversion, validated +with the FE 16-35mm F4 ZA OSS, FE 28-70mm F3.5-5.6 OSS and FE 50mm F1.8 ([ILCE-7M3 validation](#ilce-7m3-validation)). The A7CR uses a separate conversion, limited to full-frame Bayer 3:2 files with distortion Auto or forced Auto. Apart from the separate ILCE-7RM6 converter, other models and shading and CA on every model are decoded only: no warp, vignetting or CA correction is added for them. The decoded record stays with the image (`RawInfo::lens_metadata` / `RawImage::lens_metadata`, types in `crates/raw/src/lens_metadata.rs`, decoder in @@ -289,9 +290,50 @@ decode or fail without a panic. `tests/sr2_heap.rs` measures the peak heap of de `SR2Private` directory holds eight fields aliasing the whole file: 37 kB, against 134 MB when those fields were decoded. No private photographs or reference TIFFs are committed. +## ILCE-7M3 validation + +Checked on 2026-10-12 against the camera's own JPEG of the same exposure, which is an independent renderer +that applied the correction (RAW+JPEG, Lens Comp. ▸ Distortion Comp.: Auto). 27 RAW+JPEG pairs from one +ILCE-7M3: a checkerboard filling the frame on a TV (handheld) and on a desktop monitor (tripod), compressed and +uncompressed raws, apertures from wide open to f/8, plus one textured wall. Tables covered strong barrel (16 mm, +−690 at the corner), strong pincushion (70 mm, +1161) and the sign change between them. + +Method (`tools/sony_distortion_check.py`): LightCraft renders each ARW at full size with the profile correction +on and off; sub-pixel board corners (Hessian saddle points refined with `cornerSubPix`, kept only where the four +quadrants alternate, labelled into rows and columns) are matched with the JPEG's by grid position, with the +match fixed by the central corners before any fit, because a board is periodic. A similarity (zoom, rotation, +shift) is fitted, as the camera fills its frame with its own zoom; no lens coefficient is fitted. The textured wall +uses SIFT matches instead. The script was checked on synthetic boards with a known radial distortion. + +| Lens / focal length | Shots | Corrected vs JPEG: median / p95 (px at 6000) | Uncorrected median | Corner (r > 0.9) | Camera / LightCraft correction | +|---|---:|---|---:|---:|---:| +| FE 16–35 F4 ZA / 16 mm | 10 | 0.33–0.38 / 1.01–1.15 | 16.7–17.0 | 1.40–1.56 px | 1.02 | +| FE 16–35 F4 ZA / 20 mm | 4 | 0.16–0.24 / 0.34–0.50 | 3.9–4.4 | 0.15–0.24 px | 0.98 | +| FE 16–35 F4 ZA / 35 mm | 2 | 0.18–0.21 / 0.39–0.44 | 11.0–11.1 | 0.18–0.26 px | 1.01–1.02 | +| FE 28–70 / 28 mm | 2 | 0.24–0.25 / 0.59–0.64 | 13.2–13.4 | 0.92 px | 1.02 | +| FE 28–70 / 33 mm | 1 | 0.07 / 0.16 | 0.8 | 0.07 px | 1.00 | +| FE 28–70 / 50 mm | 1 | 0.30 / 0.69 | 20.8 | 1.24 px | 1.01 | +| FE 28–70 / 70 mm | 1 | 0.44 / 0.93 | 26.4 | 1.49 px | 1.01 | +| FE 50 F1.8 / 50 mm | 4 | 0.06–0.12 / 0.15–0.31 | 2.8–3.0 | 0.08–0.14 px | 1.00–1.01 | +| Wall, 16 mm (SIFT) | 1 | 0.37 / 1.06 | 5.6 | 1.22 px | — | + +Residuals are in pixels of the 6000×4000 JPEG; at the 2376 px exports used above for the ILCE-7RM4A, the +board medians are 0.02–0.17 px and the p95 at most 0.46 px. The last column fits JPEG ≈ uncorrected + +α·(corrected − uncorrected) radially: LightCraft applies the camera's amount of correction within 2 %, slightly +less for strong barrel and slightly more at 20 mm, which is what remains at the 16 mm corners (about 1.5 px of a +35 px correction). Rows and columns of corrected boards are as straight as in the JPEG in every shot (largest +bow 0.8–7.8 px in both, from the screens), against 7–55 px uncorrected. Compressed and uncompressed raws, +the TV and the monitor, and wide open and f/8 agree. Two 16 mm board shots whose board stopped short of the +corners (reach 0.8 of the half diagonal) gave 0.85 px medians and are left out of the table. + +On this body Sony's crop tags and the default crop agree (origin 12, 12, 6000×4000 of 6048×4024), so the +warp sits on the image centre; fitted against the camera, the centre is within 0.3 px. Unit tests keep the warp from +three of these tables within 0.0008 of the half diagonal (2.9 px at 6000 px) of the camera's fitted geometry. +The photographs stay with their owner; none is committed. + ## Camera coverage and regression boundary -The model check is exact: `ILCE-7RM4` (A7R IV without the A suffix), `ILCE-7M3`, unknown/missing +The model check is exact: `ILCE-7RM4` (A7R IV without the A suffix), unknown/missing model names, and every other model remain unsupported by this distortion decoder. Matching tag numbers and table lengths do not establish the scale, interpolation or crop normalization on another body. Support can expand after independent geometry validation; do not add model-name prefix matching. @@ -309,7 +351,7 @@ fail on any absent pinned file instead. |---|---|---|---| | ILCE-7RM4A | Compressed (4822) | All three tables; 257 / 1 / 17 | One distortion warp | | ILCE-9M2 | Compressed (3989) | All three; 257 / 1 / 0 | None | -| ILCE-7M3 | Compressed, uncompressed | All three; 257 / 1 / 0 | None | +| ILCE-7M3 | Compressed, uncompressed | All three; 257 / 1 / 0 | One distortion warp | | ILCE-7M4 | 14-bit, lossless L/M/S | L/M/S: all three, 272 / 1 / 17; the 14-bit file (APS-C framing, 4608 × 3072): settings only, its 11-sample tables are not interpreted | None | | ILCE-7RM2 | 12-bit uncompressed | All three; 256 / 0 / 1 | None | | DSC-RX100 | One | Shading, from the `SR2SubIFD` (the raw IFD has no tables); CA and distortion in the 11-sample layout | None | @@ -324,8 +366,8 @@ fail on any absent pinned file instead. These are **model-boundary regression checks, not new geometric validation of those models**. `sony_lens_records_of_the_public_samples` also checks the exact tables, settings, provenance and framing evidence of the three A7CR codings and the A7R IVA sample, and that across the Sony corpus only the A7R IVA's distortion is ever -applied. In particular, -refusing the A7 III tables resolves the unverified corner warp identified in review. The lossless M/S +applied, besides the A7 III's. The A7 III tables were refused at first because their corner warp was unverified; +the [ILCE-7M3 validation](#ilce-7m3-validation) measured it against the camera's own JPEGs and enabled the body. The lossless M/S files are linear YCbCr and remain excluded independently of the model check. Header and full decode must agree for every sample. Synthetic tests also verify both TIFF byte orders, exact model matching, that the warp is centred on the standard default crop while the image keeps Sony's crop, and that DNG export preserves an accepted warp without adding one to rejected diff --git a/docs/whats-new.md b/docs/whats-new.md index c6c2592003..b794069769 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -1,5 +1,15 @@ # What's new in LightCraft +## 2026-10-26 + +### Lens distortion correction for the Sony A7 III +- Raws from the Sony A7 III (ILCE-7M3) now get the camera's own distortion correction, read from the file, like + the A7R IVA's: straight lines stay straight at wide angles (the FE 16–35 F4 ZA at 16 mm shifts detail near the + corners by up to about 40 px on a 24 MP photo) and at long ones (the FE 28–70 at 70 mm). It lines up with the + camera's JPEG to well under a pixel. Optics ▸ Enable Lens Corrections turns it off and Profile Distortion scales + it; photos imported before this pick it up with Photo ▸ Reload from Disk. Vignetting and chromatic aberration + from Sony files are not applied yet. + ## 2026-10-24 ### Relative-date filters and smart albums follow the clock diff --git a/scripts/sony_distortion_check.py b/scripts/sony_distortion_check.py index ee81a118cd..9b740b17d9 100755 --- a/scripts/sony_distortion_check.py +++ b/scripts/sony_distortion_check.py @@ -69,24 +69,67 @@ def detect_corners(gray): if len(pts) == 0: return pts.reshape(-1, 2) crit = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 50, 0.005) - pts = cv2.cornerSubPix(gray, pts, (11, 11), (-1, -1), crit) - return pts.reshape(-1, 2).astype(np.float64) + pts = cv2.cornerSubPix(gray, pts, (11, 11), (-1, -1), crit).reshape(-1, 2).astype(np.float64) + # Two peaks near one corner can refine to the same point; keep one of each such pair. + drop = {j for _, j in cKDTree(pts).query_pairs(3.0)} + pts = pts[[i for i in range(len(pts)) if i not in drop]] + return pts[[is_x_corner(g, p) for p in pts]] + + +def is_x_corner(g, p, r=12): + """A board corner has alternating quadrants: equal diagonals, contrasting neighbours. + A point on a straight edge (screen texture can make one look like a saddle) does not.""" + h, w = g.shape + x, y = int(round(p[0])), int(round(p[1])) + if not (r <= x < w - r and r <= y < h - r): + return False + a, c = g[y + r, x + r], g[y - r, x - r] + b, d = g[y - r, x + r], g[y + r, x - r] + contrast = abs((a + c) - (b + d)) / 2 + return contrast > 15 and contrast > 2 * max(abs(a - c), abs(b - d)) def label_lattice(pts, centre): - """Integer (i, j) grid labels for corners connected to the one nearest the centre. - i grows along +x, j along +y; local lattice vectors follow the distortion gradually.""" + """Integer (i, j) grid labels for corners connected to one near the centre. + i grows along +x, j along +y; local lattice vectors follow the distortion gradually. + The starting vectors are the consensus of many central corners, so a stray corner (screen + moiré) next to the start can't derail it; a few starts are tried, the largest result kept.""" if len(pts) < 20: return {} tree = cKDTree(pts) - start = int(tree.query(centre)[1]) - d, idx = tree.query(pts[start], k=5) - offsets = pts[idx[1:]] - pts[start] - spacing = float(np.median(d[1:])) - a = offsets[np.argmax(offsets @ np.array([1.0, 0.0]))] - b = offsets[np.argmax(offsets @ np.array([0.0, 1.0]))] - if abs(np.dot(a, b)) > 0.5 * np.linalg.norm(a) * np.linalg.norm(b): + vectors = lattice_vectors(pts, tree, centre) + if vectors is None: return {} + best = {} + for start in tree.query(centre, k=min(9, len(pts)))[1]: + labels = grow_lattice(pts, tree, int(start), *vectors) + if len(labels) > len(best): + best = labels + if len(best) > 0.8 * len(pts): + break + return best if len(best) > 50 else {} + + +def lattice_vectors(pts, tree, centre): + """Median neighbour offsets near +x and +y among the corners nearest the centre.""" + k = min(9, len(pts)) + offsets = [] + for i in tree.query(centre, k=min(60, len(pts)))[1]: + nn = tree.query(pts[i], k=k)[1] + offsets.extend(pts[nn[1:]] - pts[i]) + offsets = np.array(offsets) + length = np.linalg.norm(offsets, axis=1) + spacing = np.median(np.sort(length.reshape(-1, k - 1), axis=1)[:, :4]) + offsets = offsets[np.abs(length - spacing) < 0.25 * spacing] + angle = np.arctan2(offsets[:, 1], offsets[:, 0]) + near_x = np.abs(np.angle(np.exp(1j * angle))) < np.pi / 4 + near_y = np.abs(np.angle(np.exp(1j * (angle - np.pi / 2)))) < np.pi / 4 + if near_x.sum() < 10 or near_y.sum() < 10: + return None + return np.median(offsets[near_x], axis=0), np.median(offsets[near_y], axis=0) + + +def grow_lattice(pts, tree, start, a, b): labels = {start: (0, 0)} taken = {(0, 0): start} vec = {start: (a, b)} @@ -109,7 +152,7 @@ def label_lattice(pts, centre): taken[key] = q vec[q] = (na, nb) queue.append(q) - return labels if len(labels) > 50 else {} + return labels def board(path): @@ -305,8 +348,9 @@ def measure(cli, work, arw): s, d = match_boards(grid, (size[0] * k, size[1] * k), jgrid, jsize) else: s, d = sift_pairs(path, jpg) - if s is not None: - out["vs_jpeg_" + tag] = agreement(s, d, jsize) + a = agreement(s, d, jsize) if s is not None else None + if a: + out["vs_jpeg_" + tag] = a if "vs_jpeg_on" in out and "vs_jpeg_off" in out and out["kind"] == "board": out["fraction"] = correction_fraction(out["vs_jpeg_on"], out["vs_jpeg_off"], jsize) for tag in ("on", "off"): From 03eda4840218ee9f5a1d12e9077387c6ed1afa62 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 00:01:10 +0000 Subject: [PATCH 16/51] LR-EDIT-OPTICS-PROFILE: keep the distortion check with the other Python tools Moves it to tools/ with a pinned requirements file, as tools/spectral_camera_table.py does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015gRtdWF1E9358FfzhzYqCS --- tools/requirements-sony-distortion-check.txt | 6 ++++++ {scripts => tools}/sony_distortion_check.py | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 tools/requirements-sony-distortion-check.txt rename {scripts => tools}/sony_distortion_check.py (97%) diff --git a/tools/requirements-sony-distortion-check.txt b/tools/requirements-sony-distortion-check.txt new file mode 100644 index 0000000000..56ab4f9e23 --- /dev/null +++ b/tools/requirements-sony-distortion-check.txt @@ -0,0 +1,6 @@ +# Python packages for tools/sony_distortion_check.py (docs/sony-lens-corrections.md). +# python3 -m venv .venv-distortion && .venv-distortion/bin/pip install -r tools/requirements-sony-distortion-check.txt +numpy==2.5.3 +scipy==1.18.1 +opencv-python-headless==5.0.0.93 +Pillow==12.3.0 diff --git a/scripts/sony_distortion_check.py b/tools/sony_distortion_check.py similarity index 97% rename from scripts/sony_distortion_check.py rename to tools/sony_distortion_check.py index 9b740b17d9..0cdc6f003a 100755 --- a/scripts/sony_distortion_check.py +++ b/tools/sony_distortion_check.py @@ -1,7 +1,9 @@ #!/usr/bin/env python3 """Compare LightCraft's Sony embedded distortion correction with the camera's own JPEG. - sony_distortion_check.py --cli target/release/lightcraft-cli --work WORKDIR PHOTO_DIR [PHOTO_DIR…] + python3 -m venv .venv-distortion && .venv-distortion/bin/pip install -r tools/requirements-sony-distortion-check.txt + .venv-distortion/bin/python tools/sony_distortion_check.py --cli target/release/lightcraft-cli \ + --work WORKDIR [--json results.json] PHOTO_DIR [PHOTO_DIR…] Every ARW with a JPG of the same name is rendered by LightCraft twice, embedded correction on and off, and measured against the camera JPEG (shot with Lens Comp. Distortion on Auto): @@ -12,8 +14,8 @@ and off, and measured against the camera JPEG (shot with Lens Comp. Distortion o Checkerboard shots use the board's corners (sub-pixel), other shots SIFT features (agreement only). Offsets are in pixels of the camera JPEG (6000 px wide on the A7 III); the "@2376" -columns rescale them to the 2376 px exports docs/sony-lens-corrections.md reports. -Needs numpy, scipy and opencv-python-headless. +columns rescale them to the 2376 px exports docs/sony-lens-corrections.md reports (method and results +there, under "ILCE-7M3 validation"). """ import argparse From 453a0f2a3b37240c4304667ac25e7b11515eabc3 Mon Sep 17 00:00:00 2001 From: Janick Martinez Esturo Date: Tue, 27 Oct 2026 11:11:28 +0100 Subject: [PATCH 17/51] LR-EDIT-OPTICS-PROFILE: address review on the ILCE-7M3 distortion PR - docs/parity-checklist.md: the row's closing summary also lists the ILCE-7M3. - docs/sony-lens-corrections.md, parity-checklist.md: state the validation set as the 26 tabled shots plus the two left out, instead of a total that did not add up. - docs/whats-new.md: the sub-pixel claim is the median; corners reach about 1.5 px. - tools/sony_distortion_check.py: re-render when the CLI or ARW is newer than the output, report the CLI's stderr on failure, keep renders of same-named files from different folders apart, and docstrings for every function. --- docs/parity-checklist.md | 2 +- docs/sony-lens-corrections.md | 4 ++-- docs/whats-new.md | 4 ++-- tools/sony_distortion_check.py | 27 +++++++++++++++++++++++---- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index b644760449..8438a9b346 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -212,7 +212,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts). | LR-EDIT-DETAIL-SUPERRES | Super resolution | P2 | ⬜ | | | | LR-EDIT-DETAIL-AISHARPEN | AI sharpen | OOS | 🚫 | | | | LR-EDIT-OPTICS-CA | Remove chromatic aberration | P1 | ✅ | `crates/ui-egui/src/panels/edit.rs` (checkbox), `ctl:optics.caRed`, `ctl:optics.caBlue` | | -| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, ILCE-7M3 (FE 16–35 F4 ZA, FE 28–70, FE 50 F1.8; 27 shots within 0.06–0.44 px median of the camera JPEG at 6000 px, from up to 26 px uncorrected), and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; no settings switch or bundled database yet | +| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, ILCE-7M3 (FE 16–35 F4 ZA, FE 28–70, FE 50 F1.8; 26 shots within 0.06–0.44 px median of the camera JPEG at 6000 px, from up to 26 px uncorrected), and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7M3, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; no settings switch or bundled database yet | ) | LR-EDIT-OPTICS-DEFRINGE | Defringe | P1 | ✅ | `ctl:optics.defringe*` | no fringe eyedropper | | LR-EDIT-OPTICS-MANUAL | Manual distortion / vignetting | P1 | ✅ | `ctl:optics.distortion`, `ctl:optics.vignetting`, `ctl:optics.vignettingMidpoint` | | diff --git a/docs/sony-lens-corrections.md b/docs/sony-lens-corrections.md index 8b91d8395b..87f141a5d2 100644 --- a/docs/sony-lens-corrections.md +++ b/docs/sony-lens-corrections.md @@ -293,8 +293,8 @@ decoded. No private photographs or reference TIFFs are committed. ## ILCE-7M3 validation Checked on 2026-10-12 against the camera's own JPEG of the same exposure, which is an independent renderer -that applied the correction (RAW+JPEG, Lens Comp. ▸ Distortion Comp.: Auto). 27 RAW+JPEG pairs from one -ILCE-7M3: a checkerboard filling the frame on a TV (handheld) and on a desktop monitor (tripod), compressed and +that applied the correction (RAW+JPEG, Lens Comp. ▸ Distortion Comp.: Auto). RAW+JPEG pairs from one +ILCE-7M3 (26 in the table below, plus two left out as explained under it): a checkerboard filling the frame on a TV (handheld) and on a desktop monitor (tripod), compressed and uncompressed raws, apertures from wide open to f/8, plus one textured wall. Tables covered strong barrel (16 mm, −690 at the corner), strong pincushion (70 mm, +1161) and the sign change between them. diff --git a/docs/whats-new.md b/docs/whats-new.md index b794069769..fb44b58441 100644 --- a/docs/whats-new.md +++ b/docs/whats-new.md @@ -5,8 +5,8 @@ ### Lens distortion correction for the Sony A7 III - Raws from the Sony A7 III (ILCE-7M3) now get the camera's own distortion correction, read from the file, like the A7R IVA's: straight lines stay straight at wide angles (the FE 16–35 F4 ZA at 16 mm shifts detail near the - corners by up to about 40 px on a 24 MP photo) and at long ones (the FE 28–70 at 70 mm). It lines up with the - camera's JPEG to well under a pixel. Optics ▸ Enable Lens Corrections turns it off and Profile Distortion scales + corners by up to about 40 px on a 24 MP photo) and at long ones (the FE 28–70 at 70 mm). Measured against the + camera's JPEG, the median error is well under a pixel (up to about 1.5 px in the 16 mm corners). Optics ▸ Enable Lens Corrections turns it off and Profile Distortion scales it; photos imported before this pick it up with Photo ▸ Reload from Disk. Vignetting and chromatic aberration from Sony files are not applied yet. diff --git a/tools/sony_distortion_check.py b/tools/sony_distortion_check.py index 0cdc6f003a..ad76c12676 100755 --- a/tools/sony_distortion_check.py +++ b/tools/sony_distortion_check.py @@ -19,8 +19,10 @@ there, under "ILCE-7M3 validation"). """ import argparse +import hashlib import json import os +import shutil import subprocess import sys @@ -34,11 +36,17 @@ REPORT_WIDTH = 2376 def render(cli, arw, out, on): + """Render `arw` with the profile correction `on` or off to `out`, reusing an output newer than the CLI and ARW.""" settings = out + ".settings.json" with open(settings, "w") as f: json.dump({"optics": {"lens_profile": on, "profile_distortion": 100}}, f) - if not os.path.exists(out): - subprocess.run([cli, "render", arw, "-o", out, "--settings", settings], check=True, capture_output=True) + exe = shutil.which(cli) or cli + current = os.path.exists(out) and os.path.getmtime(out) >= max(os.path.getmtime(exe), os.path.getmtime(arw)) + if not current: + r = subprocess.run([exe, "render", arw, "-o", out, "--settings", settings], capture_output=True) + if r.returncode != 0: + err = r.stderr.decode(errors="replace").strip() + raise RuntimeError(f"render failed ({r.returncode}) for {arw}: {err[-500:]}") return out @@ -132,6 +140,7 @@ def lattice_vectors(pts, tree, centre): def grow_lattice(pts, tree, start, a, b): + """Label corners (row, column) by stepping lattice vectors `a` and `b` out from corner `start`.""" labels = {start: (0, 0)} taken = {(0, 0): start} vec = {start: (a, b)} @@ -158,6 +167,7 @@ def grow_lattice(pts, tree, start, a, b): def board(path): + """Checkerboard corners of image `path`, keyed by (row, column), in its full-size pixels, and its size.""" gray, back, size = load_gray(path) pts = detect_corners(gray) labels = label_lattice(pts, np.array(gray.shape[::-1]) / 2.0) @@ -236,6 +246,7 @@ def sift_pairs(src_path, dst_path): # ---------------------------------------------------------------- geometry def fit_similarity(s, d): + """Least-squares similarity (zoom, rotation, shift) mapping points `s` onto `d`, as complex (scale, offset).""" zs = s[:, 0] + 1j * s[:, 1] zd = d[:, 0] + 1j * d[:, 1] ms, md = zs.mean(), zd.mean() @@ -244,6 +255,7 @@ def fit_similarity(s, d): def apply(model, p): + """Points `p` mapped by a `fit_similarity` model.""" c, t = model z = c * (p[:, 0] + 1j * p[:, 1]) + t return np.stack([z.real, z.imag], 1) @@ -311,6 +323,7 @@ def correction_fraction(on, off, size): # ---------------------------------------------------------------- driver def jpeg_for(arw): + """The camera JPEG next to `arw` (same stem), or None.""" stem = os.path.splitext(arw)[0] for ext in (".JPG", ".jpg", ".JPEG", ".jpeg"): if os.path.exists(stem + ext): @@ -319,6 +332,7 @@ def jpeg_for(arw): def exif_summary(jpg): + """Lens, focal length and aperture of `jpg` from its Exif, for the report.""" from PIL import Image tags = Image.open(jpg).getexif().get_ifd(0x8769) lens = tags.get(0xA434, "?") @@ -328,12 +342,15 @@ def exif_summary(jpg): def measure(cli, work, arw): + """Render `arw` corrected and uncorrected and measure both against its camera JPEG (None without one).""" jpg = jpeg_for(arw) if not jpg: return None name = os.path.splitext(os.path.basename(arw))[0] - on = render(cli, arw, os.path.join(work, name + ".on.png"), True) - off = render(cli, arw, os.path.join(work, name + ".off.png"), False) + # cards repeat names (DSC00001.ARW): the path's hash keeps renders of different folders apart + uid = hashlib.sha1(os.path.abspath(arw).encode()).hexdigest()[:8] + on = render(cli, arw, os.path.join(work, f"{name}.{uid}.on.png"), True) + off = render(cli, arw, os.path.join(work, f"{name}.{uid}.off.png"), False) out = {"file": name, "shot": exif_summary(jpg)} jgrid, jsize = board(jpg) out["kind"] = "board" if jgrid else "scene" @@ -362,6 +379,7 @@ def measure(cli, work, arw): def show(o): + """Print one file's measurements.""" k = REPORT_WIDTH / o["jpeg_width"] print(f"\n{o['file']} {o['shot']} [{o['kind']}]") for tag, label in (("on", "LightCraft corrected"), ("off", "LightCraft uncorrected")): @@ -380,6 +398,7 @@ def show(o): def main(): + """Measure every ARW with a camera JPEG in the given folders and print (and optionally save) the results.""" ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("--cli", required=True) ap.add_argument("--work", required=True) From f267ddb504a74da2c28d7b7c75870162a483f431 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Wed, 28 Oct 2026 03:20:10 +1000 Subject: [PATCH 18/51] LR-EDIT-OPTICS-PROFILE: pinned profiles, whole-folder install, shared hosts (review, #3073) Render caches: a database lens profile is pinned to the photo. A decode never overrides the catalog's database profile (media::source::pinned_lens), and the profile is part of the render, thumbnail and variant cache keys (media::quick::lens_key; only database profiles with the correction on, so existing thumbnails keep their keys). When a download replaces the database's files (lens_profiles::replaced), the refresh also re-reads raws whose profile came from the database and raws that named no lens; an ordinary start does not. library.refreshLensProfiles takes {all: true}. Installation: files are staged beside the database folder, SOURCE.txt is written there last, and the staged folder replaces the old one as a whole; a failed swap puts the previous database back (and recover() does at the next start). The app's own folder is read only with its marker, and installed() checks every pinned file at its pinned size. Shared hosts: with_default_lens_database() only selects the existing folder; the permission to fetch is with_lens_auto_download() (desktop only), and the first-start decision is one function, lensfun_download::fetch_on_start. CLI render / merge and the headless MCP server select the folder; lens.downloadDatabase selects the folder it downloads into. Co-Authored-By: Claude Opus 5.5 --- apps/lightcraft-cli/src/main.rs | 25 ++- apps/lightcraft/src/session.rs | 2 +- crates/engine/src/cmd/lens_refresh.rs | 77 +++++++-- crates/engine/src/config.rs | 18 ++- crates/engine/src/lens_profiles.rs | 36 ++++- crates/engine/src/lensfun_download.rs | 204 ++++++++++++++++++++---- crates/engine/src/lib.rs | 3 +- crates/engine/src/media/quick.rs | 14 ++ crates/engine/src/media/session.rs | 20 ++- crates/engine/src/media/source.rs | 22 ++- crates/engine/src/tests/lens_refresh.rs | 128 ++++++++++++++- crates/mcp/src/headless.rs | 13 +- crates/ui-egui/src/lens_db.rs | 41 +++-- crates/ui-egui/src/state.rs | 5 +- crates/ui-egui/src/tests/lens_lookup.rs | 11 ++ docs/lens-profiles.md | 18 ++- 16 files changed, 546 insertions(+), 91 deletions(-) diff --git a/apps/lightcraft-cli/src/main.rs b/apps/lightcraft-cli/src/main.rs index e5980d469e..f4d7ed8ae4 100644 --- a/apps/lightcraft-cli/src/main.rs +++ b/apps/lightcraft-cli/src/main.rs @@ -416,7 +416,7 @@ fn merge(args: &[String]) -> Result<(), String> { } i += 1; } - let mut s = Session::new().with_fs().with_default_denoise_models().with_system_clock(); + let mut s = file_session(); let paths = expand_paths(&files); let r = s.execute("library.import", &json!({"paths": paths})).map_err(|e| e.to_string())?; let mut ids: Vec = r["imported"].as_array().map(|a| a.iter().filter_map(Value::as_u64).collect()).unwrap_or_default(); @@ -667,6 +667,12 @@ fn read_settings(p: &str) -> Result { } } +/// The session `render` and `merge` develop files in: with the lens database the desktop app downloaded, when it is +/// there (never fetched from here: scripts and tests stay offline). +fn file_session() -> Session { + Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_lens_database() +} + fn render(args: &[String]) -> Result<(), String> { let mut input = None; let mut output = None; @@ -703,7 +709,7 @@ fn render(args: &[String]) -> Result<(), String> { } let input = input.ok_or("render: missing input file")?; let output = output.ok_or("render: missing -o OUTPUT")?; - let mut s = Session::new().with_fs().with_default_denoise_models().with_system_clock(); + let mut s = file_session(); let abs = expand_paths(std::slice::from_ref(&input)); let r = s.execute("library.import", &json!({"paths": abs})).map_err(|e| e.to_string())?; let id = r["imported"][0].as_u64().ok_or_else(|| format!("{input}: not a readable photo"))?; @@ -823,9 +829,6 @@ fn snapshot(args: &[String]) -> Result<(), String> { .with_default_lens_database() .with_default_slide_templates(), }; - // a snapshot reads the lens database the desktop app downloaded, but never fetches it (scripts and tests - // stay offline) - session.lens_auto_download = false; // Apply compute policy before imports or the first scripted query can discover an adapter. session.execute("app.gpu", &json!({"enabled": false})).map_err(|e| e.to_string())?; if !files.is_empty() { @@ -1046,3 +1049,15 @@ fn denoise_serve(args: &[String]) -> Result<(), String> { fn denoise_serve(_: &[String]) -> Result<(), String> { Err("denoise-serve: this build has no denoise support (build with the `denoise` feature)".into()) } + +#[cfg(test)] +mod tests { + /// `render` and `merge` read the lens database the desktop app downloaded, as the desktop does (the same raw + /// gets the same profile everywhere), but never fetch it. + #[test] + fn render_reads_the_downloaded_lens_database() { + let s = super::file_session(); + assert_eq!(lightcraft_engine::lens_profiles::host_dir(), lightcraft_engine::lensfun_download::default_dir()); + assert!(!s.lens_auto_download, "selecting it is not permission to download it"); + } +} diff --git a/apps/lightcraft/src/session.rs b/apps/lightcraft/src/session.rs index 8128820a14..97e4e77fd6 100644 --- a/apps/lightcraft/src/session.rs +++ b/apps/lightcraft/src/session.rs @@ -256,7 +256,7 @@ pub(crate) fn open_session(in_memory: bool, launch: &Launch, seed_demo: bool) -> }; // lens profiles from the app's own lensfun copy, fetched by itself on first run (not for a throwaway session) if !in_memory { - s = s.with_default_lens_database(); + s = s.with_default_lens_database().with_lens_auto_download(); } s.face_models_dir = lightcraft_engine::config::default_face_models_dir(); s.slide_templates_dir = lightcraft_engine::config::config_dir().map(|d| d.join(lightcraft_engine::slide_template::DIR_NAME)); diff --git a/crates/engine/src/cmd/lens_refresh.rs b/crates/engine/src/cmd/lens_refresh.rs index ae8e52e1cd..35ba818b27 100644 --- a/crates/engine/src/cmd/lens_refresh.rs +++ b/crates/engine/src/cmd/lens_refresh.rs @@ -11,6 +11,7 @@ //! own bookkeeping (journaled, not an undo step). Agents and tests use `library.refreshLensProfiles`. use lightcraft_catalog::{Catalog, Op, Photo, PhotoId}; +use lightcraft_develop::LensSource; use serde_json::{Value, json}; use super::{CommandSpec, always, bad, cmd}; @@ -25,15 +26,26 @@ pub struct Missing { } /// Library raws with a file, no lens profile and a lens the database `db` knows by name (`ids`: only these; virtual -/// copies follow the photo they were copied from). No file-system calls. -pub fn missing(cat: &Catalog, db: &lightcraft_lensdb::LensDb, ids: Option<&[PhotoId]>) -> Vec { +/// copies follow the photo they were copied from). With `again` (the database's files were replaced, or an agent +/// asked for every photo), also the raws whose profile came from the database (the new data may give another one, or +/// none) and the raws whose file named no lens when they were imported (a newer LightCraft may read their lens). +/// No file-system calls. +pub fn missing(cat: &Catalog, db: &lightcraft_lensdb::LensDb, ids: Option<&[PhotoId]>, again: bool) -> Vec { // a photo and its virtual copies list the photo once, in library order let mut seen = std::collections::HashSet::new(); + let from_database = |p: &Photo| p.embedded_lens.is_some_and(|l| l.source == LensSource::Database); cat.photos() .filter(|p| ids.is_none_or(|ids| ids.contains(&p.id) || p.copy_of.is_some_and(|c| ids.contains(&c)))) .map(|p| p.copy_of.and_then(|c| cat.photo(c)).map_or(&**p, |orig| &**orig)) - .filter(|p| p.develops_raw() && p.embedded_lens.is_none() && !p.meta.lens.trim().is_empty()) - .filter(|p| crate::lens_profiles::profile_name(db, &p.meta.camera, &p.meta.lens).is_some()) + .filter(|p| p.develops_raw()) + .filter(|p| { + let lens = p.meta.lens.trim(); + match &p.embedded_lens { + None if lens.is_empty() => again, + None => crate::lens_profiles::profile_name(db, &p.meta.camera, lens).is_some(), + Some(_) => again && from_database(p), + } + }) .filter_map(|p| crate::cmd::missing::checked_path(p).map(|path| Missing { id: p.id, path: path.to_string() })) .filter(|m| seen.insert(m.id)) .collect() @@ -46,34 +58,46 @@ pub fn probe(probe: &FileProbe, list: Vec) -> Vec<(Missing, std::result } /// Apply [`probe`]'s results: each photo and its virtual copies get the lens data their file gives now, in one batch -/// that is journaled but isn't an undo step. Returns the photos that gained a profile. +/// that is journaled but isn't an undo step: a profile found (or a different one from a replaced database), a +/// database profile the database no longer gives removed, and the lens's name where the catalog had none. Returns +/// the photos that gained or changed a profile. pub fn apply(s: &mut Session, results: Vec<(Missing, std::result::Result)>) -> Result> { let mut ops = Vec::new(); let mut found = Vec::new(); + let mut changed = Vec::new(); for (item, info) in results { let Ok(info) = info else { continue }; - if info.embedded_lens.is_none() { - continue; - } // the photo moved or left the library while its file was read: the result is stale, for its copies too if s.catalog.photo(item.id).and_then(|p| crate::cmd::missing::checked_path(p)) != Some(item.path.as_str()) { continue; } let family: Vec = s.catalog.photos().filter(|p| p.id == item.id || p.copy_of == Some(item.id)).map(|p| p.id).collect(); for id in family { - if let Some(p) = s.catalog.photo(id) { + let Some(p) = s.catalog.photo(id) else { continue }; + // nothing found for a photo without a database profile: a file's own correction is Reload's business + let from_database = p.embedded_lens.is_some_and(|l| l.source == LensSource::Database); + if info.embedded_lens.is_some() || from_database { let mine = crate::cmd::convert::lens_ops(id, p, &info); if !mine.is_empty() { ops.extend(mine); - found.push(id); + changed.push(id); + if info.embedded_lens.is_some() { + found.push(id); + } } } + let named = info.meta.lens.trim(); + if p.meta.lens.trim().is_empty() && !named.is_empty() { + let mut meta = p.meta.clone(); + meta.lens = named.to_string(); + ops.push(Op::SetMeta { id, meta: Box::new(meta) }); + } } } if !ops.is_empty() { s.apply_system(Op::Batch { ops })?; } - for id in &found { + for id in &changed { s.media.forget(*id); } Ok(found) @@ -96,10 +120,15 @@ fn refresh(s: &mut Session, p: &Value) -> Result { Some(Value::Array(a)) => Some(a.iter().map(|v| v.as_u64().map(PhotoId).ok_or_else(|| bad(C, "ids are photo ids"))).collect::>()?), Some(_) => return Err(bad(C, "ids is a list of photo ids")), }; + let again = match p.get("all") { + None | Some(Value::Null) => false, + Some(Value::Bool(b)) => *b, + Some(_) => return Err(bad(C, "all is true or false")), + }; let Some(db) = crate::lens_profiles::database() else { return Ok(json!({"checked": 0, "found": [], "database": false})); }; - let listed = missing(&s.catalog, &db, ids.as_deref()); + let listed = missing(&s.catalog, &db, ids.as_deref(), again); let n = listed.len(); let found = apply(s, probe(&fp, listed))?; Ok(json!({"checked": n, "found": found.iter().map(|i| i.0).collect::>(), "database": true})) @@ -108,11 +137,27 @@ fn refresh(s: &mut Session, p: &Value) -> Result { fn download(s: &mut Session, _: &Value) -> Result { const C: &str = "lens.downloadDatabase"; let dir = crate::lensfun_download::default_dir().ok_or_else(|| bad(C, "this session has no data folder"))?; - let activity = s.activity.clone(); - s.lensfun.start(&dir, crate::lensfun_download::mirrors(), crate::lensfun_download::FILES, &activity).map_err(|e| bad(C, e))?; + start_download(s, &dir, crate::lensfun_download::mirrors(), crate::lensfun_download::FILES).map_err(|e| bad(C, e))?; Ok(json!({"started": true, "dir": dir.to_string_lossy()})) } +/// Fetch the database into `dir` and select it, so this session reads it once it is in place (a session that +/// selected no database, e.g. a bare headless one, would otherwise download it and go on without it; the user's +/// `LIGHTCRAFT_LENSFUN_DIR` still wins). Until the download is whole the folder has no marker and isn't read. +pub(crate) fn start_download( + s: &mut Session, + dir: &std::path::Path, + mirrors: Vec, + files: &'static [crate::lensfun_download::Pin], +) -> std::result::Result<(), String> { + let activity = s.activity.clone(); + s.lensfun.start(dir, mirrors, files, &activity)?; + if crate::lens_profiles::host_dir().as_deref() != Some(dir) { + crate::lens_profiles::use_dir(Some(dir.to_path_buf())); + } + Ok(()) +} + fn status(s: &mut Session, _: &Value) -> Result { use crate::lensfun_download::State; let state = match s.lensfun.state() { @@ -134,7 +179,7 @@ pub fn specs() -> Vec { "Look Up Lens Profiles Again", [], None, - "{ids?: [photo ids]} — library raws without a lens profile whose lens the lens database knows are read again and get their profile (the correction is switched on where the photo's settings are still as imported); the app does this by itself when the database arrives or changes and when Enable Lens Corrections is ticked; not an undo step → {checked, found: [ids], database: bool}", + "{ids?: [photo ids], all?: bool} — library raws without a lens profile whose lens the lens database knows are read again and get their profile (the correction is switched on where the photo's settings are still as imported); with all, also raws whose profile came from the database (it may give another, or none) and raws whose file named no lens at import; the app does this by itself when the database arrives (all: when its files were replaced) and when Enable Lens Corrections is ticked; not an undo step → {checked, found: [ids], database: bool}", always, refresh ), @@ -143,7 +188,7 @@ pub fn specs() -> Vec { "Download the Lens Database", [], None, - "{} — fetch lensfun's lens database (CC BY-SA 3.0, pinned to one commit, checked file by file) into the app's data folder in the background, with an activity row; the desktop app does this by itself on first run. LIGHTCRAFT_LENSFUN_URL lists other places to fetch the same files from → {started, dir}", + "{} — fetch lensfun's lens database (CC BY-SA 3.0, pinned to one commit, checked file by file) into the app's data folder in the background, with an activity row, and read lens profiles from it once it is in place (unless LIGHTCRAFT_LENSFUN_DIR names another folder); the desktop app does this by itself on first run. LIGHTCRAFT_LENSFUN_URL lists other places to fetch the same files from → {started, dir}", always, download ), diff --git a/crates/engine/src/config.rs b/crates/engine/src/config.rs index 3088b6f119..debcc3db4a 100644 --- a/crates/engine/src/config.rs +++ b/crates/engine/src/config.rs @@ -147,9 +147,21 @@ impl Session { } /// Read lens profiles from the app's own copy of the lens database ([`crate::lensfun_download::default_dir`], - /// unless `LIGHTCRAFT_LENSFUN_DIR` names another), and let the host fetch it by itself when it is missing. - pub fn with_default_lens_database(mut self) -> Self { - crate::lens_profiles::use_dir(crate::lensfun_download::default_dir()); + /// unless `LIGHTCRAFT_LENSFUN_DIR` names another) when it is there. Every host with a file system selects it + /// (desktop, CLI, headless MCP), so the same raw gets the same profile everywhere; it never fetches anything + /// ([`Self::with_lens_auto_download`] is that permission). + pub fn with_default_lens_database(self) -> Self { + let dir = crate::lensfun_download::default_dir(); + if let Some(d) = &dir { + crate::lensfun_download::recover(d); + } + crate::lens_profiles::use_dir(dir); + self + } + + /// Let the host fetch the lens database by itself when it is missing ([`crate::lensfun_download::fetch_on_start`] + /// decides when). Separate from selecting a database that is already there ([`Self::with_default_lens_database`]). + pub fn with_lens_auto_download(mut self) -> Self { self.lens_auto_download = true; self } diff --git a/crates/engine/src/lens_profiles.rs b/crates/engine/src/lens_profiles.rs index 96dd7eb0e7..61921c319e 100644 --- a/crates/engine/src/lens_profiles.rs +++ b/crates/engine/src/lens_profiles.rs @@ -23,6 +23,8 @@ static HOST_DIR: RwLock> = RwLock::new(None); static LOADED: RwLock>)>> = RwLock::new(None); /// Bumped whenever the database in use changes ([`reload`]): photos looked up before may now find a profile. static GENERATION: AtomicU64 = AtomicU64::new(0); +/// Bumped when the database's files were replaced ([`replaced`]): profiles found before may now be different. +static REPLACED: AtomicU64 = AtomicU64::new(0); /// Use `dir` (the app's downloaded lensfun data, [`crate::lensfun_download::default_dir`]) when /// `LIGHTCRAFT_LENSFUN_DIR` is not set. Hosts with a file system call it; tests and sessions that don't stay @@ -32,6 +34,24 @@ pub fn use_dir(dir: Option) { reload(); } +/// The database's files were replaced (a download put a new database in place): read it again, and photos whose +/// profile came from the old one are looked up again too ([`replacements`]). +pub fn replaced() { + REPLACED.fetch_add(1, Ordering::Relaxed); + reload(); +} + +/// Changes whenever the database's files were replaced ([`replaced`]); selecting a folder ([`use_dir`]) or reading +/// it again doesn't change it, so photos with a profile are not read again at every start. +pub fn replacements() -> u64 { + REPLACED.load(Ordering::Relaxed) +} + +/// The folder the host selected ([`use_dir`]), whatever `LIGHTCRAFT_LENSFUN_DIR` says. +pub fn host_dir() -> Option { + HOST_DIR.read().unwrap_or_else(PoisonError::into_inner).clone() +} + /// Read the database again (after a download or an update): the next lookup sees the new data. pub fn reload() { // under the cache lock, so a load of the old files that finishes meanwhile can't be published ([`database`]) @@ -47,20 +67,30 @@ pub fn generation() -> u64 { /// The folder the database is read from: `LIGHTCRAFT_LENSFUN_DIR`, else the host's ([`use_dir`]). pub fn dir() -> Option { - std::env::var_os(ENV).filter(|v| !v.is_empty()).map(PathBuf::from).or_else(|| HOST_DIR.read().unwrap_or_else(PoisonError::into_inner).clone()) + source().map(|(d, _)| d) +} + +/// [`dir`], and whether it is the app's own downloaded folder (read only when whole, [`crate::lensfun_download::complete`]) +/// rather than the user's. +fn source() -> Option<(PathBuf, bool)> { + match std::env::var_os(ENV).filter(|v| !v.is_empty()) { + Some(d) => Some((PathBuf::from(d), false)), + None => HOST_DIR.read().unwrap_or_else(PoisonError::into_inner).clone().map(|d| (d, true)), + } } /// The database in use, read once per folder (and again after [`reload`]); `None` when none is configured, the /// folder is missing or it holds no lenses. pub fn database() -> Option> { - let dir = dir()?; + let (dir, downloaded) = source()?; if let Some((d, db)) = LOADED.read().unwrap_or_else(PoisonError::into_inner).as_ref() && *d == dir { return db.clone(); } let generation = generation(); - let db = load(&dir); + // the app's own folder without its marker is a download that never finished: not read (the start fetches it) + let db = if downloaded && !crate::lensfun_download::complete(&dir) { None } else { load(&dir) }; let mut loaded = LOADED.write().unwrap_or_else(PoisonError::into_inner); // a reload while the files were read: they may be the old ones, so they aren't kept (the next call reads again) if GENERATION.load(Ordering::Relaxed) == generation { diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index c49cd428d5..69693c9254 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -8,8 +8,11 @@ //! it is thrown away. A new pin in a later build replaces the folder's files once ([`installed`] compares the pin). //! //! The transfer is [`lightcraft_fetch`] (pure-Rust HTTPS, resume, size and hash checks) on a background thread; the -//! files land in a staging folder first and move into place only when all of them are verified, so the database is -//! never a mix of two versions. Failure is a message, never a panic. +//! files land in a staging folder beside the database folder, the marker is written there last, and only then does the +//! staged folder replace the database folder as a whole ([`publish`]): the previous complete database stays in use +//! until the new one is in place, and comes back if the swap fails. The loader reads the app's folder only when it has +//! the marker ([`complete`]), and [`installed`] also checks every pinned file is there, so a damaged folder is +//! downloaded again. Failure is a message, never a panic. use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -25,9 +28,11 @@ pub const DEFAULT_MIRROR: &str = "https://raw.githubusercontent.com/lensfun/lens pub const URL_ENV: &str = "LIGHTCRAFT_LENSFUN_URL"; /// The file written once every database file is in place: the pin it holds says which data the folder has. pub const MARKER: &str = "SOURCE.txt"; -/// The folder inside the database folder where files wait until all have arrived. +/// The suffix of the folder beside the database folder where files wait until all have arrived. #[cfg(not(target_arch = "wasm32"))] -const STAGING: &str = ".download"; +const STAGING: &str = "download"; +/// The suffix of the folder the previous database is moved to while the new one takes its place. +const PREVIOUS: &str = "previous"; /// Largest database file accepted (the biggest is under 1 MB). #[cfg(not(target_arch = "wasm32"))] const MAX_FILE: u64 = 8 << 20; @@ -97,9 +102,52 @@ pub fn default_dir() -> Option { crate::config::config_dir().map(|d| d.join("lensfun")) } -/// The folder holds the pinned data ([`MARKER`] names [`COMMIT`]). +/// The folder holds the pinned data: [`MARKER`] names [`COMMIT`] and every pinned file is there at its pinned size +/// (a folder whose files went missing is downloaded again). pub fn installed(dir: &Path) -> bool { + installed_with(dir, FILES) +} + +fn installed_with(dir: &Path, files: &[Pin]) -> bool { std::fs::read_to_string(dir.join(MARKER)).is_ok_and(|s| s.contains(COMMIT)) + && files.iter().all(|(name, size, _)| std::fs::metadata(dir.join(name)).is_ok_and(|m| m.is_file() && m.len() == *size)) +} + +/// The folder holds a whole database, of this pin or an older one: its [`MARKER`], written last, is there. The app's +/// own folder is read only then ([`crate::lens_profiles::database`]). +pub fn complete(dir: &Path) -> bool { + dir.join(MARKER).is_file() +} + +/// `dir` with `suffix` added to its name: a folder beside it (`lensfun` → `lensfun.download`). +fn beside(dir: &Path, suffix: &str) -> PathBuf { + let mut name = dir.file_name().map(|n| n.to_os_string()).unwrap_or_default(); + name.push(format!(".{suffix}")); + dir.with_file_name(name) +} + +/// Put back the previous database when a swap ([`publish`]) was interrupted between its two moves (the app quit or +/// the machine stopped): `dir` is missing and the previous folder is whole. Best effort; says why it couldn't. +pub fn recover(dir: &Path) { + let previous = beside(dir, PREVIOUS); + if !dir.exists() + && complete(&previous) + && let Err(e) = std::fs::rename(&previous, dir) + { + log::warn!("lens database: could not put back the previous database: {e}"); + } +} + +/// The first-start policy, in one place: the folder to fetch the database into without being asked, or `None`. The +/// host must allow it (`auto`, [`crate::Session::lens_auto_download`]); a folder of the user's own +/// (`LIGHTCRAFT_LENSFUN_DIR`) is never replaced; and the app's folder must lack the pinned data. +pub fn fetch_on_start(auto: bool) -> Option { + if !auto || std::env::var_os(crate::lens_profiles::ENV).is_some_and(|v| !v.is_empty()) { + return None; + } + let dir = default_dir()?; + recover(&dir); + (!installed(&dir)).then_some(dir) } /// The mirrors to try, in order: [`URL_ENV`]'s, then lensfun's own ([`DEFAULT_MIRROR`]). @@ -171,7 +219,7 @@ impl Download { /// Fetch `files` from `mirrors` into `dir` on a background thread, shown in the activity stack until it ends /// (its ✕ stops it). Started once at a time; when it ends the database is read again - /// ([`crate::lens_profiles::reload`]). + /// ([`crate::lens_profiles::replaced`]). pub fn start(&mut self, dir: &Path, mirrors: Vec, files: &'static [Pin], activity: &Activity) -> Result<(), String> { if self.running() { return Err("the lens database is already downloading".into()); @@ -218,7 +266,7 @@ impl Drop for Download { fn run(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) { match fetch_all(dir, mirrors, files, shared) { Ok(n) => { - crate::lens_profiles::reload(); + crate::lens_profiles::replaced(); shared.set(State::Done { files: n }); } Err(e) if shared.cancel.load(Ordering::Relaxed) => { @@ -234,7 +282,8 @@ fn run(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) { #[cfg(not(target_arch = "wasm32"))] fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> Result { - let staging = dir.join(STAGING); + recover(dir); + let staging = beside(dir, STAGING); std::fs::create_dir_all(&staging).map_err(|e| format!("could not create {}: {e}", staging.display()))?; let specs: Vec> = files.iter().map(|(name, size, sha)| lightcraft_fetch::FileSpec { name, size: Some(*size), sha256: Some(sha), max: MAX_FILE }).collect(); @@ -249,24 +298,49 @@ fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> } other => other.to_string(), })?; - // every file is here and checked: into place, then the marker that says so - let _ = std::fs::remove_file(dir.join(MARKER)); - for (name, _, _) in files { - std::fs::rename(staging.join(name), dir.join(name)).map_err(|e| format!("could not move {name} into {}: {e}", dir.display()))?; - } - // database files an older pin had and this one doesn't would still be read: remove them, and without the marker - // when one can't be (the next start tries again rather than reading an old file beside the new ones) - remove_stale(dir, files, |p| std::fs::remove_file(p))?; - let _ = std::fs::remove_dir_all(&staging); + // every file is here and checked. The staged folder becomes the database: first without database files another + // pin left in it (a resumed older attempt), then with the marker, written last, then in place of the old folder + // as a whole + remove_stale(&staging, files, |p| std::fs::remove_file(p))?; let readme = format!( - "lensfun lens database (data/db), commit {COMMIT}\nfrom https://github.com/lensfun/lensfun, downloaded by LightCraft.\n\ - Licence: Creative Commons Attribution-ShareAlike 3.0 (https://creativecommons.org/licenses/by-sa/3.0/), by the lensfun contributors.\n\ - Data only, read by LightCraft at run time; not part of LightCraft.\n" + "lensfun lens database (data/db), commit {COMMIT} +from https://github.com/lensfun/lensfun, downloaded by LightCraft. + Licence: Creative Commons Attribution-ShareAlike 3.0 (https://creativecommons.org/licenses/by-sa/3.0/), by the lensfun contributors. + Data only, read by LightCraft at run time; not part of LightCraft. +" ); - std::fs::write(dir.join(MARKER), readme).map_err(|e| format!("could not write {}: {e}", dir.join(MARKER).display()))?; + std::fs::write(staging.join(MARKER), readme).map_err(|e| format!("could not write {}: {e}", staging.join(MARKER).display()))?; + publish(&staging, dir, |from, to| std::fs::rename(from, to))?; Ok(files.len()) } +/// Replace the database folder `dir` with the complete `staged` one, moving with `rename`: the old folder is moved +/// aside, the staged one takes its place, and the old one is removed. When the second move fails the old folder is +/// put back, so `dir` is always either the previous complete database or the new one, never a mix. +#[cfg(not(target_arch = "wasm32"))] +fn publish(staged: &Path, dir: &Path, rename: impl Fn(&Path, &Path) -> std::io::Result<()>) -> Result<(), String> { + let previous = beside(dir, PREVIOUS); + // left by a swap that ended after its second move + if previous.exists() { + std::fs::remove_dir_all(&previous).map_err(|e| format!("could not remove {}: {e}", previous.display()))?; + } + let had = dir.exists(); + if had { + rename(dir, &previous).map_err(|e| format!("could not move the old lens database aside ({}): {e}", dir.display()))?; + } + if let Err(e) = rename(staged, dir) { + if had && let Err(back) = rename(&previous, dir) { + // (the next start puts it back: [`recover`]) + log::warn!("lens database: could not put back the previous database yet: {back}"); + } + return Err(format!("could not move the new lens database into {}: {e}", dir.display())); + } + if had && let Err(e) = std::fs::remove_dir_all(&previous) { + log::warn!("lens database: could not remove the previous database {}: {e}", previous.display()); + } + Ok(()) +} + /// Remove the XML files in `dir` that are not among `files` (nothing else in the folder is touched) with `remove`: an /// error names the first that could not be removed, or why the folder could not be listed. #[cfg(not(target_arch = "wasm32"))] @@ -303,7 +377,9 @@ mod tests { fn scratch(name: &str) -> PathBuf { let d = std::env::temp_dir().join(format!("lc-lensfun-{name}-{}", std::process::id())); - let _ = std::fs::remove_dir_all(&d); + for leftover in [d.clone(), beside(&d, STAGING), beside(&d, PREVIOUS)] { + let _ = std::fs::remove_dir_all(leftover); + } std::fs::create_dir_all(&d).unwrap(); d } @@ -367,37 +443,103 @@ mod tests { let base = serve(vec![("lenses.xml", LENS.as_bytes().to_vec()), ("cameras.xml", CAMERA.as_bytes().to_vec())]); let activity = Activity::default(); let mut d = Download::default(); - assert!(!installed(&dir)); - // left by an older pin: an XML file this one doesn't list goes, anything else stays - std::fs::create_dir_all(&dir).unwrap(); + assert!(!installed_with(&dir, pins())); + // an older pin's database: replaced as a whole, so a file this pin doesn't list is not read beside the new ones std::fs::write(dir.join("dropped.xml"), CAMERA).unwrap(); - std::fs::write(dir.join("notes.txt"), "mine").unwrap(); + std::fs::write(dir.join(MARKER), "older").unwrap(); + // a resumed older attempt left a file this pin doesn't list in the staging folder: not published + std::fs::create_dir_all(beside(&dir, STAGING)).unwrap(); + std::fs::write(beside(&dir, STAGING).join("old-staged.xml"), CAMERA).unwrap(); d.start(&dir, vec![base], pins(), &activity).unwrap(); let rows = activity.list(); assert!(rows.iter().any(|r| r.kind == "download" && r.label == "Downloading the lens database" && r.cancellable), "{rows:?}"); wait("the download never ended", || !d.running()); assert_eq!(d.state(), State::Done { files: 2 }); - assert!(installed(&dir), "the marker names the pinned commit"); - assert!(!dir.join(STAGING).exists(), "the staging folder is gone"); + assert!(installed_with(&dir, pins()), "the marker names the pinned commit and every file is there"); + assert!(!beside(&dir, STAGING).exists() && !beside(&dir, PREVIOUS).exists(), "no staging or previous folder is left"); let (db, errors) = lightcraft_lensdb::LensDb::load_dir(&dir).unwrap(); assert!(errors.is_empty() && db.lenses.len() == 1 && db.cameras.len() == 1); - assert!(!dir.join("dropped.xml").exists() && dir.join("notes.txt").exists(), "only stale database files are removed"); + assert!(!dir.join("dropped.xml").exists() && !dir.join("old-staged.xml").exists(), "only this pin's files"); wait("the row stayed", || activity.list().is_empty()); let _ = std::fs::remove_dir_all(&dir); } - /// A file that is not the pinned one (another commit, a hostile mirror) is refused and nothing is installed. + /// A file that is not the pinned one (another commit, a hostile mirror) is refused and nothing is installed: + /// the previous complete database stays as it was. #[test] fn a_file_that_is_not_the_pinned_one_installs_nothing() { let dir = scratch("bad"); + std::fs::write(dir.join("cameras.xml"), "previous").unwrap(); + std::fs::write(dir.join(MARKER), "older").unwrap(); let base = serve(vec![("lenses.xml", LENS.replace("0.01", "0.02").into_bytes()), ("cameras.xml", CAMERA.as_bytes().to_vec())]); let mut d = Download::default(); d.start(&dir, vec![base], pins(), &Activity::default()).unwrap(); wait("the download never ended", || !d.running()); assert!(matches!(d.state(), State::Failed(_)), "{:?}", d.state()); - assert!(!installed(&dir) && !dir.join("cameras.xml").exists(), "nothing moved into place"); + assert!(!installed_with(&dir, pins()) && complete(&dir), "the previous database is still whole"); + assert_eq!(std::fs::read_to_string(dir.join("cameras.xml")).unwrap(), "previous", "nothing moved into place"); // no mirror at all is an error, not a thread assert!(Download::default().start(&dir, Vec::new(), pins(), &Activity::default()).is_err()); + let _ = std::fs::remove_dir_all(beside(&dir, STAGING)); + let _ = std::fs::remove_dir_all(&dir); + } + + /// The new database replaces the old folder as a whole or not at all: when moving it into place fails, the + /// previous database is put back (and when even that fails, the next start puts it back). + #[test] + fn a_failed_swap_keeps_the_previous_database() { + let dir = scratch("swap"); + std::fs::write(dir.join("lenses.xml"), "old").unwrap(); + std::fs::write(dir.join(MARKER), "older").unwrap(); + let staged = beside(&dir, STAGING); + std::fs::create_dir_all(&staged).unwrap(); + std::fs::write(staged.join("lenses.xml"), "new").unwrap(); + std::fs::write(staged.join(MARKER), COMMIT).unwrap(); + let read = |d: &Path| std::fs::read_to_string(d.join("lenses.xml")).unwrap(); + // the staged folder can't be moved into place: the old one comes back + let calls = std::cell::Cell::new(0); + let second_fails = |from: &Path, to: &Path| { + calls.set(calls.get() + 1); + if calls.get() == 2 { Err(std::io::Error::other("in use")) } else { std::fs::rename(from, to) } + }; + let err = publish(&staged, &dir, second_fails).unwrap_err(); + assert!(err.contains("in use"), "{err}"); + assert_eq!(read(&dir), "old", "the previous database is in place"); + assert!(complete(&dir) && !beside(&dir, PREVIOUS).exists()); + // neither move back works either (the app stops here): the next start puts the previous database back + let calls = std::cell::Cell::new(0); + let all_but_first_fail = |from: &Path, to: &Path| { + calls.set(calls.get() + 1); + if calls.get() == 1 { std::fs::rename(from, to) } else { Err(std::io::Error::other("in use")) } + }; + assert!(publish(&staged, &dir, all_but_first_fail).is_err()); + assert!(!dir.exists() && complete(&beside(&dir, PREVIOUS))); + recover(&dir); + assert_eq!(read(&dir), "old"); + // and a swap that works leaves only the new database + publish(&staged, &dir, |from, to| std::fs::rename(from, to)).unwrap(); + assert_eq!(read(&dir), "new"); + assert!(!staged.exists() && !beside(&dir, PREVIOUS).exists()); + let _ = std::fs::remove_dir_all(&dir); + } + + /// A folder whose marker is there but whose database files went missing (or were cut short) is not installed, + /// so the start downloads it again. + #[test] + fn installed_needs_every_pinned_file() { + let dir = scratch("installed"); + std::fs::write(dir.join(MARKER), format!("commit {COMMIT}")).unwrap(); + assert!(complete(&dir) && !installed_with(&dir, pins()), "the marker alone is not a database"); + std::fs::write(dir.join("lenses.xml"), LENS).unwrap(); + std::fs::write(dir.join("cameras.xml"), CAMERA).unwrap(); + assert!(installed_with(&dir, pins())); + std::fs::write(dir.join("cameras.xml"), &CAMERA[..10]).unwrap(); + assert!(!installed_with(&dir, pins()), "a file cut short"); + std::fs::remove_file(dir.join("cameras.xml")).unwrap(); + assert!(!installed_with(&dir, pins()), "a file gone"); + std::fs::write(dir.join(MARKER), "an older commit").unwrap(); + std::fs::write(dir.join("cameras.xml"), CAMERA).unwrap(); + assert!(!installed_with(&dir, pins()) && complete(&dir), "an older pin: whole, but replaced"); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 0f6fee5588..fc5e4b1fc3 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -287,7 +287,8 @@ pub struct Session { pub face_downloads: face_download::Downloads, /// The lens database download ([`lensfun_download`]). pub lensfun: lensfun_download::Download, - /// The host fetches the lens database by itself when it is missing ([`Session::with_default_lens_database`]). + /// The host may fetch the lens database by itself when it is missing ([`Session::with_lens_auto_download`]; + /// when: [`lensfun_download::fetch_on_start`]). pub lens_auto_download: bool, /// Reverse geocoding: the parsed place names and their download (`metadata.*PlaceNames`). pub place_names: cmd::geocode::PlaceNames, diff --git a/crates/engine/src/media/quick.rs b/crates/engine/src/media/quick.rs index 6098ed4dde..7e8e6727e6 100644 --- a/crates/engine/src/media/quick.rs +++ b/crates/engine/src/media/quick.rs @@ -94,6 +94,20 @@ pub fn content_key(p: &Photo) -> String { } } +/// What a photo's lens profile adds to its render and thumbnail keys: a database profile (0 without one, or with the +/// correction off, where it changes nothing). When a database update gives the photo another profile, its cached +/// renders and thumbnails no longer match. (The file's own correction changes only with the file, which the content +/// key covers, so it adds nothing and existing thumbnails stay valid.) +pub fn lens_key(p: &Photo, settings: &DevelopSettings) -> u64 { + match &p.embedded_lens { + Some(l) if settings.optics.lens_profile && l.source == lightcraft_develop::LensSource::Database => { + let k = Hasher128::new().str("lens").str(&format!("{l:?}")).finish(); + (k.0 as u64) ^ ((k.0 >> 64) as u64) | 1 + } + _ => 0, + } +} + pub fn source_info(p: &Photo) -> SourceInfo { // Procedural demo scenes are scene-referred HDR (like raw files): use the filmic tone map. // (They are drawn afresh at every size, so they have no original size.) diff --git a/crates/engine/src/media/session.rs b/crates/engine/src/media/session.rs index d160b216dd..6947af055e 100644 --- a/crates/engine/src/media/session.rs +++ b/crates/engine/src/media/session.rs @@ -48,6 +48,7 @@ impl crate::Session { // Temporary "before" settings must not replace the photo's cached develop hash. let settings_hash = if before { settings.hash64() } else { self.media.settings_hashes.get(id, &settings) }; let salt = self.media.denoise_salt(id, &settings); + let lens = super::quick::lens_key(&p, &settings); let key = settings_hash ^ ((max_w as u64) << 40) ^ ((max_h as u64) << 20) @@ -55,7 +56,8 @@ impl crate::Session { ^ (level as u64) << 60 ^ id.0.wrapping_mul(0x9e37_79b9_7f4a_7c15) ^ content.rotate_left(17) - ^ salt; + ^ salt + ^ lens.rotate_left(29); let cache = thumb_bucket.map(|b| { let mut k = Hasher128::new(); k.str(&content_key).u64(settings_hash).u64(b as u64).u64(RENDER_CACHE_VERSION).u64(crate::camera_profiles::cache_key()); @@ -63,6 +65,10 @@ impl crate::Session { if salt != 0 { k.u64(salt); } + // (likewise a database lens profile: thumbnails of every other photo keep their keys) + if lens != 0 { + k.u64(lens); + } (self.media.rendered.clone(), k.finish()) }); Some(RenderJob { @@ -98,14 +104,18 @@ impl crate::Session { /// Cache key of a variant thumbnail ([`Self::variant_job`]). pub fn variant_key(p: &Photo, settings: &DevelopSettings, edge: usize) -> Hash128 { - Hasher128::new() - .str(&content_key(p)) + let mut k = Hasher128::new(); + k.str(&content_key(p)) .str("variant") .u64(settings.hash64()) .u64(edge as u64) .u64(RENDER_CACHE_VERSION) - .u64(crate::camera_profiles::cache_key()) - .finish() + .u64(crate::camera_profiles::cache_key()); + let lens = super::quick::lens_key(p, settings); + if lens != 0 { + k.u64(lens); + } + k.finish() } /// A thumbnail of `id` rendered with `settings` instead of its own (profile and preset diff --git a/crates/engine/src/media/source.rs b/crates/engine/src/media/source.rs index 51c54c60a9..6986849b37 100644 --- a/crates/engine/src/media/source.rs +++ b/crates/engine/src/media/source.rs @@ -179,14 +179,34 @@ impl DecodedSource { /// What to render these pixels against: the decoder's facts, else `header` (the catalog's) /// with any stored camera tone curve. The original's size comes from `header` either way: the /// decoder only sees the level it decoded. + /// + /// A lens profile from the lens database is the catalog's too ([`pinned_lens`]): the decoder looks the lens up in + /// the database in use when it reads the file, which may be newer than the one the photo's profile came from. pub fn info_or(&self, header: SourceInfo) -> SourceInfo { match &self.info { - Some(info) => SourceInfo { native_long: info.native_long.or(header.native_long), ..info.clone() }, + Some(info) => { + SourceInfo { native_long: info.native_long.or(header.native_long), lens: pinned_lens(info.lens, header.lens), ..info.clone() } + } None => SourceInfo { camera_tone: self.camera_tone.or(header.camera_tone), ..header }, } } } +/// The lens profile a render uses: the file's own correction as decoded (`decoded`, it is part of the file), else the +/// catalog's (`catalog`, [`lightcraft_catalog::Photo::embedded_lens`]). A database profile is never taken from the +/// decode, so a cached render, a fresh decode and the Optics panel all use the one profile the photo holds, and a +/// change to it (a database update looked up again, `cmd::lens_refresh`) changes the render keys +/// ([`super::quick::lens_key`]). +pub(crate) fn pinned_lens( + decoded: Option, + catalog: Option, +) -> Option { + match decoded { + Some(l) if l.source == lightcraft_develop::LensSource::File => Some(l), + _ => catalog, + } +} + #[derive(Clone)] pub enum SourceRef { Loaded(Box), diff --git a/crates/engine/src/tests/lens_refresh.rs b/crates/engine/src/tests/lens_refresh.rs index 1d383c6781..ab43aa280a 100644 --- a/crates/engine/src/tests/lens_refresh.rs +++ b/crates/engine/src/tests/lens_refresh.rs @@ -98,9 +98,9 @@ fn photos_get_their_profile_when_the_database_arrives() { s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap(); } let db = lightcraft_lensdb::LensDb::parse(DB).unwrap(); - let listed = crate::cmd::lens_refresh::missing(&s.catalog, &db, None); + let listed = crate::cmd::lens_refresh::missing(&s.catalog, &db, None, false); assert_eq!(listed.iter().map(|m| m.id.0).collect::>(), [1, 2], "only lenses the database knows"); - assert_eq!(crate::cmd::lens_refresh::missing(&s.catalog, &db, Some(&[PhotoId(2)])).len(), 1, "or only the photos asked for"); + assert_eq!(crate::cmd::lens_refresh::missing(&s.catalog, &db, Some(&[PhotoId(2)]), false).len(), 1, "or only the photos asked for"); // what the files give when read again let results = listed.into_iter().map(|m| (m, Ok(crate::media::ProbeInfo { embedded_lens: Some(lens(-0.01)), ..Default::default() }))).collect(); let found = crate::cmd::lens_refresh::apply(&mut s, results).unwrap(); @@ -127,3 +127,127 @@ fn photos_get_their_profile_when_the_database_arrives() { assert!(s.execute("library.refreshLensProfiles", &json!({})).is_err()); assert!(s.execute("library.refreshLensProfiles", &json!({"ids": "x"})).is_err()); } + +fn with_lens(mut p: Photo, l: Option, on: bool) -> Photo { + p.embedded_lens = l; + let mut d = (*p.develop).clone(); + d.optics.lens_profile = on; + p.develop = Arc::new(d); + p +} + +/// When the database's files were replaced, the photos whose profile came from the old database are read again +/// (they get the new profile, or lose one the database no longer gives), and so are the raws that named no lens when +/// they were imported. A profile from the file itself is left alone, and none of these are read at an ordinary start. +#[test] +fn a_replaced_database_looks_up_database_profiles_and_unnamed_photos_again() { + let mut s = Session::new(); + let file_lens = EmbeddedLens { source: LensSource::File, ..lens(-0.04) }; + for p in [ + with_lens(raw(1, "50mm F1.8", false), Some(lens(-0.01)), true), + with_lens(raw(2, "50mm F1.8", false), Some(file_lens), true), + raw(3, "", false), + with_lens(raw(4, "300mm F4", false), Some(lens(-0.02)), true), + ] { + s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap(); + } + let db = lightcraft_lensdb::LensDb::parse(DB).unwrap(); + let ids = |v: Vec| v.iter().map(|m| m.id.0).collect::>(); + assert!(crate::cmd::lens_refresh::missing(&s.catalog, &db, None, false).is_empty(), "an ordinary start reads none of them"); + let listed = crate::cmd::lens_refresh::missing(&s.catalog, &db, None, true); + assert_eq!(ids(listed.clone()), [1, 3, 4], "database profiles and unnamed raws, never the file's own correction"); + // what the files give with the new database: 1 another profile, 3 its lens (now read) and a profile, 4 nothing + let gives = |id: u64| match id { + 1 => crate::media::ProbeInfo { embedded_lens: Some(lens(-0.03)), ..Default::default() }, + 3 => { + let mut info = crate::media::ProbeInfo { embedded_lens: Some(lens(-0.01)), ..Default::default() }; + info.meta.lens = "50mm F1.8".into(); + info + } + _ => crate::media::ProbeInfo::default(), + }; + let results = listed.into_iter().map(|m| (m.clone(), Ok(gives(m.id.0)))).collect(); + let found = crate::cmd::lens_refresh::apply(&mut s, results).unwrap(); + assert_eq!(found, [PhotoId(1), PhotoId(3)]); + assert_eq!(lens_of(&s, PhotoId(1)), Some(lens(-0.03)), "the new database's profile"); + assert_eq!(lens_of(&s, PhotoId(2)), Some(file_lens), "the file's own correction stays"); + assert_eq!(lens_of(&s, PhotoId(3)), Some(lens(-0.01))); + assert_eq!(s.catalog.photo(PhotoId(3)).unwrap().meta.lens, "50mm F1.8", "the lens's name, read now"); + assert_eq!(lens_of(&s, PhotoId(4)), None, "a profile the database no longer gives is gone"); + assert!(s.undo.is_empty(), "bookkeeping, not an undo step"); +} + +/// Every render of a photo uses the one database profile the catalog holds for it: a fresh decode that looked the lens +/// up in a newer database doesn't override it (a cached preview and a fresh render agree), while the file's own +/// correction, part of the file, still comes from the decode. +#[test] +fn a_decode_never_overrides_the_photos_database_profile() { + use crate::media::DecodedSource; + let img = Arc::new(lightcraft_raster::Rgb32f::new(4, 4)); + let decoded = |l: EmbeddedLens| DecodedSource::new(img.clone(), Some(lightcraft_pipeline::SourceInfo { lens: Some(l), ..Default::default() })); + let header = |l: Option| lightcraft_pipeline::SourceInfo { lens: l, ..Default::default() }; + assert_eq!(decoded(lens(-0.05)).info_or(header(Some(lens(-0.01)))).lens, Some(lens(-0.01)), "the catalog's profile"); + assert_eq!(decoded(lens(-0.05)).info_or(header(None)).lens, None, "none until the photo is given one"); + let file_lens = EmbeddedLens { source: LensSource::File, ..lens(-0.04) }; + assert_eq!(decoded(file_lens).info_or(header(None)).lens, Some(file_lens), "the file's own correction"); +} + +/// When a photo's database profile changes (a replaced database looked up again), its render key and its thumbnail +/// and variant cache keys change, so no cached render made with the old profile is shown; with the correction off +/// the profile changes nothing, and the keys stay. +#[test] +fn a_new_database_profile_changes_the_render_and_cache_keys() { + let mut s = Session::new(); + s.catalog.apply(Op::AddPhoto { photo: Box::new(with_lens(raw(1, "50mm F1.8", false), Some(lens(-0.01)), true)) }).unwrap(); + let id = PhotoId(1); + let keys = |s: &mut Session| { + let render = s.render_job(id, 800, 600, false, true).unwrap().key; + let thumb = s.thumb_job(id, 256).unwrap().cache.unwrap().1; + let p = s.catalog.photo(id).unwrap().clone(); + (render, thumb, Session::variant_key(&p, &p.develop, 128)) + }; + let before = keys(&mut s); + s.catalog.apply(Op::SetEmbeddedLens { id, lens: Some(Box::new(lens(-0.03))) }).unwrap(); + let after = keys(&mut s); + assert_ne!(before.0, after.0, "render key"); + assert_ne!(before.1, after.1, "thumbnail cache key"); + assert_ne!(before.2, after.2, "variant cache key"); + // correction off: the profile isn't used, so changing it leaves every key (and the cached thumbnails) as they are + let off = with_lens((**s.catalog.photo(id).unwrap()).clone(), Some(lens(-0.03)), false); + s.catalog.apply(Op::SetDevelop { id, settings: off.develop.clone(), label: "Lens Corrections".into(), edited: None }).unwrap(); + let off_before = keys(&mut s); + s.catalog.apply(Op::SetEmbeddedLens { id, lens: Some(Box::new(lens(-0.05))) }).unwrap(); + assert_eq!(off_before, keys(&mut s)); +} + +/// `lens.downloadDatabase` selects the folder it downloads into, so the session reads the database once it is in +/// place (a headless session would otherwise download it and go on without it); and the app's own folder is read +/// only when it is whole (its marker, written last, is there). +#[test] +fn the_downloaded_database_is_selected_and_read_only_when_whole() { + use crate::{lens_profiles, lensfun_download}; + if std::env::var_os(lens_profiles::ENV).is_some_and(|v| !v.is_empty()) { + lightcraft_testkit::skip!(Input, "LIGHTCRAFT_LENSFUN_DIR names the folder in use"); + return; + } + let unique = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); + let root = std::env::temp_dir().join(format!("lc-lens-select-{}-{unique}", std::process::id())); + let dir = root.join("lensfun"); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("lenses.xml"), DB).unwrap(); + let before = lens_profiles::host_dir(); + let mut s = Session::new(); + // nothing listens on port 1: the download fails at once, without the network + crate::cmd::lens_refresh::start_download(&mut s, &dir, vec!["http://127.0.0.1:1/db".into()], lensfun_download::FILES).unwrap(); + assert_eq!(lens_profiles::dir().as_deref(), Some(dir.as_path()), "the session reads the folder it downloads into"); + assert!(lens_profiles::database().is_none(), "no marker: a download that never finished is not read"); + std::fs::write(dir.join(lensfun_download::MARKER), "whole").unwrap(); + lens_profiles::reload(); + assert_eq!(lens_profiles::database().map(|db| db.lenses.len()), Some(1), "whole: read"); + let t0 = std::time::Instant::now(); + while s.lensfun.running() && t0.elapsed() < std::time::Duration::from_secs(60) { + std::thread::sleep(std::time::Duration::from_millis(20)); + } + lens_profiles::use_dir(before); + let _ = std::fs::remove_dir_all(&root); +} diff --git a/crates/mcp/src/headless.rs b/crates/mcp/src/headless.rs index d62a9a707e..27431f72b4 100644 --- a/crates/mcp/src/headless.rs +++ b/crates/mcp/src/headless.rs @@ -29,7 +29,9 @@ impl Drop for Headless { impl Default for Headless { fn default() -> Self { - let session = Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models(); + // the lens database the desktop app downloaded, when it is there (a headless session never fetches it) + let session = + Session::new().with_fs().with_default_denoise_models().with_system_clock().with_default_face_models().with_default_lens_database(); Self::new(session.with_default_sam3_model().with_default_slide_templates()) } } @@ -203,6 +205,15 @@ pub fn write_image(path: &Path, img: &Rgba8, quality: u8) -> Result<(), String> mod tests { use super::*; + /// A headless session reads the lens database the desktop app downloaded, as the desktop does (the same raw gets + /// the same profile everywhere), but never fetches it by itself. + #[test] + fn a_headless_session_reads_the_downloaded_lens_database() { + let h = Headless::default(); + assert_eq!(lightcraft_engine::lens_profiles::host_dir(), lightcraft_engine::lensfun_download::default_dir()); + assert!(!h.session.lens_auto_download, "selecting it is not permission to download it"); + } + #[test] fn folder_discovery_matches_engine_extensions() { let dir = std::env::temp_dir().join(format!("lc-mcp-discovery-{}", std::process::id())); diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs index f9562e2473..1252d05c2b 100644 --- a/crates/ui-egui/src/lens_db.rs +++ b/crates/ui-egui/src/lens_db.rs @@ -1,7 +1,8 @@ //! The lens database without setup: on first run the app fetches lensfun's database by itself //! (`lightcraft_engine::lensfun_download`, an activity row while it runs), and whenever the database in use changes //! (it arrived, a newer pin replaced it) or Enable Lens Corrections is ticked on a photo without a profile, the -//! library's raws that have none are looked up again on a worker thread (`lightcraft_engine::cmd::lens_refresh`). +//! library's raws that have none are looked up again on a worker thread (`lightcraft_engine::cmd::lens_refresh`); +//! when its files were replaced, so are those whose profile came from the database and those that named no lens. use lightcraft_engine::cmd::{lens_refresh, recheck}; use lightcraft_engine::{lens_profiles, lensfun_download}; @@ -17,20 +18,14 @@ pub fn tick(app: &mut LightcraftApp) { refresh(app); } -/// Once per run, when the host asked for the app's own copy and it is missing (or from an older pin). +/// Once per run, when the start policy says so ([`lensfun_download::fetch_on_start`]: the host allows it and the +/// pinned database is missing or damaged). fn start_download(app: &mut LightcraftApp) { - if app.ui.lens_download_checked || !app.session.lens_auto_download { + if app.ui.lens_download_checked { return; } app.ui.lens_download_checked = true; - // a folder of the user's own (LIGHTCRAFT_LENSFUN_DIR) is never replaced - if std::env::var_os(lens_profiles::ENV).is_some_and(|v| !v.is_empty()) { - return; - } - let Some(dir) = lensfun_download::default_dir() else { return }; - if lensfun_download::installed(&dir) { - return; - } + let Some(dir) = lensfun_download::fetch_on_start(app.session.lens_auto_download) else { return }; let activity = app.session.activity.clone(); if let Err(e) = app.session.lensfun.start(&dir, lensfun_download::mirrors(), lensfun_download::FILES, &activity) { log::warn!("lens database: {e}"); @@ -50,9 +45,17 @@ pub fn to_look_up(session: &lightcraft_engine::Session, id: lightcraft_catalog:: out } -/// What a lookup's results belong to: the library and the lens database in use. Results for another key are stale. -pub(crate) fn lookup_key(app: &LightcraftApp) -> (u64, u64) { - (app.session.library_generation(), lens_profiles::generation()) +/// What a lookup's results belong to: the library, the lens database in use and how often its files were replaced. +/// Results for another key are stale. +pub(crate) fn lookup_key(app: &LightcraftApp) -> (u64, u64, u64) { + (app.session.library_generation(), lens_profiles::generation(), lens_profiles::replacements()) +} + +/// Whether this lookup also reads the photos that have a database profile or no lens name +/// ([`lens_refresh::missing`]'s `again`): only when the database's files were replaced since the last lookup, not at +/// every start or library switch. +pub(crate) fn look_up_again(last: Option<(u64, u64, u64)>, key: (u64, u64, u64)) -> bool { + last.map_or(key.2 > 0, |last| last.2 != key.2) } /// Look the library's raws up again when the database changed, the library changed, or photos asked for it. @@ -68,10 +71,11 @@ fn refresh(app: &mut LightcraftApp) { return; } let ids = (app.ui.lens_refreshed == Some(key)).then_some(asked); + let again = ids.is_none() && look_up_again(app.ui.lens_refreshed, key); app.ui.lens_refreshed = Some(key); let Some(probe) = recheck::file_probe(&app.session) else { return }; let Some(db) = lens_profiles::database() else { return }; - let listed = lens_refresh::missing(&app.session.catalog, &db, ids.as_deref()); + let listed = lens_refresh::missing(&app.session.catalog, &db, ids.as_deref(), again); if listed.is_empty() { return; } @@ -169,7 +173,10 @@ mod tests { let mut p = (**app.session.catalog.photo(id).expect("the photo")).clone(); p.meta.lens = "EF35mm f/1.4L II USM".into(); p.embedded_lens = None; - let dir = std::env::temp_dir().join(format!("lc-lens-db-failed-{}", std::process::id())); + let unique = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); + let root = std::env::temp_dir().join(format!("lc-lens-db-failed-{}-{unique}", std::process::id())); + // (the download stages its files beside the database folder: both live in this test's own folder) + let dir = root.join("lensfun"); // nothing listens on port 1: the download fails at once, without the network let activity = app.session.activity.clone(); app.session @@ -184,6 +191,6 @@ mod tests { if lightcraft_engine::lens_profiles::database().is_none() { assert_eq!(super::readout(&app, &p), "The lens database could not be downloaded. LightCraft tries again the next time it starts."); } - let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_dir_all(&root); } } diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 19cf3d2dc4..57595a7c8e 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -348,9 +348,10 @@ pub struct UiState { /// The first-run lens database download was considered this run (`crate::lens_db`). #[serde(skip)] pub lens_download_checked: bool, - /// The library and lens database generation whose raws were last looked up for a lens profile. + /// The library, lens database generation and database replacement count whose raws were last looked up for a + /// lens profile (`crate::lens_db::lookup_key`). #[serde(skip)] - pub lens_refreshed: Option<(u64, u64)>, + pub lens_refreshed: Option<(u64, u64, u64)>, /// Photos to look up for a lens profile on the next frame (Enable Lens Corrections ticked without one). #[serde(skip)] pub lens_lookup_ids: Vec, diff --git a/crates/ui-egui/src/tests/lens_lookup.rs b/crates/ui-egui/src/tests/lens_lookup.rs index 882e07ab4d..d2c9c10155 100644 --- a/crates/ui-egui/src/tests/lens_lookup.rs +++ b/crates/ui-egui/src/tests/lens_lookup.rs @@ -37,3 +37,14 @@ fn a_database_reloaded_during_a_lookup_makes_its_results_stale() { lightcraft_engine::lens_profiles::reload(); assert_ne!(crate::lens_db::lookup_key(&app), key); } + +/// Photos that have a database profile (or named no lens) are read again only when the database's files were +/// replaced, never at every start, library switch or folder selection: a big library isn't read at each launch. +#[test] +fn photos_with_a_profile_are_read_again_only_when_the_database_was_replaced() { + use crate::lens_db::look_up_again; + assert!(!look_up_again(None, (1, 1, 0)), "a start with the database already there"); + assert!(!look_up_again(Some((1, 1, 0)), (2, 2, 0)), "another library, or the folder selected again"); + assert!(look_up_again(Some((1, 1, 0)), (1, 2, 1)), "a download replaced it"); + assert!(look_up_again(None, (1, 2, 1)), "replaced before the first lookup"); +} diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index 5d5ac372a7..aea30a4e9e 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -11,7 +11,11 @@ run time, never bundled and never committed. Adobe LCP profiles are never used. (`/lensfun`, beside the face and denoise models; see `crates/engine/src/config.rs`), with a row in the activity stack (its ✕ stops it; the next start resumes). The files are pinned to one lensfun commit by name, size and SHA-256 (`crates/engine/src/lensfun_download.rs`); a file that differs is thrown away. A later build with - a newer pin replaces them once. `SOURCE.txt` in the folder names the commit and the licence. + a newer pin replaces them once. The files are staged in `/lensfun.download`, `SOURCE.txt` (the commit + and the licence) is written there last, and the staged folder then replaces the old one as a whole: until it is + in place the previous complete database is used, and if the swap fails it is put back. The app's folder is read + only when it has `SOURCE.txt`, and a folder whose pinned files are missing or cut short is downloaded again. + The CLI (`render`, `merge`, `snapshot`) and the headless MCP server read this folder too, but never download it. 2. By default the files come from lensfun's GitHub repository at the pinned commit. `LIGHTCRAFT_LENSFUN_URL` lists other places serving the same files (base URLs, tried first), e.g. a copy on LightCraft's own server. 3. `LIGHTCRAFT_LENSFUN_DIR` points at a database of your own instead (lensfun's `data/db`, or a Linux @@ -19,8 +23,16 @@ run time, never bundled and never committed. Adobe LCP profiles are never used. When the database arrives (or changes), every library raw without a profile whose lens it knows is read again in the background and gets its profile; a photo still as imported gets the correction switched on, as import would, and an -edited one keeps its settings. Ticking **Enable Lens Corrections** on a photo without a profile looks it up again -too. `library.refreshLensProfiles`, `lens.downloadDatabase` and `lens.databaseStatus` do the same for agents. +edited one keeps its settings. When a download replaced the database's files, the raws whose profile came from the +old database are read again too (they get the new profile, or lose one it no longer gives), and so are raws whose +file named no lens at import. Ticking **Enable Lens Corrections** on a photo without a profile looks it up again +too. `library.refreshLensProfiles` (`all: true` for the wider pass), `lens.downloadDatabase` (which also selects the +downloaded folder) and `lens.databaseStatus` do the same for agents. + +A database profile is pinned to the photo: every render (fresh decode, cached preview, thumbnail, export) uses the +profile the catalog holds, never one a decode looked up in a newer database, and the profile is part of the render +and thumbnail cache keys, so a changed profile never shows an old cached render. A file's own correction comes from +the file as before. The Optics panel says which profile is loaded: "Profile: from the file", "Profile: (lens database)", or "No lens profile found for ". A profile is never copied between photos: presets, Copy / Paste, Sync and Auto From b7da6964113e78e3698d19a44379261c603f2757 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Wed, 28 Oct 2026 03:57:32 +1000 Subject: [PATCH 19/51] LR-EDIT-OPTICS-PROFILE: review fixes (CodeRabbit, #3073) A discarded stale lookup asks for a frame so the current one runs in an idle window; Enable Lens Corrections' look-up list dedups with a set; the lens profile doc says the CLI and headless MCP never start the first-run download (lens.downloadDatabase still fetches on request) and that LIGHTCRAFT_LENSFUN_DIR only stops the first-run download. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/lens_db.rs | 6 ++++-- docs/lens-profiles.md | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs index 1252d05c2b..92b0d00178 100644 --- a/crates/ui-egui/src/lens_db.rs +++ b/crates/ui-egui/src/lens_db.rs @@ -37,8 +37,9 @@ fn start_download(app: &mut LightcraftApp) { /// selected photos are left alone unless their corrections were already on. pub fn to_look_up(session: &lightcraft_engine::Session, id: lightcraft_catalog::PhotoId) -> Vec { let mut out: Vec = Vec::new(); + let mut seen = std::collections::HashSet::new(); for t in std::iter::once(id).chain(session.selection.ids.iter().copied()) { - if !out.contains(&t) && session.catalog.photo(t).is_some_and(|p| p.embedded_lens.is_none() && p.develop.optics.lens_profile) { + if seen.insert(t) && session.catalog.photo(t).is_some_and(|p| p.embedded_lens.is_none() && p.develop.optics.lens_profile) { out.push(t); } } @@ -82,8 +83,9 @@ fn refresh(app: &mut LightcraftApp) { let work = move || lens_refresh::probe(&probe, listed); let done = move |app: &mut LightcraftApp, ctx: &egui::Context, results| { // another library, or a database reloaded while the files were read (the probe may have used either): the - // next frame looks them up again with the database in use now + // next frame looks them up again with the database in use now (asked for here: an idle window draws none) if lookup_key(app) != key { + ctx.request_repaint(); return; } match lens_refresh::apply(&mut app.session, results) { diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index aea30a4e9e..293b4c5c8c 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -15,11 +15,13 @@ run time, never bundled and never committed. Adobe LCP profiles are never used. and the licence) is written there last, and the staged folder then replaces the old one as a whole: until it is in place the previous complete database is used, and if the swap fails it is put back. The app's folder is read only when it has `SOURCE.txt`, and a folder whose pinned files are missing or cut short is downloaded again. - The CLI (`render`, `merge`, `snapshot`) and the headless MCP server read this folder too, but never download it. + The CLI (`render`, `merge`, `snapshot`) and the headless MCP server read this folder too, but never start the + first-run download; `lens.downloadDatabase` fetches it on request from any of them. 2. By default the files come from lensfun's GitHub repository at the pinned commit. `LIGHTCRAFT_LENSFUN_URL` lists other places serving the same files (base URLs, tried first), e.g. a copy on LightCraft's own server. 3. `LIGHTCRAFT_LENSFUN_DIR` points at a database of your own instead (lensfun's `data/db`, or a Linux - distribution's `/usr/share/lensfun/version_1/`); then nothing is downloaded. + distribution's `/usr/share/lensfun/version_1/`); then the first-run download doesn't start, and that folder is read + even after `lens.downloadDatabase` fetches the app's own copy. When the database arrives (or changes), every library raw without a profile whose lens it knows is read again in the background and gets its profile; a photo still as imported gets the correction switched on, as import would, and an From bab1e9c4e0a811c5b1ab5efdbd5c2e4599e732f0 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Wed, 28 Oct 2026 04:10:24 +1000 Subject: [PATCH 20/51] LR-EDIT-OPTICS-PROFILE: look up queued photos as soon as a lookup ends (CodeRabbit, #3073) The frame polls finished tasks before the lens lookup tick, so photos queued while a lookup ran are looked up on the same frame it is cleared instead of waiting for unrelated UI activity. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/app/frame.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index 8c23bd4db6..6d8063f7c0 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -65,8 +65,9 @@ impl LightcraftApp { } lightroom_import::tick(self, ctx); recheck::tick(self); - lens_db::tick(self); tasks::poll(self, ctx); + // after the poll: a lookup that just finished is cleared, so photos queued meanwhile are looked up now + lens_db::tick(self); updates_ui::tick(self, ctx); self.preview_build_status(ctx); self.save_status(ctx); From 2cd071c7229bafcbd4c93b29edf929495dd038fd Mon Sep 17 00:00:00 2001 From: OneSlips Date: Wed, 28 Oct 2026 08:29:30 +1000 Subject: [PATCH 21/51] LR-EDIT-OPTICS-PROFILE: the lens database is offered on first launch and downloads on request (#3073) Nothing is fetched without the user's click. The first desktop start without a database (and without LIGHTCRAFT_LENSFUN_DIR) asks once, "Download lens profiles?", with the size added up from the pinned files, and Download / Not Now. Download starts the existing pinned download (activity row, cancel); Not Now (or Esc) fetches nothing and the app doesn't ask again. Settings > General > Lens profiles has a Download Lens Database button and a switch that turns the offer back on. The answer is kept in ui.json (lensDatabase: ask / accepted / declined; an unreadable value asks again), and lensfun_download::fetch_on_start is the one place that decides. The CLI and headless MCP never ask and never fetch. Co-Authored-By: Claude Opus 5.5 --- apps/lightcraft/src/session.rs | 2 +- crates/engine/src/cmd/lens_refresh.rs | 6 +- crates/engine/src/config.rs | 4 +- crates/engine/src/lens_profiles.rs | 2 +- crates/engine/src/lensfun_download.rs | 91 ++++++++++++-- crates/engine/src/lib.rs | 4 +- crates/ui-egui/locales/cs.json | 14 ++- crates/ui-egui/locales/de.json | 14 ++- crates/ui-egui/locales/es.json | 14 ++- crates/ui-egui/locales/fi.json | 14 ++- crates/ui-egui/locales/fr.json | 14 ++- crates/ui-egui/locales/id.json | 14 ++- crates/ui-egui/locales/it.json | 14 ++- crates/ui-egui/locales/ja.json | 14 ++- crates/ui-egui/locales/ka.json | 14 ++- crates/ui-egui/locales/pl.json | 14 ++- crates/ui-egui/locales/pt-br.json | 14 ++- crates/ui-egui/locales/ru.json | 14 ++- crates/ui-egui/locales/sv.json | 14 ++- crates/ui-egui/locales/uk.json | 14 ++- crates/ui-egui/locales/vi.json | 14 ++- crates/ui-egui/locales/zh-hans.json | 14 ++- crates/ui-egui/locales/zh-hant.json | 14 ++- crates/ui-egui/src/dialogs.rs | 6 +- crates/ui-egui/src/dialogs/confirm.rs | 3 + .../ui-egui/src/dialogs/settings/general.rs | 44 +++++++ crates/ui-egui/src/lens_db.rs | 77 ++++++++++-- crates/ui-egui/src/state/dialog.rs | 6 + crates/ui-egui/src/state/settings.rs | 5 + crates/ui-egui/src/tests.rs | 1 + crates/ui-egui/src/tests/lens_offer.rs | 112 ++++++++++++++++++ docs/gaps.md | 6 +- docs/lens-profiles.md | 20 +++- docs/parity-checklist.md | 6 +- 34 files changed, 574 insertions(+), 59 deletions(-) create mode 100644 crates/ui-egui/src/tests/lens_offer.rs diff --git a/apps/lightcraft/src/session.rs b/apps/lightcraft/src/session.rs index 56f63a481d..46392ba017 100644 --- a/apps/lightcraft/src/session.rs +++ b/apps/lightcraft/src/session.rs @@ -255,7 +255,7 @@ pub(crate) fn open_session(in_memory: bool, launch: &Launch, seed_demo: bool) -> Launch::Ask => (Session::new().with_fs().with_default_denoise_models().with_system_clock(), None), Launch::Open { dir, .. } => open_library_session(in_memory, dir.clone(), seed_demo), }; - // lens profiles from the app's own lensfun copy, fetched by itself on first run (not for a throwaway session) + // lens profiles from the app's own lensfun copy, offered on the first launch (not for a throwaway session) if !in_memory { s = s.with_default_lens_database().with_lens_auto_download(); } diff --git a/crates/engine/src/cmd/lens_refresh.rs b/crates/engine/src/cmd/lens_refresh.rs index 35ba818b27..a32136ceff 100644 --- a/crates/engine/src/cmd/lens_refresh.rs +++ b/crates/engine/src/cmd/lens_refresh.rs @@ -1,6 +1,6 @@ //! Lens profiles looked up again when the lens database appears or changes. A photo's lens data is //! stored when it is imported ([`Photo::embedded_lens`]), so photos imported before the database was there say "no -//! profile" until something reads their file again. When the database arrives (the first-run download) or is updated, +//! profile" until something reads their file again. When the database arrives (downloaded on the user's request) or is updated, //! and when "Enable Lens Corrections" is ticked on a photo without a profile, every library raw that has no profile //! but whose lens the database knows by name is probed again and gets what Reload from Disk would give it: the //! profile, and the correction switched on where its settings are still what import gave it (an edited photo keeps @@ -144,7 +144,7 @@ fn download(s: &mut Session, _: &Value) -> Result { /// Fetch the database into `dir` and select it, so this session reads it once it is in place (a session that /// selected no database, e.g. a bare headless one, would otherwise download it and go on without it; the user's /// `LIGHTCRAFT_LENSFUN_DIR` still wins). Until the download is whole the folder has no marker and isn't read. -pub(crate) fn start_download( +pub fn start_download( s: &mut Session, dir: &std::path::Path, mirrors: Vec, @@ -188,7 +188,7 @@ pub fn specs() -> Vec { "Download the Lens Database", [], None, - "{} — fetch lensfun's lens database (CC BY-SA 3.0, pinned to one commit, checked file by file) into the app's data folder in the background, with an activity row, and read lens profiles from it once it is in place (unless LIGHTCRAFT_LENSFUN_DIR names another folder); the desktop app does this by itself on first run. LIGHTCRAFT_LENSFUN_URL lists other places to fetch the same files from → {started, dir}", + "{} — fetch lensfun's lens database (CC BY-SA 3.0, pinned to one commit, checked file by file) into the app's data folder in the background, with an activity row, and read lens profiles from it once it is in place (unless LIGHTCRAFT_LENSFUN_DIR names another folder); the desktop app offers it on its first launch and in Settings. LIGHTCRAFT_LENSFUN_URL lists other places to fetch the same files from → {started, dir}", always, download ), diff --git a/crates/engine/src/config.rs b/crates/engine/src/config.rs index debcc3db4a..f7b762a6b3 100644 --- a/crates/engine/src/config.rs +++ b/crates/engine/src/config.rs @@ -159,8 +159,8 @@ impl Session { self } - /// Let the host fetch the lens database by itself when it is missing ([`crate::lensfun_download::fetch_on_start`] - /// decides when). Separate from selecting a database that is already there ([`Self::with_default_lens_database`]). + /// Let the host offer the lens database at its start, and fetch it there once the user said yes + /// ([`crate::lensfun_download::fetch_on_start`] decides). Separate from selecting a database that is already there ([`Self::with_default_lens_database`]). pub fn with_lens_auto_download(mut self) -> Self { self.lens_auto_download = true; self diff --git a/crates/engine/src/lens_profiles.rs b/crates/engine/src/lens_profiles.rs index 61921c319e..0e7da4c9ef 100644 --- a/crates/engine/src/lens_profiles.rs +++ b/crates/engine/src/lens_profiles.rs @@ -3,7 +3,7 @@ //! When a raw carries no lens correction of its own (DNG opcodes, or a camera correction a raw reader converts, //! [`crate::files::embedded_lens`]), the lens named in its EXIF is looked up in a lensfun-format database. The //! database is lensfun's `data/db` (CC BY-SA 3.0): never bundled with the app, it is downloaded at run time into the -//! app's data folder ([`crate::lensfun_download`], which the desktop app starts by itself on first run), or it is the +//! app's data folder ([`crate::lensfun_download`], which the desktop app offers on its first launch and Settings can start), or it is the //! directory in `LIGHTCRAFT_LENSFUN_DIR` (which wins). The profile is converted to the same //! [`lightcraft_develop::EmbeddedLens`] form (marked [`LensSource::Database`]), so "Enable Profile Corrections", its //! amount sliders, the GPU path and DNG export treat both alike. Without a database nothing changes. diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index 69693c9254..fb30060f20 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -1,7 +1,8 @@ -//! The lensfun lens database, downloaded by itself on first run (zero setup). +//! The lensfun lens database, downloaded on request: the desktop app offers it once on the first launch +//! ([`fetch_on_start`]: nothing is fetched without the user's click). //! //! lensfun's `data/db` (CC BY-SA 3.0, ) is data, never bundled with LightCraft and -//! never committed to its repository: the desktop app fetches it at run time into its data folder +//! never committed to its repository: the desktop app fetches it at run time, when the user chooses Download, into its data folder //! ([`default_dir`]), with a row in the activity stack, and [`crate::lens_profiles`] reads it from there. The files are //! pinned to one lensfun commit ([`COMMIT`]) by name, size and SHA-256 ([`FILES`]), so whatever mirror serves them //! ([`URL_ENV`] lists others, e.g. a copy on LightCraft's own server), what arrives is exactly that commit's data or @@ -138,16 +139,64 @@ pub fn recover(dir: &Path) { } } -/// The first-start policy, in one place: the folder to fetch the database into without being asked, or `None`. The -/// host must allow it (`auto`, [`crate::Session::lens_auto_download`]); a folder of the user's own -/// (`LIGHTCRAFT_LENSFUN_DIR`) is never replaced; and the app's folder must lack the pinned data. -pub fn fetch_on_start(auto: bool) -> Option { - if !auto || std::env::var_os(crate::lens_profiles::ENV).is_some_and(|v| !v.is_empty()) { - return None; +/// The user's answer to the first-launch offer of the database (the desktop app keeps it in its settings). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub enum Offer { + /// Not asked yet: the first start asks. + #[default] + Ask, + /// Download was clicked (in the offer or in Settings): a start downloads the pinned database when it is missing + /// or from an older pin (an interrupted or failed download is tried again). + Accepted, + /// Not Now: nothing is downloaded and the start doesn't ask again (Settings can download it, or turn the offer + /// back on). + Declined, +} + +/// What a start does about the database ([`fetch_on_start`]). +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum Start { + Nothing, + /// Ask the user (once): download into this folder, or not now. + Ask(PathBuf), + /// Download into this folder (the user said yes before). + Fetch(PathBuf), +} + +/// The first-start policy, in one place: what a start does about the database. Nothing touches the network +/// without the user's click: the host must allow the question at all (`auto`, [`crate::Session::lens_auto_download`], +/// the desktop app only), a folder of the user's own (`LIGHTCRAFT_LENSFUN_DIR`) is never replaced, an installed +/// database needs nothing, and otherwise the user's answer (`offer`) decides: ask, download, or nothing. +pub fn fetch_on_start(auto: bool, offer: Offer) -> Start { + let own = std::env::var_os(crate::lens_profiles::ENV).is_some_and(|v| !v.is_empty()); + let dir = default_dir(); + if let Some(d) = dir.as_deref().filter(|_| auto && !own) { + recover(d); } - let dir = default_dir()?; - recover(&dir); - (!installed(&dir)).then_some(dir) + let installed = dir.as_deref().is_some_and(installed); + decide(auto, own, dir, installed, offer) +} + +/// [`fetch_on_start`] without the file system: `own` (`LIGHTCRAFT_LENSFUN_DIR` is set), the app's folder `dir` and +/// whether it holds the pinned data. +pub fn decide(auto: bool, own: bool, dir: Option, installed: bool, offer: Offer) -> Start { + let Some(dir) = dir.filter(|_| auto && !own && !installed) else { return Start::Nothing }; + match offer { + Offer::Ask => Start::Ask(dir), + Offer::Accepted => Start::Fetch(dir), + Offer::Declined => Start::Nothing, + } +} + +/// The download's size in bytes: the pinned files' sizes added up (what the offer says). +pub fn download_size() -> u64 { + FILES.iter().map(|f| f.1).fold(0u64, u64::saturating_add) +} + +/// [`download_size`] in whole megabytes for people ("about 5 MB"), never 0. +pub fn download_megabytes() -> u64 { + (download_size().saturating_add(500_000) / 1_000_000).max(1) } /// The mirrors to try, in order: [`URL_ENV`]'s, then lensfun's own ([`DEFAULT_MIRROR`]). @@ -566,6 +615,26 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// The first start asks before anything is downloaded; Download (or Settings) means a start fetches a missing + /// database; Not Now means nothing, ever, on its own; an installed database, a folder of the user's own or a host + /// that may not ask (CLI, MCP) mean nothing whatever the answer. + #[test] + fn the_start_asks_once_and_downloads_only_after_a_yes() { + let dir = || Some(PathBuf::from("lensfun")); + assert_eq!(decide(true, false, dir(), false, Offer::Ask), Start::Ask(PathBuf::from("lensfun")), "the first start asks"); + assert_eq!(decide(true, false, dir(), false, Offer::Accepted), Start::Fetch(PathBuf::from("lensfun"))); + assert_eq!(decide(true, false, dir(), false, Offer::Declined), Start::Nothing, "Not Now: never asked again"); + for offer in [Offer::Ask, Offer::Accepted, Offer::Declined] { + assert_eq!(decide(true, false, dir(), true, offer), Start::Nothing, "installed: no question, no download"); + assert_eq!(decide(false, false, dir(), false, offer), Start::Nothing, "a headless host never asks or fetches"); + assert_eq!(decide(true, true, dir(), false, offer), Start::Nothing, "the user's own folder"); + assert_eq!(decide(true, false, None, false, offer), Start::Nothing, "no data folder"); + } + // the size the offer names comes from the pins + assert_eq!(download_size(), FILES.iter().map(|f| f.1).sum::()); + assert!((4..=8).contains(&download_megabytes()), "{}", download_megabytes()); + } + /// The pinned list is lensfun's whole `data/db` at the commit: unique XML names, sane sizes, hex hashes. #[test] fn the_pins_are_well_formed() { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index fc5e4b1fc3..13f419babb 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -287,8 +287,8 @@ pub struct Session { pub face_downloads: face_download::Downloads, /// The lens database download ([`lensfun_download`]). pub lensfun: lensfun_download::Download, - /// The host may fetch the lens database by itself when it is missing ([`Session::with_lens_auto_download`]; - /// when: [`lensfun_download::fetch_on_start`]). + /// The host may offer the lens database at its start, and fetch it after the user's yes + /// ([`Session::with_lens_auto_download`]; when: [`lensfun_download::fetch_on_start`]). pub lens_auto_download: bool, /// Reverse geocoding: the parsed place names and their download (`metadata.*PlaceNames`). pub place_names: cmd::geocode::PlaceNames, diff --git a/crates/ui-egui/locales/cs.json b/crates/ui-egui/locales/cs.json index 561d832ddd..e7affd4dc5 100644 --- a/crates/ui-egui/locales/cs.json +++ b/crates/ui-egui/locales/cs.json @@ -2415,5 +2415,17 @@ "1 photo got its lens profile": "1 fotka získala profil objektivu", "Looking up lens profiles": "Hledání profilů objektivů", "Downloading the lens database": "Stahování databáze objektivů", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Databázi objektivů se nepodařilo stáhnout. LightCraft to zkusí znovu při příštím spuštění." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Databázi objektivů se nepodařilo stáhnout. LightCraft to zkusí znovu při příštím spuštění.", + "Lens Profiles": "Profily objektivů", + "Download lens profiles?": "Stáhnout profily objektivů?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft umí opravit zkreslení a vinětaci mnoha objektivů pomocí otevřené databáze objektivů (lensfun, asi {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Nic se nestáhne, dokud nezvolíte Stáhnout. Později ji můžete stáhnout v Nastavení.", + "Lens profiles": "Profily objektivů", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Profily objektivů se čtou ze složky, kterou určuje LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "Databáze objektivů je nainstalována.", + "The lens database is not installed (about {size} MB).": "Databáze objektivů není nainstalována (asi {size} MB).", + "Lens database": "Databáze objektivů", + "Download Lens Database": "Stáhnout databázi objektivů", + "Offer the lens database download when LightCraft starts": "Nabídnout stažení databáze objektivů při spuštění LightCraftu", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Otevřená databáze objektivů lensfun (CC BY-SA 3.0) opravuje zkreslení a vinětaci mnoha objektivů. Stáhne se jen, když to zvolíte." } diff --git a/crates/ui-egui/locales/de.json b/crates/ui-egui/locales/de.json index 300d73d1da..4b4d955a89 100644 --- a/crates/ui-egui/locales/de.json +++ b/crates/ui-egui/locales/de.json @@ -3447,5 +3447,17 @@ "1 photo got its lens profile": "1 Foto hat sein Objektivprofil erhalten", "Looking up lens profiles": "Objektivprofile werden gesucht", "Downloading the lens database": "Objektivdatenbank wird geladen", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Die Objektivdatenbank konnte nicht geladen werden. LightCraft versucht es beim nächsten Start erneut." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Die Objektivdatenbank konnte nicht geladen werden. LightCraft versucht es beim nächsten Start erneut.", + "Lens Profiles": "Objektivprofile", + "Download lens profiles?": "Objektivprofile laden?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft kann Verzeichnung und Vignettierung für viele Objektive mit der offenen Objektivdatenbank korrigieren (lensfun, etwa {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Nichts wird geladen, bevor Sie „Laden“ wählen. Sie können sie später in den Einstellungen laden.", + "Lens profiles": "Objektivprofile", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Objektivprofile werden aus dem Ordner gelesen, den LIGHTCRAFT_LENSFUN_DIR nennt.", + "The lens database is installed.": "Die Objektivdatenbank ist installiert.", + "The lens database is not installed (about {size} MB).": "Die Objektivdatenbank ist nicht installiert (etwa {size} MB).", + "Lens database": "Objektivdatenbank", + "Download Lens Database": "Objektivdatenbank laden", + "Offer the lens database download when LightCraft starts": "Beim Start von LightCraft anbieten, die Objektivdatenbank zu laden", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Die offene Objektivdatenbank von lensfun (CC BY-SA 3.0) korrigiert Verzeichnung und Vignettierung für viele Objektive. Sie wird nur geladen, wenn Sie es wählen." } diff --git a/crates/ui-egui/locales/es.json b/crates/ui-egui/locales/es.json index 998d7dfe3d..adc434164b 100644 --- a/crates/ui-egui/locales/es.json +++ b/crates/ui-egui/locales/es.json @@ -2365,5 +2365,17 @@ "1 photo got its lens profile": "1 foto recibió su perfil de objetivo", "Looking up lens profiles": "Buscando perfiles de objetivo", "Downloading the lens database": "Descargando la base de datos de objetivos", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "No se pudo descargar la base de datos de objetivos. LightCraft lo intentará de nuevo la próxima vez que se inicie." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "No se pudo descargar la base de datos de objetivos. LightCraft lo intentará de nuevo la próxima vez que se inicie.", + "Lens Profiles": "Perfiles de objetivo", + "Download lens profiles?": "¿Descargar perfiles de objetivo?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft puede corregir la distorsión y el viñeteado de muchos objetivos con la base de datos abierta de objetivos (lensfun, unos {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "No se descarga nada hasta que elija Descargar. Puede descargarla más tarde desde Ajustes.", + "Lens profiles": "Perfiles de objetivo", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Los perfiles de objetivo se leen de la carpeta que indica LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "La base de datos de objetivos está instalada.", + "The lens database is not installed (about {size} MB).": "La base de datos de objetivos no está instalada (unos {size} MB).", + "Lens database": "Base de datos de objetivos", + "Download Lens Database": "Descargar base de datos de objetivos", + "Offer the lens database download when LightCraft starts": "Ofrecer la descarga de la base de datos de objetivos al iniciar LightCraft", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "La base de datos abierta de objetivos de lensfun (CC BY-SA 3.0) corrige la distorsión y el viñeteado de muchos objetivos. Solo se descarga si usted lo elige." } diff --git a/crates/ui-egui/locales/fi.json b/crates/ui-egui/locales/fi.json index e0a8c622a3..faaa4b7fe6 100644 --- a/crates/ui-egui/locales/fi.json +++ b/crates/ui-egui/locales/fi.json @@ -2327,5 +2327,17 @@ "Drop to open the face model": "Pudota avataksesi kasvomallin", "Dropped Files": "Pudotetut tiedostot", "Finish Lightroom catalog import before adding photos": "Viimeistele Lightroom-luettelon tuonti ennen kuvien lisäämistä", - "Import dropped files without review": "Tuo pudotetut tiedostot ilman tarkistusta" + "Import dropped files without review": "Tuo pudotetut tiedostot ilman tarkistusta", + "Lens Profiles": "Objektiiviprofiilit", + "Download lens profiles?": "Ladataanko objektiiviprofiilit?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft voi korjata monien objektiivien vääristymän ja vinjetoinnin avoimen objektiivitietokannan avulla (lensfun, noin {size} Mt).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Mitään ei ladata, ennen kuin valitset Lataa. Voit ladata sen myöhemmin asetuksista.", + "Lens profiles": "Objektiiviprofiilit", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Objektiiviprofiilit luetaan kansiosta, jonka LIGHTCRAFT_LENSFUN_DIR nimeää.", + "The lens database is installed.": "Objektiivitietokanta on asennettu.", + "The lens database is not installed (about {size} MB).": "Objektiivitietokantaa ei ole asennettu (noin {size} Mt).", + "Lens database": "Objektiivitietokanta", + "Download Lens Database": "Lataa objektiivitietokanta", + "Offer the lens database download when LightCraft starts": "Tarjoa objektiivitietokannan latausta, kun LightCraft käynnistyy", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "lensfunin avoin objektiivitietokanta (CC BY-SA 3.0) korjaa monien objektiivien vääristymän ja vinjetoinnin. Se ladataan vain, jos valitset niin." } diff --git a/crates/ui-egui/locales/fr.json b/crates/ui-egui/locales/fr.json index 09876de29d..b804ecf6d0 100644 --- a/crates/ui-egui/locales/fr.json +++ b/crates/ui-egui/locales/fr.json @@ -2397,5 +2397,17 @@ "1 photo got its lens profile": "1 photo a reçu son profil d'objectif", "Looking up lens profiles": "Recherche des profils d'objectif", "Downloading the lens database": "Téléchargement de la base de données d'objectifs", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "La base de données d’objectifs n’a pas pu être téléchargée. LightCraft réessaiera au prochain démarrage." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "La base de données d’objectifs n’a pas pu être téléchargée. LightCraft réessaiera au prochain démarrage.", + "Lens Profiles": "Profils d’objectif", + "Download lens profiles?": "Télécharger les profils d’objectif ?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft peut corriger la distorsion et le vignetage de nombreux objectifs grâce à la base d’objectifs ouverte (lensfun, environ {size} Mo).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Rien n’est téléchargé tant que vous ne choisissez pas Télécharger. Vous pourrez la télécharger plus tard dans les Réglages.", + "Lens profiles": "Profils d’objectif", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Les profils d’objectif sont lus dans le dossier indiqué par LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "La base d’objectifs est installée.", + "The lens database is not installed (about {size} MB).": "La base d’objectifs n’est pas installée (environ {size} Mo).", + "Lens database": "Base d’objectifs", + "Download Lens Database": "Télécharger la base d’objectifs", + "Offer the lens database download when LightCraft starts": "Proposer le téléchargement de la base d’objectifs au démarrage de LightCraft", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "La base d’objectifs ouverte de lensfun (CC BY-SA 3.0) corrige la distorsion et le vignetage de nombreux objectifs. Elle n’est téléchargée que si vous le choisissez." } diff --git a/crates/ui-egui/locales/id.json b/crates/ui-egui/locales/id.json index 0d6da9b8fb..67bc9e0c54 100644 --- a/crates/ui-egui/locales/id.json +++ b/crates/ui-egui/locales/id.json @@ -2339,5 +2339,17 @@ "Drop to open the face model": "Jatuhkan untuk membuka model wajah", "Dropped Files": "File yang dijatuhkan", "Finish Lightroom catalog import before adding photos": "Selesaikan impor katalog Lightroom sebelum menambahkan foto", - "Import dropped files without review": "Impor file yang dijatuhkan tanpa tinjauan" + "Import dropped files without review": "Impor file yang dijatuhkan tanpa tinjauan", + "Lens Profiles": "Profil lensa", + "Download lens profiles?": "Unduh profil lensa?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft dapat mengoreksi distorsi dan vinyet untuk banyak lensa menggunakan basis data lensa terbuka (lensfun, sekitar {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Tidak ada yang diunduh sampai Anda memilih Unduh. Anda dapat mengunduhnya nanti dari Pengaturan.", + "Lens profiles": "Profil lensa", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Profil lensa dibaca dari folder yang disebut LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "Basis data lensa sudah terpasang.", + "The lens database is not installed (about {size} MB).": "Basis data lensa belum terpasang (sekitar {size} MB).", + "Lens database": "Basis data lensa", + "Download Lens Database": "Unduh Basis Data Lensa", + "Offer the lens database download when LightCraft starts": "Tawarkan unduhan basis data lensa saat LightCraft dimulai", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Basis data lensa terbuka lensfun (CC BY-SA 3.0) mengoreksi distorsi dan vinyet untuk banyak lensa. Basis data ini hanya diunduh jika Anda memilihnya." } diff --git a/crates/ui-egui/locales/it.json b/crates/ui-egui/locales/it.json index 91a7b4645f..e008660cc1 100644 --- a/crates/ui-egui/locales/it.json +++ b/crates/ui-egui/locales/it.json @@ -2368,5 +2368,17 @@ "1 photo got its lens profile": "1 foto ha ricevuto il profilo dell'obiettivo", "Looking up lens profiles": "Ricerca dei profili degli obiettivi", "Downloading the lens database": "Download del database degli obiettivi", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Impossibile scaricare il database degli obiettivi. LightCraft riproverà al prossimo avvio." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Impossibile scaricare il database degli obiettivi. LightCraft riproverà al prossimo avvio.", + "Lens Profiles": "Profili obiettivo", + "Download lens profiles?": "Scaricare i profili degli obiettivi?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft può correggere distorsione e vignettatura per molti obiettivi usando il database aperto degli obiettivi (lensfun, circa {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Non viene scaricato nulla finché non scegli Scarica. Puoi scaricarlo più tardi dalle Impostazioni.", + "Lens profiles": "Profili obiettivo", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "I profili obiettivo vengono letti dalla cartella indicata da LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "Il database degli obiettivi è installato.", + "The lens database is not installed (about {size} MB).": "Il database degli obiettivi non è installato (circa {size} MB).", + "Lens database": "Database obiettivi", + "Download Lens Database": "Scarica database obiettivi", + "Offer the lens database download when LightCraft starts": "Offri il download del database obiettivi all’avvio di LightCraft", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Il database aperto degli obiettivi di lensfun (CC BY-SA 3.0) corregge distorsione e vignettatura per molti obiettivi. Viene scaricato solo se lo scegli." } diff --git a/crates/ui-egui/locales/ja.json b/crates/ui-egui/locales/ja.json index 900986536e..4228d60ea0 100644 --- a/crates/ui-egui/locales/ja.json +++ b/crates/ui-egui/locales/ja.json @@ -2363,5 +2363,17 @@ "1 photo got its lens profile": "1 枚の写真にレンズプロファイルが適用されました", "Looking up lens profiles": "レンズプロファイルを検索中", "Downloading the lens database": "レンズデータベースをダウンロード中", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "レンズデータベースをダウンロードできませんでした。LightCraft は次回の起動時に再試行します。" + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "レンズデータベースをダウンロードできませんでした。LightCraft は次回の起動時に再試行します。", + "Lens Profiles": "レンズプロファイル", + "Download lens profiles?": "レンズプロファイルをダウンロードしますか?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft はオープンなレンズデータベース(lensfun、約 {size} MB)を使って、多くのレンズの歪曲収差と周辺減光を補正できます。", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "「ダウンロード」を選ぶまで何もダウンロードしません。あとで設定からダウンロードすることもできます。", + "Lens profiles": "レンズプロファイル", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "レンズプロファイルは LIGHTCRAFT_LENSFUN_DIR が指定するフォルダーから読み込まれます。", + "The lens database is installed.": "レンズデータベースはインストール済みです。", + "The lens database is not installed (about {size} MB).": "レンズデータベースはインストールされていません(約 {size} MB)。", + "Lens database": "レンズデータベース", + "Download Lens Database": "レンズデータベースをダウンロード", + "Offer the lens database download when LightCraft starts": "LightCraft の起動時にレンズデータベースのダウンロードを提案する", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "lensfun のオープンなレンズデータベース(CC BY-SA 3.0)は多くのレンズの歪曲収差と周辺減光を補正します。選んだときにだけダウンロードされます。" } diff --git a/crates/ui-egui/locales/ka.json b/crates/ui-egui/locales/ka.json index 7079370827..621186e507 100644 --- a/crates/ui-egui/locales/ka.json +++ b/crates/ui-egui/locales/ka.json @@ -2741,5 +2741,17 @@ "1 photo got its lens profile": "1 ფოტომ მიიღო ობიექტივის პროფილი", "Looking up lens profiles": "ობიექტივის პროფილების ძიება", "Downloading the lens database": "ობიექტივების ბაზის ჩამოტვირთვა", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "ობიექტივების მონაცემთა ბაზის ჩამოტვირთვა ვერ მოხერხდა. LightCraft კვლავ სცდის შემდეგ გაშვებისას." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "ობიექტივების მონაცემთა ბაზის ჩამოტვირთვა ვერ მოხერხდა. LightCraft კვლავ სცდის შემდეგ გაშვებისას.", + "Lens Profiles": "ობიექტივის პროფილები", + "Download lens profiles?": "ჩამოვტვირთო ობიექტივის პროფილები?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft-ს შეუძლია მრავალი ობიექტივის დისტორსიისა და ვინეტირების გასწორება ობიექტივების ღია ბაზით (lensfun, დაახლოებით {size} მბ).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "არაფერი ჩამოიტვირთება, სანამ „ჩამოტვირთვას“ არ აირჩევთ. მოგვიანებით მისი ჩამოტვირთვა პარამეტრებიდანაც შეგიძლიათ.", + "Lens profiles": "ობიექტივის პროფილები", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "ობიექტივის პროფილები იკითხება საქაღალდიდან, რომელსაც LIGHTCRAFT_LENSFUN_DIR ასახელებს.", + "The lens database is installed.": "ობიექტივების ბაზა დაყენებულია.", + "The lens database is not installed (about {size} MB).": "ობიექტივების ბაზა არ არის დაყენებული (დაახლოებით {size} მბ).", + "Lens database": "ობიექტივების ბაზა", + "Download Lens Database": "ობიექტივების ბაზის ჩამოტვირთვა", + "Offer the lens database download when LightCraft starts": "LightCraft-ის გაშვებისას ობიექტივების ბაზის ჩამოტვირთვის შეთავაზება", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "lensfun-ის ობიექტივების ღია ბაზა (CC BY-SA 3.0) ასწორებს მრავალი ობიექტივის დისტორსიასა და ვინეტირებას. ის მხოლოდ თქვენი არჩევით ჩამოიტვირთება." } diff --git a/crates/ui-egui/locales/pl.json b/crates/ui-egui/locales/pl.json index bc75382a21..f5e3a2cf6b 100644 --- a/crates/ui-egui/locales/pl.json +++ b/crates/ui-egui/locales/pl.json @@ -2290,5 +2290,17 @@ "1 photo got its lens profile": "1 zdjęcie otrzymało profil obiektywu", "Looking up lens profiles": "Wyszukiwanie profili obiektywów", "Downloading the lens database": "Pobieranie bazy obiektywów", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Nie udało się pobrać bazy obiektywów. LightCraft spróbuje ponownie przy następnym uruchomieniu." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Nie udało się pobrać bazy obiektywów. LightCraft spróbuje ponownie przy następnym uruchomieniu.", + "Lens Profiles": "Profile obiektywów", + "Download lens profiles?": "Pobrać profile obiektywów?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft może korygować dystorsję i winietowanie wielu obiektywów, korzystając z otwartej bazy obiektywów (lensfun, około {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Nic nie zostanie pobrane, dopóki nie wybierzesz Pobierz. Możesz ją pobrać później w Ustawieniach.", + "Lens profiles": "Profile obiektywów", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Profile obiektywów są odczytywane z folderu wskazanego przez LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "Baza obiektywów jest zainstalowana.", + "The lens database is not installed (about {size} MB).": "Baza obiektywów nie jest zainstalowana (około {size} MB).", + "Lens database": "Baza obiektywów", + "Download Lens Database": "Pobierz bazę obiektywów", + "Offer the lens database download when LightCraft starts": "Proponuj pobranie bazy obiektywów przy uruchomieniu LightCraft", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Otwarta baza obiektywów lensfun (CC BY-SA 3.0) koryguje dystorsję i winietowanie wielu obiektywów. Jest pobierana tylko wtedy, gdy to wybierzesz." } diff --git a/crates/ui-egui/locales/pt-br.json b/crates/ui-egui/locales/pt-br.json index b1ba74437c..29789d9c63 100644 --- a/crates/ui-egui/locales/pt-br.json +++ b/crates/ui-egui/locales/pt-br.json @@ -2209,5 +2209,17 @@ "1 photo got its lens profile": "1 foto recebeu o perfil da lente", "Looking up lens profiles": "Procurando perfis de lente", "Downloading the lens database": "Baixando o banco de dados de lentes", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Não foi possível baixar o banco de dados de lentes. O LightCraft tentará novamente na próxima vez que for aberto." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Não foi possível baixar o banco de dados de lentes. O LightCraft tentará novamente na próxima vez que for aberto.", + "Lens Profiles": "Perfis de lente", + "Download lens profiles?": "Baixar perfis de lente?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "O LightCraft pode corrigir distorção e vinheta de muitas lentes usando o banco de dados aberto de lentes (lensfun, cerca de {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Nada é baixado até você escolher Baixar. Você pode baixá-lo depois em Configurações.", + "Lens profiles": "Perfis de lente", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Os perfis de lente são lidos da pasta indicada por LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "O banco de dados de lentes está instalado.", + "The lens database is not installed (about {size} MB).": "O banco de dados de lentes não está instalado (cerca de {size} MB).", + "Lens database": "Banco de dados de lentes", + "Download Lens Database": "Baixar banco de dados de lentes", + "Offer the lens database download when LightCraft starts": "Oferecer o download do banco de dados de lentes ao iniciar o LightCraft", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "O banco de dados aberto de lentes do lensfun (CC BY-SA 3.0) corrige distorção e vinheta de muitas lentes. Ele só é baixado quando você escolhe." } diff --git a/crates/ui-egui/locales/ru.json b/crates/ui-egui/locales/ru.json index f34b82c585..8ad3173d7b 100644 --- a/crates/ui-egui/locales/ru.json +++ b/crates/ui-egui/locales/ru.json @@ -2356,5 +2356,17 @@ "1 photo got its lens profile": "1 фото получило профиль объектива", "Looking up lens profiles": "Поиск профилей объективов", "Downloading the lens database": "Загрузка базы объективов", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Не удалось загрузить базу данных объективов. LightCraft попробует снова при следующем запуске." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Не удалось загрузить базу данных объективов. LightCraft попробует снова при следующем запуске.", + "Lens Profiles": "Профили объективов", + "Download lens profiles?": "Загрузить профили объективов?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft может исправлять дисторсию и виньетирование для многих объективов с помощью открытой базы объективов (lensfun, около {size} МБ).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Ничего не загружается, пока вы не выберете «Загрузить». Загрузить её можно и позже в настройках.", + "Lens profiles": "Профили объективов", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Профили объективов читаются из папки, указанной в LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "База объективов установлена.", + "The lens database is not installed (about {size} MB).": "База объективов не установлена (около {size} МБ).", + "Lens database": "База объективов", + "Download Lens Database": "Загрузить базу объективов", + "Offer the lens database download when LightCraft starts": "Предлагать загрузку базы объективов при запуске LightCraft", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Открытая база объективов lensfun (CC BY-SA 3.0) исправляет дисторсию и виньетирование для многих объективов. Она загружается, только если вы это выберете." } diff --git a/crates/ui-egui/locales/sv.json b/crates/ui-egui/locales/sv.json index 66e69b7ba5..5dc2f592d8 100644 --- a/crates/ui-egui/locales/sv.json +++ b/crates/ui-egui/locales/sv.json @@ -2429,5 +2429,17 @@ "Drop to open the face model": "Släpp för att öppna ansiktsmodellen", "Dropped Files": "Släppta filer", "Finish Lightroom catalog import before adding photos": "Slutför importen av Lightroom-katalogen innan du lägger till foton", - "Import dropped files without review": "Importera släppta filer utan granskning" + "Import dropped files without review": "Importera släppta filer utan granskning", + "Lens Profiles": "Objektivprofiler", + "Download lens profiles?": "Hämta objektivprofiler?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft kan korrigera distorsion och vinjettering för många objektiv med den öppna objektivdatabasen (lensfun, cirka {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Inget hämtas förrän du väljer Hämta. Du kan hämta den senare i Inställningar.", + "Lens profiles": "Objektivprofiler", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Objektivprofiler läses från mappen som LIGHTCRAFT_LENSFUN_DIR anger.", + "The lens database is installed.": "Objektivdatabasen är installerad.", + "The lens database is not installed (about {size} MB).": "Objektivdatabasen är inte installerad (cirka {size} MB).", + "Lens database": "Objektivdatabas", + "Download Lens Database": "Hämta objektivdatabas", + "Offer the lens database download when LightCraft starts": "Erbjud att hämta objektivdatabasen när LightCraft startar", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "lensfuns öppna objektivdatabas (CC BY-SA 3.0) korrigerar distorsion och vinjettering för många objektiv. Den hämtas bara när du väljer det." } diff --git a/crates/ui-egui/locales/uk.json b/crates/ui-egui/locales/uk.json index 9743edbbd9..8288592126 100644 --- a/crates/ui-egui/locales/uk.json +++ b/crates/ui-egui/locales/uk.json @@ -2361,5 +2361,17 @@ "1 photo got its lens profile": "1 фото отримало профіль об'єктива", "Looking up lens profiles": "Пошук профілів об'єктивів", "Downloading the lens database": "Завантаження бази об'єктивів", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Не вдалося завантажити базу даних об’єктивів. LightCraft спробує знову під час наступного запуску." + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "Не вдалося завантажити базу даних об’єктивів. LightCraft спробує знову під час наступного запуску.", + "Lens Profiles": "Профілі об’єктивів", + "Download lens profiles?": "Завантажити профілі об’єктивів?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft може виправляти дисторсію та віньєтування для багатьох об’єктивів за допомогою відкритої бази об’єктивів (lensfun, близько {size} МБ).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Нічого не завантажується, доки ви не виберете «Завантажити». Завантажити її можна й пізніше в налаштуваннях.", + "Lens profiles": "Профілі об’єктивів", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Профілі об’єктивів читаються з теки, яку вказує LIGHTCRAFT_LENSFUN_DIR.", + "The lens database is installed.": "Базу об’єктивів установлено.", + "The lens database is not installed (about {size} MB).": "Базу об’єктивів не встановлено (близько {size} МБ).", + "Lens database": "База об’єктивів", + "Download Lens Database": "Завантажити базу об’єктивів", + "Offer the lens database download when LightCraft starts": "Пропонувати завантажити базу об’єктивів під час запуску LightCraft", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Відкрита база об’єктивів lensfun (CC BY-SA 3.0) виправляє дисторсію та віньєтування для багатьох об’єктивів. Вона завантажується, лише якщо ви це виберете." } diff --git a/crates/ui-egui/locales/vi.json b/crates/ui-egui/locales/vi.json index e4fb8feb81..eda44a1634 100644 --- a/crates/ui-egui/locales/vi.json +++ b/crates/ui-egui/locales/vi.json @@ -2235,5 +2235,17 @@ "Drop to open the face model": "Thả để mở mô hình khuôn mặt", "Dropped Files": "Tệp đã thả", "Finish Lightroom catalog import before adding photos": "Hoàn tất nhập danh mục Lightroom trước khi thêm ảnh", - "Import dropped files without review": "Nhập các tệp đã thả mà không xem lại" + "Import dropped files without review": "Nhập các tệp đã thả mà không xem lại", + "Lens Profiles": "Hồ sơ ống kính", + "Download lens profiles?": "Tải hồ sơ ống kính?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft có thể sửa méo hình và tối góc cho nhiều ống kính bằng cơ sở dữ liệu ống kính mở (lensfun, khoảng {size} MB).", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "Không có gì được tải xuống cho đến khi bạn chọn Tải xuống. Bạn có thể tải sau trong Cài đặt.", + "Lens profiles": "Hồ sơ ống kính", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "Hồ sơ ống kính được đọc từ thư mục mà LIGHTCRAFT_LENSFUN_DIR chỉ định.", + "The lens database is installed.": "Cơ sở dữ liệu ống kính đã được cài đặt.", + "The lens database is not installed (about {size} MB).": "Cơ sở dữ liệu ống kính chưa được cài đặt (khoảng {size} MB).", + "Lens database": "Cơ sở dữ liệu ống kính", + "Download Lens Database": "Tải cơ sở dữ liệu ống kính", + "Offer the lens database download when LightCraft starts": "Đề nghị tải cơ sở dữ liệu ống kính khi LightCraft khởi động", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "Cơ sở dữ liệu ống kính mở của lensfun (CC BY-SA 3.0) sửa méo hình và tối góc cho nhiều ống kính. Nó chỉ được tải khi bạn chọn." } diff --git a/crates/ui-egui/locales/zh-hans.json b/crates/ui-egui/locales/zh-hans.json index 8e031f89f3..3a20f70eb4 100644 --- a/crates/ui-egui/locales/zh-hans.json +++ b/crates/ui-egui/locales/zh-hans.json @@ -2207,5 +2207,17 @@ "1 photo got its lens profile": "1 张照片获得了镜头配置文件", "Looking up lens profiles": "正在查找镜头配置文件", "Downloading the lens database": "正在下载镜头数据库", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "无法下载镜头数据库。LightCraft 会在下次启动时重试。" + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "无法下载镜头数据库。LightCraft 会在下次启动时重试。", + "Lens Profiles": "镜头配置文件", + "Download lens profiles?": "下载镜头配置文件?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft 可以使用开放的镜头数据库(lensfun,约 {size} MB)校正许多镜头的畸变和暗角。", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "在您选择“下载”之前不会下载任何内容。您也可以稍后在设置中下载。", + "Lens profiles": "镜头配置文件", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "镜头配置文件从 LIGHTCRAFT_LENSFUN_DIR 指定的文件夹读取。", + "The lens database is installed.": "镜头数据库已安装。", + "The lens database is not installed (about {size} MB).": "镜头数据库未安装(约 {size} MB)。", + "Lens database": "镜头数据库", + "Download Lens Database": "下载镜头数据库", + "Offer the lens database download when LightCraft starts": "LightCraft 启动时提示下载镜头数据库", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "lensfun 的开放镜头数据库(CC BY-SA 3.0)可校正许多镜头的畸变和暗角。只有在您选择时才会下载。" } diff --git a/crates/ui-egui/locales/zh-hant.json b/crates/ui-egui/locales/zh-hant.json index 47b26508b4..00cbb69536 100644 --- a/crates/ui-egui/locales/zh-hant.json +++ b/crates/ui-egui/locales/zh-hant.json @@ -2164,5 +2164,17 @@ "1 photo got its lens profile": "1 張照片取得了鏡頭設定檔", "Looking up lens profiles": "正在查找鏡頭設定檔", "Downloading the lens database": "正在下載鏡頭資料庫", - "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "無法下載鏡頭資料庫。LightCraft 會在下次啟動時重試。" + "The lens database could not be downloaded. LightCraft tries again the next time it starts.": "無法下載鏡頭資料庫。LightCraft 會在下次啟動時重試。", + "Lens Profiles": "鏡頭描述檔", + "Download lens profiles?": "下載鏡頭描述檔?", + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).": "LightCraft 可以使用開放的鏡頭資料庫(lensfun,約 {size} MB)校正許多鏡頭的變形與暗角。", + "Nothing is downloaded until you choose Download. You can download it later from Settings.": "在您選擇「下載」之前不會下載任何內容。您也可以稍後在設定中下載。", + "Lens profiles": "鏡頭描述檔", + "Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.": "鏡頭描述檔從 LIGHTCRAFT_LENSFUN_DIR 指定的資料夾讀取。", + "The lens database is installed.": "鏡頭資料庫已安裝。", + "The lens database is not installed (about {size} MB).": "鏡頭資料庫未安裝(約 {size} MB)。", + "Lens database": "鏡頭資料庫", + "Download Lens Database": "下載鏡頭資料庫", + "Offer the lens database download when LightCraft starts": "LightCraft 啟動時提示下載鏡頭資料庫", + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.": "lensfun 的開放鏡頭資料庫(CC BY-SA 3.0)可校正許多鏡頭的變形與暗角。只有在您選擇時才會下載。" } diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 772303ffbf..4802c8a637 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -151,6 +151,7 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context, blocked: bool) { Dialog::MovePhotos { .. } => "Move Photos", Dialog::LightroomCatalog { .. } => "Lightroom Classic Catalog", Dialog::LightroomDestination { .. } => "Import Lightroom Catalog", + Dialog::LensDatabase { .. } => "Lens Profiles", Dialog::SaveEffect { .. } => "New Effect Preset", Dialog::ManageEffects { .. } => "Manage Effect Presets", Dialog::DeleteEffects { names } if names.len() == 1 => "Delete Effect Preset", @@ -208,6 +209,7 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context, blocked: bool) { Dialog::FaceModel { .. } => 460.0, Dialog::LightroomCatalog { .. } => 440.0, Dialog::LightroomDestination { .. } => 480.0, + Dialog::LensDatabase { .. } => 440.0, Dialog::ManageEffects { .. } => 560.0, _ => 380.0, }) @@ -1195,6 +1197,7 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context, blocked: bool) { ui.label(egui::RichText::new(crate::i18n::tr(what)).color(t.text_dim)); } d @ Dialog::LightroomDestination { .. } => crate::lightroom_destination::body(app, ui, d), + Dialog::LensDatabase { .. } => crate::lens_db::offer(ui), Dialog::RemoveFolder { name, count, path, disk } => { ui.label(crate::i18n::tr_format!( "Remove “{name}” and its {count} photo{} from the library?", @@ -1281,7 +1284,7 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context, blocked: bool) { let sam_nowhere = sam_by_hand(sam); let cancel = match &dlg { Dialog::SamModel { .. } if sam_running || sam_installed || sam_nowhere => "Close", - Dialog::SamModel { .. } => "Not Now", + Dialog::SamModel { .. } | Dialog::LensDatabase { .. } => "Not Now", Dialog::LightroomResult { .. } => "Close", _ => "Cancel", }; @@ -1313,6 +1316,7 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context, blocked: bool) { Dialog::RemoveFolder { .. } => "Remove", Dialog::LightroomCatalog { .. } => "Import Lightroom Catalog…", d @ Dialog::LightroomDestination { .. } => crate::lightroom_destination::ok_label(d), + Dialog::LensDatabase { .. } => "Download", Dialog::SynchronizeFolder { .. } if informational => "Close", Dialog::SynchronizeFolder { .. } => "Synchronize", Dialog::SamModel { then: Some(_), .. } if sam_installed => "Continue", diff --git a/crates/ui-egui/src/dialogs/confirm.rs b/crates/ui-egui/src/dialogs/confirm.rs index 196018e551..38a3aad46f 100644 --- a/crates/ui-egui/src/dialogs/confirm.rs +++ b/crates/ui-egui/src/dialogs/confirm.rs @@ -222,6 +222,9 @@ pub fn confirm_dialog(app: &mut LightcraftApp, dlg: &Dialog) -> Result crate::lightroom_destination::confirm(app, d), + Dialog::LensDatabase { dir, mirrors } => { + crate::lens_db::download(app, std::path::Path::new(dir), mirrors.clone()).map(|()| json!({"downloading": true})) + } Dialog::SaveEffect { name, group, fields, amount, brush } => { crate::panels::mask_effects::save_confirm(app, name, group, fields, *amount, *brush) } diff --git a/crates/ui-egui/src/dialogs/settings/general.rs b/crates/ui-egui/src/dialogs/settings/general.rs index 21b94f150f..b4f1c3831d 100644 --- a/crates/ui-egui/src/dialogs/settings/general.rs +++ b/crates/ui-egui/src/dialogs/settings/general.rs @@ -105,6 +105,50 @@ pub(super) fn general_tab(app: &mut LightcraftApp, ui: &mut egui::Ui, t: &Tokens "To hand this editor the original file itself instead (the raw, without a copy: DxO PhotoLab, for example), right-click a photo → Edit In → Open Original in Other Editor (RAW, no copy).", ), ); + #[cfg(not(target_arch = "wasm32"))] + lens_profiles(app, ui, t); +} + +/// Lens profiles: whether the lens database is here, a button that downloads it (nothing is fetched without a +/// click: the first launch offers it once, [`crate::lens_db`]), and the startup offer turned back on or off. +#[cfg(not(target_arch = "wasm32"))] +fn lens_profiles(app: &mut LightcraftApp, ui: &mut egui::Ui, t: &Tokens) { + use lightcraft_engine::lensfun_download::{self, Offer}; + heading(ui, t, crate::i18n::tr("Lens profiles")); + let running = app.session.lensfun.running(); + // (the user's own folder, LIGHTCRAFT_LENSFUN_DIR, is read instead and never replaced) + let own = std::env::var_os(lightcraft_engine::lens_profiles::ENV).is_some_and(|v| !v.is_empty()); + let installed = lensfun_download::default_dir().is_some_and(|d| lensfun_download::installed(&d)); + let status = if own { + crate::i18n::tr("Lens profiles are read from the folder LIGHTCRAFT_LENSFUN_DIR names.").to_string() + } else if running { + crate::i18n::tr("Downloading the lens database…").to_string() + } else if installed { + crate::i18n::tr("The lens database is installed.").to_string() + } else { + crate::i18n::tr_format!("The lens database is not installed (about {size} MB).", size = lensfun_download::download_megabytes()) + }; + row(ui, t, crate::i18n::tr("Lens database"), |ui| { + ui.label(RichText::new(status).color(t.text)); + let r = ui.add_enabled(!own && !running && !installed, egui::Button::new(crate::i18n::tr("Download Lens Database"))); + register(ui, "button:settingsLensDatabase", r.rect); + if r.clicked() + && let Err(e) = crate::lens_db::download_default(app) + { + app.toast(ui.ctx(), e); + } + }); + let mut offer = app.ui.settings.lens_database != Offer::Declined; + if check(ui, "settings.lensDatabaseOffer", &mut offer, "Offer the lens database download when LightCraft starts") { + app.ui.settings.lens_database = if offer { Offer::Ask } else { Offer::Declined }; + } + hint( + ui, + t, + crate::i18n::tr( + "lensfun's open lens database (CC BY-SA 3.0) corrects distortion and vignetting for many lenses. It is only downloaded when you choose to.", + ), + ); } /// The file Edit In hands an editor: format, colour space and bit depth (`button:{id}Format-…`). diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs index 92b0d00178..57bc07cab4 100644 --- a/crates/ui-egui/src/lens_db.rs +++ b/crates/ui-egui/src/lens_db.rs @@ -1,5 +1,7 @@ -//! The lens database without setup: on first run the app fetches lensfun's database by itself -//! (`lightcraft_engine::lensfun_download`, an activity row while it runs), and whenever the database in use changes +//! The lens database on request: the first launch offers lensfun's database once (its size, Download / Not Now, +//! [`offer`]), Settings ▸ General can download it any time ([`download_default`]), and nothing is fetched without +//! that click (`lightcraft_engine::lensfun_download::fetch_on_start` decides; an activity row while it runs). +//! Whenever the database in use changes //! (it arrived, a newer pin replaced it) or Enable Lens Corrections is ticked on a photo without a profile, the //! library's raws that have none are looked up again on a worker thread (`lightcraft_engine::cmd::lens_refresh`); //! when its files were replaced, so are those whose profile came from the database and those that named no lens. @@ -18,18 +20,75 @@ pub fn tick(app: &mut LightcraftApp) { refresh(app); } -/// Once per run, when the start policy says so ([`lensfun_download::fetch_on_start`]: the host allows it and the -/// pinned database is missing or damaged). +/// Once per run, what the start policy says ([`lensfun_download::fetch_on_start`]): offer the database, fetch it +/// (the user said yes before), or nothing. The offer waits for the window to be free (another dialog, the library +/// chooser). fn start_download(app: &mut LightcraftApp) { if app.ui.lens_download_checked { return; } - app.ui.lens_download_checked = true; - let Some(dir) = lensfun_download::fetch_on_start(app.session.lens_auto_download) else { return }; - let activity = app.session.activity.clone(); - if let Err(e) = app.session.lensfun.start(&dir, lensfun_download::mirrors(), lensfun_download::FILES, &activity) { - log::warn!("lens database: {e}"); + if !app.session.lens_auto_download { + app.ui.lens_download_checked = true; + return; } + if app.ui.dialog.is_some() || crate::panels::library_problem::blocks_dialog_input(app) { + return; + } + app.ui.lens_download_checked = true; + let start = lensfun_download::fetch_on_start(app.session.lens_auto_download, app.ui.settings.lens_database); + begin(app, start, lensfun_download::mirrors()); +} + +/// Act on the start policy's answer, fetching from `mirrors`. The offer counts as answered once it is shown: closing +/// it any way but Download (Not Now, Esc) is "not now", and the next start doesn't ask again. +pub(crate) fn begin(app: &mut LightcraftApp, start: lensfun_download::Start, mirrors: Vec) { + match start { + lensfun_download::Start::Nothing => {} + lensfun_download::Start::Ask(dir) => { + app.ui.settings.lens_database = lensfun_download::Offer::Declined; + app.ui.dialog = Some(crate::state::Dialog::LensDatabase { dir: dir.to_string_lossy().into_owned(), mirrors }); + } + lensfun_download::Start::Fetch(dir) => { + if let Err(e) = download(app, &dir, mirrors) { + log::warn!("lens database: {e}"); + } + } + } +} + +/// Download the database into `dir` from `mirrors` (the offer's Download, Settings' button): remembered as the +/// user's yes, shown in the activity stack, and read once it is in place. +pub(crate) fn download(app: &mut LightcraftApp, dir: &std::path::Path, mirrors: Vec) -> Result<(), String> { + app.ui.settings.lens_database = lensfun_download::Offer::Accepted; + lens_refresh::start_download(&mut app.session, dir, mirrors, lensfun_download::FILES) +} + +/// Settings' Download Lens Database: into the app's own folder, from the usual mirrors. +pub fn download_default(app: &mut LightcraftApp) -> Result<(), String> { + let dir = lensfun_download::default_dir().ok_or("this computer has no folder for LightCraft's data")?; + download(app, &dir, lensfun_download::mirrors()) +} + +/// The offer's window: what the database does, its size, and that Settings has it later. +pub fn offer(ui: &mut egui::Ui) { + let t = crate::theme::Tokens::get(ui.ctx()); + ui.label(egui::RichText::new(crate::i18n::tr("Download lens profiles?")).strong()); + ui.add_space(4.0); + ui.add( + egui::Label::new(crate::i18n::tr_format!( + "LightCraft can correct distortion and vignetting for many lenses using the open lens database (lensfun, about {size} MB).", + size = lensfun_download::download_megabytes() + )) + .wrap(), + ); + ui.add_space(4.0); + ui.add( + egui::Label::new( + egui::RichText::new(crate::i18n::tr("Nothing is downloaded until you choose Download. You can download it later from Settings.")) + .color(t.text_dim), + ) + .wrap(), + ); } /// After Enable Lens Corrections was ticked on photo `id`: the photos to look up again, those of `id` and the diff --git a/crates/ui-egui/src/state/dialog.rs b/crates/ui-egui/src/state/dialog.rs index edc4946cd3..8b11a0582e 100644 --- a/crates/ui-egui/src/state/dialog.rs +++ b/crates/ui-egui/src/state/dialog.rs @@ -246,6 +246,12 @@ pub enum Dialog { #[serde(default)] error: Option, }, + /// The first launch offers the lens database (`crate::lens_db`): its size, Download or Not Now. `dir`: where it + /// goes; `mirrors`: where it comes from. + LensDatabase { + dir: String, + mirrors: Vec, + }, /// Open Library was given a Lightroom Classic catalog (issue #891): offer File ▸ Import /// Lightroom Catalog… with `catalog` (the `.lrcat` found; `None`: ask for it) instead. /// `folder`: what was chosen. diff --git a/crates/ui-egui/src/state/settings.rs b/crates/ui-egui/src/state/settings.rs index 2d748f5a75..ee299b2eb5 100644 --- a/crates/ui-egui/src/state/settings.rs +++ b/crates/ui-egui/src/state/settings.rs @@ -193,6 +193,10 @@ pub struct AppSettings { /// gets a new one, or always merge (issue #2672; the window's "Don't ask again" picks that). #[serde(deserialize_with = "lenient")] pub lightroom_import: LightroomImport, + /// The answer to the first-launch offer of the lens database (Download / Not Now; Settings ▸ General ▸ Lens + /// profiles changes it). A value this version doesn't read asks again. + #[serde(deserialize_with = "lenient")] + pub lens_database: lightcraft_engine::lensfun_download::Offer, } /// Where File ▸ Import Lightroom Catalog… puts a catalog (Settings ▸ General ▸ Library). Saved as @@ -242,6 +246,7 @@ impl Default for AppSettings { zoom_to_focus: true, loupe_info: LoupeInfo::default(), lightroom_import: LightroomImport::Ask, + lens_database: Default::default(), } } } diff --git a/crates/ui-egui/src/tests.rs b/crates/ui-egui/src/tests.rs index 24ba679324..76ae7b37b6 100644 --- a/crates/ui-egui/src/tests.rs +++ b/crates/ui-egui/src/tests.rs @@ -42,6 +42,7 @@ mod labels; mod launch_library; mod layout_image; mod lens_lookup; +mod lens_offer; mod library_problem; mod lifecycle_037; mod lightroom_destination; diff --git a/crates/ui-egui/src/tests/lens_offer.rs b/crates/ui-egui/src/tests/lens_offer.rs new file mode 100644 index 0000000000..0a759f9437 --- /dev/null +++ b/crates/ui-egui/src/tests/lens_offer.rs @@ -0,0 +1,112 @@ +//! The lens database is offered once, on the first launch, and downloaded only after a click: Download in the offer +//! or in Settings. Not Now downloads nothing and the next start doesn't ask; an installed database and headless +//! hosts (CLI, MCP) never ask (`crate::lens_db`, `lightcraft_engine::lensfun_download::fetch_on_start`). + +use lightcraft_engine::lensfun_download::{self, Offer, Start}; +use lightcraft_engine::{Session, lens_profiles}; + +use crate::state::Dialog; +use crate::{LightcraftApp, Services}; + +/// A desktop-like app (allowed to ask) and a folder of this test's own for the database. +fn desktop(name: &str) -> (LightcraftApp, std::path::PathBuf) { + let mut session = Session::with_demo(); + session.lens_auto_download = true; + let app = LightcraftApp::new(session, Services { png: None, ..Default::default() }); + let unique = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos()); + let root = std::env::temp_dir().join(format!("lc-lens-offer-{name}-{}-{unique}", std::process::id())); + (app, root) +} + +/// Nothing listens on port 1: a download started in a test fails at once, without the network. +fn offline() -> Vec { + vec!["http://127.0.0.1:1/db".into()] +} + +fn wait_idle(app: &LightcraftApp) { + let t0 = std::time::Instant::now(); + while app.session.lensfun.running() && t0.elapsed() < std::time::Duration::from_secs(60) { + std::thread::sleep(std::time::Duration::from_millis(20)); + } +} + +/// The first start shows the offer and fetches nothing; Download starts the download (an activity row) and is +/// remembered as the user's yes. +#[test] +fn the_first_start_offers_the_database_and_fetches_nothing_until_download() { + let (mut app, root) = desktop("first"); + let dir = root.join("lensfun"); + let before = lens_profiles::host_dir(); + crate::lens_db::begin(&mut app, lensfun_download::decide(true, false, Some(dir.clone()), false, Offer::Ask), offline()); + let Some(dlg @ Dialog::LensDatabase { .. }) = app.ui.dialog.clone() else { panic!("no offer: {:?}", app.ui.dialog) }; + assert_eq!(app.session.lensfun.state(), lensfun_download::State::Idle, "nothing fetched while it asks"); + assert!(app.session.activity.list().is_empty()); + crate::panels::dialogs::confirm_dialog(&mut app, &dlg).expect("Download"); + assert_ne!(app.session.lensfun.state(), lensfun_download::State::Idle, "Download starts it"); + assert_eq!(app.ui.settings.lens_database, Offer::Accepted); + wait_idle(&app); + lens_profiles::use_dir(before); + let _ = std::fs::remove_dir_all(&root); +} + +/// Not Now (or Esc: closing the offer any way but Download) downloads nothing, is saved with the settings, and the +/// next start doesn't ask. +#[test] +fn not_now_is_remembered_and_the_next_start_does_not_ask() { + let (mut app, root) = desktop("notnow"); + crate::lens_db::begin(&mut app, Start::Ask(root.join("lensfun")), offline()); + assert!(matches!(app.ui.dialog, Some(Dialog::LensDatabase { .. }))); + app.ui.dialog = None; // Not Now + assert_eq!(app.session.lensfun.state(), lensfun_download::State::Idle, "nothing fetched"); + assert_eq!(app.ui.settings.lens_database, Offer::Declined); + // saved and read back with the app's settings + let saved = serde_json::to_value(&app.ui.settings).expect("settings save"); + let read: crate::state::AppSettings = serde_json::from_value(saved).expect("settings read"); + assert_eq!(read.lens_database, Offer::Declined); + assert_eq!(lensfun_download::decide(true, false, Some(root.join("lensfun")), false, read.lens_database), Start::Nothing, "not asked again"); + // a damaged value asks again instead of failing to read the settings + let mut damaged = serde_json::to_value(&app.ui.settings).expect("settings save"); + damaged["lensDatabase"] = serde_json::json!(42); + let read: crate::state::AppSettings = serde_json::from_value(damaged).expect("a bad value doesn't spoil the settings"); + assert_eq!(read.lens_database, Offer::Ask); +} + +/// Settings' Download Lens Database downloads it (after a Not Now, too) and remembers the yes. +#[test] +fn the_settings_button_downloads_the_database() { + let (mut app, root) = desktop("settings"); + let before = lens_profiles::host_dir(); + app.ui.settings.lens_database = Offer::Declined; + crate::lens_db::download(&mut app, &root.join("lensfun"), offline()).expect("started"); + assert_ne!(app.session.lensfun.state(), lensfun_download::State::Idle, "the download ran"); + assert_eq!(app.ui.settings.lens_database, Offer::Accepted); + assert_eq!(lens_profiles::host_dir(), Some(root.join("lensfun")), "and is read once it is in place"); + wait_idle(&app); + lens_profiles::use_dir(before); + let _ = std::fs::remove_dir_all(&root); +} + +/// An installed database means no offer and no download. +#[test] +fn an_installed_database_is_not_offered() { + let (mut app, root) = desktop("installed"); + for offer in [Offer::Ask, Offer::Accepted, Offer::Declined] { + crate::lens_db::begin(&mut app, lensfun_download::decide(true, false, Some(root.join("lensfun")), true, offer), offline()); + assert!(app.ui.dialog.is_none(), "{offer:?}"); + assert_eq!(app.session.lensfun.state(), lensfun_download::State::Idle); + } +} + +/// A headless app (CLI snapshot, MCP) never shows the offer and never fetches, whatever the settings say. +#[test] +fn a_headless_app_never_offers_or_fetches() { + let mut app = LightcraftApp::new(Session::with_demo(), Services { png: None, ..Default::default() }); + assert!(!app.session.lens_auto_download); + for offer in [Offer::Ask, Offer::Accepted] { + app.ui.settings.lens_database = offer; + app.ui.lens_download_checked = false; + crate::lens_db::tick(&mut app); + assert!(app.ui.dialog.is_none()); + assert_eq!(app.session.lensfun.state(), lensfun_download::State::Idle); + } +} diff --git a/docs/gaps.md b/docs/gaps.md index c2f0e4c18d..03a705c4c1 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Gaps: where LightCraft falls short of Lightroom -> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-27 · **Change:** minor (gap 8: lensfun's lens database downloaded on first run, looked up for the library; gap 30: the Info panel shows the selected photos' shared metadata and marks mixed fields; gap 28: ⌘⌥⇧C / ⌘⌥⇧V copy and paste metadata; gap 13: before a Lightroom catalog import the app asks merge or new library, #2672; gap 28: F8 shows / hides the right panel; import review album hierarchy, #2362; gap 20: Minolta MRW decodes; gap 10: 551 models verified; gap 15: Print Page Options — page numbers, page info, crop marks; gap 28: `\` shows / hides the Filter Bar in the grids; gap 21: Indonesian interface shipped, every message but What's New; gap 27: the Windows x64 installer verified on Windows 11; the Settings → Apps uninstall confirmation remains; gap 28: ⌘L Enable Filters switches the filter off and on; gap 15: Japanese photo and postcard paper sizes in the Print page setup and PDF) +> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-28 · **Change:** minor (gap 8: lensfun's lens database offered on the first launch and downloaded on request, looked up for the library; gap 30: the Info panel shows the selected photos' shared metadata and marks mixed fields; gap 28: ⌘⌥⇧C / ⌘⌥⇧V copy and paste metadata; gap 13: before a Lightroom catalog import the app asks merge or new library, #2672; gap 28: F8 shows / hides the right panel; import review album hierarchy, #2362; gap 20: Minolta MRW decodes; gap 10: 551 models verified; gap 15: Print Page Options — page numbers, page info, crop marks; gap 28: `\` shows / hides the Filter Bar in the grids; gap 21: Indonesian interface shipped, every message but What's New; gap 27: the Windows x64 installer verified on Windows 11; the Settings → Apps uninstall confirmation remains; gap 28: ⌘L Enable Filters switches the filter off and on; gap 15: Japanese photo and postcard paper sizes in the Print page setup and PDF) Every known shortfall, one entry each, ranked by user impact (how many Lightroom users hit it, and how hard it blocks them), then by effort. **Agents pick work from the top** (the first entry nobody is @@ -22,7 +22,7 @@ Hours are Opus 5.5 agent wall-clock hours (see [target-app-parity.md](target-app | 5 B | **AI masks: Subject, Sky, People, Background, Landscape** are heuristics; Object / Describe run SAM 3 but its download has no mirror configured | `crates/segment`; ai-masks.md; MASK rows 67% | masking is how most Lightroom users edit today | 40–70 + model decision | [target-app-parity](target-app-parity.md#ai) | | 6 B | **Compressed ORF** (every Olympus / OM body since ~2008) | measured: 66 of 78 Olympus / OM models preview-only; `orf.rs` refuses; OM bucket 3% of photographers, 3% of them fully work | +2.6 points; best gain per hour of any raw gap (~0.2) | 8–15 | [raw-parity](raw-parity.md#compressed-orf-a-clean-room-starting-point) (the two Olympus patents that describe the method) | | 7 | **Nikon High Efficiency NEF** (HE / HE★ on Z 8, Z 9, Z 6III, Z f, Z50II, Z5II when set to HE) | named and preview only (#193); needs a pure-Rust JPEG XS decoder | +1.1 points (only shots taken in HE; lossless works on the same bodies); low gain per hour (~0.04), so not a beta blocker | 20–40 | [raw-parity](raw-parity.md) | -| 8 B | **Lens profiles: lensfun only, no picker, no own calibration** (LR-EDIT-OPTICS-PROFILE): the desktop app downloads lensfun's database (CC BY-SA 3.0, pinned, never bundled) by itself on first run and looks every raw up by its lens ([lens-profiles.md](lens-profiles.md)), but those profiles are not validated against Lightroom, there is no profile picker (in the file / lens database / own calibration) and no calibration wizard; in-file corrections: DNG opcodes, RW2 in-file distortion, Sony ILCE-7RM4A, full-frame ILCE-7CR distortion (Auto only) and ILCE-7RM6 distortion (`docs/sony-ilce7rm6-lens-corrections.md`) and Nikon Z 6II / Z 8 / Z f distortion (`docs/nikon-lens-corrections.md`) | Adobe ships 3,635 lens profiles; we never use them | distortion / vignetting correction is a default step for many | 40–80 + calibration shots | [raw-parity](raw-parity.md#lens-profiles) | +| 8 B | **Lens profiles: lensfun only, no picker, no own calibration** (LR-EDIT-OPTICS-PROFILE): the desktop app offers lensfun's database (CC BY-SA 3.0, pinned, never bundled) once on the first launch and downloads it on that click or from Settings, then looks every raw up by its lens ([lens-profiles.md](lens-profiles.md)), but those profiles are not validated against Lightroom, there is no profile picker (in the file / lens database / own calibration) and no calibration wizard; in-file corrections: DNG opcodes, RW2 in-file distortion, Sony ILCE-7RM4A, full-frame ILCE-7CR distortion (Auto only) and ILCE-7RM6 distortion (`docs/sony-ilce7rm6-lens-corrections.md`) and Nikon Z 6II / Z 8 / Z f distortion (`docs/nikon-lens-corrections.md`) | Adobe ships 3,635 lens profiles; we never use them | distortion / vignetting correction is a default step for many | 40–80 + calibration shots | [raw-parity](raw-parity.md#lens-profiles) | | 9 B | **Tests don't run in CI on macOS or Windows**: Linux x64 runs `cargo xtask ci` on every PR and push to main (`.github/workflows/ci.yml`) and FreeBSD runs `cargo test`; the others build packages | `.github/workflows/` (ci, freebsd, packaging-lint, release, windows-arm64); the linux-riscv64 release job added in 0.6.0 (#844) runs only `lightcraft-cli --version` under QEMU; macOS / Windows jobs: #1148 | regressions in `cfg(target_os)` code reach macOS and Windows users | 6–12 | [target-app-parity](target-app-parity.md#by-dimension) | | 10 B | **Per-model verification thin, plus cheap per-body decode fixes**: 551 of 1,446 Adobe models verified (38%), ~85% of photographers on a verified body; Nikon 5 bodies verified, Canon 34, Leica 14 (the Q3 by a recorded check of 38 private DNGs). Cheap fixes: Z 6II packed 14-bit NEF (#769, PR #671), Sony A100 regression (#702); F828 SRF now discovered and imported as preview-only, sensor decoding still missing, DSLR-A200…A390 / A850 12-bit ARW (#535) | measured: [raw/coverage.tsv](raw/coverage.tsv) | per-body bugs (levels, crops, WB) surface only from users | 25–45 | [raw-parity](raw-parity.md#by-maker-measured) | | 11 | **Enhance: Raw Details, Super Resolution; AI denoise without shippable weights or X-Trans** | ENH rows 0%; denoise Bayer only, RawNIND weights GPL and opt-in | Fujifilm users get no AI denoise; nobody gets it out of the box | 30–60 + model decision | [raw-parity](raw-parity.md#demosaic-levels-crops-and-lens-metadata) | @@ -84,7 +84,7 @@ Beta-blocking gaps (B) total **~230–430 h**; with the stability and platform w | Date | Change | Summary | |---|---|---| -| 2026-10-27 | minor | Gap 8 shrinks: the desktop app downloads lensfun's lens database (CC BY-SA, pinned) by itself on first run and looks the library's raws up when it arrives; validation, a profile picker and own calibration remain | +| 2026-10-28 | minor | Gap 8 shrinks: the desktop app offers lensfun's lens database (CC BY-SA, pinned) once on the first launch and downloads it on request and looks the library's raws up when it arrives; validation, a profile picker and own calibration remain | | 2026-10-24 | minor | Gap 28: ⌘⌥⇧C / ⌘⌥⇧V (Ctrl+Alt+Shift+C / V on Windows and Linux) copy and paste metadata; no key leaves the entry; hours unchanged | | 2026-10-24 | minor | Gap 30 shrinks: the Info panel's Show Metadata For (Target Photo / Selected Photos, `view.infoSelectedPhotos`) shows the selected photos' shared values and marks fields that differ as Mixed; the All Metadata dialog remains; 1–3 h (was 4–8) | | 2026-10-23 | minor | Gap 13: before a Lightroom catalog import the app asks whether it merges into the open library or goes into a new one (opt-out in Settings, #2672); hours unchanged | diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index 293b4c5c8c..b0cfe0931b 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -5,22 +5,30 @@ see [sony-lens-corrections.md](sony-lens-corrections.md) and [nikon-lens-correct XML format**. The app does not ship any lens database: lensfun's data is licensed CC BY-SA 3.0 and is downloaded at run time, never bundled and never committed. Adobe LCP profiles are never used. -## Setup: none +## Setup: one click -1. On first run the desktop app downloads lensfun's `data/db` (56 XML files, about 5 MB) into its data folder +1. Nothing is downloaded without the user's click. On the first launch the desktop app asks once: "Download lens + profiles? LightCraft can correct distortion and vignetting for many lenses using the open lens database + (lensfun, about N MB)", with **Download** and **Not Now** (N is the pinned files' sizes added up). Not Now (or + Esc) downloads nothing and the app doesn't ask again; Settings ▸ General ▸ Lens profiles has **Download Lens + Database** and a switch that turns the offer back on. The answer is kept with the app's settings (`ui.json`, + `lensDatabase`: `ask` / `accepted` / `declined`; a value it can't read asks again), and + `lensfun_download::fetch_on_start` is the one place that decides. After a yes, a start that finds the database + missing or from an older pin downloads it again (an interrupted download resumes). + Download fetches lensfun's `data/db` (56 XML files, about 5 MB) into the app's data folder (`/lensfun`, beside the face and denoise models; see `crates/engine/src/config.rs`), with a row in - the activity stack (its ✕ stops it; the next start resumes). The files are pinned to one lensfun commit by name, + the activity stack (its ✕ stops it). The files are pinned to one lensfun commit by name, size and SHA-256 (`crates/engine/src/lensfun_download.rs`); a file that differs is thrown away. A later build with a newer pin replaces them once. The files are staged in `/lensfun.download`, `SOURCE.txt` (the commit and the licence) is written there last, and the staged folder then replaces the old one as a whole: until it is in place the previous complete database is used, and if the swap fails it is put back. The app's folder is read only when it has `SOURCE.txt`, and a folder whose pinned files are missing or cut short is downloaded again. - The CLI (`render`, `merge`, `snapshot`) and the headless MCP server read this folder too, but never start the - first-run download; `lens.downloadDatabase` fetches it on request from any of them. + The CLI (`render`, `merge`, `snapshot`) and the headless MCP server read this folder too, but never ask and never + fetch on their own; `lens.downloadDatabase` fetches it on request from any of them. 2. By default the files come from lensfun's GitHub repository at the pinned commit. `LIGHTCRAFT_LENSFUN_URL` lists other places serving the same files (base URLs, tried first), e.g. a copy on LightCraft's own server. 3. `LIGHTCRAFT_LENSFUN_DIR` points at a database of your own instead (lensfun's `data/db`, or a Linux - distribution's `/usr/share/lensfun/version_1/`); then the first-run download doesn't start, and that folder is read + distribution's `/usr/share/lensfun/version_1/`); then the app doesn't offer the download, and that folder is read even after `lens.downloadDatabase` fetches the app's own copy. When the database arrives (or changes), every library raw without a profile whose lens it knows is read again in the diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index bf1a29b98b..db87b08513 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,6 +1,6 @@ # Lightroom parity checklist -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (LR-EDIT-OPTICS-PROFILE: lensfun downloaded by itself on first run, profiles looked up for the library, the Optics panel names the profile; profiles never travel between lenses; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-28 · **Change:** minor (LR-EDIT-OPTICS-PROFILE: lensfun offered once on the first launch and downloaded on request, profiles looked up for the library, the Optics panel names the profile; profiles never travel between lenses; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 The measured, row-by-row checklist: one row per Lightroom feature, menu item and shortcut, what we have and what is missing. It answers *does the feature exist?* (feature breadth). Whether it is good @@ -212,7 +212,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts). | LR-EDIT-DETAIL-SUPERRES | Super resolution | P2 | ⬜ | | | | LR-EDIT-DETAIL-AISHARPEN | AI sharpen | OOS | 🚫 | | | | LR-EDIT-OPTICS-CA | Remove chromatic aberration | P1 | ✅ | `crates/ui-egui/src/panels/edit.rs` (checkbox), `ctl:optics.caRed`, `ctl:optics.caBlue` | | -| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; lens database: the desktop app downloads lensfun's `data/db` (CC BY-SA 3.0, pinned by commit, size and SHA-256, never bundled; `cmd:lens.downloadDatabase`, `crates/engine/src/lensfun_download.rs`) on first run with an activity row, and when it arrives every library raw whose lens it knows is looked up again in the background (`cmd:library.refreshLensProfiles`, `crates/engine/src/cmd/lens_refresh.rs`), as is a photo whose Enable Lens Corrections is ticked; the Optics panel names the loaded profile and its source, or says "No lens profile found for " (`crates/ui-egui/src/lens_db.rs`); presets, Copy / Paste and Sync carry only the switch and amounts, never a profile (`crates/engine/src/tests/lens_refresh.rs`); no source picker or own calibration yet | +| LR-EDIT-OPTICS-PROFILE | Lens profile corrections | P1 | 🟡 | `ctl:optics.profileDistortion`, `ctl:optics.profileVignetting`, `crates/pipeline/src/optics.rs`, `crates/raw/src/vendor/rw2.rs`, `crates/raw/src/vendor/raf_lens.rs`, `crates/engine/src/crs.rs` | uses corrections embedded in the file: DNG `WarpRectilinear` / `WarpRectilinear2` (opcode 14, DNG 1.6: degree-14 radial polynomial, valid-radius clamp, reciprocal option, applied in list order with the spec's skip rule for a `WarpRectilinear` after an optional one, so Pixel DNGs' two-stage warp is corrected; up to 3 chained stages, sampled on the CPU because the polynomials need `f64`; issue #635) / `FixVignetteRadial`, and the camera's own distortion correction in Panasonic / Leica RW2 / RWL files (tag `0x0119`, read as the equivalent `WarpRectilinear`; on 79 photos from 13 bodies and 11 lenses the corrected render lines up with the camera JPEG within 0.75 px at 1440 px at the corners, from up to 13 % of the half diagonal uncorrected; issue #256); an imported preset's `LensProfileEnable=0` leaves them on, as Lightroom applies built-in corrections regardless (inferred); Sony ILCE-7RM4A Bayer ARW raw-IFD distortion tables (`0x7037`, 16-sample layout, native 3:2 framing) use the same warp path, enabled for the independently validated ILCE-7RM4A with FE 24–105 and FE 200–600 lenses, and ILCE-7CR full-frame Bayer 3:2 files with correction Auto or forced Auto (a separate corrected-radius conversion validated against camera JPEGs with FE 24-50mm, FE 16-25mm and FE 50mm lenses); A7CR Off, APS-C, aspect crops and lossless M/S remain uncorrected; ILCE-7RM6 verified against the camera JPEG on 54 photos with three lenses (`docs/sony-ilce7rm6-lens-corrections.md`); other Sony models remain uncorrected (see `docs/sony-lens-corrections.md`); Nikon NEF distortion from NikonNEFInfo (`0xc7d5`: polynomial, frame-filling zoom and the optical-centre offset left by in-body stabilisation; `crates/raw/src/vendor/nef_lens.rs`, `docs/nikon-lens-corrections.md`) for the Z 6II, Z 8 and Z f, full-frame image area only: F-mount lenses on the FTZ match the camera JPEG within 0.2 px median, Z lenses within 0.5–1.5 px except the 24-70 f/4's wide end, where the camera applies a further correction that is not in the readable data (7 px median at 24 mm, from 16 px / 106 px p95 uncorrected); Fujifilm RAF raw-IFD tables (`0xf00b` distortion, `0xf010` vignetting; `crates/raw/src/vendor/raf_lens.rs`, `docs/fuji-lens-corrections.md`) become a `WarpRectilinear` with the camera's frame-filling zoom and a `FixVignetteRadial` gain for 59 body models with sample files checked against the camera JPEG (the table indexed by the source radius with last knot at the corner; on 67 body/lens combinations the corrected render's radial shape error against the camera JPEG falls from a median of 0.72 % to 0.05 % of the half diagonal, largest 0.42 %, and the vignetting gain of the JPEG tracks the table at a median correlation of 0.987); files whose table scale is not the recorded image's half diagonal (X-S20, X-T30 III, X-M5, X-T5 crops) and six unmatched bodies stay uncorrected; Fujifilm's lateral CA table (`0xf00f`) is real but is not applied, because our demosaic already registers the planes and the stored shift over-corrects; vignetting and lateral CA data of Olympus raws not read yet; Sony ARW shading, lateral CA and distortion tables (raw IFD `0x7031`–`0x7037`, with the encrypted `SR2SubIFD` copies as a checked fallback) are decoded with their camera settings, provenance and framing evidence for every body that records them (`crates/raw/src/lens_metadata.rs`), but only the ILCE-7RM4A, ILCE-7RM6 and ILCE-7CR use the separate distortion conversions above; Sony shading and CA semantics are not validated beyond the ILCE-7RM6's (lateral CA off) (`docs/sony-lens-corrections.md`); Reload from Disk reads the file's lens data for photos catalogued without it and turns the correction on for those still at import settings (issue #314); lens-profile database: a lensfun-format XML database in `LIGHTCRAFT_LENSFUN_DIR` (none is bundled) supplies distortion (poly3 / poly5 / PTLens), TCA and vignetting for lenses it lists (`crates/lensdb`, `crates/engine/src/lens_profiles.rs`, `docs/lens-profiles.md`); raws that name no lens model (Nikon DSLRs: maker-note lens specification only) look it up by a name built from the specification (`24-70mm f/2.8`) and take only a profile named exactly that; lens database: the desktop app offers lensfun's `data/db` once on the first launch (Download / Not Now with its size; Settings ▸ General ▸ Lens profiles downloads it later; nothing is fetched without that click; CC BY-SA 3.0, pinned by commit, size and SHA-256, never bundled; `cmd:lens.downloadDatabase`, `crates/engine/src/lensfun_download.rs`) and downloads it with an activity row, and when it arrives every library raw whose lens it knows is looked up again in the background (`cmd:library.refreshLensProfiles`, `crates/engine/src/cmd/lens_refresh.rs`), as is a photo whose Enable Lens Corrections is ticked; the Optics panel names the loaded profile and its source, or says "No lens profile found for " (`crates/ui-egui/src/lens_db.rs`); presets, Copy / Paste and Sync carry only the switch and amounts, never a profile (`crates/engine/src/tests/lens_refresh.rs`); no source picker or own calibration yet | ) | LR-EDIT-OPTICS-DEFRINGE | Defringe | P1 | ✅ | `ctl:optics.defringe*` | no fringe eyedropper | | LR-EDIT-OPTICS-MANUAL | Manual distortion / vignetting | P1 | ✅ | `ctl:optics.distortion`, `ctl:optics.vignetting`, `ctl:optics.vignettingMidpoint` | | @@ -799,7 +799,7 @@ Latin stays upright. Advanced Japanese composition remains open. Regression cove | Date | Change | Summary | |---|---|---| -| 2026-10-27 | minor | LR-EDIT-OPTICS-PROFILE: lensfun's database downloaded by itself on first run (pinned, CC BY-SA, never bundled), the library looked up when it arrives or Enable Lens Corrections is ticked, the Optics panel names the profile or the lens without one; status unchanged (🟡) | +| 2026-10-28 | minor | LR-EDIT-OPTICS-PROFILE: lensfun's database offered once on the first launch and downloaded on request (Download / Not Now, Settings) (pinned, CC BY-SA, never bundled), the library looked up when it arrives or Enable Lens Corrections is ticked, the Optics panel names the profile or the lens without one; status unchanged (🟡) | | 2026-10-25 | patch | LR-BEHAV-TITLEBAR: the top bar's title-bar gestures come from craft-window's `title_bar`: a double-click follows the system setting, and a press on a button, the search field or a panel floating over the bar never moves the window; status counts unchanged | | 2026-10-24 | minor | KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 (`keyword.nextSet`, `keyword.previousSet`) go to the next / previous keyword set; status counts unchanged (KEYC-METADATA stays 🟡) | | 2026-10-24 | minor | KEYC-METADATA: ⌘K / Ctrl+K (`panel.addKeywords`, Add Keywords) shows the Keywords panel, opens Keywording and puts the cursor in the Add keyword box; status counts unchanged (KEYC-METADATA stays 🟡) | From 0fd0ff44f7b924f77ed9e64426480980781f7891 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Wed, 28 Oct 2026 09:35:56 +1000 Subject: [PATCH 22/51] LR-EDIT-OPTICS-PROFILE: a lens database download that can't start keeps the offer open; virtual copies gathered once (#3073) - The Lens Profiles offer stays open with a toast saying why when Download can't start (from its button and from ui.dialog.confirm), and a start that failed isn't remembered as a yes. - lens_refresh::apply gathers each photo's virtual copies once per batch instead of scanning the catalog for every probe result. Tests: tests::lens_offer::a_download_that_cannot_start_keeps_the_offer_open_and_says_why, tests::lens_refresh::each_result_reaches_its_photos_virtual_copies_and_no_others. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/lens_refresh.rs | 9 ++++++- crates/engine/src/tests/lens_refresh.rs | 28 +++++++++++++++++++++ crates/ui-egui/src/control.rs | 1 + crates/ui-egui/src/dialogs.rs | 4 ++- crates/ui-egui/src/lens_db.rs | 4 ++- crates/ui-egui/src/tests/lens_offer.rs | 33 +++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/cmd/lens_refresh.rs b/crates/engine/src/cmd/lens_refresh.rs index a32136ceff..350522b6c4 100644 --- a/crates/engine/src/cmd/lens_refresh.rs +++ b/crates/engine/src/cmd/lens_refresh.rs @@ -65,13 +65,20 @@ pub fn apply(s: &mut Session, results: Vec<(Missing, std::result::Result> = std::collections::HashMap::new(); + for p in s.catalog.photos() { + if let Some(original) = p.copy_of { + copies.entry(original).or_default().push(p.id); + } + } for (item, info) in results { let Ok(info) = info else { continue }; // the photo moved or left the library while its file was read: the result is stale, for its copies too if s.catalog.photo(item.id).and_then(|p| crate::cmd::missing::checked_path(p)) != Some(item.path.as_str()) { continue; } - let family: Vec = s.catalog.photos().filter(|p| p.id == item.id || p.copy_of == Some(item.id)).map(|p| p.id).collect(); + let family = std::iter::once(item.id).chain(copies.get(&item.id).into_iter().flatten().copied()); for id in family { let Some(p) = s.catalog.photo(id) else { continue }; // nothing found for a photo without a database profile: a file's own correction is Reload's business diff --git a/crates/engine/src/tests/lens_refresh.rs b/crates/engine/src/tests/lens_refresh.rs index ab43aa280a..3797afb19a 100644 --- a/crates/engine/src/tests/lens_refresh.rs +++ b/crates/engine/src/tests/lens_refresh.rs @@ -68,6 +68,34 @@ fn sync_and_presets_keep_each_photos_own_profile() { assert_eq!(lens_of(&s, a), Some(lens(-0.05))); } +/// A batch of results gives each probed photo's profile to that photo and to its own virtual copies, never to the +/// copies of a photo that wasn't probed. +#[test] +fn each_result_reaches_its_photos_virtual_copies_and_no_others() { + let mut s = Session::new(); + for p in [raw(1, "50mm F1.8", false), raw(2, "50mm F1.8", false), raw(3, "50mm F1.8", false)] { + s.catalog.apply(Op::AddPhoto { photo: Box::new(p) }).unwrap(); + } + for (id, of) in [(11, 1), (12, 1), (21, 2), (31, 3)] { + let mut copy = raw(id, "50mm F1.8", false); + copy.copy_of = Some(PhotoId(of)); + s.catalog.apply(Op::AddPhoto { photo: Box::new(copy) }).unwrap(); + } + let probed = [1, 2].map(|id| { + let m = crate::cmd::lens_refresh::Missing { id: PhotoId(id), path: format!("/photos/{id}.cr3") }; + (m, Ok(crate::media::ProbeInfo { embedded_lens: Some(lens(-0.01)), ..Default::default() })) + }); + let mut found = crate::cmd::lens_refresh::apply(&mut s, probed.into()).unwrap(); + found.sort(); + assert_eq!(found, [1, 2, 11, 12, 21].map(PhotoId), "each probed photo and its own copies"); + for id in [1, 2, 11, 12, 21] { + assert!(s.catalog.photo(PhotoId(id)).unwrap().embedded_lens.is_some(), "photo {id} gets the profile"); + } + for id in [3, 31] { + assert!(s.catalog.photo(PhotoId(id)).unwrap().embedded_lens.is_none(), "photo {id} wasn't probed: unchanged"); + } +} + /// A raw in a library folder, as an older import left it: no lens data. fn raw(id: u64, lens_name: &str, edited: bool) -> Photo { let mut p = diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index caacbc5908..af4dc426f3 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -423,6 +423,7 @@ pub fn handle(app: &mut LightcraftApp, ctx: &egui::Context, req: &ControlRequest | crate::state::Dialog::SmartRules { .. } | crate::state::Dialog::SaveEffect { .. } | crate::state::Dialog::CaptureTime { .. } + | crate::state::Dialog::LensDatabase { .. } )) || (r.is_ok() && crate::panels::dialogs::keeps_open(app, &d)) || (r.is_err() && crate::lightroom_destination::keeps_open_on_error(app, &d)) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 4802c8a637..c0c7e27208 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -1417,7 +1417,8 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context, blocked: bool) { match confirm_dialog(app, &dlg) { // the import review stays open on an error (e.g. an unusable folder template), and so // does Export (e.g. no folder chosen) so the choices aren't lost, and Edit Capture Time - // (a time that isn't a date, or a shift it refuses) + // (a time that isn't a date, or a shift it refuses), and the lens database offer (a download that can't + // start: Download again or Not Now) Err(e) if matches!( dlg, @@ -1434,6 +1435,7 @@ pub fn show(app: &mut LightcraftApp, ctx: &egui::Context, blocked: bool) { | Dialog::SynchronizeFolder { .. } | Dialog::SaveEffect { .. } | Dialog::CaptureTime { .. } + | Dialog::LensDatabase { .. } ) => { app.toast(ctx, e) diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs index 57bc07cab4..67933e7041 100644 --- a/crates/ui-egui/src/lens_db.rs +++ b/crates/ui-egui/src/lens_db.rs @@ -59,8 +59,10 @@ pub(crate) fn begin(app: &mut LightcraftApp, start: lensfun_download::Start, mir /// Download the database into `dir` from `mirrors` (the offer's Download, Settings' button): remembered as the /// user's yes, shown in the activity stack, and read once it is in place. pub(crate) fn download(app: &mut LightcraftApp, dir: &std::path::Path, mirrors: Vec) -> Result<(), String> { + lens_refresh::start_download(&mut app.session, dir, mirrors, lensfun_download::FILES)?; + // a yes only once it started: a download that couldn't start isn't fetched again at the next start app.ui.settings.lens_database = lensfun_download::Offer::Accepted; - lens_refresh::start_download(&mut app.session, dir, mirrors, lensfun_download::FILES) + Ok(()) } /// Settings' Download Lens Database: into the app's own folder, from the usual mirrors. diff --git a/crates/ui-egui/src/tests/lens_offer.rs b/crates/ui-egui/src/tests/lens_offer.rs index 0a759f9437..3e6d51ae96 100644 --- a/crates/ui-egui/src/tests/lens_offer.rs +++ b/crates/ui-egui/src/tests/lens_offer.rs @@ -110,3 +110,36 @@ fn a_headless_app_never_offers_or_fetches() { assert_eq!(app.session.lensfun.state(), lensfun_download::State::Idle); } } + +/// A download that can't start keeps the offer open with a toast saying why (Download again or Not Now), from its +/// Download button and from `ui.dialog.confirm`, and isn't remembered as a yes. +#[test] +fn a_download_that_cannot_start_keeps_the_offer_open_and_says_why() { + use crate::headless::Headless; + let t = std::time::Duration::from_secs(20); + let app = LightcraftApp::new(Session::with_demo(), Services { png: None, ..Default::default() }); + let mut h = Headless::new(app, [1300.0, 900.0], 1.0); + h.step(); + let root = std::env::temp_dir().join(format!("lc-lens-offer-fail-{}", std::process::id())); + // no place to download from: the start is refused + let refused = Dialog::LensDatabase { dir: root.join("lensfun").to_string_lossy().into_owned(), mirrors: Vec::new() }; + let offer_open = |h: &Headless| matches!(h.app.ui.dialog, Some(Dialog::LensDatabase { .. })); + + h.app.ui.settings.lens_database = Offer::Declined; + h.app.ui.dialog = Some(refused.clone()); + h.settle(std::time::Duration::from_secs(10)); + let r = h.request("ui.clickWidget", serde_json::json!({"id": "button:dialogOk"}), t); + assert_eq!(r["ok"], true, "{r}"); + let toast = h.app.ui.toast.as_ref().map(|t| t.0.clone()).unwrap_or_default(); + assert!(toast.contains("no download location"), "Download says why it didn't start: {toast:?}"); + assert!(offer_open(&h), "the offer stays open: {:?}", h.app.ui.dialog); + assert_eq!(h.app.session.lensfun.state(), lensfun_download::State::Idle); + assert_eq!(h.app.ui.settings.lens_database, Offer::Declined, "a start that failed isn't a yes"); + + // the control channel's confirm keeps it open too + let r = h.request("ui.dialog.confirm", serde_json::json!({}), t); + assert_eq!(r["ok"], false, "{r}"); + assert!(offer_open(&h), "the offer stays open after ui.dialog.confirm: {:?}", h.app.ui.dialog); + assert_eq!(h.request("ui.dialog.cancel", serde_json::json!({}), t)["ok"], true); + assert!(h.app.ui.dialog.is_none(), "Not Now still closes it"); +} From bad1b88cd9411f4ea6b9b467d5f4073e08944d6e Mon Sep 17 00:00:00 2001 From: OneSlips Date: Sun, 1 Nov 2026 07:27:18 +1000 Subject: [PATCH 23/51] LR-EDIT-OPTICS-PROFILE: a replaced original's missing file correction isn't restored; the loupe key carries the lens profile A fresh decode that finds no file correction now renders without one, even when the catalog still holds the old file's; a database profile still comes from the catalog. The loupe view-cache key includes the photo's lens profile, as the render, thumbnail and variant keys do, so a changed database profile never shows the old loupe render. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/media/session.rs | 17 +++++++++++------ crates/engine/src/media/source.rs | 7 ++++--- crates/engine/src/tests/lens_refresh.rs | 8 +++++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/media/session.rs b/crates/engine/src/media/session.rs index a62d4debc0..f473497070 100644 --- a/crates/engine/src/media/session.rs +++ b/crates/engine/src/media/session.rs @@ -226,16 +226,21 @@ impl crate::Session { Some(crate::face_tiles::PreviewCut { path, loader, want, min, rect, aspect, edge }) } - /// Size-independent cache key of a photo's view render (loupe) for its current settings. - fn view_key(p: &Photo, apply_crop: bool) -> Hash128 { - Hasher128::new() - .str(&content_key(p)) + /// Size-independent cache key of a photo's view render (loupe) for its current settings, its lens profile + /// included as in [`Self::variant_key`]. + pub(crate) fn view_key(p: &Photo, apply_crop: bool) -> Hash128 { + let mut k = Hasher128::new(); + k.str(&content_key(p)) .str("view") .u64(p.develop.hash64()) .u64(apply_crop as u64) .u64(RENDER_CACHE_VERSION) - .u64(crate::camera_profiles::cache_key()) - .finish() + .u64(crate::camera_profiles::cache_key()); + let lens = super::quick::lens_key(p, &p.develop); + if lens != 0 { + k.u64(lens); + } + k.finish() } /// The loupe's render job: like [`Self::render_job`], and a full-quality result is kept as the diff --git a/crates/engine/src/media/source.rs b/crates/engine/src/media/source.rs index f218fee61f..52e6d9f198 100644 --- a/crates/engine/src/media/source.rs +++ b/crates/engine/src/media/source.rs @@ -197,8 +197,9 @@ impl DecodedSource { } /// The lens profile a render uses: the file's own correction as decoded (`decoded`, it is part of the file), else the -/// catalog's (`catalog`, [`lightcraft_catalog::Photo::embedded_lens`]). A database profile is never taken from the -/// decode, so a cached render, a fresh decode and the Optics panel all use the one profile the photo holds, and a +/// catalog's database profile (`catalog`, [`lightcraft_catalog::Photo::embedded_lens`]). A file correction the +/// catalog still holds but the decode no longer finds (the original was replaced) is not restored. A database profile +/// is never taken from the decode, so a cached render, a fresh decode and the Optics panel all use the one profile the photo holds, and a /// change to it (a database update looked up again, `cmd::lens_refresh`) changes the render keys /// ([`super::quick::lens_key`]). pub(crate) fn pinned_lens( @@ -207,7 +208,7 @@ pub(crate) fn pinned_lens( ) -> Option { match decoded { Some(l) if l.source == lightcraft_develop::LensSource::File => Some(l), - _ => catalog, + _ => catalog.filter(|c| c.source == lightcraft_develop::LensSource::Database), } } diff --git a/crates/engine/src/tests/lens_refresh.rs b/crates/engine/src/tests/lens_refresh.rs index 3797afb19a..42b8e7b9b6 100644 --- a/crates/engine/src/tests/lens_refresh.rs +++ b/crates/engine/src/tests/lens_refresh.rs @@ -218,6 +218,11 @@ fn a_decode_never_overrides_the_photos_database_profile() { assert_eq!(decoded(lens(-0.05)).info_or(header(None)).lens, None, "none until the photo is given one"); let file_lens = EmbeddedLens { source: LensSource::File, ..lens(-0.04) }; assert_eq!(decoded(file_lens).info_or(header(None)).lens, Some(file_lens), "the file's own correction"); + // the original was replaced by one without a correction: the decode finds none, and the catalog's old file + // correction isn't put back + let none = DecodedSource::new(img.clone(), Some(lightcraft_pipeline::SourceInfo { lens: None, ..Default::default() })); + assert_eq!(none.info_or(header(Some(file_lens))).lens, None, "no file correction the file no longer has"); + assert_eq!(none.info_or(header(Some(lens(-0.01)))).lens, Some(lens(-0.01)), "the database profile stays"); } /// When a photo's database profile changes (a replaced database looked up again), its render key and its thumbnail @@ -232,7 +237,7 @@ fn a_new_database_profile_changes_the_render_and_cache_keys() { let render = s.render_job(id, 800, 600, false, true).unwrap().key; let thumb = s.thumb_job(id, 256).unwrap().cache.unwrap().1; let p = s.catalog.photo(id).unwrap().clone(); - (render, thumb, Session::variant_key(&p, &p.develop, 128)) + (render, thumb, Session::variant_key(&p, &p.develop, 128), Session::view_key(&p, true)) }; let before = keys(&mut s); s.catalog.apply(Op::SetEmbeddedLens { id, lens: Some(Box::new(lens(-0.03))) }).unwrap(); @@ -240,6 +245,7 @@ fn a_new_database_profile_changes_the_render_and_cache_keys() { assert_ne!(before.0, after.0, "render key"); assert_ne!(before.1, after.1, "thumbnail cache key"); assert_ne!(before.2, after.2, "variant cache key"); + assert_ne!(before.3, after.3, "loupe view cache key"); // correction off: the profile isn't used, so changing it leaves every key (and the cached thumbnails) as they are let off = with_lens((**s.catalog.photo(id).unwrap()).clone(), Some(lens(-0.03)), false); s.catalog.apply(Op::SetDevelop { id, settings: off.develop.clone(), label: "Lens Corrections".into(), edited: None }).unwrap(); From 77e657814082d5baa98adda666573cdb73a8a9b1 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Sun, 1 Nov 2026 09:32:14 +1000 Subject: [PATCH 24/51] LR-EDIT-OPTICS-PROFILE: a database profile is part of the render keys with the correction off too (CodeRabbit, #3073) A raw's starting colour is fitted through its lens profile's vignetting whether or not Enable Lens Corrections is ticked, so the decoded picture depends on the profile; a database that gives the photo another profile now changes its render, thumbnail, variant and loupe keys either way. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/media/quick.rs | 15 +++++++-------- crates/engine/src/media/session.rs | 6 +++--- crates/engine/src/tests/lens_refresh.rs | 12 ++++++++---- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/media/quick.rs b/crates/engine/src/media/quick.rs index ebfb7beb52..bfdfea9c97 100644 --- a/crates/engine/src/media/quick.rs +++ b/crates/engine/src/media/quick.rs @@ -1,8 +1,6 @@ use super::render::{RenderJob, RenderResult}; use super::source::{PreviewLoader, SourceLevel}; -use super::{ - Arc, Deserialize, DevelopSettings, Hash128, Hasher128, Histogram, Photo, PhotoId, PreviewCache, Rendered, Rgba8, Serialize, Source, SourceInfo, -}; +use super::{Arc, Deserialize, Hash128, Hasher128, Histogram, Photo, PhotoId, PreviewCache, Rendered, Rgba8, Serialize, Source, SourceInfo}; /// Where a [`QuickJob`]'s stand-in image came from. #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -109,13 +107,14 @@ pub fn content_key(p: &Photo) -> String { } } -/// What a photo's lens profile adds to its render and thumbnail keys: a database profile (0 without one, or with the -/// correction off, where it changes nothing). When a database update gives the photo another profile, its cached -/// renders and thumbnails no longer match. (The file's own correction changes only with the file, which the content +/// What a photo's lens profile adds to its render and thumbnail keys: a database profile (0 without one), with the +/// correction on or off: a raw's starting colour is fitted through the profile's vignetting either way +/// (`camera_preview::starting_colour`), so the decoded picture depends on it. When a database update gives the +/// photo another profile, its cached renders and thumbnails no longer match. (The file's own correction changes only with the file, which the content /// key covers, so it adds nothing and existing thumbnails stay valid.) -pub fn lens_key(p: &Photo, settings: &DevelopSettings) -> u64 { +pub fn lens_key(p: &Photo) -> u64 { match &p.embedded_lens { - Some(l) if settings.optics.lens_profile && l.source == lightcraft_develop::LensSource::Database => { + Some(l) if l.source == lightcraft_develop::LensSource::Database => { let k = Hasher128::new().str("lens").str(&format!("{l:?}")).finish(); (k.0 as u64) ^ ((k.0 >> 64) as u64) | 1 } diff --git a/crates/engine/src/media/session.rs b/crates/engine/src/media/session.rs index f473497070..bae8deff31 100644 --- a/crates/engine/src/media/session.rs +++ b/crates/engine/src/media/session.rs @@ -52,7 +52,7 @@ impl crate::Session { // Temporary "before" settings must not replace the photo's cached develop hash. let settings_hash = if before { settings.hash64() } else { self.media.settings_hashes.get(id, &settings) }; let salt = self.media.denoise_salt(id, &settings); - let lens = super::quick::lens_key(&p, &settings); + let lens = super::quick::lens_key(&p); let key = settings_hash ^ ((max_w as u64) << 40) ^ ((max_h as u64) << 20) @@ -115,7 +115,7 @@ impl crate::Session { .u64(edge as u64) .u64(RENDER_CACHE_VERSION) .u64(crate::camera_profiles::cache_key()); - let lens = super::quick::lens_key(p, settings); + let lens = super::quick::lens_key(p); if lens != 0 { k.u64(lens); } @@ -236,7 +236,7 @@ impl crate::Session { .u64(apply_crop as u64) .u64(RENDER_CACHE_VERSION) .u64(crate::camera_profiles::cache_key()); - let lens = super::quick::lens_key(p, &p.develop); + let lens = super::quick::lens_key(p); if lens != 0 { k.u64(lens); } diff --git a/crates/engine/src/tests/lens_refresh.rs b/crates/engine/src/tests/lens_refresh.rs index 42b8e7b9b6..8885c2f7ca 100644 --- a/crates/engine/src/tests/lens_refresh.rs +++ b/crates/engine/src/tests/lens_refresh.rs @@ -226,8 +226,8 @@ fn a_decode_never_overrides_the_photos_database_profile() { } /// When a photo's database profile changes (a replaced database looked up again), its render key and its thumbnail -/// and variant cache keys change, so no cached render made with the old profile is shown; with the correction off -/// the profile changes nothing, and the keys stay. +/// and variant cache keys change, so no cached render made with the old profile is shown. With the correction off +/// they change too: the decode fits the raw's starting colour through the profile's vignetting either way. #[test] fn a_new_database_profile_changes_the_render_and_cache_keys() { let mut s = Session::new(); @@ -246,12 +246,16 @@ fn a_new_database_profile_changes_the_render_and_cache_keys() { assert_ne!(before.1, after.1, "thumbnail cache key"); assert_ne!(before.2, after.2, "variant cache key"); assert_ne!(before.3, after.3, "loupe view cache key"); - // correction off: the profile isn't used, so changing it leaves every key (and the cached thumbnails) as they are + // correction off: the starting colour's fit still uses the profile, so the keys change as well let off = with_lens((**s.catalog.photo(id).unwrap()).clone(), Some(lens(-0.03)), false); s.catalog.apply(Op::SetDevelop { id, settings: off.develop.clone(), label: "Lens Corrections".into(), edited: None }).unwrap(); let off_before = keys(&mut s); s.catalog.apply(Op::SetEmbeddedLens { id, lens: Some(Box::new(lens(-0.05))) }).unwrap(); - assert_eq!(off_before, keys(&mut s)); + let off_after = keys(&mut s); + assert_ne!(off_before.0, off_after.0, "render key, correction off"); + assert_ne!(off_before.1, off_after.1, "thumbnail cache key, correction off"); + assert_ne!(off_before.2, off_after.2, "variant cache key, correction off"); + assert_ne!(off_before.3, off_after.3, "loupe view cache key, correction off"); } /// `lens.downloadDatabase` selects the folder it downloads into, so the session reads the database once it is in From 0a9707efd6987d6046b215bc26e4790cd23100ec Mon Sep 17 00:00:00 2001 From: OneSlips Date: Sun, 1 Nov 2026 13:30:07 +1000 Subject: [PATCH 25/51] LR-EDIT-OPTICS-PROFILE: a decode from before a lens profile change never returns to the source cache (CodeRabbit, #3073) A job's source identity carries the photo's database profile with its content, and Session::accept keeps a loaded source only when both still match. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/media/quick.rs | 14 ++++++++++++++ crates/engine/src/media/session.rs | 10 +++++----- .../engine/src/media/thumbnail_hash_tests.rs | 19 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/media/quick.rs b/crates/engine/src/media/quick.rs index bfdfea9c97..78fcebdfd0 100644 --- a/crates/engine/src/media/quick.rs +++ b/crates/engine/src/media/quick.rs @@ -99,6 +99,20 @@ impl QuickJob { } /// What identifies a photo's pixels for caching: its content hash, else its source. +/// Which decode of a photo a loaded source is: its file's content and, when it has one, its database lens profile +/// ([`lens_key`]; the decode fits the starting colour through it). A job's source is kept only when this still +/// matches when the job finishes ([`super::Session::accept`]), so a decode started before a profile change never +/// returns to the cache after `cmd::lens_refresh` has dropped the photo's sources. +pub fn source_identity(p: &Photo) -> Hash128 { + let mut k = Hasher128::new(); + k.str(&content_key(p)); + let lens = lens_key(p); + if lens != 0 { + k.u64(lens); + } + k.finish() +} + pub fn content_key(p: &Photo) -> String { match (&p.content_hash, &p.source) { (Some(h), _) => h.clone(), diff --git a/crates/engine/src/media/session.rs b/crates/engine/src/media/session.rs index bae8deff31..08c0dd6f06 100644 --- a/crates/engine/src/media/session.rs +++ b/crates/engine/src/media/session.rs @@ -47,8 +47,8 @@ impl crate::Session { // share a result (a view slot showing photo A would otherwise look current for photo B) // …and so is the file's content: a file changed on disk (Reload) renders afresh let content_key = content_key(&p); - let source_key = Hasher128::new().str(&content_key).finish(); - let content = source_key.0 as u64; + let content = Hasher128::new().str(&content_key).finish().0 as u64; + let source_key = super::quick::source_identity(&p); // Temporary "before" settings must not replace the photo's cached develop hash. let settings_hash = if before { settings.hash64() } else { self.media.settings_hashes.get(id, &settings) }; let salt = self.media.denoise_salt(id, &settings); @@ -136,7 +136,7 @@ impl crate::Session { Some(RenderJob { request_id: 0, cache_generation: self.media.rendered.generation(), - source_key: Some(Hasher128::new().str(&content_key(&p)).finish()), + source_key: Some(super::quick::source_identity(&p)), photo: id, level, source, @@ -191,7 +191,7 @@ impl crate::Session { Some(RenderJob { request_id: 0, cache_generation: self.media.rendered.generation(), - source_key: Some(Hasher128::new().str(&content_key(&p)).finish()), + source_key: Some(super::quick::source_identity(&p)), photo: id, level, source, @@ -389,7 +389,7 @@ impl crate::Session { /// has wrong is corrected too ([`Self::learn_as_shot_wb`]). pub fn accept(&mut self, r: &RenderResult) { if let Some(src) = &r.loaded - && r.source_key.is_none_or(|key| self.catalog.photo(r.photo).is_some_and(|p| Hasher128::new().str(&content_key(p)).finish() == key)) + && r.source_key.is_none_or(|key| self.catalog.photo(r.photo).is_some_and(|p| super::quick::source_identity(p) == key)) { if let Some(info) = &src.info { self.learn_as_shot_wb(r.photo, info); diff --git a/crates/engine/src/media/thumbnail_hash_tests.rs b/crates/engine/src/media/thumbnail_hash_tests.rs index 3c23e9db95..d8dd22edfb 100644 --- a/crates/engine/src/media/thumbnail_hash_tests.rs +++ b/crates/engine/src/media/thumbnail_hash_tests.rs @@ -88,6 +88,25 @@ fn source_from_before_content_reload_is_not_accepted() { s.accept(&old); assert_eq!(s.media.source_usage().0, 0); } +/// A decode that started before the photo's database lens profile changed (a lens database arriving, looked up again +/// by `cmd::lens_refresh`, which drops the photo's sources) never puts its source back in the cache: the source's +/// colour was fitted through the old profile. +#[test] +fn source_from_before_a_lens_profile_change_is_not_accepted() { + let mut s = session(); + s.media.file_loader = Some(Arc::new(|_, _| Ok((Rgb32f::new(8, 8), SourceInfo::default())))); + let old = s.thumb_job(PhotoId(1), 256).unwrap().run(); + assert!(old.loaded.is_some()); + let lens = lightcraft_develop::EmbeddedLens { source: lightcraft_develop::LensSource::Database, ..Default::default() }; + s.catalog.apply(Op::SetEmbeddedLens { id: PhotoId(1), lens: Some(Box::new(lens)) }).unwrap(); + s.media.forget(PhotoId(1)); + s.accept(&old); + assert_eq!(s.media.source_usage().0, 0, "the old decode is dropped"); + // a decode made with the new profile is kept + let new = s.thumb_job(PhotoId(1), 256).unwrap().run(); + s.accept(&new); + assert_ne!(s.media.source_usage().0, 0); +} /// A panic while rendering (here in the decoder) used to end the pool's worker thread without /// an answer, so the view that asked showed "Rendering…" forever. It is the job's failure now, /// for renders and for stand-ins, answering the request it came from. From f3f183192dbec275279e83c0981442603ab51f37 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Sun, 1 Nov 2026 17:24:25 +1000 Subject: [PATCH 26/51] LR-EDIT-OPTICS-PROFILE: the hover preview follows a new lens profile, and one download at a time fills a folder The loupe's hover preview key now includes the photo's database lens profile, so a profile looked up again renders afresh. The lens database download takes a lock file beside its folder: a second download into the same folder (another window) is refused with a plain message, and a lock left by an app that quit an hour ago or more is taken over. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lensfun_download.rs | 68 +++++++++++++++++++ crates/engine/src/media/session.rs | 9 ++- .../engine/src/media/thumbnail_hash_tests.rs | 12 ++++ 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index fb30060f20..35828eab76 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -34,6 +34,13 @@ pub const MARKER: &str = "SOURCE.txt"; const STAGING: &str = "download"; /// The suffix of the folder the previous database is moved to while the new one takes its place. const PREVIOUS: &str = "previous"; +/// The suffix of the file that says a download into the folder is under way (another window or session of the +/// app may be the one downloading). +#[cfg(not(target_arch = "wasm32"))] +const LOCK: &str = "lock"; +/// A lock this old is left over from an app that quit mid-download, not a download still running. +#[cfg(not(target_arch = "wasm32"))] +const STALE_LOCK: std::time::Duration = std::time::Duration::from_secs(60 * 60); /// Largest database file accepted (the biggest is under 1 MB). #[cfg(not(target_arch = "wasm32"))] const MAX_FILE: u64 = 8 << 20; @@ -329,8 +336,48 @@ fn run(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) { } } +/// Held while one download uses a folder's staging and previous folders, across sessions and processes: a file +/// beside the folder, made only if it isn't there, removed when the download ends. +#[cfg(not(target_arch = "wasm32"))] +struct DirLock(PathBuf); + +#[cfg(not(target_arch = "wasm32"))] +impl DirLock { + fn take(dir: &Path) -> Result { + let path = beside(dir, LOCK); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|e| format!("could not create {}: {e}", parent.display()))?; + } + for _ in 0..2 { + match std::fs::OpenOptions::new().write(true).create_new(true).open(&path) { + Ok(_) => return Ok(DirLock(path)), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + let stale = + std::fs::metadata(&path).and_then(|m| m.modified()).ok().and_then(|t| t.elapsed().ok()).is_some_and(|age| age > STALE_LOCK); + if !stale { + return Err("the lens database is already downloading (in another LightCraft window)".into()); + } + // left by an app that quit mid-download: taken over + let _ = std::fs::remove_file(&path); + } + Err(e) => return Err(format!("could not create {}: {e}", path.display())), + } + } + Err("the lens database is already downloading (in another LightCraft window)".into()) + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for DirLock { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } +} + #[cfg(not(target_arch = "wasm32"))] fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> Result { + // (from before the recovery through the swap: another download of this folder would move the same folders) + let _lock = DirLock::take(dir)?; recover(dir); let staging = beside(dir, STAGING); std::fs::create_dir_all(&staging).map_err(|e| format!("could not create {}: {e}", staging.display()))?; @@ -424,6 +471,27 @@ mod tests { use std::net::TcpListener; use std::time::Duration; + /// Two downloads into one folder (two windows, or two sessions of one app) never share its staging and previous + /// folders: the second is refused while the first holds the folder, and may go once it ends; a lock left by an + /// app that quit mid-download is taken over. + #[test] + fn one_download_at_a_time_holds_a_folder() { + let d = scratch("lock"); + let _ = std::fs::remove_file(beside(&d, LOCK)); + let first = DirLock::take(&d).unwrap(); + let second = DirLock::take(&d); + assert!(second.as_ref().is_err_and(|e| e.contains("already downloading")), "{:?}", second.as_ref().err()); + drop(first); + let again = DirLock::take(&d).unwrap(); + std::mem::forget(again); + // (the app quit with the lock in place an hour and more ago) + let old = std::time::SystemTime::now() - STALE_LOCK - Duration::from_secs(60); + std::fs::OpenOptions::new().write(true).open(beside(&d, LOCK)).unwrap().set_modified(old).unwrap(); + let taken = DirLock::take(&d).unwrap(); + drop(taken); + assert!(!beside(&d, LOCK).exists(), "removed when the download ends"); + } + fn scratch(name: &str) -> PathBuf { let d = std::env::temp_dir().join(format!("lc-lensfun-{name}-{}", std::process::id())); for leftover in [d.clone(), beside(&d, STAGING), beside(&d, PREVIOUS)] { diff --git a/crates/engine/src/media/session.rs b/crates/engine/src/media/session.rs index 08c0dd6f06..2c34996481 100644 --- a/crates/engine/src/media/session.rs +++ b/crates/engine/src/media/session.rs @@ -102,7 +102,14 @@ impl crate::Session { pub fn preview_job(&mut self, id: PhotoId, max_w: usize, max_h: usize, apply_crop: bool, settings: &DevelopSettings) -> Option { let mut job = self.render_job(id, max_w, max_h, false, apply_crop)?; job.settings = Arc::new(settings.clone()); - job.key = settings.hash64() ^ ((max_w as u64) << 40) ^ ((max_h as u64) << 20) ^ (apply_crop as u64) ^ (job.level as u64) << 60; + // (the photo's database lens profile stays part of it: a profile looked up again renders afresh) + let lens = self.catalog.photo(id).map_or(0, |p| super::quick::lens_key(p)); + job.key = settings.hash64() + ^ ((max_w as u64) << 40) + ^ ((max_h as u64) << 20) + ^ (apply_crop as u64) + ^ (job.level as u64) << 60 + ^ lens.rotate_left(29); Some(job) } diff --git a/crates/engine/src/media/thumbnail_hash_tests.rs b/crates/engine/src/media/thumbnail_hash_tests.rs index d8dd22edfb..23fe23c735 100644 --- a/crates/engine/src/media/thumbnail_hash_tests.rs +++ b/crates/engine/src/media/thumbnail_hash_tests.rs @@ -107,6 +107,18 @@ fn source_from_before_a_lens_profile_change_is_not_accepted() { s.accept(&new); assert_ne!(s.media.source_usage().0, 0); } +/// The loupe's hover preview (a preset or profile under the pointer) is cached by its key, so a database lens +/// profile looked up again gives it a new key and the hover renders afresh instead of showing the old correction. +#[test] +fn a_hover_preview_key_follows_the_lens_profile() { + let mut s = session(); + let settings = s.catalog.photo(PhotoId(1)).unwrap().develop.clone(); + let before = s.preview_job(PhotoId(1), 640, 480, true, &settings).unwrap().key; + let lens = lightcraft_develop::EmbeddedLens { source: lightcraft_develop::LensSource::Database, ..Default::default() }; + s.catalog.apply(Op::SetEmbeddedLens { id: PhotoId(1), lens: Some(Box::new(lens)) }).unwrap(); + let after = s.preview_job(PhotoId(1), 640, 480, true, &settings).unwrap().key; + assert_ne!(before, after); +} /// A panic while rendering (here in the decoder) used to end the pool's worker thread without /// an answer, so the view that asked showed "Rendering…" forever. It is the job's failure now, /// for renders and for stand-ins, answering the request it came from. From 3698966e2fb438a6a9740d4f6141d96657cb6d21 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Sun, 1 Nov 2026 20:33:43 +1000 Subject: [PATCH 27/51] LR-EDIT-OPTICS-PROFILE: the lens database folder is held by an operating-system lock, and startup recovery waits its turn (CodeRabbit, #3073) DirLock is now File::try_lock on the file beside the folder (flock / LockFileEx), released by the system when the file closes or the process ends: no age-based takeover, so a long download is never taken over and a crash never leaves the folder locked. recover takes the lock and skips while a download holds it (the start paths: with_default_lens_database and fetch_on_start); fetch_all, which holds it, recovers through recover_locked. The hover-preview key test also swaps one database profile for another. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lensfun_download.rs | 120 +++++++++++------- .../engine/src/media/thumbnail_hash_tests.rs | 7 + 2 files changed, 81 insertions(+), 46 deletions(-) diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index 35828eab76..94447ace73 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -38,9 +38,6 @@ const PREVIOUS: &str = "previous"; /// app may be the one downloading). #[cfg(not(target_arch = "wasm32"))] const LOCK: &str = "lock"; -/// A lock this old is left over from an app that quit mid-download, not a download still running. -#[cfg(not(target_arch = "wasm32"))] -const STALE_LOCK: std::time::Duration = std::time::Duration::from_secs(60 * 60); /// Largest database file accepted (the biggest is under 1 MB). #[cfg(not(target_arch = "wasm32"))] const MAX_FILE: u64 = 8 << 20; @@ -135,12 +132,27 @@ fn beside(dir: &Path, suffix: &str) -> PathBuf { } /// Put back the previous database when a swap ([`publish`]) was interrupted between its two moves (the app quit or -/// the machine stopped): `dir` is missing and the previous folder is whole. Best effort; says why it couldn't. +/// the machine stopped): `dir` is missing and the previous folder is whole. Best effort; says why it couldn't. Done +/// under the folder's download lock (`DirLock`) and skipped while a download, in this or another LightCraft, holds +/// it: that download is between its own two moves, not interrupted, and puts the folder right itself. pub fn recover(dir: &Path) { - let previous = beside(dir, PREVIOUS); - if !dir.exists() - && complete(&previous) - && let Err(e) = std::fs::rename(&previous, dir) + if !needs_recovery(dir) { + return; + } + #[cfg(not(target_arch = "wasm32"))] + let Ok(_lock) = DirLock::take(dir) else { return }; + recover_locked(dir); +} + +/// What a swap interrupted between its two moves leaves: no database folder, and a whole previous one. +fn needs_recovery(dir: &Path) -> bool { + !dir.exists() && complete(&beside(dir, PREVIOUS)) +} + +/// [`recover`] by a caller that already holds the folder's lock (a download). +fn recover_locked(dir: &Path) { + if needs_recovery(dir) + && let Err(e) = std::fs::rename(beside(dir, PREVIOUS), dir) { log::warn!("lens database: could not put back the previous database: {e}"); } @@ -336,10 +348,16 @@ fn run(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) { } } -/// Held while one download uses a folder's staging and previous folders, across sessions and processes: a file -/// beside the folder, made only if it isn't there, removed when the download ends. +/// Held while one download (or a [`recover`]) uses a folder's staging and previous folders, across sessions and +/// processes: an exclusive operating-system lock on a file beside the folder (`File::try_lock`: `flock` on Unix, +/// `LockFileEx` on Windows), held by the open file. The system lets go of it when the file closes, on drop or when the +/// process ends however it ends, so an app that crashed never leaves the folder locked and no age guess is needed. +/// The file itself stays (empty): removing it would let a second app lock a new file of the same name while the first +/// still holds the old one. #[cfg(not(target_arch = "wasm32"))] -struct DirLock(PathBuf); +struct DirLock { + _file: std::fs::File, +} #[cfg(not(target_arch = "wasm32"))] impl DirLock { @@ -348,29 +366,18 @@ impl DirLock { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).map_err(|e| format!("could not create {}: {e}", parent.display()))?; } - for _ in 0..2 { - match std::fs::OpenOptions::new().write(true).create_new(true).open(&path) { - Ok(_) => return Ok(DirLock(path)), - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { - let stale = - std::fs::metadata(&path).and_then(|m| m.modified()).ok().and_then(|t| t.elapsed().ok()).is_some_and(|age| age > STALE_LOCK); - if !stale { - return Err("the lens database is already downloading (in another LightCraft window)".into()); - } - // left by an app that quit mid-download: taken over - let _ = std::fs::remove_file(&path); - } - Err(e) => return Err(format!("could not create {}: {e}", path.display())), - } + let file = std::fs::OpenOptions::new() + .read(true) + .write(true) + .create(true) + .truncate(false) + .open(&path) + .map_err(|e| format!("could not create {}: {e}", path.display()))?; + match file.try_lock() { + Ok(()) => Ok(DirLock { _file: file }), + Err(std::fs::TryLockError::WouldBlock) => Err("the lens database is already downloading (in another LightCraft window)".into()), + Err(std::fs::TryLockError::Error(e)) => Err(format!("could not lock {}: {e}", path.display())), } - Err("the lens database is already downloading (in another LightCraft window)".into()) - } -} - -#[cfg(not(target_arch = "wasm32"))] -impl Drop for DirLock { - fn drop(&mut self) { - let _ = std::fs::remove_file(&self.0); } } @@ -378,7 +385,7 @@ impl Drop for DirLock { fn fetch_all(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) -> Result { // (from before the recovery through the swap: another download of this folder would move the same folders) let _lock = DirLock::take(dir)?; - recover(dir); + recover_locked(dir); let staging = beside(dir, STAGING); std::fs::create_dir_all(&staging).map_err(|e| format!("could not create {}: {e}", staging.display()))?; let specs: Vec> = @@ -472,24 +479,45 @@ mod tests { use std::time::Duration; /// Two downloads into one folder (two windows, or two sessions of one app) never share its staging and previous - /// folders: the second is refused while the first holds the folder, and may go once it ends; a lock left by an - /// app that quit mid-download is taken over. + /// folders: the second is refused while the first holds the folder, however old the first's lock file is, and + /// may go once it ends; the file an app that quit mid-download leaves holds nothing (the system let go of it). #[test] fn one_download_at_a_time_holds_a_folder() { let d = scratch("lock"); let _ = std::fs::remove_file(beside(&d, LOCK)); let first = DirLock::take(&d).unwrap(); - let second = DirLock::take(&d); - assert!(second.as_ref().is_err_and(|e| e.contains("already downloading")), "{:?}", second.as_ref().err()); - drop(first); - let again = DirLock::take(&d).unwrap(); - std::mem::forget(again); - // (the app quit with the lock in place an hour and more ago) - let old = std::time::SystemTime::now() - STALE_LOCK - Duration::from_secs(60); + // (a download running for a week on a slow line: its lock file is old, and it still holds the folder) + let old = std::time::SystemTime::now() - Duration::from_secs(7 * 24 * 60 * 60); std::fs::OpenOptions::new().write(true).open(beside(&d, LOCK)).unwrap().set_modified(old).unwrap(); - let taken = DirLock::take(&d).unwrap(); - drop(taken); - assert!(!beside(&d, LOCK).exists(), "removed when the download ends"); + for _ in 0..3 { + let second = DirLock::take(&d); + assert!(second.as_ref().is_err_and(|e| e.contains("already downloading")), "{:?}", second.as_ref().err()); + } + drop(first); + drop(DirLock::take(&d).unwrap()); + // (what an app that quit mid-download leaves: the file, unlocked) + assert!(beside(&d, LOCK).exists()); + drop(DirLock::take(&d).unwrap()); + let _ = std::fs::remove_file(beside(&d, LOCK)); + } + + /// The start's recovery moves nothing while a download holds the folder: between that download's two moves the + /// folder is missing and the previous one whole, which is not an interrupted swap. + #[test] + fn recovery_leaves_a_folder_a_download_holds() { + let dir = scratch("recover-locked"); + std::fs::remove_dir_all(&dir).unwrap(); + let previous = beside(&dir, PREVIOUS); + std::fs::create_dir_all(&previous).unwrap(); + std::fs::write(previous.join(MARKER), "older").unwrap(); + let held = DirLock::take(&dir).unwrap(); + recover(&dir); + assert!(!dir.exists() && complete(&previous), "a download's swap is left alone"); + drop(held); + recover(&dir); + assert!(complete(&dir) && !previous.exists(), "an interrupted swap is put back"); + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_file(beside(&dir, LOCK)); } fn scratch(name: &str) -> PathBuf { diff --git a/crates/engine/src/media/thumbnail_hash_tests.rs b/crates/engine/src/media/thumbnail_hash_tests.rs index 23fe23c735..dccc6c28c0 100644 --- a/crates/engine/src/media/thumbnail_hash_tests.rs +++ b/crates/engine/src/media/thumbnail_hash_tests.rs @@ -118,6 +118,13 @@ fn a_hover_preview_key_follows_the_lens_profile() { s.catalog.apply(Op::SetEmbeddedLens { id: PhotoId(1), lens: Some(Box::new(lens)) }).unwrap(); let after = s.preview_job(PhotoId(1), 640, 480, true, &settings).unwrap().key; assert_ne!(before, after); + // one database profile looked up again as another (a newer database): the key follows the profile's contents, + // not only whether there is one + let vignette = lightcraft_develop::EmbeddedVignette { k: [-0.3, 0.0, 0.0, 0.0, 0.0], radius: 1.0, ..Default::default() }; + let other = lightcraft_develop::EmbeddedLens { source: lightcraft_develop::LensSource::Database, vignette: Some(vignette), ..Default::default() }; + s.catalog.apply(Op::SetEmbeddedLens { id: PhotoId(1), lens: Some(Box::new(other)) }).unwrap(); + let again = s.preview_job(PhotoId(1), 640, 480, true, &settings).unwrap().key; + assert_ne!(after, again, "a different database profile"); } /// A panic while rendering (here in the decoder) used to end the pool's worker thread without /// an answer, so the view that asked showed "Rendering…" forever. It is the job's failure now, From a20887e09736438b290e66f118b5b33213dc6204 Mon Sep 17 00:00:00 2001 From: Julian Zahnow <54999052+Enthendor@users.noreply.github.com> Date: Sun, 1 Nov 2026 11:54:10 +0100 Subject: [PATCH 28/51] LR-EDIT-OPTICS-PROFILE: record the LensModel strings seen for the Sigma 10-18 mm exception Co-Authored-By: Claude Opus 5.5 --- crates/raw/src/vendor/arw_lens_rm6.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/raw/src/vendor/arw_lens_rm6.rs b/crates/raw/src/vendor/arw_lens_rm6.rs index 579dcf67ab..5a215ef7a2 100644 --- a/crates/raw/src/vendor/arw_lens_rm6.rs +++ b/crates/raw/src/vendor/arw_lens_rm6.rs @@ -78,7 +78,8 @@ struct LensException { /// The measured lens exceptions (docs/sony-ilce7rm6-lens-corrections.md → ILCE-6700). const LENS_EXCEPTIONS: &[LensException] = &[ // Sigma 10-18 mm F2.8 DC DN at its wide end: 97 photos at 10 mm and 2 at 10.4-10.5 mm. The body's rule leaves - // 1.36-1.38 px there; from 11.7 mm on the body's rule fits better + // 1.36-1.38 px there; from 11.7 mm on the body's rule fits better. `lens` is the only LensModel in 336 files of + // this lens (native E-mount, no adapter); the 116 measured copies carry body firmware "ILCE-6700 v2.00" LensException { body: "ILCE-6700", lens: "10-18mm F2.8 DC DN | Contemporary 023", max_focal_mm: 10.5, knot_spacing: 416.0, unit: 14250.0 }, ]; From 172467bfc810103dd9cae416fbc35d4b2c965da9 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Sun, 1 Nov 2026 21:55:59 +1000 Subject: [PATCH 29/51] LR-EDIT-OPTICS-PROFILE: a failed swap keeps the only whole lens database, and the folder lock is proved across processes (CodeRabbit, #3073) publish removes a leftover previous database only when the database folder is there; without it (a recovery that couldn't put it back) the previous copy stays until the new one is in place. recover logs why it skipped when it can't take the lock, and DirLock unlocks explicitly on drop so a process another thread is spawning can't keep it. New tests: a_failed_swap_keeps_the_only_whole_database, another_process_holding_the_folder_refuses_a_download_here (a child process holds the lock). Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/lensfun_download.rs | 104 ++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/lensfun_download.rs b/crates/engine/src/lensfun_download.rs index 94447ace73..c24b07f7a9 100644 --- a/crates/engine/src/lensfun_download.rs +++ b/crates/engine/src/lensfun_download.rs @@ -140,7 +140,13 @@ pub fn recover(dir: &Path) { return; } #[cfg(not(target_arch = "wasm32"))] - let Ok(_lock) = DirLock::take(dir) else { return }; + let _lock = match DirLock::take(dir) { + Ok(lock) => lock, + Err(e) => { + log::warn!("lens database: the previous database is not put back now: {e}"); + return; + } + }; recover_locked(dir); } @@ -353,10 +359,18 @@ fn run(dir: &Path, mirrors: &[String], files: &[Pin], shared: &Shared) { /// `LockFileEx` on Windows), held by the open file. The system lets go of it when the file closes, on drop or when the /// process ends however it ends, so an app that crashed never leaves the folder locked and no age guess is needed. /// The file itself stays (empty): removing it would let a second app lock a new file of the same name while the first -/// still holds the old one. +/// still holds the old one. Dropping it unlocks explicitly: a process another thread is spawning holds a copy of the +/// file until it execs, and on Unix the lock would last as long as that copy. #[cfg(not(target_arch = "wasm32"))] struct DirLock { - _file: std::fs::File, + file: std::fs::File, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Drop for DirLock { + fn drop(&mut self) { + let _ = self.file.unlock(); + } } #[cfg(not(target_arch = "wasm32"))] @@ -374,7 +388,7 @@ impl DirLock { .open(&path) .map_err(|e| format!("could not create {}: {e}", path.display()))?; match file.try_lock() { - Ok(()) => Ok(DirLock { _file: file }), + Ok(()) => Ok(DirLock { file }), Err(std::fs::TryLockError::WouldBlock) => Err("the lens database is already downloading (in another LightCraft window)".into()), Err(std::fs::TryLockError::Error(e)) => Err(format!("could not lock {}: {e}", path.display())), } @@ -423,11 +437,12 @@ from https://github.com/lensfun/lensfun, downloaded by LightCraft. #[cfg(not(target_arch = "wasm32"))] fn publish(staged: &Path, dir: &Path, rename: impl Fn(&Path, &Path) -> std::io::Result<()>) -> Result<(), String> { let previous = beside(dir, PREVIOUS); - // left by a swap that ended after its second move - if previous.exists() { + let had = dir.exists(); + // left by a swap that ended after its second move: `dir` is the database, so this copy can go. Without `dir` + // (a recovery that couldn't put it back) it may be the only whole database: kept until the new one is in place + if had && previous.exists() { std::fs::remove_dir_all(&previous).map_err(|e| format!("could not remove {}: {e}", previous.display()))?; } - let had = dir.exists(); if had { rename(dir, &previous).map_err(|e| format!("could not move the old lens database aside ({}): {e}", dir.display()))?; } @@ -438,7 +453,9 @@ fn publish(staged: &Path, dir: &Path, rename: impl Fn(&Path, &Path) -> std::io:: } return Err(format!("could not move the new lens database into {}: {e}", dir.display())); } - if had && let Err(e) = std::fs::remove_dir_all(&previous) { + if previous.exists() + && let Err(e) = std::fs::remove_dir_all(&previous) + { log::warn!("lens database: could not remove the previous database {}: {e}", previous.display()); } Ok(()) @@ -520,6 +537,77 @@ mod tests { let _ = std::fs::remove_file(beside(&dir, LOCK)); } + /// The lock holds across processes: while another process (another LightCraft) holds the folder, a download + /// here is refused; once that process is gone (killed, like a crash), the system has let go and it may start. + #[test] + fn another_process_holding_the_folder_refuses_a_download_here() { + let dir = scratch("lock-child"); + let ready = dir.join("child-locked"); + let exe = std::env::current_exe().unwrap(); + let mut child = std::process::Command::new(exe) + .args(["--exact", "lensfun_download::tests::lock_holder_child", "--ignored", "--test-threads=1"]) + .env("LC_LENSFUN_LOCK_CHILD_DIR", &dir) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .spawn() + .unwrap(); + let deadline = std::time::Instant::now() + Duration::from_secs(60); + while !ready.exists() { + assert!(child.try_wait().unwrap().is_none(), "the child exited before locking"); + assert!(std::time::Instant::now() < deadline, "the child never locked"); + std::thread::sleep(Duration::from_millis(20)); + } + let here = DirLock::take(&dir); + let refused = here.as_ref().is_err_and(|e| e.contains("already downloading")); + child.kill().unwrap(); + child.wait().unwrap(); + assert!(refused, "{:?}", here.as_ref().err()); + drop(DirLock::take(&dir).expect("the system let go of the dead process's lock")); + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_file(beside(&dir, LOCK)); + } + + /// Run by [`another_process_holding_the_folder_refuses_a_download_here`] in a child process: take the folder's + /// lock, say so, and wait to be killed. + #[test] + #[ignore = "helper process for another_process_holding_the_folder_refuses_a_download_here"] + fn lock_holder_child() { + let Some(dir) = std::env::var_os("LC_LENSFUN_LOCK_CHILD_DIR") else { return }; + let dir = PathBuf::from(dir); + let _held = DirLock::take(&dir).unwrap(); + std::fs::write(dir.join("child-locked"), b"").unwrap(); + std::thread::sleep(Duration::from_secs(120)); + } + + /// A swap into a folder that is missing, with the previous database still beside it (a recovery that couldn't + /// put it back), keeps that previous database until the new one is in place: when the new one can't be moved + /// in, the previous one is still there for the next start to put back. + #[test] + fn a_failed_swap_keeps_the_only_whole_database() { + let dir = scratch("keep-previous"); + std::fs::remove_dir_all(&dir).unwrap(); + let previous = beside(&dir, PREVIOUS); + std::fs::create_dir_all(&previous).unwrap(); + std::fs::write(previous.join(MARKER), "older").unwrap(); + let staged = beside(&dir, STAGING); + std::fs::create_dir_all(&staged).unwrap(); + std::fs::write(staged.join(MARKER), COMMIT).unwrap(); + let err = publish(&staged, &dir, |_, _| Err(std::io::Error::other("in use"))).unwrap_err(); + assert!(err.contains("in use"), "{err}"); + assert!(complete(&previous), "the previous database is kept"); + recover(&dir); + assert_eq!(std::fs::read_to_string(dir.join(MARKER)).unwrap(), "older", "and the next start puts it back"); + // a swap that works into a missing folder removes the previous copy once the new one is in place + std::fs::remove_dir_all(&dir).unwrap(); + std::fs::create_dir_all(&previous).unwrap(); + std::fs::write(previous.join(MARKER), "older").unwrap(); + publish(&staged, &dir, |from, to| std::fs::rename(from, to)).unwrap(); + assert_eq!(std::fs::read_to_string(dir.join(MARKER)).unwrap(), COMMIT); + assert!(!previous.exists()); + let _ = std::fs::remove_dir_all(&dir); + let _ = std::fs::remove_file(beside(&dir, LOCK)); + } + fn scratch(name: &str) -> PathBuf { let d = std::env::temp_dir().join(format!("lc-lensfun-{name}-{}", std::process::id())); for leftover in [d.clone(), beside(&d, STAGING), beside(&d, PREVIOUS)] { From 2ad7207127f758a6602e5e52712f7b8c57b3794e Mon Sep 17 00:00:00 2001 From: OneSlips Date: Mon, 2 Nov 2026 03:14:38 +1000 Subject: [PATCH 30/51] LR-MASK-GEOMETRY: masks stay on the photo under lens corrections, Upright, Transform and Rotate Mask coordinates lived on the corrected frame, so a Transform, Upright or lens-correction change moved the photo under them and the masked look stayed behind, offset from the person it was drawn on (4.3 px on a 240 px render under Vertical 25 + Rotate 4: about 100 px at 6000 px). Rotate Left / Right turned the crop but not the masks. A mask's coordinates are now on the photo before the warp (Mask::on_photo, catalog format 10): the CPU render, the GPU render, Auto Mask brush windows and the on-photo tools map them through the warp (Frame::for_mask, Warp::from_source). Rotate Left / Right turns masks with the photo (mask_space::turn). The AI models see the photo without lens corrections, Upright, Transform, crop or flips, so stored Object / Prompt selections stay on their content and changing those doesn't ask the model again. Older masks (no on_photo) render where they were and move onto the photo, without moving, at their next edit (mask_space::attach). Co-Authored-By: Claude Opus 5.5 --- crates/catalog/src/journal.rs | 3 +- crates/catalog/src/tests_format_version.rs | 4 +- crates/develop/src/settings.rs | 22 +- crates/engine/src/cmd/library/selection.rs | 9 +- crates/engine/src/lib.rs | 39 +++ crates/engine/src/segment.rs | 22 +- crates/engine/src/tests.rs | 1 + crates/engine/src/tests/mask_geometry.rs | 113 ++++++++ crates/gpu/src/render.rs | 6 + crates/gpu/tests/mask_geometry.rs | 140 ++++++++++ crates/pipeline/src/geometry.rs | 90 ++++++ crates/pipeline/src/lib.rs | 3 + crates/pipeline/src/mask_space.rs | 274 +++++++++++++++++++ crates/pipeline/src/masks.rs | 22 +- crates/pipeline/src/optics.rs | 33 +++ crates/pipeline/src/spots.rs | 15 +- crates/pipeline/src/tests_mask_geometry.rs | 182 ++++++++++++ crates/ui-egui/src/panels/detail/geometry.rs | 66 ++++- crates/ui-egui/src/panels/detail/masks.rs | 15 +- docs/parity-checklist.md | 4 +- 20 files changed, 1031 insertions(+), 32 deletions(-) create mode 100644 crates/engine/src/tests/mask_geometry.rs create mode 100644 crates/gpu/tests/mask_geometry.rs create mode 100644 crates/pipeline/src/mask_space.rs create mode 100644 crates/pipeline/src/tests_mask_geometry.rs diff --git a/crates/catalog/src/journal.rs b/crates/catalog/src/journal.rs index 2600d794a4..ddcfde7edd 100644 --- a/crates/catalog/src/journal.rs +++ b/crates/catalog/src/journal.rs @@ -52,6 +52,7 @@ //! | 7 | Kelvin white balance for raws whose camera has matrices (#730) | `Op::SetAsShotWb`, `WhiteBalance.scale` | //! | 8 | History cursor: Go to History Step adds no step | `Op::SetHistoryCursor`, `Photo.history_cursor` | //! | 9 | preview-only recheck | `Photo.preview_checked`, `Op::SetContent.preview_checked` | +//! | 10 | masks stay on the photo under lens corrections, Upright and Transform | `Mask.on_photo` (develop settings) | //! //! Rules: //! - **Bump [`VERSION`]** (and add a row above) in the change that adds an [`Op`] variant or a @@ -76,7 +77,7 @@ pub const LOG: &str = "catalog.log"; const FORMAT: &str = "lightcraft-catalog"; /// The catalog format this build writes (and the newest it reads). See the module docs → /// *Format versions*; bump it whenever an [`Op`] variant or a serialized field is added. -pub const VERSION: u32 = 9; +pub const VERSION: u32 = 10; /// When [`Journal::wants_snapshot`] says it's time to compact the log. #[derive(Clone, Copy, Debug)] diff --git a/crates/catalog/src/tests_format_version.rs b/crates/catalog/src/tests_format_version.rs index e31d7fe0c4..87ced1b871 100644 --- a/crates/catalog/src/tests_format_version.rs +++ b/crates/catalog/src/tests_format_version.rs @@ -238,8 +238,8 @@ fn op_variants_are_versioned() { Op::SetHistoryCursor { .. } => 8, } } - /// v9: `Photo.preview_checked` and `Op::SetContent.preview_checked`. - const NEWEST_FIELDS: u32 = 9; + /// v10: `Mask.on_photo` (v9: `Photo.preview_checked` and `Op::SetContent.preview_checked`). + const NEWEST_FIELDS: u32 = 10; let newest = since(&Op::SetHistoryCursor { id: PhotoId(1), cursor: None }); assert_eq!(newest.max(NEWEST_FIELDS), VERSION, "the newest op's or fields' version must be the current format version"); } diff --git a/crates/develop/src/settings.rs b/crates/develop/src/settings.rs index 739171abd9..2c880bc69e 100644 --- a/crates/develop/src/settings.rs +++ b/crates/develop/src/settings.rs @@ -1054,15 +1054,35 @@ pub struct Mask { /// Refine Edges 0..100: the mask's edges snap to the photo's (guided filter). #[serde(skip_serializing_if = "is_zero")] pub refine: f64, + /// Where its coordinates (gradients, brush strokes, AI selections) live: `true`, on the photo as + /// shot (user orientation, before lens corrections, Upright and Transform), so the mask stays + /// on the same content whatever those do; `false`, a mask from before that, on the corrected + /// frame. Read as `false` when absent; [`Mask::default`] (every new mask) is `true`, and an + /// edit converts an old one (`lightcraft_pipeline::mask_space::attach`). + #[serde(default, skip_serializing_if = "is_false")] + pub on_photo: bool, } fn is_zero(v: &f64) -> bool { *v == 0.0 } +fn is_false(v: &bool) -> bool { + !*v +} + impl Default for Mask { fn default() -> Self { - Self { id: 0, name: "Mask 1".into(), visible: true, invert: false, components: Vec::new(), adjust: LocalAdjustments::default(), refine: 0.0 } + Self { + id: 0, + name: "Mask 1".into(), + visible: true, + invert: false, + components: Vec::new(), + adjust: LocalAdjustments::default(), + refine: 0.0, + on_photo: true, + } } } diff --git a/crates/engine/src/cmd/library/selection.rs b/crates/engine/src/cmd/library/selection.rs index 964b2a8c85..40130adb9d 100644 --- a/crates/engine/src/cmd/library/selection.rs +++ b/crates/engine/src/cmd/library/selection.rs @@ -182,12 +182,17 @@ pub(super) fn rotate(s: &mut Session, p: &Value, cw: bool) -> Result { let targets = s.targets(p)?; let mut ops = Vec::new(); for id in targets { - if let Some(d) = s.develop_of(id) { - let mut d = (*d).clone(); + if let Some(before) = s.develop_of(id) { + let mut d = (*before).clone(); d.orientation = d.orientation.rotated(cw); // the crop lives in the oriented frame, so it turns with the image (else it keeps the old width and height and cuts the photo) let turn = lightcraft_geom::Orientation::Normal.rotated(cw); d.crop.geometry.rect = turn.map_norm_rect(d.crop.geometry.rect); + // and so do the masks: on the photo first (an old one), then turned with it + s.attach_masks(id, &before, &mut d); + for m in &mut d.masks { + lightcraft_pipeline::mask_space::turn(m, turn); + } d.crop.aspect = d.crop.aspect.map(|(w, h)| (h, w)); ops.extend(s.develop_op(id, d, "Rotate")); } diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 163a102680..84cef73d85 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1023,6 +1023,17 @@ impl Session { /// The op that sets a photo's develop settings and appends a History entry (for batches). pub fn develop_op(&self, id: PhotoId, settings: DevelopSettings, label: &str) -> Option { self.catalog.photo(id)?; + let mut settings = settings; + // old masks (on the corrected frame) go onto the photo, from the frame they were drawn on + if settings.masks.iter().any(|m| !m.on_photo) { + let before = match &self.interaction { + Some(i) if i.photo == id => Some(i.original.clone()), + _ => self.develop_of(id), + }; + if let Some(b) = before { + self.attach_masks(id, &b, &mut settings); + } + } let settings = Arc::new(settings); let step = lightcraft_catalog::HistoryStep { label: label.into(), settings: settings.clone() }; Some(Op::Batch { @@ -1030,11 +1041,39 @@ impl Session { }) } + /// Put the old masks of `d` (on the corrected frame, `Mask::on_photo` false) on photo `id` + /// before the warp, from the frame of `drawn` (the settings they were drawn under): they + /// render where they did and from then on stay on their content when lens corrections, + /// Upright or Transform change. + pub fn attach_masks(&self, id: PhotoId, drawn: &DevelopSettings, d: &mut DevelopSettings) { + if d.masks.iter().all(|m| m.on_photo) { + return; + } + let Some(p) = self.catalog.photo(id) else { return }; + let frame = lightcraft_pipeline::geometry::Frame::with_lens( + p.width.max(1) as usize, + p.height.max(1) as usize, + &drawn.effective(), + false, + p.embedded_lens.as_ref(), + ); + for m in &mut d.masks { + lightcraft_pipeline::mask_space::attach(m, &frame); + } + } + pub fn begin_interaction(&mut self, label: &str) -> Result<()> { if self.interaction.is_some() { self.end_interaction()?; } let id = self.active().ok_or_else(|| EngineError::Other("no active photo".into()))?; + // old masks go onto the photo before a drag moves the frame under them (they then stay on + // their content while it does, not only once it ends) + if let Some(d) = self.develop_of(id).filter(|d| d.masks.iter().any(|m| !m.on_photo)) { + let mut on = (*d).clone(); + self.attach_masks(id, &d, &mut on); + self.apply_silent(Op::SetDevelop { id, settings: Arc::new(on), label: label.into(), edited: None })?; + } let original = self.develop_of(id).unwrap_or_default(); self.interaction = Some(Interaction { label: label.into(), photo: id, original, crop_edited: false }); Ok(()) diff --git a/crates/engine/src/segment.rs b/crates/engine/src/segment.rs index b69a6dca27..53ee4a5f83 100644 --- a/crates/engine/src/segment.rs +++ b/crates/engine/src/segment.rs @@ -376,12 +376,23 @@ fn clicks_of(include: &[Point], exclude: &[Point]) -> Vec { } impl Session { - /// The key and settings of photo `id` as the model sees it: its look without masks, uncropped. + /// The key and settings of photo `id` as the model sees it: its look without masks, uncropped + /// and unflipped, and without lens corrections, Upright and Transform. Mask coordinates + /// (clicks, stored selections) live on the photo before those (`Mask::on_photo`), so a + /// selection stays on its content when they change, and changing them doesn't ask again. #[cfg_attr(not(feature = "sam"), allow(dead_code))] - fn segment_key(&self, id: PhotoId) -> Option<(u64, lightcraft_develop::DevelopSettings)> { + pub(crate) fn segment_key(&self, id: PhotoId) -> Option<(u64, lightcraft_develop::DevelopSettings)> { let p = self.catalog.photo(id)?; let mut d = (*p.develop).clone(); d.masks.clear(); + // (the crop doesn't show in the uncropped input: it doesn't ask again either) + d.crop = Default::default(); + d.geometry = Default::default(); + d.optics.lens_profile = false; + d.optics.distortion = 0.0; + d.optics.remove_ca = false; + d.optics.ca_red = 0.0; + d.optics.ca_blue = 0.0; let content = crate::media::content_key(p); let mut h = mix(0xcbf2_9ce4_8422_2325, d.hash64()); h = mix(h, id.0); @@ -410,6 +421,13 @@ impl Session { region: Option<[f64; 4]>, ) -> Result { let dir = self.segmenter.model_dir()?; + // old masks go onto the photo first: the selection that comes back is in its coordinates + if let Some(cur) = self.develop_of(id).filter(|d| d.masks.iter().any(|m| !m.on_photo)) { + let mut on = (*cur).clone(); + self.attach_masks(id, &cur, &mut on); + self.apply_silent(lightcraft_catalog::Op::SetDevelop { id, settings: std::sync::Arc::new(on), label: "Masks".into(), edited: None }) + .map_err(|e| e.to_string())?; + } let (key, mut d) = self.segment_key(id).ok_or("no such photo")?; let p = self.catalog.photo(id).ok_or("no such photo")?.clone(); let missing = match &p.source { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 37b300dbfc..9a409cbc9a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -33,6 +33,7 @@ mod libops; mod library; mod lightroom_folder; mod mask_effects; +mod mask_geometry; mod merge; mod move_to_share; mod organize; diff --git a/crates/engine/src/tests/mask_geometry.rs b/crates/engine/src/tests/mask_geometry.rs new file mode 100644 index 0000000000..9c5681a4f7 --- /dev/null +++ b/crates/engine/src/tests/mask_geometry.rs @@ -0,0 +1,113 @@ +//! Masks stay on the photo's content when its geometry changes: Rotate +//! Left / Right turns them with the photo, an old mask (on the corrected frame) goes onto the +//! photo at its next edit without moving, and the AI models see the photo the masks live on +//! (no lens corrections, Upright, Transform, crop or flips), so changing those leaves the AI +//! selections as they are. + +use lightcraft_develop::{DevelopSettings, Mask, MaskComponent, MaskOp, MaskShape}; +use lightcraft_geom::Point; +use serde_json::json; + +use crate::Session; + +fn active_dev(s: &Session) -> DevelopSettings { + (*s.develop_of(s.active().unwrap()).unwrap()).clone() +} + +fn radial_at(p: Point) -> Mask { + Mask { + id: 1, + components: vec![MaskComponent { + name: None, + op: MaskOp::Add, + invert: false, + shape: MaskShape::Radial { center: p, rx: 0.1, ry: 0.05, angle: 20.0, feather: 50.0, invert: false }, + }], + ..Default::default() + } +} + +fn centre(d: &DevelopSettings) -> (Point, f64) { + match &d.masks[0].components[0].shape { + MaskShape::Radial { center, angle, .. } => (*center, *angle), + other => panic!("{other:?}"), + } +} + +#[test] +fn rotating_the_photo_turns_its_masks_with_it() { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + let mut d = active_dev(&s); + let p = Point::new(0.2, 0.3); + d.masks = vec![radial_at(p)]; + s.set_develop(id, d, "Mask").unwrap(); + s.execute("photo.rotateRight", &json!({})).unwrap(); + let (c, a) = centre(&active_dev(&s)); + // a quarter turn clockwise: (x, y) → (1 − y, x), and the ellipse turns along + assert!((c.x - 0.7).abs() < 1e-9 && (c.y - 0.2).abs() < 1e-9, "{c:?}"); + assert_eq!(a, 110.0); + s.execute("photo.rotateLeft", &json!({})).unwrap(); + let (c, a) = centre(&active_dev(&s)); + assert!((c.x - p.x).abs() < 1e-9 && (c.y - p.y).abs() < 1e-9 && a == 20.0, "{c:?} {a}"); +} + +#[test] +fn an_old_mask_goes_onto_the_photo_at_its_next_edit_without_moving() { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + let mut d = active_dev(&s); + d.geometry.vertical = 30.0; + d.geometry.rotate = 5.0; + let p = Point::new(0.3, 0.4); + let mut m = radial_at(p); + m.on_photo = false; + d.masks = vec![m]; + // as a catalog from before keeps it + s.catalog.apply(lightcraft_catalog::Op::SetDevelop { id, settings: std::sync::Arc::new(d.clone()), label: "Old".into(), edited: None }).unwrap(); + assert!(!active_dev(&s).masks[0].on_photo); + d.light.exposure = 0.5; + s.set_develop(id, d.clone(), "Exposure").unwrap(); + let now = active_dev(&s); + assert!(now.masks[0].on_photo, "attached at the edit"); + let photo = s.catalog.photo(id).unwrap(); + let frame = lightcraft_pipeline::geometry::Frame::with_lens(photo.width as usize, photo.height as usize, &d, false, None); + let (c, _) = centre(&now); + let back = frame.for_mask(true).mask_to_norm(c); + assert!((back.x - p.x).abs() < 1e-6 && (back.y - p.y).abs() < 1e-6, "renders where it did: {back:?}"); + assert!((c.x - p.x).hypot(c.y - p.y) > 0.005, "its place on the photo differs from the corrected frame's: {c:?}"); + // a later Transform change leaves it on the photo where it is + let mut d = now.clone(); + d.geometry.vertical = -10.0; + s.set_develop(id, d, "Transform").unwrap(); + assert_eq!(centre(&active_dev(&s)).0, c); +} + +#[test] +fn the_ai_models_see_the_photo_the_masks_live_on() { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + let key = s.segment_key(id).unwrap().0; + let mut d = active_dev(&s); + d.geometry.vertical = 20.0; + d.optics.distortion = 30.0; + d.crop.geometry.angle = 5.0; + d.crop.flip_h = true; + s.set_develop(id, d.clone(), "Geometry").unwrap(); + assert_eq!(s.segment_key(id).unwrap().0, key, "geometry doesn't change what the models see: the AI masks stay valid"); + d.light.exposure = 1.0; + s.set_develop(id, d, "Exposure").unwrap(); + assert_ne!(s.segment_key(id).unwrap().0, key, "the look still does"); +} + +/// A mask saved before `Mask::on_photo` reads as an old mask (on the corrected frame), a new mask +/// is on the photo, and an old one is written as it was read. +#[test] +fn a_mask_saved_before_reads_as_an_old_mask() { + let old: Mask = serde_json::from_value(json!({"id": 3, "name": "Mask 3", "components": []})).unwrap(); + assert!(!old.on_photo); + assert!(Mask::default().on_photo); + let written = serde_json::to_value(&old).unwrap(); + assert!(written.get("on_photo").is_none(), "{written}"); + assert_eq!(serde_json::to_value(Mask::default()).unwrap()["on_photo"], json!(true)); +} diff --git a/crates/gpu/src/render.rs b/crates/gpu/src/render.rs index 9d6172d9c3..f3794f47a2 100644 --- a/crates/gpu/src/render.rs +++ b/crates/gpu/src/render.rs @@ -997,6 +997,11 @@ fn masks(cx: &mut Cx<'_>, lin: &Buf, prep: &Prep, plan: &Plan<'_>, host: &mut Ho } let comp_plane = cx.gpu.buffer(n); for (mi, m) in list.iter().enumerate() { + // its coordinates on the photo before the warp (`masks::evaluate_one`): through a lens or + // perspective warp the kernels' affine positions don't hold, so its shapes run on the CPU + let mframe = plan.frame.for_mask(m.on_photo); + let frame = &*mframe; + let warped = frame.mask_warp().is_some(); let mut first = true; for comp in &m.components { let op = match comp.op { @@ -1012,6 +1017,7 @@ fn masks(cx: &mut Cx<'_>, lin: &Buf, prep: &Prep, plan: &Plan<'_>, host: &mut Ho let mut aux: Vec = Vec::new(); let mut own: Option = None; let kind = match &comp.shape { + MaskShape::Linear { .. } | MaskShape::Radial { .. } if warped => None, MaskShape::Linear { start, end } => { let (a, b) = (frame.norm_to_long(*start), frame.norm_to_long(*end)); let d = b - a; diff --git a/crates/gpu/tests/mask_geometry.rs b/crates/gpu/tests/mask_geometry.rs new file mode 100644 index 0000000000..fc7d856196 --- /dev/null +++ b/crates/gpu/tests/mask_geometry.rs @@ -0,0 +1,140 @@ +//! On the GPU, masks stay on the image content under every geometry change: +//! a mask drawn around a mark still brightens that mark after Straighten, a crop, a flip, a quarter +//! turn or a lens / perspective warp, rendered fresh or through the view's stage cache. (The CPU +//! side is `lightcraft-pipeline`'s `tests_mask_geometry`.) + +use std::sync::Arc; + +use lightcraft_develop::{BrushStroke, DevelopSettings, LocalAdjustments, Mask, MaskComponent, MaskOp, MaskShape, SegMask}; +use lightcraft_geom::{CropGeometry, Point, Rect}; +use lightcraft_pipeline::{RenderRequest, SourceInfo, StageCache}; +use lightcraft_raster::{Rgb32f, Rgba8}; + +const W: usize = 240; +const H: usize = 160; +const MARK: (f64, f64) = (0.3, 0.32); + +fn gpu() -> bool { + let ok = lightcraft_gpu::available(); + if !ok { + lightcraft_testkit::skip!(Gpu, "no GPU adapter"); + } + ok +} + +fn marked(mark: bool) -> Rgb32f { + let (mx, my) = (MARK.0 * W as f64, MARK.1 * H as f64); + Rgb32f::from_fn(W, H, |x, y| { + let d = ((x as f64 + 0.5 - mx).powi(2) + (y as f64 + 0.5 - my).powi(2)).sqrt(); + if d < 2.5 && mark { [0.9; 3] } else { [0.05; 3] } + }) +} + +fn with_mask(shape: MaskShape) -> DevelopSettings { + let mut s = DevelopSettings::default(); + s.masks.push(Mask { + id: 1, + components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape }], + adjust: LocalAdjustments { exposure: 2.0, ..Default::default() }, + ..Default::default() + }); + s +} + +fn shapes() -> Vec<(&'static str, MaskShape)> { + let l = W.max(H) as f64; + const SIDE: usize = 64; + let logits: Vec = (0..SIDE * SIDE) + .map(|i| { + let (x, y) = ((i % SIDE) as f64 + 0.5, (i / SIDE) as f64 + 0.5); + let (dx, dy) = ((x / SIDE as f64 - MARK.0) * W as f64, (y / SIDE as f64 - MARK.1) * H as f64); + if (dx * dx + dy * dy).sqrt() < 10.0 { 8.0 } else { -8.0 } + }) + .collect(); + vec![ + ("radial", MaskShape::Radial { center: Point::new(MARK.0, MARK.1), rx: 12.0 / l, ry: 12.0 / l, angle: 0.0, feather: 40.0, invert: false }), + ( + "brush", + MaskShape::Brush { + strokes: vec![BrushStroke { + points: vec![Point::new(MARK.0 - 0.005, MARK.1), Point::new(MARK.0 + 0.005, MARK.1)], + size: 8.0 / l, + ..Default::default() + }], + }, + ), + ( + "object", + MaskShape::Object { + hint: vec![Point::new(MARK.0, MARK.1)], + exclude: Vec::new(), + seg: Some(SegMask::from_logits(SIDE, &logits)), + detail: Vec::new(), + edge: 0.0, + }, + ), + ] +} + +fn geometries() -> Vec<(&'static str, Box)> { + vec![ + ("straighten", Box::new(|s: &mut DevelopSettings| s.crop.geometry = CropGeometry { rect: Rect::new(0.08, 0.1, 0.92, 0.9), angle: 7.0 })), + ("crop", Box::new(|s: &mut DevelopSettings| s.crop.geometry.rect = Rect::new(0.1, 0.15, 0.7, 0.8))), + ("flip", Box::new(|s: &mut DevelopSettings| s.crop.flip_h = true)), + ( + "perspective", + Box::new(|s: &mut DevelopSettings| { + s.geometry.vertical = 25.0; + s.geometry.rotate = 4.0; + }), + ), + ("distortion", Box::new(|s: &mut DevelopSettings| s.optics.distortion = 40.0)), + ] +} + +fn luma(img: &Rgba8, i: usize) -> f64 { + let p = img.data[i]; + (p[0] as f64 + p[1] as f64 + p[2] as f64) / 3.0 +} + +fn centroid(w: usize, weights: impl Iterator) -> (f64, f64) { + let (mut sx, mut sy, mut n) = (0.0, 0.0, 0.0); + for (i, v) in weights.enumerate() { + sx += v * ((i % w) as f64 + 0.5); + sy += v * ((i / w) as f64 + 0.5); + n += v; + } + assert!(n > 0.0, "nothing found"); + (sx / n, sy / n) +} + +fn on_gpu(src: &Arc, s: &DevelopSettings, cache: Option<&StageCache>) -> Rgba8 { + let req = RenderRequest::fit(W, H); + lightcraft_gpu::try_render(src, &SourceInfo::default(), s, &req, cache).map(|r| r.image).unwrap_or_else(|e| panic!("GPU render failed: {e:?}")) +} + +#[test] +fn masks_stay_on_the_content_under_every_geometry_change_on_the_gpu() { + if !gpu() { + return; + } + let src = Arc::new(marked(true)); + for (name, shape) in shapes() { + for (what, change) in geometries() { + let mut s = with_mask(shape.clone()); + let cache = StageCache::default(); + on_gpu(&src, &s, Some(&cache)); + change(&mut s); + let unmasked = DevelopSettings { masks: Vec::new(), ..s.clone() }; + let plain = on_gpu(&src, &unmasked, None); + // (where the marked photo differs from an unmarked one: a warp leaves light blank canvas) + let bare = on_gpu(&Arc::new(marked(false)), &unmasked, None); + let mark = centroid(plain.width, (0..plain.data.len()).map(|i| (luma(&plain, i) - luma(&bare, i)).max(0.0))); + for (how, m) in [("fresh", on_gpu(&src, &s, None)), ("cached", on_gpu(&src, &s, Some(&cache)))] { + let at = centroid(m.width, (0..m.data.len()).map(|i| (luma(&m, i) - luma(&plain, i)).max(0.0))); + let d = ((at.0 - mark.0).powi(2) + (at.1 - mark.1).powi(2)).sqrt(); + assert!(d < 2.0, "{name} after {what} ({how}): the mask's effect is at {at:?}, the mark at {mark:?}"); + } + } + } +} diff --git a/crates/pipeline/src/geometry.rs b/crates/pipeline/src/geometry.rs index 641d50216b..14743959cc 100644 --- a/crates/pipeline/src/geometry.rs +++ b/crates/pipeline/src/geometry.rs @@ -33,6 +33,10 @@ pub struct Frame { pub warp: Option, /// Set when this frame draws only a window of a larger output (see [`Frame::window`]). pub view: Option, + /// Mask coordinates are on the photo before the warp ([`Frame::for_mask`], + /// `lightcraft_develop::Mask::on_photo`); otherwise they are normalized coordinates of this + /// frame's transformed image, like spots and the crop. + pub masks_on_photo: bool, } /// Where a windowed [`Frame`]'s pixels sit in the full output: its offset and the full output's @@ -77,6 +81,7 @@ impl Frame { flip_v: s.crop.flip_v, warp: (!warp.is_identity()).then_some(warp), view: None, + masks_on_photo: false, }; if apply_crop && s.geometry.constrain_crop { f.constrain_to_image(); @@ -223,6 +228,57 @@ impl Frame { self.out_to_norm(out_w, out_h).inverse().unwrap_or(Affine::IDENTITY) } + /// This frame for evaluating mask `m`: whether its coordinates are on the photo before the + /// warp ([`lightcraft_develop::Mask::on_photo`]). + pub fn for_mask(&self, on_photo: bool) -> std::borrow::Cow<'_, Frame> { + if self.masks_on_photo == on_photo { + std::borrow::Cow::Borrowed(self) + } else { + std::borrow::Cow::Owned(Frame { masks_on_photo: on_photo, ..self.clone() }) + } + } + + /// The warp mask coordinates go through: the lens / perspective warp when masks are on the + /// photo and the warp moves pixels, else none (mask coordinates are then this frame's + /// normalized coordinates). + pub fn mask_warp(&self) -> Option<&Warp> { + self.warp.as_ref().filter(|w| self.masks_on_photo && w.moves_pixels()) + } + + /// Output pixel → mask coordinates (see [`Frame::masks_on_photo`]). + pub fn out_to_mask(&self, out_w: usize, out_h: usize) -> MaskMap { + MaskMap { + o2t: self.out_to_oriented(out_w, out_h), + to_norm: self.out_to_norm(out_w, out_h), + ow: self.ow, + oh: self.oh, + warp: self.mask_warp().cloned(), + } + } + + /// Mask coordinates → output pixel (the inverse of [`Frame::out_to_mask`]). + pub fn mask_to_out(&self, out_w: usize, out_h: usize) -> impl Fn(Point) -> Point + use<> { + let to_out = self.norm_to_out(out_w, out_h); + let (warp, ow, oh) = (self.mask_warp().cloned(), self.ow, self.oh); + move |n: Point| to_out.apply(mask_to_norm(warp.as_ref(), ow, oh, n)) + } + + /// Normalized coordinates of this frame's transformed image → mask coordinates. + pub fn norm_to_mask(&self, n: Point) -> Point { + match self.mask_warp() { + Some(w) => { + let s = w.to_source(Point::new(n.x * self.ow, n.y * self.oh), 1); + Point::new(s.x / self.ow, s.y / self.oh) + } + None => n, + } + } + + /// Mask coordinates → normalized coordinates of this frame's transformed image. + pub fn mask_to_norm(&self, n: Point) -> Point { + mask_to_norm(self.mask_warp(), self.ow, self.oh, n) + } + /// Convert normalized oriented coordinates to "long-edge units" (isotropic), used by mask shapes. pub fn norm_to_long(&self, p: Point) -> Point { let l = self.ow.max(self.oh); @@ -287,6 +343,40 @@ impl Frame { } } +/// Mask coordinates `n` (on the photo before `warp`, normalized to `ow × oh`) → normalized +/// transformed coordinates. A point the warp can't invert (far outside a strong one) stays put. +fn mask_to_norm(warp: Option<&Warp>, ow: f64, oh: f64, n: Point) -> Point { + match warp.and_then(|w| w.from_source(Point::new(n.x * ow, n.y * oh))) { + Some(t) => Point::new(t.x / ow, t.y / oh), + None => n, + } +} + +/// Output pixel → mask coordinates of a frame ([`Frame::out_to_mask`]). +#[derive(Clone, Debug)] +pub struct MaskMap { + /// Output px → transformed px, and → normalized transformed coordinates. + o2t: Affine, + to_norm: Affine, + ow: f64, + oh: f64, + warp: Option, +} + +impl MaskMap { + /// The mask coordinates at output position `p` (px; pixel centres at `x + 0.5`). + #[inline] + pub fn apply(&self, p: Point) -> Point { + match &self.warp { + Some(w) => { + let s = w.to_source(self.o2t.apply(p), 1); + Point::new(s.x / self.ow, s.y / self.oh) + } + None => self.to_norm.apply(p), + } + } +} + /// How a render resamples its source (see [`Frame::sample_plan`]). #[derive(Clone, Debug)] pub struct SamplePlan { diff --git a/crates/pipeline/src/lib.rs b/crates/pipeline/src/lib.rs index d56e8fe82d..365f8445da 100644 --- a/crates/pipeline/src/lib.rs +++ b/crates/pipeline/src/lib.rs @@ -34,6 +34,7 @@ pub mod geometry; pub mod local; pub mod local_tone; pub mod lut; +pub mod mask_space; pub mod masks; pub mod optics; pub mod output; @@ -754,6 +755,8 @@ mod tests_hdr; #[cfg(test)] mod tests_local; #[cfg(test)] +mod tests_mask_geometry; +#[cfg(test)] mod tests_nr_scale; #[cfg(test)] mod tests_tone; diff --git a/crates/pipeline/src/mask_space.rs b/crates/pipeline/src/mask_space.rs new file mode 100644 index 0000000000..d2e15e1205 --- /dev/null +++ b/crates/pipeline/src/mask_space.rs @@ -0,0 +1,274 @@ +//! Moving a mask's coordinates between frames: [`attach`] puts an old mask's coordinates (on the +//! corrected frame) on the photo before the warp ([`lightcraft_develop::Mask::on_photo`]), and +//! [`turn`] follows a quarter turn or flip of the photo (Rotate Left / Right). Gradients, brush +//! strokes and clicks move point by point; stored AI segmentations are resampled. + +use lightcraft_develop::{Mask, MaskShape, SegMask}; +use lightcraft_geom::{Orientation, Point}; + +use crate::geometry::Frame; +use crate::masks::SegGrid; + +/// Put mask `m`, drawn on the corrected (transformed) image of `frame`, on the photo before the +/// warp: it then renders where it did under `frame` and stays on that content whatever lens +/// corrections, Upright or Transform do later. A mask already on the photo is left alone. +pub fn attach(m: &mut Mask, frame: &Frame) { + if m.on_photo { + return; + } + m.on_photo = true; + let f = frame.for_mask(true); + if f.mask_warp().is_none() { + // without a warp both are the same coordinates + return; + } + let fwd = |p: Point| f.norm_to_mask(p); + let back = |p: Point| f.mask_to_norm(p); + remap(m, &fwd, &back, 0.0); +} + +/// Follow `turn` (the change of the user orientation, e.g. a quarter turn clockwise): mask +/// coordinates are on the oriented photo, so they turn with it. Radial angles turn by the same +/// quarter turns; sizes (long-edge units) don't change. +pub fn turn(m: &mut Mask, turn: Orientation) { + if turn == Orientation::Normal { + return; + } + let fwd = |p: Point| { + let (x, y) = turn.map(p.x, p.y, 1.0, 1.0); + Point::new(x, y) + }; + let inv = turn.inverse(); + let back = |p: Point| { + let (x, y) = inv.map(p.x, p.y, 1.0, 1.0); + Point::new(x, y) + }; + let (flip, quarters) = turn.to_parts(); + // a radial's angle is clockwise on screen: a quarter turn clockwise adds 90°; a mirror negates it first + let delta = 90.0 * quarters as f64; + remap_turn(m, &fwd, &back, flip, delta); +} + +fn remap_turn(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point, flip: bool, delta: f64) { + if flip { + for c in &mut m.components { + if let MaskShape::Radial { angle, .. } = &mut c.shape { + *angle = -*angle; + } + } + } + remap(m, fwd, back, delta); +} + +/// Move every place `m` stores: `fwd` maps an old point to the new frame, `back` a new point to +/// the old one (for resampling segmentations); radial angles change by `delta` degrees. +fn remap(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point, delta: f64) { + for c in &mut m.components { + match &mut c.shape { + MaskShape::Linear { start, end } => { + *start = fwd(*start); + *end = fwd(*end); + } + MaskShape::Radial { center, angle, .. } => { + *center = fwd(*center); + if delta != 0.0 { + // (kept within −180..180°) + *angle = (*angle + delta + 180.0).rem_euclid(360.0) - 180.0; + } + } + MaskShape::Brush { strokes } => { + for s in strokes { + s.points.iter_mut().for_each(|p| *p = fwd(*p)); + } + } + MaskShape::Object { hint, exclude, seg, detail, .. } => { + hint.iter_mut().chain(exclude.iter_mut()).for_each(|p| *p = fwd(*p)); + remap_seg(seg, fwd, back); + detail.iter_mut().for_each(|d| *d = moved(d, fwd, back).unwrap_or_else(|| d.clone())); + } + MaskShape::Prompt { seg, detail, .. } => { + remap_seg(seg, fwd, back); + detail.iter_mut().for_each(|d| *d = moved(d, fwd, back).unwrap_or_else(|| d.clone())); + } + // computed from the photo at each render (its mattes follow the warp), or colours, + // tones and depths, which have no place (a colour range's samples are colours) + MaskShape::Subject + | MaskShape::Sky + | MaskShape::Background + | MaskShape::People { .. } + | MaskShape::Landscape { .. } + | MaskShape::ColorRange { .. } + | MaskShape::LuminanceRange { .. } + | MaskShape::DepthRange { .. } => {} + } + } +} + +fn remap_seg(seg: &mut Option, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point) { + if let Some(s) = seg.as_ref() + && let Some(m) = moved(s, fwd, back) + { + *seg = Some(m); + } +} + +/// The box around `r` mapped by `f` (its corners and edge midpoints), inside 0..1. +fn map_rect(r: [f64; 4], f: &dyn Fn(Point) -> Point) -> [f64; 4] { + let (mx, my) = ((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0); + let pts = + [(r[0], r[1]), (r[2], r[1]), (r[0], r[3]), (r[2], r[3]), (mx, r[1]), (mx, r[3]), (r[0], my), (r[2], my)].map(|(x, y)| f(Point::new(x, y))); + let finite = pts.iter().filter(|p| p.x.is_finite() && p.y.is_finite()); + let (mut x0, mut y0, mut x1, mut y1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN); + for p in finite { + (x0, y0, x1, y1) = (x0.min(p.x), y0.min(p.y), x1.max(p.x), y1.max(p.y)); + } + if x0 > x1 || y0 > y1 { + return r; + } + [x0.clamp(0.0, 1.0), y0.clamp(0.0, 1.0), x1.clamp(0.0, 1.0), y1.clamp(0.0, 1.0)] +} + +/// Segmentation `s` resampled onto the new frame: a grid of the same side over the mapped box +/// (the whole image for a whole-image one), each cell taking the old logit at its centre mapped +/// back. `None` when the data is damaged (it is kept as it is). +fn moved(s: &SegMask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point) -> Option { + let old = SegGrid::new(s)?; + let side = s.side as usize; + let rect = match s.rect { + None => None, + Some(r) => Some(map_rect(r, fwd)).filter(|b| b[2] > b[0] && b[3] > b[1]), + }; + let b = rect.unwrap_or([0.0, 0.0, 1.0, 1.0]); + let logits: Vec = (0..side * side) + .map(|i| { + let (x, y) = ((i % side) as f64 + 0.5, (i / side) as f64 + 0.5); + let n = Point::new(b[0] + x / side as f64 * (b[2] - b[0]), b[1] + y / side as f64 * (b[3] - b[1])); + old.logit(back(n)) + }) + .collect(); + Some(match rect { + Some(r) => SegMask::from_logits_in(side, &logits, r), + None => SegMask::from_logits(side, &logits), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use lightcraft_develop::{BrushStroke, DevelopSettings, MaskComponent, MaskOp}; + + fn comp(shape: MaskShape) -> MaskComponent { + MaskComponent { name: None, op: MaskOp::Add, invert: false, shape } + } + + fn mask(shape: MaskShape) -> Mask { + Mask { components: vec![comp(shape)], ..Default::default() } + } + + fn seg_at(p: Point) -> SegMask { + const SIDE: usize = 32; + let l: Vec = (0..SIDE * SIDE) + .map(|i| { + let (x, y) = (((i % SIDE) as f64 + 0.5) / SIDE as f64, ((i / SIDE) as f64 + 0.5) / SIDE as f64); + if (x - p.x).hypot(y - p.y) < 0.08 { 8.0 } else { -8.0 } + }) + .collect(); + SegMask::from_logits(SIDE, &l) + } + + fn object_at(p: Point) -> MaskShape { + MaskShape::Object { hint: vec![p], exclude: Vec::new(), seg: Some(seg_at(p)), detail: Vec::new(), edge: 0.0 } + } + + /// The centre of what `s` selects (sampled on a fine grid). + fn centre_of(s: &SegMask) -> Point { + let g = SegGrid::new(s).unwrap(); + let (mut sx, mut sy, mut n) = (0.0, 0.0, 0.0); + for i in 0..200 * 200 { + let p = Point::new(((i % 200) as f64 + 0.5) / 200.0, ((i / 200) as f64 + 0.5) / 200.0); + if g.contains(p) && g.logit(p) > 0.0 { + (sx, sy, n) = (sx + p.x, sy + p.y, n + 1.0); + } + } + assert!(n > 0.0, "nothing selected"); + Point::new(sx / n, sy / n) + } + + fn seg_of(m: &Mask, i: usize) -> &SegMask { + match &m.components[i].shape { + MaskShape::Object { seg: Some(s), .. } => s, + other => panic!("{other:?}"), + } + } + + #[test] + fn a_quarter_turn_turns_every_place_a_mask_keeps() { + let p = Point::new(0.2, 0.3); + let mut m = mask(MaskShape::Radial { center: p, rx: 0.1, ry: 0.05, angle: 10.0, feather: 50.0, invert: false }); + m.components.push(comp(MaskShape::Brush { strokes: vec![BrushStroke { points: vec![p], ..Default::default() }] })); + m.components.push(comp(object_at(p))); + turn(&mut m, Orientation::Rotate90); + // a quarter turn clockwise: (x, y) → (1 − y, x) + let want = Point::new(0.7, 0.2); + let MaskShape::Radial { center, angle, .. } = &m.components[0].shape else { panic!() }; + assert!((center.x - want.x).abs() < 1e-9 && (center.y - want.y).abs() < 1e-9, "{center:?}"); + assert_eq!(*angle, 100.0); + let MaskShape::Brush { strokes } = &m.components[1].shape else { panic!() }; + assert!((strokes[0].points[0].x - want.x).abs() < 1e-9); + let MaskShape::Object { hint, .. } = &m.components[2].shape else { panic!() }; + assert!((hint[0].x - want.x).abs() < 1e-9 && (hint[0].y - want.y).abs() < 1e-9, "{hint:?}"); + let c = centre_of(seg_of(&m, 2)); + assert!((c.x - want.x).abs() < 0.04 && (c.y - want.y).abs() < 0.04, "{c:?}"); + // and back + turn(&mut m, Orientation::Rotate270); + let MaskShape::Radial { center, angle, .. } = &m.components[0].shape else { panic!() }; + assert!((center.x - p.x).abs() < 1e-9 && (center.y - p.y).abs() < 1e-9 && *angle == 10.0); + } + + #[test] + fn attaching_an_old_mask_keeps_where_it_renders() { + let mut s = DevelopSettings::default(); + s.geometry.vertical = 30.0; + s.geometry.rotate = 5.0; + let frame = Frame::new(300, 200, &s, false); + let p = Point::new(0.3, 0.4); + let mut m = mask(MaskShape::Linear { start: p, end: Point::new(0.6, 0.4) }); + m.components.push(comp(object_at(p))); + m.on_photo = false; + attach(&mut m, &frame); + assert!(m.on_photo); + let on = frame.for_mask(true); + let MaskShape::Linear { start, .. } = &m.components[0].shape else { panic!() }; + let back = on.mask_to_norm(*start); + assert!((back.x - p.x).abs() < 1e-6 && (back.y - p.y).abs() < 1e-6, "{back:?}"); + assert!((start.x - p.x).hypot(start.y - p.y) > 0.005, "the warp moved it on the photo: {start:?}"); + let c = on.mask_to_norm(centre_of(seg_of(&m, 1))); + assert!((c.x - p.x).abs() < 0.04 && (c.y - p.y).abs() < 0.04, "{c:?}"); + // attaching again changes nothing + let before = m.clone(); + attach(&mut m, &frame); + assert_eq!(m, before); + } + + #[test] + fn damaged_or_hostile_masks_move_without_a_panic() { + let mut m = + mask(MaskShape::Radial { center: Point::new(f64::NAN, f64::INFINITY), rx: 0.1, ry: 0.1, angle: f64::NAN, feather: 0.0, invert: false }); + m.components.push(comp(MaskShape::Object { + hint: vec![Point::new(1e300, -1e300)], + exclude: Vec::new(), + seg: Some(SegMask { side: 1024, data: "not base64".into(), rect: Some([f64::NAN, 0.0, 2.0, -1.0]) }), + detail: vec![SegMask { side: 0, data: String::new(), rect: None }], + edge: 0.0, + })); + m.on_photo = false; + let mut s = DevelopSettings::default(); + s.geometry.vertical = 80.0; + s.optics.distortion = 100.0; + attach(&mut m, &Frame::new(300, 200, &s, false)); + for o in [Orientation::Rotate90, Orientation::FlipH, Orientation::Rotate180] { + turn(&mut m, o); + } + assert!(m.on_photo); + } +} diff --git a/crates/pipeline/src/masks.rs b/crates/pipeline/src/masks.rs index ca182ea81d..85c920bc9d 100644 --- a/crates/pipeline/src/masks.rs +++ b/crates/pipeline/src/masks.rs @@ -152,6 +152,8 @@ pub fn evaluate(masks: &[Mask], frame: &Frame, w: usize, h: usize, img: &Rgb32f, /// scaled by its amount. #[allow(clippy::too_many_arguments)] pub fn evaluate_one(m: &Mask, frame: &Frame, w: usize, h: usize, img: &Rgb32f, log_l: &Plane, ev: f32, mattes: Option<&Mattes>) -> Plane { + // its coordinates on the photo before the warp, or (an old mask) on the corrected frame + let frame = &*frame.for_mask(m.on_photo); let mut alpha = Plane::new(w, h); let mut first = true; for comp in &m.components { @@ -195,7 +197,7 @@ pub fn evaluate_one(m: &Mask, frame: &Frame, w: usize, h: usize, img: &Rgb32f, l /// Per-pixel positions in long-edge units for output pixel centres. fn for_each_pos(frame: &Frame, w: usize, h: usize, out: &mut Plane, f: impl Fn(Point, usize) -> f32 + Sync + Send) { - let m = frame.out_to_norm(w, h); + let m = frame.out_to_mask(w, h); let (ow, oh, l) = (frame.ow, frame.oh, frame.ow.max(frame.oh)); for_rows(&mut out.data, w, |y, row| { for (x, v) in row.iter_mut().enumerate() { @@ -266,7 +268,7 @@ pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &R MaskShape::Sky => { // Classical sky heuristic until the segmenter lands (M12): bright, smooth, blue-ish or // unsaturated, and connected to the top of the frame. - let m = frame.out_to_norm(w, h); + let m = frame.out_to_mask(w, h); for (i, v) in out.data.iter_mut().enumerate() { let (x, y) = (i % w, i / w); let n = m.apply(Point::new(x as f64 + 0.5, y as f64 + 0.5)); @@ -293,7 +295,7 @@ pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &R MaskShape::Prompt { .. } => {} MaskShape::Subject | MaskShape::Object { .. } | MaskShape::People { .. } => { // Saliency heuristic: centre-weighted local contrast (replaced by the segmenter in M12). - let m = frame.out_to_norm(w, h); + let m = frame.out_to_mask(w, h); let blur = lightcraft_raster::blur::gaussian(log_l, 0.03 * frame_px(frame, w)); for (i, v) in out.data.iter_mut().enumerate() { let (x, y) = (i % w, i / w); @@ -315,24 +317,24 @@ pub fn shape_alpha(shape: &MaskShape, frame: &Frame, w: usize, h: usize, img: &R } /// A decoded [`SegMask`]: its logits and the normalized rectangle they cover. -struct SegGrid { +pub struct SegGrid { logits: Vec, side: usize, r: [f64; 4], } impl SegGrid { - fn new(seg: &SegMask) -> Option { + pub fn new(seg: &SegMask) -> Option { Some(SegGrid { logits: seg.logits()?, side: seg.side as usize, r: seg.bounds()? }) } - fn contains(&self, n: Point) -> bool { + pub fn contains(&self, n: Point) -> bool { n.x >= self.r[0] && n.x <= self.r[2] && n.y >= self.r[1] && n.y <= self.r[3] } /// The bilinearly sampled logit at normalized image point `n` (cell centres at /// `(i + 0.5) / side` of the rectangle). - fn logit(&self, n: Point) -> f32 { + pub fn logit(&self, n: Point) -> f32 { let side = self.side; let at = |x: usize, y: usize| self.logits.get(y.min(side - 1) * side + x.min(side - 1)).copied().unwrap_or(-16.0); let u = (n.x - self.r[0]) / (self.r[2] - self.r[0]); @@ -385,7 +387,7 @@ fn sample_seg(seg: Option<&SegMask>, detail: &[SegMask], gain: f32, frame: &Fram if coarse.is_none() && patches.is_empty() { return; } - let m = frame.out_to_norm(w, h); + let m = frame.out_to_mask(w, h); for_rows(&mut out.data, w, |y, row| { for (x, v) in row.iter_mut().enumerate() { let n = m.apply(Point::new(x as f64 + 0.5, y as f64 + 0.5)); @@ -571,7 +573,7 @@ pub fn brush_dabs(s: &BrushStroke, frame: &Frame, w: usize, h: usize) -> BrushDa /// Ask for one more than the threshold to tell "at it" from "past it": a result shorter than /// `max` is the whole stroke. pub fn brush_dabs_within(s: &BrushStroke, frame: &Frame, w: usize, h: usize, max: usize) -> BrushDabs { - let to_out = frame.norm_to_out(w, h); + let to_out = frame.mask_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)); @@ -588,7 +590,7 @@ pub fn brush_dabs_within(s: &BrushStroke, frame: &Frame, w: usize, h: usize, max if emitted >= max { break; } - let q = to_out.apply(*p); + let q = to_out(*p); let before = dabs.len(); if push_dabs_within(&mut dabs, &mut emitted, prev, q, r, max) { let pq = s.pressure_at(i); diff --git a/crates/pipeline/src/optics.rs b/crates/pipeline/src/optics.rs index b0d7cc7f91..11789b8b55 100644 --- a/crates/pipeline/src/optics.rs +++ b/crates/pipeline/src/optics.rs @@ -201,6 +201,39 @@ impl Warp { self.corrected_to_source(self.to_corrected(p), ch) } + /// Source px → transformed px (green plane): the inverse of [`Warp::to_source`], by Newton's + /// method on it (the warps are smooth and close to the identity, so a few steps reach a + /// hundredth of a pixel). `None` when it doesn't converge (a point far outside a strong warp). + pub fn from_source(&self, s: Point) -> Option { + if !(s.x.is_finite() && s.y.is_finite()) { + return None; + } + let f = |p: Point| self.to_source(p, 1); + // start from where the perspective alone puts it (the lens part moves pixels little) + let mut t = self.from_corrected(s); + let h = 1e-3 * self.w.max(self.h).max(1.0); + for _ in 0..24 { + let v = f(t); + let (ex, ey) = (v.x - s.x, v.y - s.y); + if !(ex.is_finite() && ey.is_finite()) { + return None; + } + if ex.abs() < 1e-4 && ey.abs() < 1e-4 { + return Some(t); + } + let (dx, dy) = (f(Point::new(t.x + h, t.y)), f(Point::new(t.x, t.y + h))); + let (a, c) = ((dx.x - v.x) / h, (dx.y - v.y) / h); + let (b, d) = ((dy.x - v.x) / h, (dy.y - v.y) / h); + let det = a * d - b * c; + if !det.is_finite() || det.abs() < 1e-12 { + return None; + } + t = Point::new(t.x - (d * ex - b * ey) / det, t.y - (a * ey - c * ex) / det); + } + let v = f(t); + ((v.x - s.x).abs() < 1e-2 && (v.y - s.y).abs() < 1e-2).then_some(t) + } + /// Vignetting compensation gain at source position `src` (px). pub fn gain(&self, src: Point) -> f32 { let mut g = 1.0; diff --git a/crates/pipeline/src/spots.rs b/crates/pipeline/src/spots.rs index a875ffb7fc..0bd52f2c11 100644 --- a/crates/pipeline/src/spots.rs +++ b/crates/pipeline/src/spots.rs @@ -161,17 +161,18 @@ pub fn window_for_reads_checked( win: crate::PixelWindow, ) -> Option { let spots = &s.spots; - let autos: Vec<&lightcraft_develop::BrushStroke> = s + // (with whether its mask's coordinates are on the photo before the warp) + let autos: Vec<(bool, &lightcraft_develop::BrushStroke)> = s .masks .iter() .filter(|m| m.visible) - .flat_map(|m| &m.components) - .filter_map(|c| match &c.shape { - lightcraft_develop::MaskShape::Brush { strokes } => Some(strokes), + .flat_map(|m| m.components.iter().map(move |c| (m.on_photo, c))) + .filter_map(|(on, c)| match &c.shape { + lightcraft_develop::MaskShape::Brush { strokes } => Some(strokes.iter().map(move |st| (on, st))), _ => None, }) .flatten() - .filter(|st| st.auto_mask) + .filter(|(_, st)| st.auto_mask) .collect(); if spots.is_empty() && autos.is_empty() { return Some(win); @@ -205,10 +206,10 @@ pub fn window_for_reads_checked( // Auto Mask strokes weigh each dab by its similarity to the pixel under the dab's centre and // refine the stroke over a couple of radii: each dab matters to pixels within 2.5 radii let mut dab_boxes: Vec<[f64; 4]> = Vec::new(); - for st in autos { + for (on_photo, st) in autos { // One more than the window takes, so a stroke past it is still recognised as past it // without the other 1.8 million dabs being built first (#1520). - let dabs = crate::masks::brush_dabs_within(st, frame, full_w, full_h, MAX_DABS_CONSIDERED + 1); + let dabs = crate::masks::brush_dabs_within(st, &frame.for_mask(on_photo), full_w, full_h, MAX_DABS_CONSIDERED + 1); let r = dabs.r; if dabs.dabs.len() > MAX_DABS_CONSIDERED { continue; diff --git a/crates/pipeline/src/tests_mask_geometry.rs b/crates/pipeline/src/tests_mask_geometry.rs new file mode 100644 index 0000000000..177d9c98bd --- /dev/null +++ b/crates/pipeline/src/tests_mask_geometry.rs @@ -0,0 +1,182 @@ +//! Masks stay on the image content under every geometry change: a mask +//! drawn around a mark in the photo still brightens that mark after Straighten, a crop, a flip or +//! a lens / perspective warp, rendered fresh or through a view's stage cache. + +use std::sync::Arc; + +use lightcraft_develop::{BrushStroke, DevelopSettings, LocalAdjustments, Mask, MaskComponent, MaskOp, MaskShape, SegMask}; +use lightcraft_geom::{CropGeometry, Point, Rect}; +use lightcraft_raster::{Rgb32f, Rgba8}; + +use crate::{RenderRequest, SourceInfo, StageCache, render, render_cached}; + +const W: usize = 240; +const H: usize = 160; +/// Where the mark sits, normalized. +const MARK: (f64, f64) = (0.3, 0.32); + +/// Mid grey with a small bright mark at [`MARK`]. +fn marked(mark: bool) -> Rgb32f { + let (mx, my) = (MARK.0 * W as f64, MARK.1 * H as f64); + Rgb32f::from_fn(W, H, |x, y| { + let d = ((x as f64 + 0.5 - mx).powi(2) + (y as f64 + 0.5 - my).powi(2)).sqrt(); + if d < 2.5 && mark { [0.9; 3] } else { [0.05; 3] } + }) +} + +fn with_mask(shape: MaskShape) -> DevelopSettings { + let mut s = DevelopSettings::default(); + s.masks.push(Mask { + id: 1, + components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape }], + adjust: LocalAdjustments { exposure: 2.0, ..Default::default() }, + ..Default::default() + }); + s +} + +fn radial() -> MaskShape { + let l = W.max(H) as f64; + // (radii in long-edge units) + MaskShape::Radial { center: Point::new(MARK.0, MARK.1), rx: 12.0 / l, ry: 12.0 / l, angle: 0.0, feather: 40.0, invert: false } +} + +/// An AI object selection (a stored segmentation) covering the mark. +fn subject() -> MaskShape { + const SIDE: usize = 64; + let logits: Vec = (0..SIDE * SIDE) + .map(|i| { + let (x, y) = ((i % SIDE) as f64 + 0.5, (i / SIDE) as f64 + 0.5); + let (dx, dy) = ((x / SIDE as f64 - MARK.0) * W as f64, (y / SIDE as f64 - MARK.1) * H as f64); + if (dx * dx + dy * dy).sqrt() < 10.0 { 8.0 } else { -8.0 } + }) + .collect(); + MaskShape::Object { + hint: vec![Point::new(MARK.0, MARK.1)], + exclude: Vec::new(), + seg: Some(SegMask::from_logits(SIDE, &logits)), + detail: Vec::new(), + edge: 0.0, + } +} + +fn luma(img: &Rgba8, i: usize) -> f64 { + let p = img.data[i]; + (p[0] as f64 + p[1] as f64 + p[2] as f64) / 3.0 +} + +/// The mark's centre in a render with `s` (where the marked photo differs from an unmarked one: +/// a warp leaves light blank canvas around the photo). +fn mark_at(s: &DevelopSettings) -> (f64, f64) { + let req = RenderRequest::fit(W, H); + let info = SourceInfo::default(); + let (a, b) = (render(&marked(true), &info, s, &req).image, render(&marked(false), &info, s, &req).image); + centroid(a.width, (0..a.data.len()).map(|i| (luma(&a, i) - luma(&b, i)).max(0.0))) +} + +fn centroid(w: usize, weights: impl Iterator) -> (f64, f64) { + let (mut sx, mut sy, mut n) = (0.0, 0.0, 0.0); + for (i, v) in weights.enumerate() { + sx += v * ((i % w) as f64 + 0.5); + sy += v * ((i / w) as f64 + 0.5); + n += v; + } + assert!(n > 0.0, "nothing found"); + (sx / n, sy / n) +} + +/// Where the mask brightened the render (`plain` is the same render without the mask). +fn mask_at(masked: &Rgba8, plain: &Rgba8) -> (f64, f64) { + centroid(masked.width, (0..masked.data.len()).map(|i| (luma(masked, i) - luma(plain, i)).max(0.0))) +} + +/// The geometry changes a user makes after drawing a mask. +pub(crate) fn geometries() -> Vec<(&'static str, Box)> { + vec![ + ("straighten", Box::new(|s: &mut DevelopSettings| s.crop.geometry = CropGeometry { rect: Rect::new(0.08, 0.1, 0.92, 0.9), angle: 7.0 })), + ( + "straighten the other way", + Box::new(|s: &mut DevelopSettings| s.crop.geometry = CropGeometry { rect: Rect::new(0.08, 0.1, 0.92, 0.9), angle: -9.0 }), + ), + ("crop", Box::new(|s: &mut DevelopSettings| s.crop.geometry.rect = Rect::new(0.1, 0.15, 0.7, 0.8))), + ("flip", Box::new(|s: &mut DevelopSettings| s.crop.flip_h = true)), + ("flip vertical", Box::new(|s: &mut DevelopSettings| s.crop.flip_v = true)), + ( + "perspective", + Box::new(|s: &mut DevelopSettings| { + s.geometry.vertical = 25.0; + s.geometry.rotate = 4.0; + }), + ), + ("distortion", Box::new(|s: &mut DevelopSettings| s.optics.distortion = 40.0)), + ] +} + +fn check(name: &str, shape: MaskShape) { + let src = Arc::new(marked(true)); + let req = RenderRequest::fit(W, H); + let info = SourceInfo::default(); + for (what, change) in geometries() { + let mut s = with_mask(shape.clone()); + // a view that rendered the photo before the change (its stage cache holds the mask) + let cache = StageCache::default(); + render_cached(&src, &info, &s, &req, &cache); + change(&mut s); + let plain = DevelopSettings { masks: Vec::new(), ..s.clone() }; + let p = render(&src, &info, &plain, &req).image; + let mark = mark_at(&plain); + for (how, m) in [("fresh", render(&src, &info, &s, &req).image), ("cached", render_cached(&src, &info, &s, &req, &cache).image)] { + let at = mask_at(&m, &p); + let d = ((at.0 - mark.0).powi(2) + (at.1 - mark.1).powi(2)).sqrt(); + assert!(d < 2.0, "{name} after {what} ({how}): the mask's effect is at {at:?}, the mark at {mark:?}"); + } + } +} + +#[test] +fn a_drawn_mask_stays_on_the_content_under_every_geometry_change() { + check("radial", radial()); +} + +fn brush() -> MaskShape { + let l = W.max(H) as f64; + MaskShape::Brush { + strokes: vec![BrushStroke { + points: vec![Point::new(MARK.0 - 0.005, MARK.1), Point::new(MARK.0 + 0.005, MARK.1)], + size: 8.0 / l, + ..Default::default() + }], + } +} + +#[test] +fn a_brushed_mask_stays_on_the_content_under_every_geometry_change() { + check("brush", brush()); +} + +/// A mask from before masks were on the photo keeps its place on the corrected frame: an old edit +/// renders as it did. +#[test] +fn an_old_mask_stays_where_it_was_drawn_on_the_corrected_frame() { + let src = Arc::new(marked(true)); + let req = RenderRequest::fit(W, H); + let info = SourceInfo::default(); + let mut s = with_mask(radial()); + s.masks[0].on_photo = false; + s.geometry.vertical = 25.0; + s.geometry.rotate = 6.0; + let unmasked = DevelopSettings { masks: Vec::new(), ..s.clone() }; + let plain = render(&src, &info, &unmasked, &req).image; + let at = mask_at(&render(&src, &info, &s, &req).image, &plain); + let frame = crate::geometry::Frame::new(W, H, &s, true); + let (w, h) = frame.fit(W, H); + let want = frame.norm_to_out(w, h).apply(Point::new(MARK.0, MARK.1)); + assert!((at.0 - want.x).hypot(at.1 - want.y) < 2.0, "{at:?} vs {want:?}"); + let mark = mark_at(&unmasked); + assert!((at.0 - mark.0).hypot(at.1 - mark.1) > 3.0, "and not on the mark, which the warp moved: {at:?} vs {mark:?}"); +} + +#[test] +fn an_ai_mask_stays_on_the_content_under_every_geometry_change() { + check("object", subject()); +} diff --git a/crates/ui-egui/src/panels/detail/geometry.rs b/crates/ui-egui/src/panels/detail/geometry.rs index d679010f17..2129ab993d 100644 --- a/crates/ui-egui/src/panels/detail/geometry.rs +++ b/crates/ui-egui/src/panels/detail/geometry.rs @@ -3,12 +3,17 @@ use super::info::label_badge_rect; use super::{Affine, Frame, LightcraftApp, PhotoId, Point, Pos2, Rect, Zoom, json, pos2, vec2}; /// Screen ↔ normalized-image mapping for the displayed frame. -#[derive(Clone, Copy)] +#[derive(Clone)] pub struct CanvasMap { pub(super) rect: Rect, to_norm: Affine, from_norm: Affine, k: f32, + /// The frame's lens / perspective warp (with the oriented size it works in), when it moves + /// pixels: a mask on the photo (`Mask::on_photo`) has its coordinates before it. + warp: Option>, + /// Whether `norm` / `screen` speak a mask's coordinates on the photo ([`CanvasMap::for_mask`]). + on_photo: bool, } impl CanvasMap { @@ -16,12 +21,33 @@ impl CanvasMap { let k = 8.0f32; let (w, h) = ((rect.width() * k).round().max(1.0) as usize, (rect.height() * k).round().max(1.0) as usize); let to_norm = frame.out_to_norm(w, h); - CanvasMap { rect, to_norm, from_norm: to_norm.inverse().unwrap_or(Affine::IDENTITY), k } + let warp = frame.warp.as_ref().filter(|w| w.moves_pixels()).map(|w| std::sync::Arc::new((w.clone(), frame.ow, frame.oh))); + CanvasMap { rect, to_norm, from_norm: to_norm.inverse().unwrap_or(Affine::IDENTITY), k, warp, on_photo: false } + } + /// The mapping for a mask's coordinates: on the photo before lens corrections, Upright and + /// Transform (`on_photo`, every mask made now), or an old mask's on the corrected frame. + pub(crate) fn for_mask(&self, on_photo: bool) -> CanvasMap { + CanvasMap { on_photo, ..self.clone() } + } + /// [`Self::for_mask`] for the mask `id` of `d` (none, or a new mask: on the photo). + pub(crate) fn for_mask_id(&self, d: &lightcraft_develop::DevelopSettings, id: Option) -> CanvasMap { + self.for_mask(id.and_then(|id| d.masks.iter().find(|m| m.id == id)).is_none_or(|m| m.on_photo)) } pub fn norm(&self, p: Pos2) -> Point { - self.to_norm.apply(Point::new(((p.x - self.rect.left()) * self.k) as f64, ((p.y - self.rect.top()) * self.k) as f64)) + let n = self.to_norm.apply(Point::new(((p.x - self.rect.left()) * self.k) as f64, ((p.y - self.rect.top()) * self.k) as f64)); + match self.warp.as_deref().filter(|_| self.on_photo) { + Some((w, ow, oh)) => { + let s = w.to_source(Point::new(n.x * ow, n.y * oh), 1); + Point::new(s.x / ow, s.y / oh) + } + None => n, + } } pub fn screen(&self, n: Point) -> Pos2 { + let n = match self.warp.as_deref().filter(|_| self.on_photo) { + Some((w, ow, oh)) => w.from_source(Point::new(n.x * ow, n.y * oh)).map_or(n, |t| Point::new(t.x / ow, t.y / oh)), + None => n, + }; let q = self.from_norm.apply(n); pos2(self.rect.left() + q.x as f32 / self.k, self.rect.top() + q.y as f32 / self.k) } @@ -383,3 +409,37 @@ mod snap_tests { assert_eq!((bad.width(), bad.height()), (1088.0, 720.0)); } } + +#[cfg(test)] +mod mask_map_tests { + use super::*; + use lightcraft_develop::DevelopSettings; + + fn close(a: Point, b: Point) -> bool { + (a.x - b.x).abs() < 1e-4 && (a.y - b.y).abs() < 1e-4 + } + + /// Under a perspective warp, a mask's on-screen place goes into its coordinates on the photo + /// (`Mask::on_photo`) and back: the point clicked is the point the render puts the mask on, and + /// its handle draws where it was clicked. + #[test] + fn a_mask_drawn_on_a_warped_photo_lands_on_the_content_under_the_pointer() { + let mut d = DevelopSettings::default(); + d.geometry.vertical = 30.0; + d.geometry.rotate = 5.0; + let frame = Frame::new(400, 300, &d, true); + let m = CanvasMap::new(&frame, Rect::from_min_size(pos2(100.0, 50.0), vec2(400.0, 300.0))); + let q = pos2(220.0, 140.0); + let on = m.for_mask(true).norm(q); + let plain = m.for_mask(false).norm(q); + assert!(!close(on, plain), "the warp moves the photo under the frame: {on:?} {plain:?}"); + // the render maps the mask's coordinates through the warp to the same frame point + assert!(close(frame.for_mask(true).mask_to_norm(on), plain)); + assert!((m.for_mask(true).screen(on) - q).length() < 0.01, "and its handle draws there"); + // a new mask (no id) and a mask of today are on the photo; an old one isn't + let mut d = d.clone(); + d.masks.push(lightcraft_develop::Mask { id: 7, on_photo: false, ..Default::default() }); + assert!(close(m.for_mask_id(&d, None).norm(q), on)); + assert!(close(m.for_mask_id(&d, Some(7)).norm(q), plain)); + } +} diff --git a/crates/ui-egui/src/panels/detail/masks.rs b/crates/ui-egui/src/panels/detail/masks.rs index d8e789b7a0..1cd0c589db 100644 --- a/crates/ui-egui/src/panels/detail/masks.rs +++ b/crates/ui-egui/src/panels/detail/masks.rs @@ -324,6 +324,8 @@ pub(super) fn mask_overlay(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &eg } for m in &d.masks { let sel = Some(m.id) == active; + // (its coordinates on the photo, or an old mask's on the corrected frame) + let map = &map.for_mask(m.on_photo); // Ctrl+Shift+O (`mask_overlay_all`) shows every visible mask's region; outline each one // too, not just the selected mask. Handles and the rotation grip stay on the selected // mask so nothing else becomes draggable. The component check keeps this in step with the @@ -439,6 +441,7 @@ fn component_outline( fn object_marks(app: &LightcraftApp, p: &egui::Painter, map: &CanvasMap, d: &DevelopSettings, active: Option) { let pending = app.session.segmenter.pending_clicks().cloned(); for m in d.masks.iter().filter(|m| Some(m.id) == active) { + let map = &map.for_mask(m.on_photo); for (k, c) in m.components.iter().enumerate() { if let MaskShape::Object { hint, exclude, .. } = &c.shape { let (hint, exclude) = match pending.as_ref().filter(|q| (q.mask, q.comp) == (m.id, k)) { @@ -503,7 +506,8 @@ fn object_tool( if own_pin_at(grips, app.session.active_mask, q) { return; } - let n = map.norm(q); + // (a click goes into the selected mask, or a new one: in its coordinates) + let n = map.for_mask_id(d, app.session.active_mask).norm(q); let exclude = ui.input(|i| i.modifiers.alt); match app.run_typed("mask.objectPoint", json!({"x": n.x, "y": n.y, "exclude": exclude})) { Ok(_) => { @@ -527,6 +531,8 @@ fn brush_tool( d: &DevelopSettings, ) { let active = app.session.active_mask; + // the stroke goes into the selected mask (or a new one): in its coordinates + let map = &map.for_mask_id(d, active); let long = (map.rect.width().max(map.rect.height())) as f64; let r = (app.ui.brush_size as f64 * long) as f32; if let Some(h) = resp.hover_pos() { @@ -635,6 +641,7 @@ fn grip_gestures( let m = d.masks.iter().find(|m| Some(m.id) == active)?; // Handles take precedence; otherwise grab the selected radial component under // the press, including components after the first in compound masks. + let map = &map.for_mask(m.on_photo); let at = map.norm(q); if let Some(ci) = m.components.iter().position(|c| radial_body_contains(&c.shape, at, map)) { return Some((m.id, ci, 0)); @@ -655,6 +662,7 @@ fn grip_gestures( if resp.dragged() && let (Some(Gesture::MaskHandle { mask, comp, handle, original_shape }), Some(q)) = (app.gesture.clone(), resp.interact_pointer_pos()) { + let map = &map.for_mask_id(d, Some(mask)); let n = map.norm(q); let press_origin = ui.input(|i| i.pointer.press_origin()).unwrap_or(q); let n0 = map.norm(press_origin); @@ -692,6 +700,7 @@ fn mask_keys(app: &mut LightcraftApp, ui: &mut egui::Ui, map: &CanvasMap, d: &De for &(mid, ci, handle, q) in grips { let Some(m) = d.masks.iter().find(|m| m.id == mid) else { continue }; let Some(shape) = m.components.get(ci).map(|c| &c.shape) else { continue }; + let map = &map.for_mask(m.on_photo); let at = map.norm(q); let mask_name = if m.name.trim().is_empty() { format!("{} {mid}", crate::i18n::tr("Mask")) } else { m.name.clone() }; let name = mask_handle_name(&mask_name, shape, handle); @@ -713,7 +722,9 @@ fn mask_keys(app: &mut LightcraftApp, ui: &mut egui::Ui, map: &CanvasMap, d: &De /// Long-edge units → normalized units for x and y. fn frame_long_norm(map: &CanvasMap) -> (f64, f64) { - // derive from the mapping: 1 normalized unit in x and y in screen points + // derive from the mapping: 1 normalized unit in x and y in screen points (of the frame's + // proportions, which a lens or perspective warp doesn't change) + let map = map.for_mask(false); let o = map.screen(Point::new(0.0, 0.0)); let x = map.screen(Point::new(1.0, 0.0)).distance(o) as f64; let y = map.screen(Point::new(0.0, 1.0)).distance(o) as f64; diff --git a/docs/parity-checklist.md b/docs/parity-checklist.md index 5e851e721d..d12a8b1cb7 100644 --- a/docs/parity-checklist.md +++ b/docs/parity-checklist.md @@ -1,6 +1,6 @@ # Lightroom parity checklist -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-30 · **Change:** minor (LR-BEHAV-ACCESS: the Flatpak may talk to the accessibility bus; LR-LIB-ALBUM: album commands leave out photos the library no longer has and record nothing when they add nothing; LR-BEHAV-DRAGDROP: a right-click on a text field while photos are carried from the grid opens no Cut / Copy / Paste menu; LR-BEHAV-DRAGDROP: a right-click while photos are carried from the grid opens no context menu; LR-LIB-VIRTUALCOPY: default numbering is preflighted across the requested batch and explicit names bypass suffix allocation; LR-BEHAV-DRAGDROP: only the primary button drags photos out of the grid and drops them; KEYC-RATING: deleting a keyword while Enable Filters has the filter put aside no longer leaves an empty one there; LR-BEHAV-DRAGDROP: a drop's destination is named with its parent when another folder or album has the same name; LR-VIEW-FILMSTRIP: photos drag from the filmstrip to the same sidebar targets as from the grid; LRC-LIB-LIBFOLDERS: a deep row's count follows its name in a narrow panel; LR-FILT-FLAG / LRC-LIB-FILTERBAR: the flag toggles combine, Picked + Unflagged, #2588; LR-VIEW-PHOTOGRID, LR-VIEW-FILMSTRIP, LR-VIEW-COMPARE, LR-VIEW-SURVEY, LRC-LIB-REFVIEW, LRC-LIB-PEOPLE: the active photo's selection ring shows on light themes and in Compare, Survey and the face tiles, and needs a loaded thumbnail; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-META-LOCATION: Info GPS text is fully validated before metadata changes; LR-MAP-TRACKLOG decodes GPX character references in point fields; malformed references error; CDATA stays literal; LR-BEHAV-ACCESS: the Thumbnail size control is a slider to screen readers; LR-BEHAV-ACCESS: the Detail photo is named with its file name; LR-VIEW-PHOTOGRID: the grid's empty-view message names its shortcut as this platform writes it and as it is bound; LR-BEHAV-ACCESS: the grid's date headers are named as they read; LR-BEHAV-ACCESS: the sidebar's disclosure triangles name their row; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌘⌥⇧K Set Keyword Shortcut and ⇧K; KEYC-METADATA: ⌘⇧K / Ctrl+Shift+K Edit Keywords; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LRC-LIB-FOLDERS: Local's built-in places (Pictures, Desktop, Downloads, Home) offer no Rename Folder… or Move Folder To…, and `folder.rename` / `folder.move` refuse the home folder and its Pictures, Desktop and Downloads; LRC-LIB-CATALOG-SWITCH: a library's catalog.snap or folder given at launch opens that library; LR-IMP-ADD-DIALOG / LR-IMP-MOVE: checked rename-sequence exhaustion; LR-IMP-ADD-DIALOG: an import asks the disk about the probes a scan left, and for each photo's sidecar, up to 16 files at a time; LRC-LIB-LIBFOLDERS: Don't ask again in the Move Photos question is kept only when the move started; a refused move leaves the question on; LR-IMP-ADD-DIALOG: remembered import thumbnail size, density-aware bounded previews and one job per candidate/size bucket (#2237, part of #2056); import loupe remains open; counts unchanged; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-MERGE-HDR / LR-MERGE-PANO / LR-MERGE-HDRPANO: the dialog makes a new preview each time it opens, also after a failed one, #2708; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LRC-LIB-MISSING: `library.missing` from the control channel checks the files on a worker thread instead of the UI thread; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-CROP-AUTO: Auto straighten reads the vertical vanishing point and the horizon; LR-META-EXIF-FULL: reject extra EXIF clock components so invalid capture-time candidates fall through to valid metadata; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-META-XMP / LRC-MAP-TRACKLOG: malformed ISO times are not projected or matched; LR-IMP-FORMATS: JPEG ICC APP2 chunk validation; LR-BEHAV-TITLEBAR: a dialog leaves the title bar undimmed, as a macOS sheet does; LR-BEHAV-ACCESS: the bars are toolbars and the panels landmarks for screen readers; LR-BEHAV-ACCESS: every dialog and prompt is exposed as a dialog (a modal one, an alert dialog for the prompts); LR-BEHAV-ACCESS: icon-only buttons are named by their label, with the shortcut as the description; MENU-HELP-HELP: on macOS the Help menu is registered with the application only once the menu bar is installed (muda ignores the call before), so AppKit knows which menu is Help; LR-BEHAV-ACCESS: a slider's value field is named ` value`, with how to use it as its description; MENU-BAR: every shortcut the app shows on macOS lists its modifiers ⌃⌥⇧⌘, as the menu bar does; MENU-BAR: on macOS two menu rows that run the same command each get their own native row and label (File ▸ Export… and File ▸ Export with Preset ▸ Custom…; File ▸ New Album… and Photo ▸ Add to Album ▸ New Album…); LR-BEHAV-DIALOGS: a one-line prompt that is refused stays open and says why, and a New Folder on Disk says it was made; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-VIEW-HISTOGRAM: the shadow and highlight clipping buttons take their tile and chevron from the theme, so the chevron reads in every theme; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-MASK-BRUSH: extreme brush segments skip the intermediate dabs; LR-MASK-BRUSH: with the mask overlay hidden, the stroke being painted no longer tints the photo) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-02 · **Change:** minor (LR-MASK-LINEAR: masks stay on the photo's content under lens corrections, Upright, Transform and Rotate; LR-BEHAV-ACCESS: the Flatpak may talk to the accessibility bus; LR-LIB-ALBUM: album commands leave out photos the library no longer has and record nothing when they add nothing; LR-BEHAV-DRAGDROP: a right-click on a text field while photos are carried from the grid opens no Cut / Copy / Paste menu; LR-BEHAV-DRAGDROP: a right-click while photos are carried from the grid opens no context menu; LR-LIB-VIRTUALCOPY: default numbering is preflighted across the requested batch and explicit names bypass suffix allocation; LR-BEHAV-DRAGDROP: only the primary button drags photos out of the grid and drops them; KEYC-RATING: deleting a keyword while Enable Filters has the filter put aside no longer leaves an empty one there; LR-BEHAV-DRAGDROP: a drop's destination is named with its parent when another folder or album has the same name; LR-VIEW-FILMSTRIP: photos drag from the filmstrip to the same sidebar targets as from the grid; LRC-LIB-LIBFOLDERS: a deep row's count follows its name in a narrow panel; LR-FILT-FLAG / LRC-LIB-FILTERBAR: the flag toggles combine, Picked + Unflagged, #2588; LR-VIEW-PHOTOGRID, LR-VIEW-FILMSTRIP, LR-VIEW-COMPARE, LR-VIEW-SURVEY, LRC-LIB-REFVIEW, LRC-LIB-PEOPLE: the active photo's selection ring shows on light themes and in Compare, Survey and the face tiles, and needs a loaded thumbnail; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-META-LOCATION: Info GPS text is fully validated before metadata changes; LR-MAP-TRACKLOG decodes GPX character references in point fields; malformed references error; CDATA stays literal; LR-BEHAV-ACCESS: the Thumbnail size control is a slider to screen readers; LR-BEHAV-ACCESS: the Detail photo is named with its file name; LR-VIEW-PHOTOGRID: the grid's empty-view message names its shortcut as this platform writes it and as it is bound; LR-BEHAV-ACCESS: the grid's date headers are named as they read; LR-BEHAV-ACCESS: the sidebar's disclosure triangles name their row; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌘⌥⇧K Set Keyword Shortcut and ⇧K; KEYC-METADATA: ⌘⇧K / Ctrl+Shift+K Edit Keywords; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LRC-LIB-FOLDERS: Local's built-in places (Pictures, Desktop, Downloads, Home) offer no Rename Folder… or Move Folder To…, and `folder.rename` / `folder.move` refuse the home folder and its Pictures, Desktop and Downloads; LRC-LIB-CATALOG-SWITCH: a library's catalog.snap or folder given at launch opens that library; LR-IMP-ADD-DIALOG / LR-IMP-MOVE: checked rename-sequence exhaustion; LR-IMP-ADD-DIALOG: an import asks the disk about the probes a scan left, and for each photo's sidecar, up to 16 files at a time; LRC-LIB-LIBFOLDERS: Don't ask again in the Move Photos question is kept only when the move started; a refused move leaves the question on; LR-IMP-ADD-DIALOG: remembered import thumbnail size, density-aware bounded previews and one job per candidate/size bucket (#2237, part of #2056); import loupe remains open; counts unchanged; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-MERGE-HDR / LR-MERGE-PANO / LR-MERGE-HDRPANO: the dialog makes a new preview each time it opens, also after a failed one, #2708; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LRC-LIB-MISSING: `library.missing` from the control channel checks the files on a worker thread instead of the UI thread; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-CROP-AUTO: Auto straighten reads the vertical vanishing point and the horizon; LR-META-EXIF-FULL: reject extra EXIF clock components so invalid capture-time candidates fall through to valid metadata; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-META-XMP / LRC-MAP-TRACKLOG: malformed ISO times are not projected or matched; LR-IMP-FORMATS: JPEG ICC APP2 chunk validation; LR-BEHAV-TITLEBAR: a dialog leaves the title bar undimmed, as a macOS sheet does; LR-BEHAV-ACCESS: the bars are toolbars and the panels landmarks for screen readers; LR-BEHAV-ACCESS: every dialog and prompt is exposed as a dialog (a modal one, an alert dialog for the prompts); LR-BEHAV-ACCESS: icon-only buttons are named by their label, with the shortcut as the description; MENU-HELP-HELP: on macOS the Help menu is registered with the application only once the menu bar is installed (muda ignores the call before), so AppKit knows which menu is Help; LR-BEHAV-ACCESS: a slider's value field is named ` value`, with how to use it as its description; MENU-BAR: every shortcut the app shows on macOS lists its modifiers ⌃⌥⇧⌘, as the menu bar does; MENU-BAR: on macOS two menu rows that run the same command each get their own native row and label (File ▸ Export… and File ▸ Export with Preset ▸ Custom…; File ▸ New Album… and Photo ▸ Add to Album ▸ New Album…); LR-BEHAV-DIALOGS: a one-line prompt that is refused stays open and says why, and a New Folder on Disk says it was made; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-VIEW-HISTOGRAM: the shadow and highlight clipping buttons take their tile and chevron from the theme, so the chevron reads in every theme; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-FILT-SEARCH-META: `>=` / `<=` in fielded search tokens; MENU-FILE-CLOSE, KEY-CLOSEWINDOW: the notes say Close Window quits the app, after the same check as Quit; MENU-EDIT-SELECTNONE, MENU-EDIT-PASTESELECTED: Edit lists Select Only Active Photo after Deselect All and Paste Settings from Previous after Paste Selected Settings…; on macOS no doubled separators are left where Settings… moved to the app menu; LR-BEHAV-TITLEBAR: a double-click follows the system setting, and a panel over the bar keeps the window still; KEYC-METADATA, LRC-LIB-KEYWORDS: ⌥0 / ⌥⇧0 go to the next / previous keyword set; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; KEYC-METADATA: ⌘K / Ctrl+K Add Keywords; LRC-DEV-HISTOGRAM-DRAG: the histogram's five tonal zones — hover for the value, drag to adjust the Basic control the press locked, double-click to reset; LR-VIEW-HISTOGRAM note updated; MENU-BAR: on macOS a menu item that yields its key to a focused text field or to shortcut recording really lets it go; LR-MERGE-HDR / LR-MERGE-PANO: Lanczos-3 warps; LR-IMP-LOCAL: browsing from the sidebar records the folder; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LR-IMP-ADD-DIALOG: hierarchical import album destination reconciled with main, #2362; KEY-SLIDER-NUDGE: vertical wheel nudges need a click or drag in the row; leaving it disarms them; LR-EDIT-OPTICS-PROFILE: Fujifilm nine-knot lens tables read at the plain radius; LR-VIEW-ZOOM: only a full-size drag within 20 ms clears a slow-drag verdict; LRC-LIB-LIBFOLDERS, LR-BEHAV-BG-TASKS: Synchronize Folder's scan shows its folder listing and stops it on ✕; LRC-LIB-LIBFOLDERS: Synchronize Folder says a folder is up to date; LR-LIB-STACK: filmstrip stack badge click; LRC-LIB-REFVIEW / CATALOG-SWITCH: Escape preserves edits; catalog-local pins; deleted and malformed references refused; LRC-LIB-COMPARE: the Info panel labels the active photo while culling, #2398; KEYC-RATING: `\` shows / hides the Filter Bar in the grids; LR-VIEW-PHOTOGRID: the grid reads only the photos an import batch or an edit touched, not the whole list; earlier: LRC-PRINT-PAGE: Print Page Options; LR-META-XMP: fewer false unimported-XMP warnings; LR-MASK-BRUSH: extreme brush segments skip the intermediate dabs; LR-MASK-BRUSH: with the mask overlay hidden, the stroke being painted no longer tints the photo) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6 The measured, row-by-row checklist: one row per Lightroom feature, menu item and shortcut, what we have and what is missing. It answers *does the feature exist?* (feature breadth). Whether it is good @@ -297,7 +297,7 @@ differences and missing bindings are in [ui-parity.md](ui-parity.md#shortcuts). | 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`, `crates/ui-egui/src/brush_live.rs` | size/feather/flow/density/erase; Create New Mask starts a separate brush in paint mode; the stroke shows while the button is held with the committed stroke's dab spacing, feather and flow, so fast strokes have no gaps (#517); with the mask overlay hidden (O / the eye, or after a mask slider hid it) or a colour overlay at opacity 0 it is not tinted while painting either, so dodging and burning is judged on the photo, and its effect shows once the stroke is committed (#2549); coverage builds up by passes, not hold time (holding the brush still adds nothing, live or committed, #518); oversized path segments skip intermediate dabs instead of allocating a million samples; Auto Mask: dabs weighted by similarity to the colour under the dab centre, refined by a guided filter on luminance (CPU + GPU); ⇧-click draws a straight stroke from where the last stroke ended (`connect`), tapering size, feather and flow from that stroke's values when they changed; no A/B brushes; pen pressure (Brush ▸ Pen Pressure, on by default; `pressure` per point in `mask.brushStroke`) scales each dab's size (a tenth to all of it) and flow, live and committed alike, where winit reports a pen force (Windows; not macOS / Linux tablets yet); no tilt | -| LR-MASK-LINEAR | Linear gradient | P0 | ✅ | `cmd:tool.linear`, `cmd:mask.update` | | +| LR-MASK-LINEAR | Linear gradient | P0 | ✅ | `cmd:tool.linear`, `cmd:mask.update`, `crates/pipeline/src/mask_space.rs` | gradients, brushes and AI selections stay on the photo's content when lens corrections, Upright, Transform or Rotate Left / Right change (`Mask.on_photo`; CPU and GPU) | | 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, respecting section eyes and the requested HDR/SDR rendition), Pick button, Refine slider; at most 64 samples (`MAX_COLOR_RANGE_SAMPLES`, render cost is pixels × samples) however the mask arrives: `mask.add` / `mask.update`, a sidecar's `lc:settings`, a preset, `develop.merge`, `render --settings` or the catalog and history keep the first 64 (issue #2556) | | 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 (the pressed endpoint stays selected through fast/crossing drags; one undo step per drag), Smoothness, Show Luminance Map (B&W photo with the range tinted) | From 432fbed3d5f123aee9dca7d494171ec540187d86 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Mon, 2 Nov 2026 04:34:36 +1000 Subject: [PATCH 31/51] LR-MASK-GEOMETRY: review fixes: old masks keep their region and frame, clicks and handles in mask coordinates CodeRabbit's review of #3550: - An old gradient goes onto the photo through the warp's local linear part: a radial's radii and angle, a linear's direction and width (the mean alpha error halves; the largest drops 3x). - Old masks an edit brings in (paste, sync, preset, history step, sidecar) go onto the photo from the settings they came with; masks an edit keeps, from the settings before it. - A click on an old Object mask attaches the mask and the click before the model reads them. - The handles and arrow keys read a mask after Begin Interaction attached it, so a drag never mixes frames; a linear gradient's guides are drawn through the warp. - A brush point the warp can't place lays no dab; a resampled zoomed-in pass takes the whole-photo selection past its own area; mapped boxes sample 16 points per edge. - The CPU and GPU geometry tests also cover a quarter turn. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/masks.rs | 20 ++ crates/engine/src/lib.rs | 32 ++- crates/engine/src/tests/mask_geometry.rs | 61 +++++ crates/gpu/tests/mask_geometry.rs | 11 + crates/pipeline/src/geometry.rs | 10 +- crates/pipeline/src/mask_space.rs | 251 ++++++++++++++++--- crates/pipeline/src/masks.rs | 3 +- crates/pipeline/src/tests_mask_geometry.rs | 34 +++ crates/ui-egui/src/panels/detail/geometry.rs | 30 +++ crates/ui-egui/src/panels/detail/masks.rs | 65 ++++- 10 files changed, 465 insertions(+), 52 deletions(-) diff --git a/crates/engine/src/cmd/masks.rs b/crates/engine/src/cmd/masks.rs index 5fc083f51f..4f5d4aeb06 100644 --- a/crates/engine/src/cmd/masks.rs +++ b/crates/engine/src/cmd/masks.rs @@ -464,6 +464,26 @@ pub fn specs() -> Vec { let exclude = bool_or(p, "exclude", false); let id = s.active().ok_or_else(|| bad(c, "no active photo"))?; let mid = mask_id(p, s.active_mask, c)?; + // an old mask (on the corrected frame, as the click then is) goes onto the photo + // first, and the click with it: the model works on the photo before lens + // corrections, Upright and Transform, and reads every click there + let mut at = at; + if let Some(cur) = s.develop_of(id).filter(|d| d.masks.iter().any(|m| m.id == mid && !m.on_photo)) { + if let Some(f) = s.mask_frame(id, &cur) { + let n = f.for_mask(true).norm_to_mask(at); + if n.x.is_finite() && n.y.is_finite() { + at = Point::new(n.x.clamp(0.0, 1.0), n.y.clamp(0.0, 1.0)); + } + } + let mut on = (*cur).clone(); + s.attach_masks(id, &cur, &mut on); + s.apply_silent(lightcraft_catalog::Op::SetDevelop { + id, + settings: std::sync::Arc::new(on), + label: "Masks".into(), + edited: None, + })?; + } let d = s.develop_of(id).unwrap_or_default(); let m = d.masks.iter().find(|m| m.id == mid).ok_or_else(|| bad(c, format!("no mask {mid}")))?; let k = m diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 84cef73d85..f0c502bf13 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1024,15 +1024,17 @@ impl Session { pub fn develop_op(&self, id: PhotoId, settings: DevelopSettings, label: &str) -> Option { self.catalog.photo(id)?; let mut settings = settings; - // old masks (on the corrected frame) go onto the photo, from the frame they were drawn on + // old masks (on the corrected frame) go onto the photo, from the frame they were drawn on: + // masks this edit kept, under the photo's settings before it (a geometry edit then moves + // the photo, not them); masks it brought in (paste, sync, a preset, a history step, a + // sidecar), under the settings they came with, where they render now if settings.masks.iter().any(|m| !m.on_photo) { let before = match &self.interaction { Some(i) if i.photo == id => Some(i.original.clone()), _ => self.develop_of(id), }; - if let Some(b) = before { - self.attach_masks(id, &b, &mut settings); - } + let drawn = before.filter(|b| b.masks == settings.masks).unwrap_or_else(|| Arc::new(settings.clone())); + self.attach_masks(id, &drawn, &mut settings); } let settings = Arc::new(settings); let step = lightcraft_catalog::HistoryStep { label: label.into(), settings: settings.clone() }; @@ -1049,19 +1051,25 @@ impl Session { if d.masks.iter().all(|m| m.on_photo) { return; } - let Some(p) = self.catalog.photo(id) else { return }; - let frame = lightcraft_pipeline::geometry::Frame::with_lens( - p.width.max(1) as usize, - p.height.max(1) as usize, - &drawn.effective(), - false, - p.embedded_lens.as_ref(), - ); + let Some(frame) = self.mask_frame(id, drawn) else { return }; for m in &mut d.masks { lightcraft_pipeline::mask_space::attach(m, &frame); } } + /// The uncropped frame of photo `id` under settings `d`, with its lens: where mask + /// coordinates go through its warp ([`lightcraft_pipeline::geometry::Frame::for_mask`]). + pub(crate) fn mask_frame(&self, id: PhotoId, d: &DevelopSettings) -> Option { + let p = self.catalog.photo(id)?; + Some(lightcraft_pipeline::geometry::Frame::with_lens( + p.width.max(1) as usize, + p.height.max(1) as usize, + &d.effective(), + false, + p.embedded_lens.as_ref(), + )) + } + pub fn begin_interaction(&mut self, label: &str) -> Result<()> { if self.interaction.is_some() { self.end_interaction()?; diff --git a/crates/engine/src/tests/mask_geometry.rs b/crates/engine/src/tests/mask_geometry.rs index 9c5681a4f7..4a1044450f 100644 --- a/crates/engine/src/tests/mask_geometry.rs +++ b/crates/engine/src/tests/mask_geometry.rs @@ -111,3 +111,64 @@ fn a_mask_saved_before_reads_as_an_old_mask() { assert!(written.get("on_photo").is_none(), "{written}"); assert_eq!(serde_json::to_value(Mask::default()).unwrap()["on_photo"], json!(true)); } + +/// Old masks an edit brings in (paste, sync, a preset, a history step, a sidecar) go onto the +/// photo from the settings they came with, where they render now, not from the photo's settings +/// before the edit (review of #3550). +#[test] +fn old_masks_an_edit_brings_in_go_onto_the_photo_from_the_settings_they_came_with() { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + // the photo now: no Transform + let before = active_dev(&s); + // what comes in: a Transform and an old mask drawn under it + let p = Point::new(0.3, 0.4); + let mut incoming = before.clone(); + incoming.geometry.vertical = 30.0; + incoming.geometry.rotate = 5.0; + let mut m = radial_at(p); + m.on_photo = false; + incoming.masks = vec![m]; + s.set_develop(id, incoming.clone(), "Paste Settings").unwrap(); + let now = active_dev(&s); + assert!(now.masks[0].on_photo); + let photo = s.catalog.photo(id).unwrap(); + let frame = lightcraft_pipeline::geometry::Frame::with_lens(photo.width as usize, photo.height as usize, &incoming, false, None); + let back = frame.for_mask(true).mask_to_norm(centre(&now).0); + assert!((back.x - p.x).abs() < 1e-6 && (back.y - p.y).abs() < 1e-6, "renders where it did under its own settings: {back:?}"); +} + +/// A click on an old Object mask goes onto the photo with the mask before the model reads it: the +/// stored clicks and the new one are then in the same coordinates (review of #3550). +#[test] +fn a_click_on_an_old_object_mask_goes_onto_the_photo_with_it() { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + let mut d = active_dev(&s); + d.geometry.vertical = 30.0; + d.geometry.rotate = 5.0; + let p = Point::new(0.3, 0.4); + d.masks = vec![Mask { + id: 1, + on_photo: false, + components: vec![MaskComponent { + name: None, + op: MaskOp::Add, + invert: false, + shape: MaskShape::Object { hint: vec![p], exclude: Vec::new(), seg: None, detail: Vec::new(), edge: 0.0 }, + }], + ..Default::default() + }]; + s.catalog.apply(lightcraft_catalog::Op::SetDevelop { id, settings: std::sync::Arc::new(d.clone()), label: "Old".into(), edited: None }).unwrap(); + s.active_mask = Some(1); + // (no model: the command fails at the model, after the mask went onto the photo) + s.segmenter.dir = Some(std::env::temp_dir().join("lightcraft-no-model-mask-geometry")); + let _ = s.execute("mask.objectPoint", &json!({"x": 0.6, "y": 0.5})); + let now = active_dev(&s); + assert!(now.masks[0].on_photo); + let photo = s.catalog.photo(id).unwrap(); + let frame = lightcraft_pipeline::geometry::Frame::with_lens(photo.width as usize, photo.height as usize, &d, false, None); + let MaskShape::Object { hint, .. } = &now.masks[0].components[0].shape else { panic!() }; + let want = frame.for_mask(true).norm_to_mask(p); + assert!((hint[0].x - want.x).abs() < 1e-9 && (hint[0].y - want.y).abs() < 1e-9, "{hint:?} vs {want:?}"); +} diff --git a/crates/gpu/tests/mask_geometry.rs b/crates/gpu/tests/mask_geometry.rs index fc7d856196..1cbd741a0e 100644 --- a/crates/gpu/tests/mask_geometry.rs +++ b/crates/gpu/tests/mask_geometry.rs @@ -89,6 +89,17 @@ fn geometries() -> Vec<(&'static str, Box)> { }), ), ("distortion", Box::new(|s: &mut DevelopSettings| s.optics.distortion = 40.0)), + ( + // Rotate Right: the photo and its masks turn together (`photo.rotateRight`) + "quarter turn", + Box::new(|s: &mut DevelopSettings| { + let turn = lightcraft_geom::Orientation::Normal.rotated(true); + s.orientation = s.orientation.rotated(true); + for m in &mut s.masks { + lightcraft_pipeline::mask_space::turn(m, turn); + } + }), + ), ] } diff --git a/crates/pipeline/src/geometry.rs b/crates/pipeline/src/geometry.rs index 14743959cc..3fa043f5e1 100644 --- a/crates/pipeline/src/geometry.rs +++ b/crates/pipeline/src/geometry.rs @@ -256,11 +256,15 @@ impl Frame { } } - /// Mask coordinates → output pixel (the inverse of [`Frame::out_to_mask`]). - pub fn mask_to_out(&self, out_w: usize, out_h: usize) -> impl Fn(Point) -> Point + use<> { + /// Mask coordinates → output pixel (the inverse of [`Frame::out_to_mask`]); `None` for a + /// point the warp can't invert (far outside a strong one), which has no place in the output. + pub fn mask_to_out(&self, out_w: usize, out_h: usize) -> impl Fn(Point) -> Option + use<> { let to_out = self.norm_to_out(out_w, out_h); let (warp, ow, oh) = (self.mask_warp().cloned(), self.ow, self.oh); - move |n: Point| to_out.apply(mask_to_norm(warp.as_ref(), ow, oh, n)) + move |n: Point| match &warp { + Some(w) => w.from_source(Point::new(n.x * ow, n.y * oh)).map(|t| to_out.apply(Point::new(t.x / ow, t.y / oh))), + None => Some(to_out.apply(n)), + } } /// Normalized coordinates of this frame's transformed image → mask coordinates. diff --git a/crates/pipeline/src/mask_space.rs b/crates/pipeline/src/mask_space.rs index d2e15e1205..d5858ab686 100644 --- a/crates/pipeline/src/mask_space.rs +++ b/crates/pipeline/src/mask_space.rs @@ -12,6 +12,10 @@ use crate::masks::SegGrid; /// Put mask `m`, drawn on the corrected (transformed) image of `frame`, on the photo before the /// warp: it then renders where it did under `frame` and stays on that content whatever lens /// corrections, Upright or Transform do later. A mask already on the photo is left alone. +/// +/// Points move exactly; a gradient's shape (a radial's radii and angle, a linear's direction and +/// width) goes through the warp's local linear part at the gradient, so it covers the same region +/// as long as the warp is close to linear across it (lens and perspective warps are smooth). pub fn attach(m: &mut Mask, frame: &Frame) { if m.on_photo { return; @@ -24,7 +28,89 @@ pub fn attach(m: &mut Mask, frame: &Frame) { } let fwd = |p: Point| f.norm_to_mask(p); let back = |p: Point| f.mask_to_norm(p); - remap(m, &fwd, &back, 0.0); + let l = f.ow.max(f.oh).max(1e-9); + let (sx, sy) = (f.ow / l, f.oh / l); + for c in &mut m.components { + match &mut c.shape { + MaskShape::Linear { start, end } => (*start, *end) = attach_linear(*start, *end, &fwd, sx, sy), + MaskShape::Radial { center, rx, ry, angle, .. } => { + let mid = *center; + *center = fwd(mid); + if let Some(j) = jacobian(&fwd, mid, sx, sy) + && let Some((nrx, nry, na)) = attach_ellipse(j, *rx, *ry, *angle) + { + (*rx, *ry, *angle) = (nrx, nry, na); + } + } + _ => {} + } + } + remap(m, &fwd, &back, 0.0, false); +} + +/// The local linear part of `f` (normalized → normalized) at `p`, in long-edge units (isotropic; +/// `sx`, `sy` are the frame's sides over its long side): `[a, b, c, d]` takes `(dx, dy)` to +/// `(a dx + b dy, c dx + d dy)`. `None` where `f` isn't finite or collapses. +fn jacobian(f: &dyn Fn(Point) -> Point, p: Point, sx: f64, sy: f64) -> Option<[f64; 4]> { + const H: f64 = 1e-4; + let (o, px, py) = (f(p), f(Point::new(p.x + H, p.y)), f(Point::new(p.x, p.y + H))); + let j = [(px.x - o.x) / H, (py.x - o.x) * sx / (H * sy), (px.y - o.y) * sy / (H * sx), (py.y - o.y) / H]; + let det = j[0] * j[3] - j[1] * j[2]; + (j.iter().all(|v| v.is_finite()) && det.is_finite() && det.abs() > 1e-9).then_some(j) +} + +/// A linear gradient from `start` to `end` (normalized, on the corrected frame) on the photo: +/// its start moves through `f`, and its direction and length follow the local linear part, so +/// its bands (lines at right angles to it) land on the same content. The end moves through `f` +/// alone where that part isn't defined. +fn attach_linear(start: Point, end: Point, f: &dyn Fn(Point) -> Point, sx: f64, sy: f64) -> (Point, Point) { + let (a, plain_end) = (f(start), f(end)); + let mid = Point::new((start.x + end.x) / 2.0, (start.y + end.y) / 2.0); + let Some([ja, jb, jc, jd]) = jacobian(f, mid, sx, sy) else { return (a, plain_end) }; + // the bands are level sets of (p - start)·d / |d|²: through J they keep that form with the + // normal J^-T d, and the same |d|² sets the length + let d = ((end.x - start.x) * sx, (end.y - start.y) * sy); + let det = ja * jd - jb * jc; + let n = ((jd * d.0 - jc * d.1) / det, (-jb * d.0 + ja * d.1) / det); + let (dd, nn) = (d.0 * d.0 + d.1 * d.1, n.0 * n.0 + n.1 * n.1); + if !(dd.is_finite() && nn.is_finite()) || nn < 1e-18 { + return (a, plain_end); + } + let k = dd / nn; + let e = Point::new(a.x + n.0 * k / sx, a.y + n.1 * k / sy); + if e.x.is_finite() && e.y.is_finite() { (a, e) } else { (a, plain_end) } +} + +/// An ellipse of radii `rx`, `ry` (long-edge units) turned by `angle` degrees, through the linear +/// map `j` (long-edge units): the radii and angle of the ellipse it becomes, `rx` staying the +/// radius along the image of the old `rx` axis. `None` when the result isn't usable. +fn attach_ellipse(j: [f64; 4], rx: f64, ry: f64, angle: f64) -> Option<(f64, f64, f64)> { + let (s, c) = angle.to_radians().sin_cos(); + // M = J · R(angle) · diag(rx, ry): its columns are the images of the two semi-axes + let m = [(j[0] * c + j[1] * s) * rx, (j[1] * c - j[0] * s) * ry, (j[2] * c + j[3] * s) * rx, (j[3] * c - j[2] * s) * ry]; + // the new ellipse's axes: eigenvectors of M Mᵀ, its radii the square roots of the eigenvalues + let (p, q, r) = (m[0] * m[0] + m[1] * m[1], m[0] * m[2] + m[1] * m[3], m[2] * m[2] + m[3] * m[3]); + let (half, det) = ((p + r) / 2.0, p * r - q * q); + let root = (half * half - det).max(0.0).sqrt(); + let (l1, l2) = (half + root, (half - root).max(0.0)); + let v1 = if q.abs() > 1e-15 * (p.abs() + r.abs()) { + let (x, y) = (q, l1 - p); + let n = x.hypot(y); + (x / n, y / n) + } else if p >= r { + (1.0, 0.0) + } else { + (0.0, 1.0) + }; + let v2 = (-v1.1, v1.0); + // the old rx axis, mapped + let a = (m[0], m[2]); + let along1 = (a.0 * v1.0 + a.1 * v1.1).abs() >= (a.0 * v2.0 + a.1 * v2.1).abs(); + let (axis, nrx, nry) = if along1 { (v1, l1.sqrt(), l2.sqrt()) } else { (v2, l2.sqrt(), l1.sqrt()) }; + // (pointing the way the old axis went) + let axis = if axis.0 * a.0 + axis.1 * a.1 < 0.0 { (-axis.0, -axis.1) } else { axis }; + let na = axis.1.atan2(axis.0).to_degrees(); + (nrx.is_finite() && nry.is_finite() && na.is_finite() && nrx > 0.0 && nry > 0.0).then_some((nrx, nry, na)) } /// Follow `turn` (the change of the user orientation, e.g. a quarter turn clockwise): mask @@ -46,10 +132,6 @@ pub fn turn(m: &mut Mask, turn: Orientation) { let (flip, quarters) = turn.to_parts(); // a radial's angle is clockwise on screen: a quarter turn clockwise adds 90°; a mirror negates it first let delta = 90.0 * quarters as f64; - remap_turn(m, &fwd, &back, flip, delta); -} - -fn remap_turn(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point, flip: bool, delta: f64) { if flip { for c in &mut m.components { if let MaskShape::Radial { angle, .. } = &mut c.shape { @@ -57,25 +139,27 @@ fn remap_turn(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) - } } } - remap(m, fwd, back, delta); + remap(m, &fwd, &back, delta, true); } /// Move every place `m` stores: `fwd` maps an old point to the new frame, `back` a new point to -/// the old one (for resampling segmentations); radial angles change by `delta` degrees. -fn remap(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point, delta: f64) { +/// the old one (for resampling segmentations); radial angles change by `delta` degrees. With +/// `gradients` false, linear and radial gradients are left to the caller. +fn remap(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point, delta: f64, gradients: bool) { for c in &mut m.components { match &mut c.shape { - MaskShape::Linear { start, end } => { + MaskShape::Linear { start, end } if gradients => { *start = fwd(*start); *end = fwd(*end); } - MaskShape::Radial { center, angle, .. } => { + MaskShape::Radial { center, angle, .. } if gradients => { *center = fwd(*center); if delta != 0.0 { // (kept within −180..180°) *angle = (*angle + delta + 180.0).rem_euclid(360.0) - 180.0; } } + MaskShape::Linear { .. } | MaskShape::Radial { .. } => {} MaskShape::Brush { strokes } => { for s in strokes { s.points.iter_mut().for_each(|p| *p = fwd(*p)); @@ -83,13 +167,9 @@ fn remap(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Poi } MaskShape::Object { hint, exclude, seg, detail, .. } => { hint.iter_mut().chain(exclude.iter_mut()).for_each(|p| *p = fwd(*p)); - remap_seg(seg, fwd, back); - detail.iter_mut().for_each(|d| *d = moved(d, fwd, back).unwrap_or_else(|| d.clone())); - } - MaskShape::Prompt { seg, detail, .. } => { - remap_seg(seg, fwd, back); - detail.iter_mut().for_each(|d| *d = moved(d, fwd, back).unwrap_or_else(|| d.clone())); + remap_segs(seg, detail, fwd, back); } + MaskShape::Prompt { seg, detail, .. } => remap_segs(seg, detail, fwd, back), // computed from the photo at each render (its mattes follow the warp), or colours, // tones and depths, which have no place (a colour range's samples are colours) MaskShape::Subject @@ -104,23 +184,39 @@ fn remap(m: &mut Mask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Poi } } -fn remap_seg(seg: &mut Option, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point) { +/// A stored selection and its zoomed-in passes, resampled onto the new frame. Where a pass's new +/// box reaches past what it covered, it takes the whole-photo selection there (what the render +/// showed), not a copy of its own edge. +fn remap_segs(seg: &mut Option, detail: &mut [SegMask], fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point) { + let coarse = seg.as_ref().and_then(SegGrid::new); + for d in detail.iter_mut() { + if let Some(m) = moved(d, fwd, back, coarse.as_ref()) { + *d = m; + } + } if let Some(s) = seg.as_ref() - && let Some(m) = moved(s, fwd, back) + && let Some(m) = moved(s, fwd, back, None) { *seg = Some(m); } } -/// The box around `r` mapped by `f` (its corners and edge midpoints), inside 0..1. +/// Points along each edge of a box [`map_rect`] maps: a lens warp bends an edge between its ends, +/// most where it crosses the centre lines. +const RECT_EDGE_SAMPLES: usize = 16; + +/// The box around `r` mapped by `f` (points along its edges), inside 0..1. fn map_rect(r: [f64; 4], f: &dyn Fn(Point) -> Point) -> [f64; 4] { - let (mx, my) = ((r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0); - let pts = - [(r[0], r[1]), (r[2], r[1]), (r[0], r[3]), (r[2], r[3]), (mx, r[1]), (mx, r[3]), (r[0], my), (r[2], my)].map(|(x, y)| f(Point::new(x, y))); - let finite = pts.iter().filter(|p| p.x.is_finite() && p.y.is_finite()); let (mut x0, mut y0, mut x1, mut y1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN); - for p in finite { - (x0, y0, x1, y1) = (x0.min(p.x), y0.min(p.y), x1.max(p.x), y1.max(p.y)); + for i in 0..=RECT_EDGE_SAMPLES { + let t = i as f64 / RECT_EDGE_SAMPLES as f64; + let (x, y) = (r[0] + t * (r[2] - r[0]), r[1] + t * (r[3] - r[1])); + for q in [(x, r[1]), (x, r[3]), (r[0], y), (r[2], y)] { + let p = f(Point::new(q.0, q.1)); + if p.x.is_finite() && p.y.is_finite() { + (x0, y0, x1, y1) = (x0.min(p.x), y0.min(p.y), x1.max(p.x), y1.max(p.y)); + } + } } if x0 > x1 || y0 > y1 { return r; @@ -130,8 +226,10 @@ fn map_rect(r: [f64; 4], f: &dyn Fn(Point) -> Point) -> [f64; 4] { /// Segmentation `s` resampled onto the new frame: a grid of the same side over the mapped box /// (the whole image for a whole-image one), each cell taking the old logit at its centre mapped -/// back. `None` when the data is damaged (it is kept as it is). -fn moved(s: &SegMask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point) -> Option { +/// back. A cell whose centre maps back outside what `s` covered takes `outside`'s logit there +/// (for a zoomed-in pass: the whole-photo selection), else selects nothing. `None` when the data +/// is damaged (it is kept as it is). +fn moved(s: &SegMask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Point, outside: Option<&SegGrid>) -> Option { let old = SegGrid::new(s)?; let side = s.side as usize; let rect = match s.rect { @@ -143,7 +241,8 @@ fn moved(s: &SegMask, fwd: &dyn Fn(Point) -> Point, back: &dyn Fn(Point) -> Poin .map(|i| { let (x, y) = ((i % side) as f64 + 0.5, (i / side) as f64 + 0.5); let n = Point::new(b[0] + x / side as f64 * (b[2] - b[0]), b[1] + y / side as f64 * (b[3] - b[1])); - old.logit(back(n)) + let o = back(n); + if old.contains(o) { old.logit(o) } else { outside.map_or(-16.0, |g| g.logit(o)) } }) .collect(); Some(match rect { @@ -271,4 +370,100 @@ mod tests { } assert!(m.on_photo); } + + /// A Transform and lens warp strong enough to bend shapes across a gradient. + fn warped() -> DevelopSettings { + let mut s = DevelopSettings::default(); + s.geometry.vertical = 40.0; + s.geometry.horizontal = -25.0; + s.geometry.rotate = 8.0; + s.optics.distortion = 60.0; + s + } + + /// The mean and largest difference between mask `a` and `b` rendered under `s`. + fn alpha_diff(a: &Mask, b: &Mask, s: &DevelopSettings) -> (f32, f32) { + let frame = Frame::new(300, 200, s, true); + let (w, h) = frame.fit(300, 200); + let img = lightcraft_raster::Rgb32f::new(w, h); + let log_l = lightcraft_raster::Plane::new(w, h); + let pa = crate::masks::evaluate_one(a, &frame, w, h, &img, &log_l, 0.0, None); + let pb = crate::masks::evaluate_one(b, &frame, w, h, &img, &log_l, 0.0, None); + let d: Vec = pa.data.iter().zip(&pb.data).map(|(x, y)| (x - y).abs()).collect(); + (d.iter().sum::() / d.len().max(1) as f32, d.iter().copied().fold(0.0, f32::max)) + } + + /// An old gradient keeps its region when it goes onto the photo: its shape goes through the + /// warp's local linear part, not only its centre or ends (review of #3550). + #[test] + fn attaching_an_old_gradient_keeps_its_region() { + let s = warped(); + let frame = Frame::new(300, 200, &s, false); + for shape in [ + MaskShape::Radial { center: Point::new(0.3, 0.35), rx: 0.18, ry: 0.07, angle: 30.0, feather: 30.0, invert: false }, + MaskShape::Linear { start: Point::new(0.2, 0.25), end: Point::new(0.45, 0.6) }, + ] { + let mut old = mask(shape.clone()); + old.on_photo = false; + let mut on = old.clone(); + attach(&mut on, &frame); + // what moving the points alone would give + let mut naive = old.clone(); + naive.on_photo = true; + remap(&mut naive, &|p| frame.for_mask(true).norm_to_mask(p), &|p| frame.for_mask(true).mask_to_norm(p), 0.0, true); + let (mean, max) = alpha_diff(&old, &on, &s); + let (naive_mean, naive_max) = alpha_diff(&old, &naive, &s); + // (the warp isn't linear across the whole gradient: a little is left at its far edge) + assert!(mean < 0.01, "{shape:?}: mean {mean}, max {max}"); + assert!(naive_mean > 1.5 * mean && naive_max > 2.0 * max, "{shape:?}: points alone {naive_mean} / {naive_max} vs {mean} / {max}"); + } + } + + /// Where a zoomed-in pass's new box reaches past what it covered (a turned box's corners), it + /// takes the whole-photo selection, not a copy of its own edge (review of #3550). + #[test] + fn a_moved_detail_pass_takes_the_whole_selection_past_its_own_area() { + const SIDE: usize = 16; + let mut s = DevelopSettings::default(); + s.geometry.rotate = 20.0; + let frame = Frame::new(300, 200, &s, false); + let detail = SegMask::from_logits_in(SIDE, &[8.0; SIDE * SIDE], [0.4, 0.4, 0.6, 0.6]); + let coarse = SegMask::from_logits(SIDE, &[-8.0; SIDE * SIDE]); + let mut m = + mask(MaskShape::Object { hint: vec![Point::new(0.5, 0.5)], exclude: Vec::new(), seg: Some(coarse), detail: vec![detail], edge: 0.0 }); + m.on_photo = false; + attach(&mut m, &frame); + let MaskShape::Object { detail, .. } = &m.components[0].shape else { panic!() }; + let g = SegGrid::new(&detail[0]).unwrap(); + let b = detail[0].bounds().unwrap(); + // a corner of the new box is outside the turned patch: unselected there, selected inside + let inset = |t: f64| Point::new(b[0] + t * (b[2] - b[0]), b[1] + t * (b[3] - b[1])); + assert!(g.logit(inset(0.03)) < 0.0, "{}", g.logit(inset(0.03))); + assert!(g.logit(inset(0.5)) > 0.0); + } + + /// The box around a mapped rectangle holds the whole bent edge, also off the centre lines + /// (review of #3550). + #[test] + fn a_mapped_box_holds_its_bent_edges() { + let mut s = DevelopSettings::default(); + s.optics.distortion = -80.0; + let f = Frame::new(300, 200, &s, false).for_mask(true).into_owned(); + let fwd = |p: Point| f.norm_to_mask(p); + let r = [0.2, 0.1, 0.9, 0.4]; + let b = map_rect(r, &fwd); + let (mut x0, mut y0, mut x1, mut y1) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN); + for i in 0..=2000 { + let t = i as f64 / 2000.0; + let (x, y) = (r[0] + t * (r[2] - r[0]), r[1] + t * (r[3] - r[1])); + for q in [(x, r[1]), (x, r[3]), (r[0], y), (r[2], y)] { + let p = fwd(Point::new(q.0, q.1)); + (x0, y0, x1, y1) = (x0.min(p.x), y0.min(p.y), x1.max(p.x), y1.max(p.y)); + } + } + let want = [x0.clamp(0.0, 1.0), y0.clamp(0.0, 1.0), x1.clamp(0.0, 1.0), y1.clamp(0.0, 1.0)]; + for k in 0..4 { + assert!((b[k] - want[k]).abs() < 5e-4, "{b:?} vs {want:?}"); + } + } } diff --git a/crates/pipeline/src/masks.rs b/crates/pipeline/src/masks.rs index 85c920bc9d..097e080a7e 100644 --- a/crates/pipeline/src/masks.rs +++ b/crates/pipeline/src/masks.rs @@ -590,7 +590,8 @@ pub fn brush_dabs_within(s: &BrushStroke, frame: &Frame, w: usize, h: usize, max if emitted >= max { break; } - let q = to_out(*p); + // (a point the warp can't place lays no dab, rather than one at its unwarped place) + let Some(q) = to_out(*p) else { continue }; let before = dabs.len(); if push_dabs_within(&mut dabs, &mut emitted, prev, q, r, max) { let pq = s.pressure_at(i); diff --git a/crates/pipeline/src/tests_mask_geometry.rs b/crates/pipeline/src/tests_mask_geometry.rs index 177d9c98bd..0a12171100 100644 --- a/crates/pipeline/src/tests_mask_geometry.rs +++ b/crates/pipeline/src/tests_mask_geometry.rs @@ -109,6 +109,17 @@ pub(crate) fn geometries() -> Vec<(&'static str, Box CanvasMap { CanvasMap { on_photo, ..self.clone() } } + /// Whether `norm` / `screen` go through a lens or perspective warp (a mask's coordinates on a + /// warped photo). + pub(crate) fn warped(&self) -> bool { + self.on_photo && self.warp.is_some() + } /// [`Self::for_mask`] for the mask `id` of `d` (none, or a new mask: on the photo). pub(crate) fn for_mask_id(&self, d: &lightcraft_develop::DevelopSettings, id: Option) -> CanvasMap { self.for_mask(id.and_then(|id| d.masks.iter().find(|m| m.id == id)).is_none_or(|m| m.on_photo)) @@ -442,4 +447,29 @@ mod mask_map_tests { assert!(close(m.for_mask_id(&d, None).norm(q), on)); assert!(close(m.for_mask_id(&d, Some(7)).norm(q), plain)); } + + /// A linear gradient's guides on a warped photo follow its bands through the warp: every + /// point drawn is on the band through the gradient's start or end (review of #3550). + #[test] + fn a_linear_gradients_guides_follow_the_warp() { + let mut d = DevelopSettings::default(); + d.geometry.vertical = 35.0; + d.optics.distortion = 50.0; + let frame = Frame::new(400, 300, &d, true); + let m = CanvasMap::new(&frame, Rect::from_min_size(pos2(100.0, 50.0), vec2(400.0, 300.0))).for_mask(true); + assert!(m.warped()); + let (start, end) = (Point::new(0.3, 0.3), Point::new(0.5, 0.6)); + // (long-edge units: the frame is 400 × 300) + let (sx, sy) = (1.0, 0.75); + let dd = ((end.x - start.x) * sx, (end.y - start.y) * sy); + let t = |q: Point| ((q.x - start.x) * sx * dd.0 + (q.y - start.y) * sy * dd.1) / (dd.0 * dd.0 + dd.1 * dd.1); + for (at, want) in [(start, 0.0), (end, 1.0)] { + let guide = super::super::masks::linear_guide(&m, start, end, at); + assert!(guide.len() > 2, "drawn in steps"); + for g in guide.iter().filter(|g| m.rect.expand(-2.0).contains(**g)) { + let got = t(m.norm(*g)); + assert!((got - want).abs() < 1e-3, "{g:?}: band {got}, want {want}"); + } + } + } } diff --git a/crates/ui-egui/src/panels/detail/masks.rs b/crates/ui-egui/src/panels/detail/masks.rs index 1cd0c589db..8aae1daf11 100644 --- a/crates/ui-egui/src/panels/detail/masks.rs +++ b/crates/ui-egui/src/panels/detail/masks.rs @@ -418,11 +418,9 @@ fn component_outline( } MaskShape::Linear { start, end } => { let (a, b) = (map.screen(*start), map.screen(*end)); - let dir = (b - a).normalized(); - let perp = vec2(-dir.y, dir.x) * 3000.0; let (a1, a2): (u8, u8) = if sel { (230, 110) } else { (170, 80) }; - for (q, alpha) in [(a, a1), (b, a2)] { - p.line_segment([q - perp, q + perp], Stroke::new(1.0, Color32::from_white_alpha(alpha))); + for (at, alpha) in [(*start, a1), (*end, a2)] { + p.add(egui::Shape::line(linear_guide(map, *start, *end, at), Stroke::new(1.0, Color32::from_white_alpha(alpha)))); } if sel { for (i, q) in [a, b].iter().enumerate() { @@ -436,6 +434,34 @@ fn component_outline( } } +/// A linear gradient's guide through `at` (its start or end): the line at right angles to it, as +/// screen points. In a mask's coordinates on the photo, a lens or perspective warp bends and +/// turns it on screen, so it is drawn through the warp in steps; else it is a straight line. +pub(super) fn linear_guide(map: &CanvasMap, start: Point, end: Point, at: Point) -> Vec { + let (a, b) = (map.screen(start), map.screen(end)); + if !map.warped() { + let dir = (b - a).normalized(); + let perp = vec2(-dir.y, dir.x) * 3000.0; + let q = map.screen(at); + return vec![q - perp, q + perp]; + } + // in long-edge units the perpendicular is a right angle; it spans the whole photo + let l = frame_long_norm(map); + let d = ((end.x - start.x) / l.0, (end.y - start.y) / l.1); + let n = d.0.hypot(d.1); + if !(n.is_finite() && n > 1e-12) { + return vec![a, a]; + } + let perp = (-d.1 / n * l.0, d.0 / n * l.1); + const STEPS: usize = 48; + (0..=STEPS) + .map(|i| { + let t = -1.5 + 3.0 * i as f64 / STEPS as f64; + map.screen(Point::new(at.x + perp.0 * t, at.y + perp.1 * t)) + }) + .collect() +} + /// Object selections (SAM 3) of the selected mask: each click, green to include, red to exclude /// (clicks still on their way to the model included). fn object_marks(app: &LightcraftApp, p: &egui::Painter, map: &CanvasMap, d: &DevelopSettings, active: Option) { @@ -650,13 +676,18 @@ fn grip_gestures( matches!(m.components.first()?.shape, MaskShape::Linear { .. }).then_some((m.id, 0, 0)) }); if let Some((mask, comp, handle)) = grip - && let Some(original_shape) = d.masks.iter().find(|m| m.id == mask).and_then(|m| m.components.get(comp)).map(|c| c.shape.clone()) + && d.masks.iter().find(|m| m.id == mask).and_then(|m| m.components.get(comp)).is_some() { if Some(mask) != active { let _ = app.run("mask.select", json!({"id": mask})); } let _ = app.run("develop.beginInteraction", json!({"label": "Edit Mask"})); - app.gesture = Some(Gesture::MaskHandle { mask, comp, handle, original_shape }); + // (read after it begins: an old mask is on the photo from then on, in its coordinates) + if let Some(original_shape) = current_shape(app, mask, comp) + .or_else(|| d.masks.iter().find(|m| m.id == mask).and_then(|m| m.components.get(comp)).map(|c| c.shape.clone())) + { + app.gesture = Some(Gesture::MaskHandle { mask, comp, handle, original_shape }); + } } } if resp.dragged() @@ -712,14 +743,32 @@ fn mask_keys(app: &mut LightcraftApp, ui: &mut egui::Ui, map: &CanvasMap, d: &De if app.session.active_mask != Some(mid) { let _ = app.run("mask.select", json!({"id": mid})); } - let to = Point::new(at.x + dx, at.y + dy); - let new_shape = moved_shape(shape, handle, Point::new(dx, dy), to, map, false); let _ = app.run("develop.beginInteraction", json!({"label": "Edit Mask"})); + // (read after it begins: an old mask is on the photo from then on, in its coordinates) + let (shape, map) = match (current_shape(app, mid, ci), current_on_photo(app, mid)) { + (Some(s), Some(on)) => (s, map.for_mask(on)), + _ => (shape.clone(), map.clone()), + }; + let at = map.norm(q); + let to = Point::new(at.x + dx, at.y + dy); + let new_shape = moved_shape(&shape, handle, Point::new(dx, dy), to, &map, false); let _ = app.run("mask.update", json!({"id": mid, "component": ci, "shape": new_shape})); let _ = app.run("develop.endInteraction", json!({})); } } +/// The shape of component `comp` of mask `mask` of the active photo as the session has it now. +fn current_shape(app: &LightcraftApp, mask: u32, comp: usize) -> Option { + let d = app.session.develop_of(app.session.active()?)?; + d.masks.iter().find(|m| m.id == mask)?.components.get(comp).map(|c| c.shape.clone()) +} + +/// Whether mask `mask` of the active photo is on the photo now (`Mask::on_photo`). +fn current_on_photo(app: &LightcraftApp, mask: u32) -> Option { + let d = app.session.develop_of(app.session.active()?)?; + d.masks.iter().find(|m| m.id == mask).map(|m| m.on_photo) +} + /// Long-edge units → normalized units for x and y. fn frame_long_norm(map: &CanvasMap) -> (f64, f64) { // derive from the mapping: 1 normalized unit in x and y in screen points (of the frame's From d55b2a56dff9e86f416ee0263b751f96a3481047 Mon Sep 17 00:00:00 2001 From: r3d42-git <279915996+r3d42-git@users.noreply.github.com> Date: Sun, 1 Nov 2026 19:42:08 +0100 Subject: [PATCH 32/51] LR-EDIT-OPTICS-PROFILE: prefer usable calibration on equal matches Prefer a known calibration crop only on exact lens-score ties, preserving higher-score selection. Keep hostile numeric model fixtures active with valid calibration crop. Co-authored-by: Codex --- crates/lensdb/src/lib.rs | 2 +- crates/lensdb/src/tests.rs | 48 +++++++++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index 4569fdcdc5..93ee3ede46 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -206,7 +206,7 @@ impl LensDb { { score *= 0.8; } - if score >= 0.6 && best.is_none_or(|(s, _)| score > s) { + if score >= 0.6 && best.is_none_or(|(s, b)| score > s || (score == s && b.crop_factor.is_none() && lens.crop_factor.is_some())) { best = Some((score, lens)); } } diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs index 9776137039..13cef0e478 100644 --- a/crates/lensdb/src/tests.rs +++ b/crates/lensdb/src/tests.rs @@ -303,6 +303,52 @@ fn crop_xml(parent: &str, calibration: &str) -> String { ) } +#[test] +fn tied_lens_matches_prefer_a_known_calibration_crop() { + let name = spec_name([50.0, 50.0, 1.8, 1.8]); + for (parent, unknown, known) in [ + ("", "", r#"cropfactor="1""#), + ("bad", "", r#"cropfactor="1""#), + ("1", r#"cropfactor="bad""#, ""), + ("1", r#"cropfactor="bad""#, r#"cropfactor="1""#), + ] { + let calibration = |attributes: &str| format!(r#""#); + for (first, second) in [(unknown, known), (known, unknown)] { + let xml = format!( + "Test{name}{parent}{}{}", + calibration(first), + calibration(second), + ); + let db = LensDb::parse(&xml).unwrap(); + assert_eq!(db.lenses.len(), 2); + assert_eq!(db.lenses.iter().filter(|p| p.crop_factor.is_none()).count(), 1); + for profile in [db.find_lens(None, &name, None), db.find_lens_named(None, &name, None)] { + let profile = profile.unwrap(); + assert_eq!(profile.crop_factor, Some(1.0), "{parent}: {first}, {second}"); + assert!(profile.correction(&shot(50.0)).is_some()); + } + } + } +} + +#[test] +fn a_higher_lens_score_still_wins_with_unknown_crop() { + let mut db = db(); + let camera = db.cameras[0].clone(); + let mut unknown = db.lenses[0].clone(); + unknown.crop_factor = None; + let mut known = unknown.clone(); + known.crop_factor = Some(1.0); + known.mounts = vec!["Other Mount".into()]; + let name = unknown.models[0].clone(); + for lenses in [vec![unknown.clone(), known.clone()], vec![known, unknown]] { + db.lenses = lenses; + for profile in [db.find_lens(None, &name, Some(&camera)), db.find_lens_named(None, &name, Some(&camera))] { + assert_eq!(profile.unwrap().crop_factor, None, "the better mount match outranks crop availability"); + } + } +} + #[test] fn unknown_calibration_crop_is_not_assumed_full_frame() { for parent in [ @@ -423,7 +469,7 @@ fn hostile_numbers_give_finite_corrections_or_none() { r#"MM 1{v}{v}"# ), format!( - r#"MM 1"# + r#"MM 1"# ), format!( r#"MM 11"# From 749a63ba58dc164f7b279363a9bceb13e57b1d63 Mon Sep 17 00:00:00 2001 From: OneSlips Date: Mon, 2 Nov 2026 06:46:00 +1000 Subject: [PATCH 33/51] LR-MASK-GEOMETRY: review fixes: kept masks stay put and turn with a synced orientation, off-photo clicks refused - develop_op attaches each old mask from the frame it was drawn on: a mask the photo keeps under its settings before the edit, a mask the edit brings in under the settings it came with (a preset that adds a mask and changes Transform no longer moves the kept one). - A sync or paste that changes the orientation (Crop group without Masks) turns the masks the photo keeps, as Rotate does; Rotate still turns them once. - mask.objectPoint refuses a click on an old Object mask that lands off the photo instead of moving it onto the photo's edge (a hair of rounding still counts as on it). - A linear gradient's guide is cut where the warp can't be undone, so no line joins a warped point to an unwarped one; a handle drag maps the pointer with the mask's live on_photo on the frame its interaction began. - Tests: kept vs brought-in masks, orientation change, off-photo click, pending clicks in mask coordinates (with a model build), guide runs, the detail-pass fallback takes the coarse selection. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/masks.rs | 9 +- crates/engine/src/lib.rs | 46 ++++-- crates/engine/src/tests/mask_geometry.rs | 141 +++++++++++++++++++ crates/engine/src/tests/segment.rs | 2 +- crates/pipeline/src/mask_space.rs | 7 +- crates/ui-egui/src/panels/detail/geometry.rs | 27 +++- crates/ui-egui/src/panels/detail/masks.rs | 44 ++++-- 7 files changed, 248 insertions(+), 28 deletions(-) diff --git a/crates/engine/src/cmd/masks.rs b/crates/engine/src/cmd/masks.rs index 4f5d4aeb06..171435e22b 100644 --- a/crates/engine/src/cmd/masks.rs +++ b/crates/engine/src/cmd/masks.rs @@ -470,10 +470,15 @@ pub fn specs() -> Vec { let mut at = at; if let Some(cur) = s.develop_of(id).filter(|d| d.masks.iter().any(|m| m.id == mid && !m.on_photo)) { if let Some(f) = s.mask_frame(id, &cur) { + // a click on the corrected frame's blank space is off the photo, not on + // its edge (a hair past it, from rounding, is on the edge) + const EDGE: f64 = 1e-6; let n = f.for_mask(true).norm_to_mask(at); - if n.x.is_finite() && n.y.is_finite() { - at = Point::new(n.x.clamp(0.0, 1.0), n.y.clamp(0.0, 1.0)); + let on = |v: f64| v.is_finite() && (-EDGE..=1.0 + EDGE).contains(&v); + if !(on(n.x) && on(n.y)) { + return Err(bad(c, "the point is outside the photo")); } + at = Point::new(n.x.clamp(0.0, 1.0), n.y.clamp(0.0, 1.0)); } let mut on = (*cur).clone(); s.attach_masks(id, &cur, &mut on); diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index f0c502bf13..eea895fcaf 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1027,14 +1027,25 @@ impl Session { // old masks (on the corrected frame) go onto the photo, from the frame they were drawn on: // masks this edit kept, under the photo's settings before it (a geometry edit then moves // the photo, not them); masks it brought in (paste, sync, a preset, a history step, a - // sidecar), under the settings they came with, where they render now - if settings.masks.iter().any(|m| !m.on_photo) { - let before = match &self.interaction { - Some(i) if i.photo == id => Some(i.original.clone()), - _ => self.develop_of(id), - }; - let drawn = before.filter(|b| b.masks == settings.masks).unwrap_or_else(|| Arc::new(settings.clone())); - self.attach_masks(id, &drawn, &mut settings); + // sidecar), under the settings they came with, where they render now. Kept masks also + // turn with an orientation change the edit makes (a sync or paste of the Crop group). + let before = match &self.interaction { + Some(i) if i.photo == id => Some(i.original.clone()), + _ => self.develop_of(id), + }; + let turn = before.as_ref().and_then(|b| orientation_change(b.orientation, settings.orientation)); + if settings.masks.iter().any(|m| !m.on_photo) || turn.is_some() { + let kept_frame = before.as_ref().and_then(|b| self.mask_frame(id, b)); + let new_frame = self.mask_frame(id, &settings); + for m in &mut settings.masks { + let kept = before.as_ref().is_some_and(|b| b.masks.contains(m)); + if let Some(f) = if kept { kept_frame.as_ref() } else { new_frame.as_ref() } { + lightcraft_pipeline::mask_space::attach(m, f); + } + if let (true, Some(t)) = (kept, turn) { + lightcraft_pipeline::mask_space::turn(m, t); + } + } } let settings = Arc::new(settings); let step = lightcraft_catalog::HistoryStep { label: label.into(), settings: settings.clone() }; @@ -1420,3 +1431,22 @@ mod tests; use tests::face_recognize as tests_face_recognize; #[cfg(test)] use tests::xmp as tests_xmp; + +/// The turn or flip that takes orientation `from` to `to` (`None` when they are the same): what the +/// masks, on the oriented photo, must follow ([`lightcraft_pipeline::mask_space::turn`]). +pub(crate) fn orientation_change(from: lightcraft_geom::Orientation, to: lightcraft_geom::Orientation) -> Option { + use lightcraft_geom::Orientation; + if from == to { + return None; + } + let inv = from.inverse(); + let probes = [(0.2, 0.3), (0.7, 0.1), (0.4, 0.9)]; + let close = |a: (f64, f64), b: (f64, f64)| (a.0 - b.0).abs() < 1e-9 && (a.1 - b.1).abs() < 1e-9; + (0..8u8).map(|k| Orientation::from_parts(k >= 4, k % 4)).find(|t| { + probes.iter().all(|&(x, y)| { + let (rx, ry) = inv.map(x, y, 1.0, 1.0); + let (tx, ty) = from.map(rx, ry, 1.0, 1.0); + close(t.map(tx, ty, 1.0, 1.0), to.map(rx, ry, 1.0, 1.0)) + }) + }) +} diff --git a/crates/engine/src/tests/mask_geometry.rs b/crates/engine/src/tests/mask_geometry.rs index 4a1044450f..c2a1edef85 100644 --- a/crates/engine/src/tests/mask_geometry.rs +++ b/crates/engine/src/tests/mask_geometry.rs @@ -172,3 +172,144 @@ fn a_click_on_an_old_object_mask_goes_onto_the_photo_with_it() { let want = frame.for_mask(true).norm_to_mask(p); assert!((hint[0].x - want.x).abs() < 1e-9 && (hint[0].y - want.y).abs() < 1e-9, "{hint:?} vs {want:?}"); } + +/// An old mask the photo keeps, when an edit both brings in a mask and changes the geometry (a +/// preset), goes onto the photo from the photo's settings before the edit, where it rendered; the +/// mask brought in, from the settings it came with (review of #3550). +#[test] +fn a_kept_old_mask_attaches_under_the_settings_before_an_edit_that_adds_one() { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + let mut d = active_dev(&s); + d.geometry.vertical = 30.0; + d.geometry.rotate = 5.0; + let p = Point::new(0.3, 0.4); + let mut kept = radial_at(p); + kept.on_photo = false; + d.masks = vec![kept]; + s.catalog.apply(lightcraft_catalog::Op::SetDevelop { id, settings: std::sync::Arc::new(d.clone()), label: "Old".into(), edited: None }).unwrap(); + // the preset: another Transform and a new old mask + let mut incoming = d.clone(); + incoming.geometry.vertical = -20.0; + incoming.geometry.rotate = 0.0; + let q = Point::new(0.6, 0.6); + let mut added = radial_at(q); + added.id = 2; + added.on_photo = false; + incoming.masks.push(added); + s.set_develop(id, incoming.clone(), "Preset").unwrap(); + let now = active_dev(&s); + assert!(now.masks.iter().all(|m| m.on_photo)); + let photo = s.catalog.photo(id).unwrap(); + let frame = |d: &DevelopSettings| lightcraft_pipeline::geometry::Frame::with_lens(photo.width as usize, photo.height as usize, d, false, None); + let at = |m: &Mask| match &m.components[0].shape { + MaskShape::Radial { center, .. } => *center, + other => panic!("{other:?}"), + }; + let back = frame(&d).for_mask(true).mask_to_norm(at(&now.masks[0])); + assert!((back.x - p.x).abs() < 1e-6 && (back.y - p.y).abs() < 1e-6, "the kept mask, from the frame it was drawn on: {back:?}"); + let back = frame(&incoming).for_mask(true).mask_to_norm(at(&now.masks[1])); + assert!((back.x - q.x).abs() < 1e-6 && (back.y - q.y).abs() < 1e-6, "the added mask, from its own: {back:?}"); +} + +/// A sync or paste of the Crop group without the Masks group turns the photo: its masks turn with +/// it, as Rotate does (review of #3550). +#[test] +fn masks_turn_with_an_orientation_a_sync_brings() { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + let mut d = active_dev(&s); + let p = Point::new(0.2, 0.3); + d.masks = vec![radial_at(p)]; + s.set_develop(id, d.clone(), "Mask").unwrap(); + let mut synced = d.clone(); + synced.orientation = d.orientation.rotated(true); + s.set_develop(id, synced, "Sync Settings").unwrap(); + let (c, a) = centre(&active_dev(&s)); + assert!((c.x - 0.7).abs() < 1e-9 && (c.y - 0.2).abs() < 1e-9 && a == 110.0, "{c:?} {a}"); + // Rotate itself still turns them once + s.execute("photo.rotateLeft", &json!({})).unwrap(); + let (c, a) = centre(&active_dev(&s)); + assert!((c.x - p.x).abs() < 1e-9 && (c.y - p.y).abs() < 1e-9 && a == 20.0, "{c:?} {a}"); +} + +/// The change between two orientations is the turn Rotate makes, and none between equal ones. +#[test] +fn the_change_between_orientations_is_the_turn_made() { + use lightcraft_geom::Orientation; + for k in 0..8u8 { + let o = Orientation::from_parts(k >= 4, k % 4); + assert_eq!(crate::orientation_change(o, o), None); + for cw in [true, false] { + assert_eq!(crate::orientation_change(o, o.rotated(cw)), Some(Orientation::Normal.rotated(cw)), "{o:?} {cw}"); + } + } +} + +/// An old Object mask with a Transform active, and a click. +fn old_object_mask() -> (Session, lightcraft_catalog::PhotoId, DevelopSettings, Point) { + let mut s = Session::with_demo(); + let id = s.active().unwrap(); + let mut d = active_dev(&s); + d.geometry.vertical = 30.0; + d.geometry.rotate = 5.0; + let p = Point::new(0.3, 0.4); + d.masks = vec![Mask { + id: 1, + on_photo: false, + components: vec![MaskComponent { + name: None, + op: MaskOp::Add, + invert: false, + shape: MaskShape::Object { hint: vec![p], exclude: Vec::new(), seg: None, detail: Vec::new(), edge: 0.0 }, + }], + ..Default::default() + }]; + s.catalog.apply(lightcraft_catalog::Op::SetDevelop { id, settings: std::sync::Arc::new(d.clone()), label: "Old".into(), edited: None }).unwrap(); + s.active_mask = Some(1); + (s, id, d, p) +} + +/// A click on an old Object mask that lands off the photo (the corrected frame's blank space) is +/// refused, not moved onto the photo's edge, and leaves the mask as it was (review of #3550). +#[test] +fn a_click_off_the_photo_on_an_old_object_mask_is_refused() { + let (mut s, id, d, _) = old_object_mask(); + let photo = s.catalog.photo(id).unwrap(); + let frame = lightcraft_pipeline::geometry::Frame::with_lens(photo.width as usize, photo.height as usize, &d, false, None); + let f = frame.for_mask(true); + let off = [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0), (0.5, 0.0), (0.5, 1.0)] + .map(|(x, y)| Point::new(x, y)) + .into_iter() + .find(|p| { + let n = f.norm_to_mask(*p); + !(n.x.is_finite() && n.y.is_finite()) || n.x < -0.01 || n.x > 1.01 || n.y < -0.01 || n.y > 1.01 + }) + .expect("a corner of the transformed frame is off the photo"); + let e = s.execute("mask.objectPoint", &json!({"x": off.x, "y": off.y})).unwrap_err().to_string(); + assert!(e.contains("outside the photo"), "{e}"); + assert_eq!(active_dev(&s).masks, d.masks, "nothing changed"); +} + +/// The click on its way to the model is in the mask's coordinates with the stored ones (with a +/// model folder that looks installed; skipped where the build has no model support). +#[test] +fn a_pending_click_on_an_old_object_mask_is_in_the_masks_coordinates() { + if !crate::segment::Segmenter::AVAILABLE { + lightcraft_testkit::skip!(Model, "built without the segmentation model"); + return; + } + let (mut s, id, d, p) = old_object_mask(); + s.segmenter.background = true; + s.segmenter.dir = Some(super::segment::damaged_model("mask-geometry-pending")); + let r = s.execute("mask.objectPoint", &json!({"x": 0.6, "y": 0.5})).unwrap(); + assert_eq!(r["pending"], true); + let photo = s.catalog.photo(id).unwrap(); + let frame = lightcraft_pipeline::geometry::Frame::with_lens(photo.width as usize, photo.height as usize, &d, false, None); + let f = frame.for_mask(true); + let pending = s.segmenter.pending_clicks().expect("the click is pending").clone(); + assert_eq!(pending.hint.len(), 2); + for (got, want) in pending.hint.iter().zip([f.norm_to_mask(p), f.norm_to_mask(Point::new(0.6, 0.5))]) { + assert!((got.x - want.x).abs() < 1e-9 && (got.y - want.y).abs() < 1e-9, "{got:?} vs {want:?}"); + } +} diff --git a/crates/engine/src/tests/segment.rs b/crates/engine/src/tests/segment.rs index ceb2365fbe..b649dbdd7e 100644 --- a/crates/engine/src/tests/segment.rs +++ b/crates/engine/src/tests/segment.rs @@ -17,7 +17,7 @@ fn tmp(name: &str) -> std::path::PathBuf { } /// A folder that looks installed but holds no usable model. -fn damaged_model(name: &str) -> std::path::PathBuf { +pub(super) fn damaged_model(name: &str) -> std::path::PathBuf { let d = tmp(name); std::fs::create_dir_all(&d).unwrap(); std::fs::write(d.join("model.safetensors"), b"\x10\x00\x00\x00\x00\x00\x00\x00{not json at all}").unwrap(); diff --git a/crates/pipeline/src/mask_space.rs b/crates/pipeline/src/mask_space.rs index d5858ab686..cb8853d795 100644 --- a/crates/pipeline/src/mask_space.rs +++ b/crates/pipeline/src/mask_space.rs @@ -428,7 +428,7 @@ mod tests { s.geometry.rotate = 20.0; let frame = Frame::new(300, 200, &s, false); let detail = SegMask::from_logits_in(SIDE, &[8.0; SIDE * SIDE], [0.4, 0.4, 0.6, 0.6]); - let coarse = SegMask::from_logits(SIDE, &[-8.0; SIDE * SIDE]); + let coarse = SegMask::from_logits(SIDE, &[8.0; SIDE * SIDE]); let mut m = mask(MaskShape::Object { hint: vec![Point::new(0.5, 0.5)], exclude: Vec::new(), seg: Some(coarse), detail: vec![detail], edge: 0.0 }); m.on_photo = false; @@ -436,9 +436,10 @@ mod tests { let MaskShape::Object { detail, .. } = &m.components[0].shape else { panic!() }; let g = SegGrid::new(&detail[0]).unwrap(); let b = detail[0].bounds().unwrap(); - // a corner of the new box is outside the turned patch: unselected there, selected inside + // a corner of the new box is outside the turned patch: it takes the whole-photo selection + // (selected here), not a fixed "unselected" let inset = |t: f64| Point::new(b[0] + t * (b[2] - b[0]), b[1] + t * (b[3] - b[1])); - assert!(g.logit(inset(0.03)) < 0.0, "{}", g.logit(inset(0.03))); + assert!(g.logit(inset(0.03)) > 0.0, "{}", g.logit(inset(0.03))); assert!(g.logit(inset(0.5)) > 0.0); } diff --git a/crates/ui-egui/src/panels/detail/geometry.rs b/crates/ui-egui/src/panels/detail/geometry.rs index d8fa401cd4..bfee31813d 100644 --- a/crates/ui-egui/src/panels/detail/geometry.rs +++ b/crates/ui-egui/src/panels/detail/geometry.rs @@ -49,10 +49,19 @@ impl CanvasMap { } } pub fn screen(&self, n: Point) -> Pos2 { + self.try_screen(n).unwrap_or_else(|| self.place(n)) + } + /// [`Self::screen`], or `None` where the warp can't be undone at `n` (a point a strong + /// warp folds away), for drawing that must not join such a point to its neighbours. + pub(crate) fn try_screen(&self, n: Point) -> Option { let n = match self.warp.as_deref().filter(|_| self.on_photo) { - Some((w, ow, oh)) => w.from_source(Point::new(n.x * ow, n.y * oh)).map_or(n, |t| Point::new(t.x / ow, t.y / oh)), + Some((w, ow, oh)) => w.from_source(Point::new(n.x * ow, n.y * oh)).map(|t| Point::new(t.x / ow, t.y / oh))?, None => n, }; + Some(self.place(n)) + } + /// A point on the displayed frame (normalized) on screen. + fn place(&self, n: Point) -> Pos2 { let q = self.from_norm.apply(n); pos2(self.rect.left() + q.x as f32 / self.k, self.rect.top() + q.y as f32 / self.k) } @@ -464,12 +473,24 @@ mod mask_map_tests { let dd = ((end.x - start.x) * sx, (end.y - start.y) * sy); let t = |q: Point| ((q.x - start.x) * sx * dd.0 + (q.y - start.y) * sy * dd.1) / (dd.0 * dd.0 + dd.1 * dd.1); for (at, want) in [(start, 0.0), (end, 1.0)] { - let guide = super::super::masks::linear_guide(&m, start, end, at); + let guide: Vec = super::super::masks::linear_guide(&m, start, end, at).concat(); assert!(guide.len() > 2, "drawn in steps"); - for g in guide.iter().filter(|g| m.rect.expand(-2.0).contains(**g)) { + let inside: Vec<&Pos2> = guide.iter().filter(|g| m.rect.expand(-2.0).contains(**g)).collect(); + assert!(inside.len() > 2, "the guide crosses the canvas: {}", inside.len()); + for g in inside { let got = t(m.norm(*g)); assert!((got - want).abs() < 1e-3, "{g:?}: band {got}, want {want}"); } } } + + /// A guide is cut where a sample can't go through the warp, so no line joins a warped point + /// to an unwarped one (review of #3550). + #[test] + fn a_guide_breaks_where_the_warp_cant_be_undone() { + let (a, b, c) = (pos2(1.0, 1.0), pos2(2.0, 2.0), pos2(3.0, 3.0)); + let runs = super::super::masks::runs([Some(a), Some(b), None, Some(c), None, None, Some(a), Some(b)]); + assert_eq!(runs, vec![vec![a, b], vec![a, b]], "single points are dropped, the rest split"); + assert!(super::super::masks::runs([None, Some(a), None]).is_empty()); + } } diff --git a/crates/ui-egui/src/panels/detail/masks.rs b/crates/ui-egui/src/panels/detail/masks.rs index 8aae1daf11..49c8692688 100644 --- a/crates/ui-egui/src/panels/detail/masks.rs +++ b/crates/ui-egui/src/panels/detail/masks.rs @@ -420,7 +420,9 @@ fn component_outline( let (a, b) = (map.screen(*start), map.screen(*end)); let (a1, a2): (u8, u8) = if sel { (230, 110) } else { (170, 80) }; for (at, alpha) in [(*start, a1), (*end, a2)] { - p.add(egui::Shape::line(linear_guide(map, *start, *end, at), Stroke::new(1.0, Color32::from_white_alpha(alpha)))); + for run in linear_guide(map, *start, *end, at) { + p.add(egui::Shape::line(run, Stroke::new(1.0, Color32::from_white_alpha(alpha)))); + } } if sel { for (i, q) in [a, b].iter().enumerate() { @@ -437,29 +439,48 @@ fn component_outline( /// A linear gradient's guide through `at` (its start or end): the line at right angles to it, as /// screen points. In a mask's coordinates on the photo, a lens or perspective warp bends and /// turns it on screen, so it is drawn through the warp in steps; else it is a straight line. -pub(super) fn linear_guide(map: &CanvasMap, start: Point, end: Point, at: Point) -> Vec { +pub(super) fn linear_guide(map: &CanvasMap, start: Point, end: Point, at: Point) -> Vec> { let (a, b) = (map.screen(start), map.screen(end)); if !map.warped() { let dir = (b - a).normalized(); let perp = vec2(-dir.y, dir.x) * 3000.0; let q = map.screen(at); - return vec![q - perp, q + perp]; + return vec![vec![q - perp, q + perp]]; } // in long-edge units the perpendicular is a right angle; it spans the whole photo let l = frame_long_norm(map); let d = ((end.x - start.x) / l.0, (end.y - start.y) / l.1); let n = d.0.hypot(d.1); if !(n.is_finite() && n > 1e-12) { - return vec![a, a]; + return Vec::new(); } let perp = (-d.1 / n * l.0, d.0 / n * l.1); const STEPS: usize = 48; - (0..=STEPS) - .map(|i| { - let t = -1.5 + 3.0 * i as f64 / STEPS as f64; - map.screen(Point::new(at.x + perp.0 * t, at.y + perp.1 * t)) - }) - .collect() + runs((0..=STEPS).map(|i| { + let t = -1.5 + 3.0 * i as f64 / STEPS as f64; + map.try_screen(Point::new(at.x + perp.0 * t, at.y + perp.1 * t)) + })) +} + +/// The unbroken runs (two points or more) of a sampled line whose missing samples (`None`) cut it. +pub(super) fn runs(samples: impl IntoIterator>) -> Vec> { + let mut out: Vec> = Vec::new(); + let mut cur: Vec = Vec::new(); + for s in samples { + match s { + Some(q) => cur.push(q), + None => { + if cur.len() >= 2 { + out.push(std::mem::take(&mut cur)); + } + cur.clear(); + } + } + } + if cur.len() >= 2 { + out.push(cur); + } + out } /// Object selections (SAM 3) of the selected mask: each click, green to include, red to exclude @@ -693,7 +714,8 @@ fn grip_gestures( if resp.dragged() && let (Some(Gesture::MaskHandle { mask, comp, handle, original_shape }), Some(q)) = (app.gesture.clone(), resp.interact_pointer_pos()) { - let map = &map.for_mask_id(d, Some(mask)); + // (the live flag: the drag's first frame began the interaction, which put an old mask on the photo) + let map = ¤t_on_photo(app, mask).map_or_else(|| map.for_mask_id(d, Some(mask)), |on| map.for_mask(on)); let n = map.norm(q); let press_origin = ui.input(|i| i.pointer.press_origin()).unwrap_or(q); let n0 = map.norm(press_origin); From d4ccd82d5600dc0d2cc25b90505013bd0285fbca Mon Sep 17 00:00:00 2001 From: OneSlips Date: Mon, 2 Nov 2026 07:53:40 +1000 Subject: [PATCH 34/51] LR-MASK-GEOMETRY: review fixes: guides reach the photo from off-photo ends and break at folds - A warped linear guide is sampled from where its line enters the photo to where it leaves it (with a margin), not a fixed 1.5 long-edge units around the end, so a gradient dragged off the photo still shows its guides; 48 to 256 steps. - A run is also cut where a step is far longer than its usual one (a chord across a fold). - The moved-detail-pass test checks a negative whole-photo selection as well as a positive one. Co-Authored-By: Claude Opus 5.5 --- crates/pipeline/src/mask_space.rs | 33 +++++++------- crates/ui-egui/src/panels/detail/geometry.rs | 22 ++++++++++ crates/ui-egui/src/panels/detail/masks.rs | 45 ++++++++++++++++++-- 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/crates/pipeline/src/mask_space.rs b/crates/pipeline/src/mask_space.rs index cb8853d795..36b998ab5e 100644 --- a/crates/pipeline/src/mask_space.rs +++ b/crates/pipeline/src/mask_space.rs @@ -420,27 +420,30 @@ mod tests { } /// Where a zoomed-in pass's new box reaches past what it covered (a turned box's corners), it - /// takes the whole-photo selection, not a copy of its own edge (review of #3550). + /// takes the whole-photo selection: neither a copy of its own edge (a negative whole-photo + /// selection) nor a fixed "unselected" (a positive one) (review of #3550). #[test] fn a_moved_detail_pass_takes_the_whole_selection_past_its_own_area() { const SIDE: usize = 16; let mut s = DevelopSettings::default(); s.geometry.rotate = 20.0; let frame = Frame::new(300, 200, &s, false); - let detail = SegMask::from_logits_in(SIDE, &[8.0; SIDE * SIDE], [0.4, 0.4, 0.6, 0.6]); - let coarse = SegMask::from_logits(SIDE, &[8.0; SIDE * SIDE]); - let mut m = - mask(MaskShape::Object { hint: vec![Point::new(0.5, 0.5)], exclude: Vec::new(), seg: Some(coarse), detail: vec![detail], edge: 0.0 }); - m.on_photo = false; - attach(&mut m, &frame); - let MaskShape::Object { detail, .. } = &m.components[0].shape else { panic!() }; - let g = SegGrid::new(&detail[0]).unwrap(); - let b = detail[0].bounds().unwrap(); - // a corner of the new box is outside the turned patch: it takes the whole-photo selection - // (selected here), not a fixed "unselected" - let inset = |t: f64| Point::new(b[0] + t * (b[2] - b[0]), b[1] + t * (b[3] - b[1])); - assert!(g.logit(inset(0.03)) > 0.0, "{}", g.logit(inset(0.03))); - assert!(g.logit(inset(0.5)) > 0.0); + for whole in [8.0f32, -8.0] { + let detail = SegMask::from_logits_in(SIDE, &[8.0; SIDE * SIDE], [0.4, 0.4, 0.6, 0.6]); + let coarse = SegMask::from_logits(SIDE, &[whole; SIDE * SIDE]); + let mut m = + mask(MaskShape::Object { hint: vec![Point::new(0.5, 0.5)], exclude: Vec::new(), seg: Some(coarse), detail: vec![detail], edge: 0.0 }); + m.on_photo = false; + attach(&mut m, &frame); + let MaskShape::Object { detail, .. } = &m.components[0].shape else { panic!() }; + let g = SegGrid::new(&detail[0]).unwrap(); + let b = detail[0].bounds().unwrap(); + // a corner of the new box is outside the turned patch: the whole-photo selection's sign + let inset = |t: f64| Point::new(b[0] + t * (b[2] - b[0]), b[1] + t * (b[3] - b[1])); + let corner = g.logit(inset(0.03)); + assert!((corner > 0.0) == (whole > 0.0), "whole {whole}: corner {corner}"); + assert!(g.logit(inset(0.5)) > 0.0, "whole {whole}: the patch itself stays selected"); + } } /// The box around a mapped rectangle holds the whole bent edge, also off the centre lines diff --git a/crates/ui-egui/src/panels/detail/geometry.rs b/crates/ui-egui/src/panels/detail/geometry.rs index bfee31813d..b02c5eb9c1 100644 --- a/crates/ui-egui/src/panels/detail/geometry.rs +++ b/crates/ui-egui/src/panels/detail/geometry.rs @@ -492,5 +492,27 @@ mod mask_map_tests { let runs = super::super::masks::runs([Some(a), Some(b), None, Some(c), None, None, Some(a), Some(b)]); assert_eq!(runs, vec![vec![a, b], vec![a, b]], "single points are dropped, the rest split"); assert!(super::super::masks::runs([None, Some(a), None]).is_empty()); + // a chord far longer than the usual step (across a fold) cuts the run too + let line: Vec = (0..10).map(|i| pos2(i as f32, 0.0)).chain((0..10).map(|i| pos2(200.0 + i as f32, 0.0))).collect(); + let cut = super::super::masks::split_jumps(vec![line]); + assert_eq!(cut.len(), 2, "{cut:?}"); + assert!(cut.iter().all(|r| r.len() == 10)); + } + + /// A guide through an end far off the photo still crosses the canvas (review of #3550). + #[test] + fn a_guide_through_an_end_off_the_photo_still_crosses_it() { + let mut d = DevelopSettings::default(); + d.geometry.vertical = 35.0; + d.optics.distortion = 50.0; + let frame = Frame::new(400, 300, &d, true); + let m = CanvasMap::new(&frame, Rect::from_min_size(pos2(100.0, 50.0), vec2(400.0, 300.0))).for_mask(true); + // a gradient drawn off the photo's right side (start and end both there): its guides are + // horizontal and cross the photo from 2.5 photo widths away + let (start, end) = (Point::new(3.5, 0.4), Point::new(3.5, 0.6)); + for at in [start, end] { + let guide: Vec = super::super::masks::linear_guide(&m, start, end, at).concat(); + assert!(guide.iter().filter(|g| m.rect.expand(-2.0).contains(**g)).count() > 2, "{at:?}: the guide crosses the canvas"); + } } } diff --git a/crates/ui-egui/src/panels/detail/masks.rs b/crates/ui-egui/src/panels/detail/masks.rs index 49c8692688..2150d513b8 100644 --- a/crates/ui-egui/src/panels/detail/masks.rs +++ b/crates/ui-egui/src/panels/detail/masks.rs @@ -455,11 +455,48 @@ pub(super) fn linear_guide(map: &CanvasMap, start: Point, end: Point, at: Point) return Vec::new(); } let perp = (-d.1 / n * l.0, d.0 / n * l.1); - const STEPS: usize = 48; - runs((0..=STEPS).map(|i| { - let t = -1.5 + 3.0 * i as f64 / STEPS as f64; + // from where the line enters the photo to where it leaves it (with a margin), measured from + // `at`, so a guide whose end lies off the photo still reaches it + let pp = perp.0 * perp.0 + perp.1 * perp.1; + let ts = [(0.0, 0.0), (1.0, 0.0), (0.0, 1.0), (1.0, 1.0)].map(|(x, y)| ((x - at.x) * perp.0 + (y - at.y) * perp.1) / pp); + let (lo, hi) = ts.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), t| (lo.min(*t), hi.max(*t))); + let pad = 0.1 * (hi - lo); + let (lo, hi) = (lo - pad, hi + pad); + if !(lo.is_finite() && hi.is_finite() && hi > lo) { + return Vec::new(); + } + // 48 steps across about a photo's width, at most 256 + let steps = ((hi - lo) * 16.0).ceil().clamp(48.0, 256.0) as usize; + split_jumps(runs((0..=steps).map(|i| { + let t = lo + (hi - lo) * i as f64 / steps as f64; map.try_screen(Point::new(at.x + perp.0 * t, at.y + perp.1 * t)) - })) + }))) +} + +/// `runs` cut again where a step is far longer than the run's usual one: a strong warp can fold +/// the line, and a chord across the fold isn't part of it. +pub(super) fn split_jumps(runs: Vec>) -> Vec> { + let mut out = Vec::new(); + for run in runs { + let mut steps: Vec = run.windows(2).filter_map(|w| Some(w.first()?.distance(*w.get(1)?))).collect(); + steps.sort_by(f32::total_cmp); + let usual = steps.get(steps.len() / 2).copied().unwrap_or(0.0); + let limit = (8.0 * usual).max(2.0); + let mut cur: Vec = Vec::new(); + for q in run { + if cur.last().is_some_and(|p: &Pos2| p.distance(q) > limit) { + if cur.len() >= 2 { + out.push(std::mem::take(&mut cur)); + } + cur.clear(); + } + cur.push(q); + } + if cur.len() >= 2 { + out.push(cur); + } + } + out } /// The unbroken runs (two points or more) of a sampled line whose missing samples (`None`) cut it. From d72f3840bcb627e326a504d3a6f5173266fd88ac Mon Sep 17 00:00:00 2001 From: r3d42-git <279915996+r3d42-git@users.noreply.github.com> Date: Mon, 2 Nov 2026 10:21:22 +0100 Subject: [PATCH 35/51] LR-EDIT-OPTICS-PROFILE: prefer known crop in fixed-lens score ties Use the same exact-score known-crop preference as best_lens in the built-in fixed-lens fallback. Preserve higher-score priority and cover both profile orders through the public lookup API. Validated with 33 lensdb and 5 engine lens-profile tests, plus full cargo xtask ci (8/8, Rust 1.95.0, Apple M5 Max Metal and WASM). The new tie regression fails against the previously published selector. No workflows or lockfile changes. Co-authored-by: Codex --- crates/lensdb/src/lib.rs | 2 +- crates/lensdb/src/tests.rs | 36 ++++++++++++++++++++++++++++++++++++ docs/lens-profiles.md | 2 ++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index 93ee3ede46..dd86413370 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -245,7 +245,7 @@ impl LensDb { continue; } let score = focals.iter().filter(|f| (lo..=hi).contains(*f)).count() as f64 / focals.len() as f64; - if score > 0.0 && best.is_none_or(|(s, _)| score > s) { + if score > 0.0 && best.is_none_or(|(s, b)| score > s || (score == s && b.crop_factor.is_none() && p.crop_factor.is_some())) { best = Some((score, p)); } } diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs index 13cef0e478..b0a880fc65 100644 --- a/crates/lensdb/src/tests.rs +++ b/crates/lensdb/src/tests.rs @@ -455,6 +455,42 @@ fn built_in_lens_matches_by_camera_mount_and_focal_range() { assert_eq!(exif_focal_range(&tokens(&huge)), Some((6.0, 6.0))); } +#[test] +fn tied_built_in_lens_matches_prefer_a_known_calibration_crop() { + let mut db = db(); + let cam = db.cameras[0].clone(); + let mut unknown = db.lenses[0].clone(); + unknown.models = vec!["Testcam Compact".into()]; + unknown.crop_factor = None; + let mut known = unknown.clone(); + known.crop_factor = Some(1.5); + for lenses in [vec![unknown.clone(), known.clone()], vec![known, unknown]] { + db.lenses = lenses; + let lens = db.find_lens(None, "24-70 mm f/2.8", Some(&cam)).unwrap(); + assert_eq!(lens.crop_factor, Some(1.5), "an exact focal-range score tie prefers the known crop in either order"); + assert!(lens.correction(&shot(24.0)).is_some()); + } +} + +#[test] +fn a_higher_built_in_lens_score_still_wins_with_unknown_crop() { + let mut db = db(); + let cam = db.cameras[0].clone(); + let mut unknown = db.lenses[0].clone(); + unknown.models = vec!["Testcam Compact".into()]; + unknown.crop_factor = None; + unknown.tca.clear(); + unknown.vignetting.clear(); + let mut known = unknown.clone(); + known.crop_factor = Some(1.5); + known.distortion[1].0 = 100.0; + for lenses in [vec![unknown.clone(), known.clone()], vec![known, unknown]] { + db.lenses = lenses; + let lens = db.find_lens(None, "24-70 mm f/2.8", Some(&cam)).unwrap(); + assert_eq!(lens.crop_factor, None, "the higher focal-range score remains the primary criterion in either order"); + } +} + /// A hand-edited or corrupt database may carry any number: infinities, NaN, enormous or negative /// coefficients, absurd crop factors and aspect ratios. Whatever loads must give corrections that /// are `None` or entirely finite, for any shot including nonsense ones; names of any shape find diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index fd7c1d329b..8906b7ff59 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -48,6 +48,8 @@ the image centre (lensfun's `
` offsets are not read). Missing or invalid only finite values from 0.05 through 20 are accepted. A calibration inherits the lens's crop factor only when its `cropfactor` attribute is omitted; an explicit invalid override remains unknown. Profiles with an unknown calibration crop factor are not applied. An explicit crop factor of 1 still means full frame. +On exact lookup-score ties, both named and built-in fixed-lens matching prefer a known calibration crop; a higher +match score still wins even when its crop is unknown. ## Limits From 6fee4fbacd8689a6e93f47132ba2b36969137a2d Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 17:46:15 +0000 Subject: [PATCH 36/51] security(lensdb): bound the names calibrations copy, read database files through the opened regular file --- crates/lensdb/src/lib.rs | 30 +++++++++++++++++++++++++----- crates/lensdb/src/tests.rs | 25 ++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index cf6b2ec145..e2910125c0 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -30,6 +30,9 @@ pub use model::{Distortion, Tca, Vignetting}; const MAX_FILE_BYTES: u64 = 16 << 20; /// Most files read from one directory. const MAX_FILES: usize = 2000; +/// Most bytes of lens names that profiles may copy per parsed document (each calibration owns a copy): a real +/// database copies about a megabyte. +const MAX_PROFILE_COPY_BYTES: usize = 32 << 20; /// Largest radial error (half-diagonal units) accepted when converting a distortion to the DNG polynomial. pub const MAX_WARP_ERROR: f64 = 0.001; /// Largest relative error accepted when converting vignetting to the DNG polynomial. @@ -144,7 +147,8 @@ impl LensDb { let root = xml::parse(xml)?; let Some(top) = root.kid("lensdatabase") else { return Err(Error::Xml("no element".into())) }; let cameras = top.kids("camera").filter_map(camera).collect(); - let lenses = top.kids("lens").flat_map(lens).collect(); + let mut budget = MAX_PROFILE_COPY_BYTES; + let lenses = top.kids("lens").flat_map(|n| lens(n, &mut budget)).collect(); Ok(LensDb { cameras, lenses }) } @@ -272,11 +276,22 @@ fn exif_focal_range(exif: &[String]) -> Option<(f64, f64)> { } fn read_capped(path: &Path) -> Result { - let len = std::fs::metadata(path).map_err(|e| Error::Io(e.to_string()))?.len(); - if len > MAX_FILE_BYTES { + use std::io::Read; + let io = |e: std::io::Error| Error::Io(e.to_string()); + // the opened file is checked, not the pathname: a link or FIFO swapped in after a pathname check is refused too + let file = std::fs::File::open(path).map_err(io)?; + let meta = file.metadata().map_err(io)?; + if !meta.is_file() { + return Err(Error::Io(format!("{}: not a regular file", path.display()))); + } + if meta.len() > MAX_FILE_BYTES { + return Err(Error::TooLarge); + } + let mut bytes = Vec::new(); + file.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes).map_err(io)?; + if bytes.len() as u64 > MAX_FILE_BYTES { return Err(Error::TooLarge); } - let bytes = std::fs::read(path).map_err(|e| Error::Io(e.to_string()))?; Ok(String::from_utf8_lossy(&bytes).into_owned()) } @@ -369,7 +384,7 @@ fn positive(v: Option) -> Option { } /// One `` element → one profile per `` (newer files give each its own crop factor and aspect). -fn lens(n: &xml::Node) -> Vec { +fn lens(n: &xml::Node, copy_budget: &mut usize) -> Vec { let Some(maker) = n.kid_text("maker") else { return Vec::new() }; let models: Vec = n.kids("model").map(|m| m.text.trim().to_string()).filter(|m| !m.is_empty()).collect(); if models.is_empty() { @@ -379,7 +394,12 @@ fn lens(n: &xml::Node) -> Vec { let aspect = aspect_of(n.kid_text("aspect-ratio")).unwrap_or(1.5); let rectilinear = n.kid_text("type").is_none_or(|t| t.eq_ignore_ascii_case("rectilinear")); let mounts = n.kids("mount").map(|m| m.text.trim().to_string()).filter(|m| !m.is_empty()).collect::>(); + // every calibration owns a copy of the lens's names: calibrations past the document's shared copy budget are left out + let meta_bytes = maker.len() + models.iter().chain(&mounts).map(|m| m.len() + 1).sum::() + 1; + let take = n.kids("calibration").count().min(*copy_budget / meta_bytes); + *copy_budget -= take * meta_bytes; n.kids("calibration") + .take(take) .map(|cal| { let mut p = Profile { maker: maker.to_string(), diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs index 71c9a4a0eb..033111de7f 100644 --- a/crates/lensdb/src/tests.rs +++ b/crates/lensdb/src/tests.rs @@ -232,7 +232,8 @@ fn fisheyes_get_no_rectilinear_warp_and_bad_shots_none() { #[test] fn load_dir_reads_xml_files_and_reports_bad_ones() { let dir = std::env::temp_dir().join(format!("lensdb-test-{}", std::process::id())); - std::fs::create_dir_all(&dir).unwrap(); + // exclusive: an existing (possibly planted) entry fails the test instead of being written through + std::fs::create_dir(&dir).unwrap(); std::fs::write(dir.join("a.xml"), DB).unwrap(); std::fs::write(dir.join("b.xml"), ""; + let xml = format!("M{name}{}", cal.repeat(2000)); + let db = LensDb::parse(&xml).unwrap(); + assert!(!db.lenses.is_empty()); + assert!(db.lenses.len() * 400_000 <= MAX_PROFILE_COPY_BYTES); +} + +#[cfg(unix)] +#[test] +fn load_dir_refuses_a_non_regular_xml_entry() { + let dir = std::env::temp_dir().join(format!("lensdb-test-dev-{}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + std::os::unix::fs::symlink("/dev/zero", dir.join("z.xml")).unwrap(); + let (db, errs) = LensDb::load_dir(&dir).unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(db.is_empty()); + assert_eq!(errs.len(), 1, "{errs:?}"); +} From a93172c2f7c6b570669281c6b106208463cf46f2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 17:47:46 -0500 Subject: [PATCH 37/51] security(engine): Find Missing Photos hashes only regular files, no further than their size (#3704) --- crates/engine/src/cmd/missing.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/missing.rs b/crates/engine/src/cmd/missing.rs index e5a17ab5db..17454cac26 100644 --- a/crates/engine/src/cmd/missing.rs +++ b/crates/engine/src/cmd/missing.rs @@ -163,7 +163,11 @@ fn stored_hash(p: &Photo) -> Option<&str> { /// The content hash of the file at `path` (as import computes it), if it can be read. Read in /// pieces: the file is whatever is on the disk, of any size. fn file_hash(path: &str) -> Option { - hash_reader(std::fs::File::open(path).ok()?) + use std::io::Read; + // the opened file must be regular (a FIFO or device would never end), and no more than its size at open is read + let file = std::fs::File::open(path).ok()?; + let meta = file.metadata().ok().filter(|m| m.is_file())?; + hash_reader(file.take(meta.len())) } /// The content hash of what `r` gives, read in pieces (the hash import computes of the whole). @@ -230,6 +234,9 @@ mod tests { std::fs::write(&f, b"pixels").unwrap(); assert_eq!(file_hash(f.to_str().unwrap()), Some(lightcraft_preview::hash_bytes(b"pixels").to_string())); assert_eq!(file_hash(dir.0.join("nope").to_str().unwrap()), None); + assert_eq!(file_hash(dir.0.to_str().unwrap()), None, "a folder has no content hash"); + #[cfg(unix)] + assert_eq!(file_hash("/dev/zero"), None, "a device never ends"); } } From a5803fe49fbf180c8918aa1b69a55c877b480d8f Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 17:48:05 -0500 Subject: [PATCH 38/51] security(engine, faces): index repeated membership scans in photo move, keywords, import planning, sync and rename (#3709) --- crates/engine/src/cmd/browse.rs | 3 ++- crates/engine/src/cmd/library/specs.rs | 20 +++++++++------ crates/engine/src/import/commit.rs | 7 +++--- crates/engine/src/import/job.rs | 11 ++++++--- crates/engine/src/rename/plan.rs | 20 ++++++++++++--- crates/engine/src/rename/tests.rs | 34 ++++++++++++++++++++++++++ crates/engine/src/sync.rs | 6 +++-- crates/faces/src/matching.rs | 8 ++++-- 8 files changed, 86 insertions(+), 23 deletions(-) diff --git a/crates/engine/src/cmd/browse.rs b/crates/engine/src/cmd/browse.rs index 49f75181ca..9a235306c6 100644 --- a/crates/engine/src/cmd/browse.rs +++ b/crates/engine/src/cmd/browse.rs @@ -433,6 +433,7 @@ fn move_to_folder(s: &mut Session, p: &Value) -> Result { } let mut plans: Vec = Vec::new(); let mut taken: std::collections::HashSet = std::collections::HashSet::new(); + let mut moving: std::collections::HashSet<&str> = std::collections::HashSet::new(); let mut clashes: Vec = Vec::new(); for id in ids { let ph = s.catalog.photo(id).ok_or_else(|| bad(C, format!("no photo {}", id.0)))?; @@ -442,7 +443,7 @@ fn move_to_folder(s: &mut Session, p: &Value) -> Result { let lightcraft_catalog::Source::File { path } = &ph.source else { return Err(bad(C, format!("{} has no file on disk", ph.file_name))); }; - if plans.iter().any(|pl| pl.from_path.as_deref() == Some(path.as_str())) { + if !moving.insert(path.as_str()) { continue; // a virtual copy of a photo already moving } let src = Path::new(path); diff --git a/crates/engine/src/cmd/library/specs.rs b/crates/engine/src/cmd/library/specs.rs index a2a842106a..b99e97c054 100644 --- a/crates/engine/src/cmd/library/specs.rs +++ b/crates/engine/src/cmd/library/specs.rs @@ -757,25 +757,29 @@ pub fn specs() -> Vec { } } // keywords are stored cleaned (`a | b ` is `a|b`), each once whatever its case - use lightcraft_catalog::keywords::{clean, same}; - let add = |m: &mut lightcraft_catalog::Meta, k: &str| { + use lightcraft_catalog::keywords::clean; + // (a set of the lower-cased keywords keeps this linear however many are supplied) + let mut seen: std::collections::HashSet = m.keywords.iter().map(|x| x.to_lowercase()).collect(); + let add = |m: &mut lightcraft_catalog::Meta, seen: &mut std::collections::HashSet, k: &str| { let k = clean(k); - if !k.is_empty() && !m.keywords.iter().any(|x| same(x, &k)) { + if !k.is_empty() && seen.insert(k.to_lowercase()) { m.keywords.push(k); } }; if let Some(k) = strs("keywords") { m.keywords.clear(); + seen.clear(); for k in k { - add(&mut m, &k); + add(&mut m, &mut seen, &k); } } for k in strs("addKeywords").unwrap_or_default() { - add(&mut m, &k); + add(&mut m, &mut seen, &k); } - for k in strs("removeKeywords").unwrap_or_default() { - let k = clean(&k); - m.keywords.retain(|x| !same(&clean(x), &k)); + let removed: std::collections::HashSet = + strs("removeKeywords").unwrap_or_default().iter().map(|k| clean(k).to_lowercase()).collect(); + if !removed.is_empty() { + m.keywords.retain(|x| !removed.contains(&clean(x).to_lowercase())); } ops.push(Op::SetMeta { id, meta: Box::new(m) }); } diff --git a/crates/engine/src/import/commit.rs b/crates/engine/src/import/commit.rs index a731488a00..df7cfe418a 100644 --- a/crates/engine/src/import/commit.rs +++ b/crates/engine/src/import/commit.rs @@ -9,6 +9,10 @@ use super::{HashMap, Op, Path, PathBuf, Photo, PhotoId, ProbeInfo, Session, Sour pub(super) struct Plan { pub(super) slots: Vec, pub(super) transfers: Vec, + /// The content hashes and name families of [`Plan::transfers`], so each new file is checked against them in + /// constant time rather than by scanning the pending transfers. + pub(super) pending_hashes: std::collections::HashSet, + pub(super) pending_families: std::collections::HashSet<(String, String)>, } pub(super) enum Slot { @@ -26,9 +30,6 @@ pub(super) struct Transfer { pub(super) how: How, /// Copy as DNG. pub(super) dng: bool, - /// The folder (lowercase) and [`name_family`]: transfers of one family can take each other's - /// names, so they never run at the same time. - pub(super) family: (String, String), } pub(super) enum How { diff --git a/crates/engine/src/import/job.rs b/crates/engine/src/import/job.rs index 3269edb02a..8846738fe9 100644 --- a/crates/engine/src/import/job.rs +++ b/crates/engine/src/import/job.rs @@ -329,7 +329,7 @@ impl ImportJob { // a copy of a file whose transfer is still pending: a duplicate only if that one succeeds if !opts.local && let Some(h) = &info.content_hash - && plan.transfers.iter().any(|t| t.info.content_hash.as_ref() == Some(h)) + && plan.pending_hashes.contains(h) { self.run_plan(&mut plan, &mut out, cancel); if cancel.load(Relaxed) { @@ -403,7 +403,7 @@ impl ImportJob { // into the same folder ends up with: that one goes first let as_named = name.as_deref().unwrap_or(if own.is_empty() { "photo" } else { own.as_str() }); let family = (dir.to_string_lossy().to_lowercase(), name_family(as_named)); - if plan.transfers.iter().any(|t| t.family == family) { + if plan.pending_families.contains(&family) { self.run_plan(&mut plan, &mut out, cancel); if cancel.load(Relaxed) { break; @@ -421,7 +421,10 @@ impl ImportJob { // Copy as DNG: the copied raw becomes a DNG (the copy is ours to replace) let dng = opts.convert_dng && mode == ImportMode::Copy && raw && !info.format.eq_ignore_ascii_case("DNG"); plan.slots.push(Slot::Transfer); - plan.transfers.push(Transfer { path, info, sidecar, dir, how, dng, family }); + // (transfers of one folder and name family can take each other's names, so they never run together) + plan.pending_families.insert(family); + plan.pending_hashes.extend(info.content_hash.clone()); + plan.transfers.push(Transfer { path, info, sidecar, dir, how, dng }); continue; } (ImportMode::Move, _) => { @@ -444,6 +447,8 @@ impl ImportJob { /// Run the transfers `plan` collected (on several threads) and append its items to `out` in /// file order. Transfers not started because `cancel` was set leave no item. fn run_plan(&mut self, plan: &mut Plan, out: &mut Prepared, cancel: &std::sync::atomic::AtomicBool) { + plan.pending_hashes.clear(); + plan.pending_families.clear(); let mut done = run_transfers(std::mem::take(&mut plan.transfers), self.file_bytes.as_ref(), cancel, &self.done).into_iter(); for slot in std::mem::take(&mut plan.slots) { let item = match slot { diff --git a/crates/engine/src/rename/plan.rs b/crates/engine/src/rename/plan.rs index 4c9620bf13..924fc9a95e 100644 --- a/crates/engine/src/rename/plan.rs +++ b/crates/engine/src/rename/plan.rs @@ -27,7 +27,7 @@ pub fn plan_rename_photos(photos: &[std::sync::Arc], template: &str, star /// Plan renaming `photos`: `exists` says whether something answers to a path, `same_file` whether /// two spellings name one file (see [`MoveFs`]). -fn plan_rename_core( +pub(super) fn plan_rename_core( photos: &[std::sync::Arc], template: &str, start: usize, @@ -35,6 +35,9 @@ fn plan_rename_core( same_file: &dyn Fn(&Path, &Path) -> bool, ) -> Vec { let mut taken: HashSet = HashSet::new(); // lower-case target paths/names claimed in this batch + // Per first-choice target: how many leading suffixes are all claimed in this batch (`taken` only grows), so the + // next photo with the same first choice starts after them instead of rebuilding each one. + let mut claimed_prefix: std::collections::HashMap = std::collections::HashMap::new(); let mut out = Vec::new(); for (i, p) in photos.iter().enumerate() { let seq = start.saturating_add(i); @@ -53,7 +56,9 @@ fn plan_rename_core( let (to, to_path) = match &p.source { Source::File { path } => { let dir = Path::new(path).parent().map(Path::to_path_buf).unwrap_or_default(); - let mut k = 0; + let base = folded_path(&dir.join(&want).to_string_lossy()); + let mut k = claimed_prefix.get(&base).copied().unwrap_or(0); + let mut all_taken = true; loop { let name = candidate(k); let tp = dir.join(&name).to_string_lossy().to_string(); @@ -64,18 +69,25 @@ fn plan_rename_core( let same = from == to || (key == folded_path(path) && exists(to) && same_file(from, to)); // free: not claimed in this batch and not on disk (unless it is this very file). // A file this batch moves away still counts as taken: simple and safe. - if !taken.contains(&key) && (same || !exists(to)) { + let claimed = taken.contains(&key); + if !claimed && (same || !exists(to)) { taken.insert(key); + if all_taken { + claimed_prefix.insert(base, k + 1); + } break (name, Some(tp)); } + all_taken &= claimed; k += 1; } } Source::Demo { .. } => { - let mut k = 0; + let base = format!("demo:{}", want.to_lowercase()); + let mut k = claimed_prefix.get(&base).copied().unwrap_or(0); loop { let name = candidate(k); if taken.insert(format!("demo:{}", name.to_lowercase())) { + claimed_prefix.insert(base, k + 1); // (every candidate before it was claimed: nothing but `taken` rejects a demo name) break (name, None); } k += 1; diff --git a/crates/engine/src/rename/tests.rs b/crates/engine/src/rename/tests.rs index d8ef8de10d..fcad01d274 100644 --- a/crates/engine/src/rename/tests.rs +++ b/crates/engine/src/rename/tests.rs @@ -588,3 +588,37 @@ fn number_folder_and_metadata_tokens() { // a bare {seq} is padded to the caller's width assert_eq!(expand_tokens("{seq}|{seq:2}", &p, 7, 3), "007|07"); } + +/// Many photos wanting one name take its suffixes in order without each starting over at the bare name. +#[test] +fn photos_sharing_a_target_name_do_not_rescan_the_suffixes_already_claimed() { + let photos: Vec> = (0..300) + .map(|i| { + std::sync::Arc::new(Photo::new( + PhotoId(i + 1), + Source::File { path: format!("/lib/src{i}.jpg") }, + &format!("src{i}.jpg"), + "JPEG", + 0, + 0, + "2024-01-01T00:00:00Z", + )) + }) + .collect(); + let checks = std::cell::Cell::new(0usize); + let plans = plan::plan_rename_core( + &photos, + "same", + 1, + &|_| { + checks.set(checks.get() + 1); + false + }, + &|_, _| false, + ); + let mut names: Vec<&str> = plans.iter().map(|p| p.to.as_str()).collect(); + names.sort_unstable(); + names.dedup(); + assert_eq!(names.len(), 300); + assert!(checks.get() <= 3 * 300, "{} disk checks", checks.get()); +} diff --git a/crates/engine/src/sync.rs b/crates/engine/src/sync.rs index 3e69a074e9..9bbbcbec18 100644 --- a/crates/engine/src/sync.rs +++ b/crates/engine/src/sync.rs @@ -334,8 +334,9 @@ fn match_moved( ) -> HashMap { // distinct missing files (a photo's virtual copies share its file), with what is known of them let mut files: Vec<(&str, Option<&Photo>)> = Vec::new(); + let mut seen_paths: std::collections::HashSet<&str> = std::collections::HashSet::new(); for m in missing { - if !files.iter().any(|(p, _)| *p == m.path) { + if seen_paths.insert(m.path.as_str()) { files.push((m.path.as_str(), labels.photo(PhotoId(m.id)).map(|p| &**p))); } } @@ -363,7 +364,8 @@ fn match_moved( } } let mut found: HashMap<(u64, u32, u32), Vec<&str>> = HashMap::new(); - for c in candidates.iter().filter(usable).filter(|c| !moved.values().any(|to| *to == c.path)) { + let taken: std::collections::HashSet<&str> = moved.values().map(String::as_str).collect(); + for c in candidates.iter().filter(usable).filter(|c| !taken.contains(c.path.as_str())) { if let Some(k) = key(c.file_size, c.width, c.height) { found.entry(k).or_default().push(c.path.as_str()); } diff --git a/crates/faces/src/matching.rs b/crates/faces/src/matching.rs index d4f1e5484d..f052cb4443 100644 --- a/crates/faces/src/matching.rs +++ b/crates/faces/src/matching.rs @@ -54,12 +54,16 @@ fn dot(a: &[f32], b: &[f32]) -> f32 { /// best first. Names are compared case-insensitively and shown as first seen. pub fn rank(query: &[f32], gallery: &[(&str, &[f32])]) -> Vec { let mut people: Vec<(String, String, Vec)> = Vec::new(); // (key, shown name, similarities) + let mut at: std::collections::HashMap = std::collections::HashMap::new(); // key -> index in `people` for (name, emb) in gallery { let Some(c) = cosine(query, emb) else { continue }; let key = name.to_lowercase(); - match people.iter_mut().find(|p| p.0 == key) { + match at.get(&key).and_then(|&i| people.get_mut(i)) { Some(p) => p.2.push(c), - None => people.push((key, (*name).to_string(), vec![c])), + None => { + at.insert(key.clone(), people.len()); + people.push((key, (*name).to_string(), vec![c])); + } } } let mut out: Vec = people From b2c1d7064a85bea3dbc57aae23f509252b08b2b1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 17:48:25 -0500 Subject: [PATCH 39/51] security(geocode): bound dataset reads, per-line fields, and the inflate error text (#3718) --- crates/geocode/src/lib.rs | 31 +++++++++++++++++++++++++++++-- crates/geocode/src/unzip.rs | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/geocode/src/lib.rs b/crates/geocode/src/lib.rs index de0bea5c90..9346e13089 100644 --- a/crates/geocode/src/lib.rs +++ b/crates/geocode/src/lib.rs @@ -102,6 +102,24 @@ pub struct GeoNames { countries: HashMap, } +/// Most bytes of one dump file read into memory (the extracted `cities*.txt` is far below this). +const MAX_FILE_BYTES: u64 = 256 << 20; + +/// A regular file's text, at most [`MAX_FILE_BYTES`] of it (the opened file is checked, never its pathname). +fn read_text(path: &Path) -> std::io::Result { + use std::io::Read; + let file = std::fs::File::open(path)?; + if !file.metadata()?.is_file() { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "not a regular file")); + } + let mut bytes = Vec::new(); + file.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes)?; + if bytes.len() as u64 > MAX_FILE_BYTES { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "file too large")); + } + Ok(String::from_utf8_lossy(&bytes).into_owned()) +} + fn cell(p: LatLon) -> (i32, i32) { // valid positions only: |lat| ≤ 90, |lon| ≤ 180, so the casts are in range // longitude 180 is the same meridian as -180: key it as column -180, the wrap `reverse` probes @@ -121,7 +139,8 @@ impl GeoNames { if g.cities.len() >= MAX_PLACES { break; } - let f: Vec<&str> = line.split('\t').collect(); + // only the first 11 columns are used: a line of tabs must not build a field for each + let f: Vec<&str> = line.split('\t').take(11).collect(); // columns: 1 name, 4 latitude, 5 longitude, 8 country code, 10 admin1 code let (Some(name), Some(lat), Some(lon), Some(cc)) = (f.get(1), f.get(4), f.get(5), f.get(8)) else { continue }; let (Ok(lat), Ok(lon)) = (lat.trim().parse::(), lon.trim().parse::()) else { continue }; @@ -152,7 +171,7 @@ impl GeoNames { /// Load from a folder holding [`GEONAMES_FILES`] (the last two are optional: without them /// states stay blank and countries are ISO codes). pub fn load(dir: &Path) -> Result { - let read = |n: &str| std::fs::read(dir.join(n)).map(|b| String::from_utf8_lossy(&b).into_owned()); + let read = |n: &str| read_text(&dir.join(n)); let cities = read(GEONAMES_FILES[0]).map_err(|e| GeocodeError::Missing(format!("the place names aren't downloaded ({e})")))?; let g = GeoNames::parse(&cities, read(GEONAMES_FILES[1]).ok().as_deref(), read(GEONAMES_FILES[2]).ok().as_deref()); if g.is_empty() { @@ -262,6 +281,14 @@ mod tests { } } + #[test] + fn a_line_of_tabs_and_a_non_regular_file_are_handled() { + assert!(GeoNames::parse(&"\t".repeat(100_000), None, None).is_empty()); + assert!(read_text(&std::env::temp_dir()).is_err()); + #[cfg(unix)] + assert!(read_text(Path::new("/dev/zero")).is_err()); + } + #[test] fn high_latitude_cities_two_cells_away() { let city = |lat: f64, lon: f64| GeoNames::parse(&format!("1\tX\tX\t\t{lat}\t{lon}\tP\tP\tNO\t\t\n"), None, None); diff --git a/crates/geocode/src/unzip.rs b/crates/geocode/src/unzip.rs index d422297a7f..49d86bafde 100644 --- a/crates/geocode/src/unzip.rs +++ b/crates/geocode/src/unzip.rs @@ -62,7 +62,7 @@ pub fn extract(zip: &[u8], name: &str, max: usize) -> Result, GeocodeErr 0 if data.len() <= max => Ok(data.to_vec()), 0 => Err(GeocodeError::Invalid(format!("{name} is larger than {max} bytes"))), 8 => { - let decoded = miniz_oxide::inflate::decompress_to_vec_with_limit(data, max).map_err(|e| bad(&format!("deflate: {e:?}")))?; + let decoded = miniz_oxide::inflate::decompress_to_vec_with_limit(data, max).map_err(|e| bad(&format!("deflate: {:?}", e.status)))?; if decoded.len() != usize_ { return Err(bad("uncompressed size")); } From 5c96a70058ea67ace1d27a89d54bd5b9996f6e30 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 17:48:44 -0500 Subject: [PATCH 40/51] security(logging): a log message cannot begin a record of its own (#3720) --- apps/lightcraft/src/logging.rs | 14 +++++++++++++- apps/lightcraft/src/logging/tests.rs | 8 ++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/apps/lightcraft/src/logging.rs b/apps/lightcraft/src/logging.rs index dd174972de..70d06a8c48 100644 --- a/apps/lightcraft/src/logging.rs +++ b/apps/lightcraft/src/logging.rs @@ -167,7 +167,19 @@ pub fn rotate(dir: &Path) -> std::io::Result<(PathBuf, File)> { /// One formatted log line: ` [] : \n`. pub fn format_line(ts: &str, level: log::Level, thread: &str, target: &str, message: &str) -> String { let message = message.strip_suffix('\n').unwrap_or(message); - format!("{ts} {level:<5} [{thread}] {target}: {message}\n") + // A message can carry text from files and peers (a file name, a rejected value): it must not be able to start a + // record of its own. Later lines of a multi-line message (a panic report) are indented, so none begins like a + // record; other control characters (a bare CR, an escape sequence) are shown escaped. + let mut safe = String::with_capacity(message.len()); + for c in message.chars() { + match c { + '\n' => safe.push_str("\n "), + '\t' => safe.push(c), + c if c.is_control() => safe.extend(c.escape_default()), + c => safe.push(c), + } + } + format!("{ts} {level:<5} [{thread}] {target}: {safe}\n") } /// Where formatted lines go besides standard error: kept in memory until a file is attached, diff --git a/apps/lightcraft/src/logging/tests.rs b/apps/lightcraft/src/logging/tests.rs index b801c7d75d..c96707d824 100644 --- a/apps/lightcraft/src/logging/tests.rs +++ b/apps/lightcraft/src/logging/tests.rs @@ -167,7 +167,11 @@ fn a_line_carries_time_level_thread_target_and_message() { "2026-10-08T07:59:17.728Z INFO [main] lightcraft: library /photos: 12 photos\n" ); // A multi-line message keeps its lines; the record still ends in one newline. - assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\nb\n")); + // later lines are indented and control characters escaped, so a message cannot forge a record + assert!(format_line("t", log::Level::Error, "w", "x", "a\nb\n").ends_with("x: a\n b\n")); + let forged = format_line("t", log::Level::Info, "w", "x", "name\r\n2026-01-01T00:00:00Z ERROR [main] x: fake\u{1b}[2J"); + assert!(forged.lines().skip(1).all(|l| l.starts_with(' ')), "{forged:?}"); + assert!(!forged.contains('\r') && !forged.contains('\u{1b}'), "{forged:?}"); } #[test] @@ -357,7 +361,7 @@ fn a_panic_report_goes_to_the_file_and_never_deadlocks() { logger.record_panic("main", "while the sink is held"); } let text = read(&path); - assert!(text.contains(" ERROR [render] lightcraft: panicked at crates/x/src/lib.rs:3:5:\nboom\n"), "{text}"); + assert!(text.contains(" ERROR [render] lightcraft: panicked at crates/x/src/lib.rs:3:5:\n boom\n"), "{text}"); assert!(!text.contains("while the sink is held"), "{text}"); let _ = std::fs::remove_dir_all(&dir); } From 03e03f382ed740b753111f125fe32c83d554a36b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 17:49:04 -0500 Subject: [PATCH 41/51] security(tests): test fixture directories are created exclusively, never reused from a predictable name (#3721) --- crates/engine/src/denoise/gpu_tests.rs | 3 ++- crates/gpu/src/backend.rs | 8 ++++++++ xtask/src/ci_report.rs | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/denoise/gpu_tests.rs b/crates/engine/src/denoise/gpu_tests.rs index bb80cecdad..bff11cc20c 100644 --- a/crates/engine/src/denoise/gpu_tests.rs +++ b/crates/engine/src/denoise/gpu_tests.rs @@ -5,7 +5,8 @@ use lightcraft_denoise::manifest::{Domain, Gain}; fn folder(name: &str) -> PathBuf { let dir = std::env::temp_dir().join(format!("lc-engine-gpu-{}-{name}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); - std::fs::create_dir_all(&dir).unwrap(); + // exclusive: a directory planted at this predictable name fails the test instead of receiving its files + std::fs::create_dir(&dir).unwrap(); dir } diff --git a/crates/gpu/src/backend.rs b/crates/gpu/src/backend.rs index d970003e73..8d8456a265 100644 --- a/crates/gpu/src/backend.rs +++ b/crates/gpu/src/backend.rs @@ -427,6 +427,8 @@ mod tests { let dir = std::env::temp_dir().join(format!("lc-gpu-marker-{}", std::process::id())); let m = dir.join("gpu-init.marker"); let _ = std::fs::remove_dir_all(&dir); + // exclusive: a directory planted at this predictable name fails the test instead of receiving the marker + std::fs::create_dir(&dir).unwrap(); // a local scope counter: other tests create real devices through the process-wide one let active = Mutex::new(0); let seen = with_marker_on(Some(m.clone()), &active, "device creation", || m.exists()); @@ -447,6 +449,8 @@ mod tests { let dir = std::env::temp_dir().join(format!("lc-gpu-global-{}", std::process::id())); let m = dir.join("gpu-init.marker"); let _ = std::fs::remove_dir_all(&dir); + // exclusive: a directory planted at this predictable name fails the test instead of receiving the marker + std::fs::create_dir(&dir).unwrap(); set_init_marker(Some(m.clone())); let seen = with_init_marker(Backends::DX12, || m.exists()); set_init_marker(None); @@ -501,6 +505,8 @@ mod tests { let dir = std::env::temp_dir().join(format!("lc-gpu-scopes-{}", std::process::id())); let m = dir.join("gpu-init.marker"); let _ = std::fs::remove_dir_all(&dir); + // exclusive: a directory planted at this predictable name fails the test instead of receiving the marker + std::fs::create_dir(&dir).unwrap(); let active = Mutex::new(0); arm(&m, &active, "device creation"); assert!(read_init_marker(&m).unwrap().contains("device creation")); @@ -524,6 +530,8 @@ mod tests { let dir = std::env::temp_dir().join(format!("lc-gpu-panic-{}", std::process::id())); let m = dir.join("gpu-init.marker"); let _ = std::fs::remove_dir_all(&dir); + // exclusive: a directory planted at this predictable name fails the test instead of receiving the marker + std::fs::create_dir(&dir).unwrap(); let active = Mutex::new(0); let r = std::panic::catch_unwind(|| with_marker_on(Some(m.clone()), &active, "kernel compilation", || panic!("compile"))); assert!(r.is_err()); diff --git a/xtask/src/ci_report.rs b/xtask/src/ci_report.rs index 8be686ef11..ec5da20f3b 100644 --- a/xtask/src/ci_report.rs +++ b/xtask/src/ci_report.rs @@ -505,7 +505,8 @@ mod tests { fn tmp(name: &str) -> PathBuf { let d = std::env::temp_dir().join(format!("xtask-ci-report-{name}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&d); - std::fs::create_dir_all(&d).unwrap(); + // exclusive: a directory planted at this predictable name fails the test instead of receiving its files + std::fs::create_dir(&d).unwrap(); d } From f7e43a4a45dd4381fb70366797b1df76d0f7f444 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 23:58:25 +0000 Subject: [PATCH 42/51] address review: charge name allocations and only kept calibrations to the copy budget --- crates/lensdb/src/lib.rs | 107 +++++++++++++++++++++---------------- crates/lensdb/src/tests.rs | 23 ++++++++ 2 files changed, 83 insertions(+), 47 deletions(-) diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index e2910125c0..91f6723c1c 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -33,6 +33,8 @@ const MAX_FILES: usize = 2000; /// Most bytes of lens names that profiles may copy per parsed document (each calibration owns a copy): a real /// database copies about a megabyte. const MAX_PROFILE_COPY_BYTES: usize = 32 << 20; +/// What one copied name costs besides its bytes: the `String` slot in its `Vec` and the allocator's header and rounding. +const NAME_ALLOCATION: usize = std::mem::size_of::() + 32; /// Largest radial error (half-diagonal units) accepted when converting a distortion to the DNG polynomial. pub const MAX_WARP_ERROR: f64 = 0.001; /// Largest relative error accepted when converting vignetting to the DNG polynomial. @@ -394,55 +396,66 @@ fn lens(n: &xml::Node, copy_budget: &mut usize) -> Vec { let aspect = aspect_of(n.kid_text("aspect-ratio")).unwrap_or(1.5); let rectilinear = n.kid_text("type").is_none_or(|t| t.eq_ignore_ascii_case("rectilinear")); let mounts = n.kids("mount").map(|m| m.text.trim().to_string()).filter(|m| !m.is_empty()).collect::>(); - // every calibration owns a copy of the lens's names: calibrations past the document's shared copy budget are left out - let meta_bytes = maker.len() + models.iter().chain(&mounts).map(|m| m.len() + 1).sum::() + 1; - let take = n.kids("calibration").count().min(*copy_budget / meta_bytes); - *copy_budget -= take * meta_bytes; - n.kids("calibration") - .take(take) - .map(|cal| { - let mut p = Profile { - maker: maker.to_string(), - models: models.clone(), - mounts: mounts.clone(), - crop_factor: crop_of(cal.attr("cropfactor")).unwrap_or(crop), - aspect_ratio: aspect_of(cal.attr("aspect-ratio")).unwrap_or(aspect), - rectilinear, - distortion: Vec::new(), - tca: Vec::new(), - vignetting: Vec::new(), - }; - for c in &cal.children { - let Some(focal) = positive(c.num("focal")) else { continue }; - let k = |name: &str| c.num(name).unwrap_or(0.0); - match (c.name.as_str(), c.attr("model")) { - ("distortion", Some("poly3")) => p.distortion.push((focal, Distortion::Poly3 { k1: k("k1") })), - ("distortion", Some("poly5")) => p.distortion.push((focal, Distortion::Poly5 { k1: k("k1"), k2: k("k2") })), - ("distortion", Some("ptlens")) => p.distortion.push((focal, Distortion::PtLens { a: k("a"), b: k("b"), c: k("c") })), - ("tca", Some("linear")) => { - let one = |name: &str| c.num(name).unwrap_or(1.0); - p.tca.push((focal, Tca::Linear { kr: one("kr"), kb: one("kb") })); - } - ("tca", Some("poly3")) => { - let one = |name: &str| c.num(name).unwrap_or(1.0); - p.tca.push((focal, Tca::Poly3 { vr: one("vr"), vb: one("vb"), cr: k("cr"), cb: k("cb"), br: k("br"), bb: k("bb") })); - } - ("vignetting", Some("pa")) => { - let (Some(aperture), distance) = (positive(c.num("aperture")), positive(c.num("distance")).unwrap_or(1000.0)) else { - continue; - }; - let model = Vignetting { k1: k("k1"), k2: k("k2"), k3: k("k3") }; - p.vignetting.push(VignettingPoint { focal, aperture, distance, model }); - } - _ => {} + // every kept calibration owns a copy of the lens's names, charged to the document's shared copy budget (the + // strings' own allocations count, not only their bytes): when it can't cover another copy, the rest are left out + let meta_bytes = std::mem::size_of::() + + NAME_ALLOCATION + + maker.len() + + models.iter().chain(&mounts).map(|m| m.len() + NAME_ALLOCATION).sum::(); + let mut out = Vec::new(); + for cal in n.kids("calibration") { + if *copy_budget < meta_bytes { + break; + } + let mut p = Profile { + maker: String::new(), + models: Vec::new(), + mounts: Vec::new(), + crop_factor: crop_of(cal.attr("cropfactor")).unwrap_or(crop), + aspect_ratio: aspect_of(cal.attr("aspect-ratio")).unwrap_or(aspect), + rectilinear, + distortion: Vec::new(), + tca: Vec::new(), + vignetting: Vec::new(), + }; + for c in &cal.children { + let Some(focal) = positive(c.num("focal")) else { continue }; + let k = |name: &str| c.num(name).unwrap_or(0.0); + match (c.name.as_str(), c.attr("model")) { + ("distortion", Some("poly3")) => p.distortion.push((focal, Distortion::Poly3 { k1: k("k1") })), + ("distortion", Some("poly5")) => p.distortion.push((focal, Distortion::Poly5 { k1: k("k1"), k2: k("k2") })), + ("distortion", Some("ptlens")) => p.distortion.push((focal, Distortion::PtLens { a: k("a"), b: k("b"), c: k("c") })), + ("tca", Some("linear")) => { + let one = |name: &str| c.num(name).unwrap_or(1.0); + p.tca.push((focal, Tca::Linear { kr: one("kr"), kb: one("kb") })); } + ("tca", Some("poly3")) => { + let one = |name: &str| c.num(name).unwrap_or(1.0); + p.tca.push((focal, Tca::Poly3 { vr: one("vr"), vb: one("vb"), cr: k("cr"), cb: k("cb"), br: k("br"), bb: k("bb") })); + } + ("vignetting", Some("pa")) => { + let (Some(aperture), distance) = (positive(c.num("aperture")), positive(c.num("distance")).unwrap_or(1000.0)) else { + continue; + }; + let model = Vignetting { k1: k("k1"), k2: k("k2"), k3: k("k3") }; + p.vignetting.push(VignettingPoint { focal, aperture, distance, model }); + } + _ => {} } - p.distortion.sort_by(|a, b| a.0.total_cmp(&b.0)); - p.tca.sort_by(|a, b| a.0.total_cmp(&b.0)); - p - }) - .filter(|p| !p.distortion.is_empty() || !p.tca.is_empty() || !p.vignetting.is_empty()) - .collect() + } + // a calibration with nothing to apply is dropped before its names are copied or charged + if p.distortion.is_empty() && p.tca.is_empty() && p.vignetting.is_empty() { + continue; + } + *copy_budget -= meta_bytes; + p.maker = maker.to_string(); + p.models = models.clone(); + p.mounts = mounts.clone(); + p.distortion.sort_by(|a, b| a.0.total_cmp(&b.0)); + p.tca.sort_by(|a, b| a.0.total_cmp(&b.0)); + out.push(p); + } + out } /// Linear interpolation weights in a sorted key list: the bracketing indices and the weight of the upper one. diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs index 033111de7f..e535330a6e 100644 --- a/crates/lensdb/src/tests.rs +++ b/crates/lensdb/src/tests.rs @@ -383,6 +383,29 @@ fn calibrations_past_the_shared_copy_budget_are_left_out() { assert!(db.lenses.len() * 400_000 <= MAX_PROFILE_COPY_BYTES); } +/// Unusable calibrations (nothing to apply) are not copied or charged, so they can't use up the +/// budget a later usable one of the same lens needs. +#[test] +fn unusable_calibrations_do_not_spend_the_copy_budget() { + let name = "x".repeat(8 << 20); + let empty = ""; + let good = ""; + let xml = format!("M{name}{}{good}", empty.repeat(4)); + let db = LensDb::parse(&xml).unwrap(); + assert_eq!(db.lenses.len(), 1, "the usable calibration after the empty ones is kept"); +} + +/// Many tiny names cost their allocations, not only their bytes. +#[test] +fn many_tiny_names_are_charged_their_allocations() { + let models = "a".repeat(400_000); + let good = ""; + let xml = format!("M{models}{good}{good}{good}{good}"); + let db = LensDb::parse(&xml).unwrap(); + let names: usize = db.lenses.iter().map(|p| p.models.len()).sum(); + assert!(db.lenses.len() < 4 && names * std::mem::size_of::() <= MAX_PROFILE_COPY_BYTES, "{} profiles", db.lenses.len()); +} + #[cfg(unix)] #[test] fn load_dir_refuses_a_non_regular_xml_entry() { From b0c4aa8cc8acac043af6d767b3bfec30d0ab0c0a Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 22:32:53 -0500 Subject: [PATCH 43/51] security(raw): linear sparse planning, distinct-byte allocation bound, opcode list budget, ORF strip length (#3679) * security(raw): linear sparse planning, distinct-byte allocation bound, opcode list budget, ORF strip length * address review: zero opcode cost for no-op ops, compact the sparse plan buffer --- crates/raw/src/opcodes.rs | 37 +++++++++++++++++++ crates/raw/src/opcodes/tests.rs | 27 ++++++++++++++ crates/raw/src/sparse.rs | 65 ++++++++++++++++++++++++++------- crates/raw/src/tiffraw.rs | 40 +++++++++++++++++++- crates/raw/src/vendor/orf.rs | 5 +++ 5 files changed, 158 insertions(+), 16 deletions(-) diff --git a/crates/raw/src/opcodes.rs b/crates/raw/src/opcodes.rs index 4d3e463b0e..2f5d51fae0 100644 --- a/crates/raw/src/opcodes.rs +++ b/crates/raw/src/opcodes.rs @@ -566,8 +566,45 @@ const MAX_BAD_PIXELS: usize = 1 << 22; /// Apply a list to an interleaved `w × h × cpp` buffer. `cfa` (when the data is a mosaic) is used by the /// bad-pixel opcodes. `scale` is the value of 1.0 in the buffer: 65535 for list 1 (raw 16-bit values), 1 for /// lists 2 and 3 (normalised values). +/// Whole-image passes one opcode list may cost, all its ops together (a real DNG's list is a +/// handful of gain maps and tables). Ops past it, in list order, are left out: a list repeating a +/// full-image polynomial thousands of times was billions of evaluations on an ordinary raw. +const MAX_LIST_PASSES: usize = 32; + +/// Sample visits `op` costs on a `w × h × cpp` buffer (coefficient evaluations for a polynomial). +fn op_cost(op: &Opcode, w: usize, h: usize, cpp: usize) -> usize { + let visits = |a: &Area| { + let rows = (a.bottom as usize).min(h).saturating_sub(a.top as usize).div_ceil(a.row_pitch.max(1) as usize); + let cols = (a.right as usize).min(w).saturating_sub(a.left as usize).div_ceil(a.col_pitch.max(1) as usize); + let planes = (a.plane as usize).saturating_add(a.planes.max(1) as usize).min(cpp).saturating_sub(a.plane as usize); + rows.saturating_mul(cols).saturating_mul(planes) + }; + match op { + Opcode::MapPolynomial { area, coefficients } => visits(area).saturating_mul(coefficients.len().max(1)), + Opcode::MapTable { area, .. } + | Opcode::GainMap { area, .. } + | Opcode::DeltaPerRow { area, .. } + | Opcode::DeltaPerColumn { area, .. } + | Opcode::ScalePerRow { area, .. } + | Opcode::ScalePerColumn { area, .. } => visits(area), + // `apply_list` does nothing for these, so they spend none of the budget + Opcode::Unknown { .. } + | Opcode::TrimBounds { .. } + | Opcode::WarpFisheye { .. } + | Opcode::WarpRectilinear { .. } + | Opcode::WarpRectilinear2 { .. } => 0, + _ => w.saturating_mul(h).saturating_mul(cpp), + } +} + pub fn apply_list(list: &[Opcode], buf: &mut [f32], w: usize, h: usize, cpp: usize, cfa: Option<&Cfa>, scale: f32) { + let mut budget = w.saturating_mul(h).saturating_mul(cpp).max(1).saturating_mul(MAX_LIST_PASSES); for op in list { + let cost = op_cost(op, w, h, cpp); + if cost > budget { + continue; + } + budget -= cost; match op { Opcode::MapTable { area, table } => { let n = table.len(); diff --git a/crates/raw/src/opcodes/tests.rs b/crates/raw/src/opcodes/tests.rs index d1be999e98..48840ff23a 100644 --- a/crates/raw/src/opcodes/tests.rs +++ b/crates/raw/src/opcodes/tests.rs @@ -456,3 +456,30 @@ fn cropped_lens_corrections_correct_the_crop_like_the_whole() { assert!(warps[0].cropped(w, h, crate::Rect::new(30, 0, 70, 50)).is_none()); assert!(warps[0].cropped(w, h, crate::Rect::new(usize::MAX, 0, 70, 50)).is_none()); } + +/// Ten thousand full-image polynomials (a few hundred KB of opcode data): the list is held to +/// `MAX_LIST_PASSES` passes of work, the first ones applied and the rest left out. +#[test] +fn a_list_repeating_full_image_work_stops_at_its_budget() { + let (w, h) = (64usize, 48usize); + let a = Area { top: 0, left: 0, bottom: h as u32, right: w as u32, plane: 0, planes: 1, row_pitch: 1, col_pitch: 1 }; + // v + 0.001 each, two coefficients: each costs two passes + let list = vec![Opcode::MapPolynomial { area: a, coefficients: vec![0.001, 1.0] }; 10_000]; + let mut buf = vec![0.0f32; w * h]; + apply_list(&list, &mut buf, w, h, 1, None, 1.0); + let applied = (buf[0] / 0.001).round() as usize; + assert_eq!(applied, MAX_LIST_PASSES / 2, "{}", buf[0]); + assert!(buf.iter().all(|v| *v == buf[0])); +} + +/// Opcodes `apply_list` does nothing for spend none of the budget: a later real one still runs. +#[test] +fn ops_that_apply_nothing_cost_no_budget() { + let (w, h) = (8usize, 8usize); + let a = Area { top: 0, left: 0, bottom: h as u32, right: w as u32, plane: 0, planes: 1, row_pitch: 1, col_pitch: 1 }; + let mut list = vec![Opcode::Unknown { id: 9999, flags: 0, params: Vec::new() }; 4 * MAX_LIST_PASSES]; + list.push(Opcode::MapPolynomial { area: a, coefficients: vec![0.5, 1.0] }); + let mut buf = vec![0.0f32; w * h]; + apply_list(&list, &mut buf, w, h, 1, None, 1.0); + assert_eq!(buf[0], 0.5); +} diff --git a/crates/raw/src/sparse.rs b/crates/raw/src/sparse.rs index 8a404a25ea..893ba79b20 100644 --- a/crates/raw/src/sparse.rs +++ b/crates/raw/src/sparse.rs @@ -121,7 +121,8 @@ impl Ranges { /// The parts of `[start, end)` not covered. pub fn gaps(&self, start: u64, end: u64) -> Ranges { - let mut out = Ranges::new(); + // the gaps come in order, apart from each other: pushed, not merged in one by one + let mut out = Vec::new(); let mut at = start; for &(a, b) in &self.0 { if b <= at { @@ -131,24 +132,20 @@ impl Ranges { break; } if a > at { - out.insert(at, a); + out.push((at, a)); } at = at.max(b); } if at < end { - out.insert(at, end); + out.push((at, end)); } - out + Ranges(out) } /// These ranges without the ones in `other`. pub fn minus(&self, other: &Ranges) -> Ranges { let mut out = Ranges::new(); - for &(a, b) in &self.0 { - for &(x, y) in other.gaps(a, b).spans() { - out.insert(x, y); - } - } + out.extend(self.0.iter().flat_map(|&(a, b)| other.gaps(a, b).0)); out } } @@ -201,12 +198,17 @@ fn plan(bytes: &[u8], loaded: &Ranges, preview: bool) -> HeaderPlan { struct Want<'a> { len: u64, loaded: &'a Ranges, - need: Ranges, + /// What is missing, as found; merged once into the plan (merging each find into the ranges so + /// far was a copy of them per find: quadratic over a file of many out-of-line values). + need: Vec<(u64, u64)>, + /// `need` is merged when it reaches this long, so repeated finds of one gap stay bounded by + /// the distinct gaps (merging when it doubles keeps the cost linear). + compact_at: usize, } impl<'a> Want<'a> { fn new(bytes: &[u8], loaded: &'a Ranges) -> Self { - Self { len: bytes.len() as u64, loaded, need: Ranges::new() } + Self { len: bytes.len() as u64, loaded, need: Vec::new(), compact_at: 64 } } /// Whether `[start, end)` (clipped to the file) is loaded; asks for what isn't. @@ -215,15 +217,21 @@ impl<'a> Want<'a> { if self.loaded.contains(start, end) { return true; } - for &(a, b) in self.loaded.gaps(start, end).spans() { - self.need.insert(a, b); + self.need.extend(self.loaded.gaps(start, end).0); + if self.need.len() >= self.compact_at { + let mut merged = Ranges::new(); + merged.extend(std::mem::take(&mut self.need)); + self.need = merged.spans().to_vec(); + self.compact_at = (self.need.len() * 2).max(64); } false } /// `Need` when something is missing. fn into_plan(self) -> Option { - (!self.need.is_empty()).then_some(HeaderPlan::Need(self.need)) + let mut need = Ranges::new(); + need.extend(self.need); + (!need.is_empty()).then_some(HeaderPlan::Need(need)) } } @@ -559,6 +567,35 @@ mod tests { assert_eq!(r.total(), 20); } + #[test] + fn a_plan_of_many_missing_values_is_gathered_in_one_merge() { + // 200 000 loaded spans with a byte missing between each: every gap wanted once + let mut loaded = Ranges::new(); + loaded.extend((0..20_000u64).map(|i| (i * 4, i * 4 + 3))); + let bytes = vec![0u8; 80_000]; + let mut w = Want::new(&bytes, &loaded); + for i in 0..20_000u64 { + assert!(!w.want(i * 4 + 2, i * 4 + 4)); + } + let Some(HeaderPlan::Need(need)) = w.into_plan() else { panic!("a plan") }; + assert_eq!(need.spans().len(), 20_000); + assert_eq!(need.total(), 20_000); + assert_eq!(loaded.gaps(0, 12).spans(), &[(3, 4), (7, 8), (11, 12)]); + } + + #[test] + fn repeated_wants_of_one_gap_keep_the_buffer_bounded() { + let loaded = Ranges::new(); + let bytes = vec![0u8; 1000]; + let mut w = Want::new(&bytes, &loaded); + for _ in 0..100_000 { + assert!(!w.want(10, 20)); + assert!(w.need.len() <= 64); + } + let Some(HeaderPlan::Need(need)) = w.into_plan() else { panic!("a plan") }; + assert_eq!(need.spans(), &[(10, 20)]); + } + #[test] fn hostile_input_never_panics_and_ends() { let mut loaded = Ranges::new(); diff --git a/crates/raw/src/tiffraw.rs b/crates/raw/src/tiffraw.rs index 75186d2d3b..deb64ea595 100644 --- a/crates/raw/src/tiffraw.rs +++ b/crates/raw/src/tiffraw.rs @@ -63,8 +63,20 @@ pub fn check_image(data: &[u8], info: &ImageInfo) -> Result { return Err(RawError::Limit("image data chunks far larger than the image")); } // Plausibility bound before allocating: no supported coding stores more than ~2000 samples per byte - // (Deflate of constant data is the extreme), so tiny files cannot trigger huge allocations. - let available: u64 = chunks.iter().map(|c| chunk_bytes(data, c).map_or(0, |s| s.len() as u64)).sum(); + // (Deflate of constant data is the extreme), so tiny files cannot trigger huge allocations. The + // bytes are counted once however many chunks point at them: a grid of tiles sharing one small + // payload holds that payload's samples, not the grid's. + let mut spans: Vec<(u64, u64)> = + chunks.iter().filter_map(|c| Some((c.offset, c.offset.checked_add(chunk_bytes(data, c)?.len() as u64)?))).collect(); + spans.sort_unstable(); + let (mut available, mut end) = (0u64, 0u64); + for (a, b) in spans { + let a = a.max(end); + if b > a { + available = available.saturating_add(b - a); + end = b; + } + } if available.saturating_mul(2048) < total as u64 { return Err(RawError::Corrupt(format!("{available} bytes of image data cannot hold {total} samples"))); } @@ -429,6 +441,30 @@ mod lossy_tests { use super::*; use lightcraft_tiff::image::Layout; + /// 65 536 tiles all pointing at the same 256 bytes: the image's samples can't come from them, + /// and nothing of its size is allocated to find that out. + #[test] + fn tiles_sharing_one_payload_count_its_bytes_once() { + let file = vec![0u8; 8 + 256]; + let info = ImageInfo { + width: 4096, + height: 4096, + bits_per_sample: vec![8], + samples_per_pixel: 1, + compression: comp::NONE, + photometric: 34892, + planar: 1, + predictor: 1, + sample_format: 1, + new_subfile_type: 0, + layout: Layout::Tiles { tile_width: 16, tile_height: 16 }, + offsets: vec![8; 65_536], + byte_counts: vec![256; 65_536], + }; + let err = check_image(&file, &info).unwrap_err().to_string(); + assert!(err.contains("cannot hold"), "{err}"); + } + /// A lossy-DNG-style image: two 16×8 tiles, each a baseline JPEG, the right one cut short /// by the image edge. #[test] diff --git a/crates/raw/src/vendor/orf.rs b/crates/raw/src/vendor/orf.rs index 172f83013b..ee1d1f48d2 100644 --- a/crates/raw/src/vendor/orf.rs +++ b/crates/raw/src/vendor/orf.rs @@ -230,6 +230,11 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result { (RawData::U16(d), 12) } else if total * 8 >= (n as u64) * 12 && total * 8 < (n as u64) * 13 && chunks.len() == 1 { let src = chunk_bytes(bytes, &chunks[0]).ok_or_else(|| RawError::Corrupt("ORF strip outside file".into()))?; + // the strip as it is in the file (clipped at its end), not as its tag states it: one too short + // for the samples is a truncated file, refused before they are allocated + if (src.len() as u64).saturating_mul(8) < (n as u64).saturating_mul(12) { + return Err(RawError::Corrupt("ORF strip shorter than its samples".into())); + } // the depth is fixed here, so a header-only probe needs the samples only when the file doesn't state its // colour-filter layout let d = if mode == Mode::Full || stated.is_none() { From 9eb7b25e317b6e66beb6b9c8f862da73f3de0aae Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 22:50:49 -0500 Subject: [PATCH 44/51] security(engine): sidecar copies, backups and version checks read regular files only (#3677) * security(engine): sidecar copies, backups and version checks read regular files only * address review: open the sidecar source and check the handle is the regular file that was checked --- crates/engine/src/rename/fs.rs | 60 +++++++++++++++++++++++- crates/engine/src/sidecar.rs | 5 ++ crates/engine/src/tests/move_to_share.rs | 13 +++++ 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/rename/fs.rs b/crates/engine/src/rename/fs.rs index 71de1a7843..30b6a3e30c 100644 --- a/crates/engine/src/rename/fs.rs +++ b/crates/engine/src/rename/fs.rs @@ -46,6 +46,9 @@ pub(crate) trait MoveFs { fn packet_version(&self, path: &Path) -> std::io::Result { use std::io::Read; let mut file = std::fs::File::open(path)?; + if !file.metadata()?.is_file() { + return Err(std::io::Error::other(format!("{} is not a regular file", path.display()))); + } let mut hash = lightcraft_preview::Hasher128::new(); let mut len = 0u64; let mut buffer = [0u8; 16 * 1024]; @@ -141,8 +144,14 @@ impl MoveFs for RealFs { .map(|k| b.with_file_name(format!(".{name}.lc-partial-{}-{k}", std::process::id()))) .find(|p| !self.exists(p)) .ok_or_else(|| std::io::Error::other(format!("no free temporary name beside {}", b.display())))?; + // a sidecar found beside the photo by its name is copied only when it is a plain file: a + // link there would have its target's contents copied into a new, readable file + let checked = std::fs::symlink_metadata(a)?; + if !checked.is_file() { + return Err(std::io::Error::other(format!("{} is not a regular file", a.display()))); + } let copied = (|| { - let mut from = std::fs::File::open(a)?; + let mut from = open_regular(a, &checked)?; let mut f = std::fs::OpenOptions::new().write(true).create_new(true).open(&partial)?; std::io::copy(&mut from, &mut f)?; f.sync_all() @@ -163,6 +172,33 @@ impl MoveFs for RealFs { } } +/// Opens `a` for reading and checks the open handle is the regular file `checked` (the +/// `symlink_metadata` of `a` taken before): the path check alone can be outrun by a swap for a +/// link, whose target the open would follow. +fn open_regular(a: &Path, checked: &std::fs::Metadata) -> std::io::Result { + let mut options = std::fs::OpenOptions::new(); + options.read(true); + // FILE_FLAG_OPEN_REPARSE_POINT: a symbolic link is opened itself, and then refused below + #[cfg(windows)] + std::os::windows::fs::OpenOptionsExt::custom_flags(&mut options, 0x0020_0000); + let file = options.open(a)?; + let opened = file.metadata()?; + #[cfg(unix)] + let same = { + use std::os::unix::fs::MetadataExt; + (opened.dev(), opened.ino()) == (checked.dev(), checked.ino()) + }; + #[cfg(not(unix))] + let same = { + let _ = checked; + true + }; + if !opened.is_file() || !same { + return Err(std::io::Error::other(format!("{} is not a regular file", a.display()))); + } + Ok(file) +} + /// Move `a` to `b` on another volume: copy (never replacing), compare the copy with the original /// byte for byte, and only then remove the original. A copy that doesn't match is removed. fn move_across_volumes(fs: &RealFs, a: &Path, b: &Path) -> std::io::Result<()> { @@ -240,3 +276,25 @@ pub(super) fn move_one(fs: &dyn MoveFs, a: &Path, b: &Path) -> std::io::Result<( Err(e2) => std::io::Error::other(format!("{e}; it could not be given its old name back either ({e2}): it is now {}", tmp.display())), }) } + +#[cfg(all(test, unix))] +mod open_regular_tests { + use super::open_regular; + + #[test] + fn a_link_swapped_in_after_the_check_is_not_followed() { + let dir = std::env::temp_dir().join(format!("lightcraft-open-regular-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let (file, other, link) = (dir.join("a.xmp"), dir.join("secret"), dir.join("link.xmp")); + std::fs::write(&file, b"x").unwrap(); + std::fs::write(&other, b"secret").unwrap(); + let checked = std::fs::symlink_metadata(&file).unwrap(); + assert!(open_regular(&file, &checked).is_ok()); + // the name now holds a link to another file: the handle is not the one checked + std::fs::remove_file(&file).unwrap(); + std::os::unix::fs::symlink(&other, &link).unwrap(); + std::fs::rename(&link, &file).unwrap(); + assert!(open_regular(&file, &checked).is_err(), "the link's target must not be opened"); + std::fs::remove_dir_all(&dir).unwrap(); + } +} diff --git a/crates/engine/src/sidecar.rs b/crates/engine/src/sidecar.rs index 9f3e518a49..252dd911a6 100644 --- a/crates/engine/src/sidecar.rs +++ b/crates/engine/src/sidecar.rs @@ -455,6 +455,11 @@ fn write_atomic(path: &Path, data: &[u8]) -> std::io::Result<()> { /// Copy `path` to a new `.bak-` (`-1`, `-2`… if taken; never overwriting). fn backup(path: &Path, stamp: &str) -> std::io::Result { use std::io::Write; + // only a plain file is copied aside: a link under the sidecar's name would have its target's + // contents copied into a new, readable file + if !std::fs::symlink_metadata(path)?.is_file() { + return Err(std::io::Error::other(format!("{} is not a regular file", path.display()))); + } let bytes = std::fs::read(path)?; let stamp: String = stamp.chars().filter(char::is_ascii_alphanumeric).collect(); let name = path.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); diff --git a/crates/engine/src/tests/move_to_share.rs b/crates/engine/src/tests/move_to_share.rs index ed824b29d8..e7602818e4 100644 --- a/crates/engine/src/tests/move_to_share.rs +++ b/crates/engine/src/tests/move_to_share.rs @@ -254,6 +254,19 @@ fn other_device(here: &std::path::Path) -> Option { } } +#[cfg(unix)] +#[test] +fn a_link_is_not_copied_as_its_target() { + use crate::rename::MoveFs; + let dir = Scratch::new("copy-link"); + std::fs::create_dir_all(dir.0.join("local")).unwrap(); + std::fs::create_dir_all(dir.0.join("share")).unwrap(); + std::fs::write(dir.0.join("local/private.txt"), b"secret").unwrap(); + std::os::unix::fs::symlink(dir.0.join("local/private.txt"), dir.0.join("local/photo.xmp")).unwrap(); + assert!(crate::rename::RealFs.copy_no_replace(&dir.0.join("local/photo.xmp"), &dir.0.join("share/photo.xmp")).is_err()); + assert!(!dir.0.join("share/photo.xmp").exists()); +} + #[test] fn the_copy_across_volumes_is_whole_or_absent_and_never_replaces() { use crate::rename::MoveFs; From 00b020cf2fa1d71c1b5e2f35ec805be0ad8e8404 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 22:51:11 -0500 Subject: [PATCH 45/51] catalog: bound keyword list path expansion and index synonym merges (#3641) * catalog: bound keyword list path expansion and index synonym merges * address review: hold keyword list writer to the path budget the reader enforces * address review: charge the emitted parent path in the keyword list writer --- crates/catalog/src/keywords.rs | 82 ++++++++++++++++++++-------- crates/catalog/src/keywords/tests.rs | 55 +++++++++++++++++++ 2 files changed, 115 insertions(+), 22 deletions(-) diff --git a/crates/catalog/src/keywords.rs b/crates/catalog/src/keywords.rs index d36ab11cd6..5fb095767b 100644 --- a/crates/catalog/src/keywords.rs +++ b/crates/catalog/src/keywords.rs @@ -87,6 +87,12 @@ pub fn from_file(flat: &[String], hierarchical: &[String]) -> Vec { /// The deepest a keyword list file may nest keywords: a file is input, so its nesting is bounded. pub const MAX_LEVELS: usize = 64; +/// The most bytes of full keyword paths a keyword list file may spell out. Every line names its +/// keyword by its whole path (`parent|child`), so a file of one long keyword with many short ones +/// inside it would otherwise grow into far more memory than its own size (CWE-400). Lightroom +/// Classic's own lists of tens of thousands of keywords come to a few MiB. +pub const MAX_LIST_PATH_BYTES: usize = 64 << 20; // 64 MiB + /// A keyword list as written by [`Catalog::keyword_list_text`]: the file's text, the keywords in /// it (paths), and what the format can't hold and so is left out (a keyword, with those inside it, /// as its path; a synonym as `path {synonym}`). @@ -114,6 +120,8 @@ pub fn parse_keyword_list(text: &str) -> Result> { let mut synonyms: std::collections::HashMap> = std::collections::HashMap::new(); // the keyword at each level above the line: its path and where it is in `out` let mut above: Vec<(String, usize)> = Vec::new(); + // bytes of full paths spelled out so far, checked against `MAX_LIST_PATH_BYTES` before each + let mut path_bytes = 0usize; for (i, line) in text.split('\n').enumerate() { let n = i + 1; if line.trim().is_empty() { @@ -162,6 +170,11 @@ pub fn parse_keyword_list(text: &str) -> Result> { return Err(bad(n, "“|” separates a keyword's levels: a name can't hold one")); } above.truncate(depth); + let parent_len = above.last().map_or(0, |(parent, _)| parent.len().saturating_add(1)); + path_bytes = path_bytes.saturating_add(parent_len.saturating_add(name.len())); + if path_bytes > MAX_LIST_PATH_BYTES { + return Err(bad(n, &format!("the keywords' full paths come to more than {} MiB", MAX_LIST_PATH_BYTES >> 20))); + } let path = match above.last() { Some((parent, _)) => format!("{parent}{SEP}{name}"), None => name.to_string(), @@ -189,6 +202,14 @@ pub fn closest<'a>(k: &str, from: &'a [String]) -> Option<&'a String> { from.iter().filter(|f| is_under(k, f)).max_by_key(|f| f.split(SEP).count()) } +/// Add each of `more` to `synonyms` that isn't there in any case, in order. One lowered set of +/// what is there, not a search of the growing list per synonym: an imported list can give a +/// keyword many synonyms, and the search was quadratic in them (CWE-407). +fn merge_synonyms(synonyms: &mut Vec, more: impl IntoIterator) { + let mut seen: std::collections::HashSet = synonyms.iter().map(|s| s.to_lowercase()).collect(); + synonyms.extend(more.into_iter().filter(|s| seen.insert(s.to_lowercase()))); +} + /// Keep the first of case-insensitively equal keywords. fn dedupe(v: &mut Vec) { let mut seen = std::collections::HashSet::new(); @@ -402,11 +423,7 @@ impl Catalog { Some(there) => { there.info.new_keywords_inside |= l.info.new_keywords_inside; there.info.keyword_shortcut |= l.info.keyword_shortcut; - for s in l.info.synonyms { - if !there.info.synonyms.iter().any(|x| same(x, &s)) { - there.info.synonyms.push(s); - } - } + merge_synonyms(&mut there.info.synonyms, l.info.synonyms); } None => { list.insert(path.to_lowercase(), ListedKeyword { path, info: l.info }); @@ -467,8 +484,11 @@ impl Catalog { let defaults = KeywordInfo::default(); let info = |path: &str| self.keyword_info(path).unwrap_or(&defaults); let mut out = ExportKeywords::default(); - let push = |v: &mut Vec, s: &str| { - if !s.is_empty() && !v.iter().any(|x| same(x, s)) { + // what each list holds, lowered: a search of the list per name was quadratic in a keyword's + // synonyms, which an imported list sets (CWE-407) + let (mut flat_seen, mut paths_seen) = (std::collections::HashSet::new(), std::collections::HashSet::new()); + let push = |v: &mut Vec, seen: &mut std::collections::HashSet, s: &str| { + if !s.is_empty() && seen.insert(s.to_lowercase()) { v.push(s.to_string()); } }; @@ -481,11 +501,11 @@ impl Catalog { let levels: Vec<&str> = path.split(SEP).collect(); let mut names = |at: usize, i: &KeywordInfo| { if let Some(name) = levels.get(at) { - push(&mut out.flat, name); + push(&mut out.flat, &mut flat_seen, name); } if i.export_synonyms { for s in &i.synonyms { - push(&mut out.flat, s.trim()); + push(&mut out.flat, &mut flat_seen, s.trim()); } } }; @@ -499,7 +519,7 @@ impl Catalog { } } } - push(&mut out.hierarchical, &path); + push(&mut out.hierarchical, &mut paths_seen, &path); } out } @@ -509,6 +529,13 @@ impl Catalog { /// keyword, a keyword left out of export in square brackets; by name. What the format can't hold /// is left out and named, so the file always reads back. pub fn keyword_list_text(&self) -> KeywordListFile { + self.keyword_list_text_within(MAX_LIST_PATH_BYTES) + } + + /// [`keyword_list_text`](Self::keyword_list_text) with the full paths written held to + /// `max_path_bytes` as [`parse_keyword_list`] holds them: a keyword past it is named + /// unwritable, with its subtree, so the file reads back. + fn keyword_list_text_within(&self, max_path_bytes: usize) -> KeywordListFile { // a name the list would read as something else, or not at all fn unwritable(name: &str) -> bool { name.trim().is_empty() @@ -519,14 +546,27 @@ impl Catalog { || (name.starts_with('[') && name.ends_with(']')) || (name.starts_with('{') && name.ends_with('}')) } - fn write(c: &Catalog, nodes: &[KeywordNode], depth: usize, file: &mut KeywordListFile) { + fn write( + c: &Catalog, + nodes: &[KeywordNode], + depth: usize, + file: &mut KeywordListFile, + path_bytes: &mut usize, + max_path_bytes: usize, + parent_len: usize, + ) { let tabs = "\t".repeat(depth); for n in nodes { // the keywords inside go with it: under its parent they'd be other keywords - if depth >= MAX_LEVELS || unwritable(&n.name) { + // (a line's full path is what the reader counts against its path budget) + // the path the reader rebuilds from the lines written: the parent as emitted, a + // separator, the name (a child's `n.path` can spell its parent in another case) + let emitted = if depth == 0 { n.name.len() } else { parent_len + SEP.len_utf8() + n.name.len() }; + if depth >= MAX_LEVELS || unwritable(&n.name) || path_bytes.saturating_add(emitted) > max_path_bytes { file.unwritable.push(n.path.clone()); continue; } + *path_bytes += emitted; let info = c.keyword_info(&n.path).cloned().unwrap_or_default(); if info.include_on_export { file.text.push_str(&format!("{tabs}{}\n", n.name)); @@ -536,19 +576,21 @@ impl Catalog { file.written.push(n.path.clone()); // the reader trims synonyms and takes one per spelling: an empty one, or another's // in a different case, is no loss; a line break or a “|” is - let mut synonyms: Vec<&str> = Vec::new(); + // (one lowered set, not a search of those written so far per synonym: quadratic + // in a keyword's synonyms, which an imported list sets — CWE-407) + let mut synonyms: std::collections::HashSet = std::collections::HashSet::new(); for s in info.synonyms.iter().map(|s| s.trim()) { - if s.is_empty() || synonyms.iter().any(|x| same(x, s)) { + if s.is_empty() || synonyms.contains(&s.to_lowercase()) { continue; } if s.chars().any(char::is_control) || s.contains(SEP) { file.unwritable.push(format!("{} {{{s}}}", n.path)); } else { - synonyms.push(s); + synonyms.insert(s.to_lowercase()); file.text.push_str(&format!("{tabs}\t{{{s}}}\n")); } } - write(c, &n.children, depth + 1, file); + write(c, &n.children, depth + 1, file, path_bytes, max_path_bytes, emitted); } } let mut file = KeywordListFile::default(); @@ -568,7 +610,7 @@ impl Catalog { } } } - write(self, &self.keyword_tree(), 0, &mut file); + write(self, &self.keyword_tree(), 0, &mut file, &mut 0, max_path_bytes, 0); file.unwritable.extend(over_deep); file } @@ -632,11 +674,7 @@ impl Catalog { // one the library has: the list's synonyms join its own (listed only if it gains some) let mut info_now = list.get(&key).map(|l| l.info.clone()).unwrap_or_default(); let before = info_now.synonyms.len(); - for s in &info.synonyms { - if !info_now.synonyms.iter().any(|x| same(x, s)) { - info_now.synonyms.push(s.clone()); - } - } + merge_synonyms(&mut info_now.synonyms, info.synonyms.iter().cloned()); if info_now.synonyms.len() > before { let path = list.get(&key).map_or(path, |l| l.path.clone()); list.insert(key, ListedKeyword { path, info: info_now }); diff --git a/crates/catalog/src/keywords/tests.rs b/crates/catalog/src/keywords/tests.rs index 6f50bd482e..6b9e505c0f 100644 --- a/crates/catalog/src/keywords/tests.rs +++ b/crates/catalog/src/keywords/tests.rs @@ -556,6 +556,17 @@ fn the_keyword_list_is_written_as_tab_indented_text() { assert_eq!(c.keyword_list_text().text, "beach\nEvents\n\tWeddings\n[Places]\n\tLisbon\n\t\t{Lisboa}\n"); } +/// The path budget the reader enforces holds for the writer too: a keyword past it, and its +/// subtree, is named unwritable instead of making a file `keyword.import` rejects. +#[test] +fn the_keyword_list_written_stays_within_the_path_budget() { + let (c, _) = lib(&[&["Places|Lisbon", "beach"], &["Events|Weddings"]]); + let file = c.keyword_list_text_within(17); + assert_eq!(file.text, "beach\nEvents\nPlaces\n"); + assert_eq!(file.unwritable, ["Events|Weddings", "Places|Lisbon"]); + assert_eq!(c.keyword_list_text().unwritable, Vec::::new()); +} + /// Reading a list gives each keyword's path and attributes: what was written comes back, and a /// file from elsewhere (Windows line ends, a byte-order mark, blank lines) reads too. #[test] @@ -765,3 +776,47 @@ fn keyword_tree_caps_a_keyword_at_the_list_depth() { let deepest = tree_paths(&c.keyword_tree()).iter().map(|(p, _)| p.split(SEP).count()).max().unwrap_or(0); assert_eq!(deepest, MAX_LEVELS); } + +/// A keyword list file spells each keyword's whole path, so one long keyword with many short ones +/// inside it expands into far more than its own size: past `MAX_LIST_PATH_BYTES` it is refused +/// with the line where that happened, instead of growing without bound (CWE-400). +#[test] +fn a_list_whose_paths_expand_past_the_budget_is_refused() { + let parent = "p".repeat(1 << 20); + let children = MAX_LIST_PATH_BYTES / parent.len() + 2; + let mut text = format!("{parent}\n"); + for i in 0..children { + text.push_str(&format!("\tc{i}\n")); + } + let err = parse_keyword_list(&text).unwrap_err().to_string(); + assert!(err.contains("full paths come to more than"), "{err}"); + // a list within the budget still reads + let ok = parse_keyword_list(&format!("{parent}\n\tchild\n")).unwrap(); + assert_eq!(ok.len(), 2); + assert_eq!(ok[1].0, format!("{parent}|child")); +} + +/// Many synonyms joining a keyword the library has, its export and its keyword list file each keep +/// every spelling once, in order, without a search of the growing list per synonym (CWE-407). +#[test] +fn many_synonyms_are_gathered_once_each_in_order() { + const N: usize = 40_000; + let mut c = Catalog::new(); + c.apply(Op::SetKeyword { path: "travel".into(), info: Some(with_synonyms(&["Trip"])) }).unwrap(); + let mut incoming: Vec = (0..N).map(|i| format!("syn{i}")).collect(); + incoming.push("TRIP".into()); + incoming.push("SYN7".into()); + let started = std::time::Instant::now(); + let (op, added, updated) = c.import_keywords_ops(&[("travel".into(), KeywordInfo { synonyms: incoming, ..KeywordInfo::default() })]); + c.apply(op).unwrap(); + assert_eq!((added, updated), (0, 1)); + let synonyms = c.keyword_info("travel").unwrap().synonyms.clone(); + assert_eq!(synonyms.len(), N + 1); + assert_eq!(synonyms[0], "Trip"); + assert_eq!(synonyms[8], "syn7"); + let exported = c.export_keywords(&["travel".into()]); + assert_eq!(exported.flat.len(), N + 2); + let file = c.keyword_list_text(); + assert_eq!(file.text.lines().count(), N + 2); + assert!(started.elapsed() < std::time::Duration::from_secs(20), "took {:?}", started.elapsed()); +} From ab39f7dbbcf719340590603a171beae8817ede95 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 22:58:42 -0500 Subject: [PATCH 46/51] web: a restore plans its writes first, each storage key once and within the file's bytes; workers are capped (#3669) --- apps/lightcraft-web/src/backup.rs | 36 ++++++++++++++++++++++++++++++ apps/lightcraft-web/src/safety.rs | 5 ++--- apps/lightcraft-web/src/web.rs | 2 +- apps/lightcraft-web/src/workers.rs | 19 +++++++++++++++- 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/apps/lightcraft-web/src/backup.rs b/apps/lightcraft-web/src/backup.rs index 281a32296d..6a4dd205e5 100644 --- a/apps/lightcraft-web/src/backup.rs +++ b/apps/lightcraft-web/src/backup.rs @@ -233,6 +233,25 @@ pub fn parse_central(cd: &[u8]) -> Result, String> { Ok(out) } +/// What a restore writes, each storage key once, checked against the whole directory before any +/// payload is read: a backup names a file once, so a directory naming the same destination again +/// is damaged or altered, and its entries' bytes can't add up to more than the file holds (a +/// directory can point many entries at one stored range and have each read, checked and written). +pub fn restore_plan<'a>(entries: &'a [ZipEntry], dir: &str, file_len: u64) -> Result, String> { + let mut seen = std::collections::HashSet::new(); + let mut plan = Vec::new(); + let mut bytes = 0u64; + for e in entries { + let Some(key) = restore_key(&e.name, dir) else { continue }; + if !seen.insert(key.clone()) { + return Err(format!("{}: listed twice (damaged or altered backup)", e.name)); + } + bytes = bytes.checked_add(e.size).filter(|&b| b <= file_len).ok_or_else(|| format!("{}: entries larger than the backup itself", e.name))?; + plan.push((key, e)); + } + Ok(plan) +} + /// Offset of an entry's data from its local header's first 30 bytes. pub fn data_offset(entry: &ZipEntry, local: &[u8]) -> Result { if rd32(local, 0) != Some(LOCAL_SIG) { @@ -342,6 +361,23 @@ mod tests { assert!(!valid_library_dir("library-../x") && !valid_library_dir("originals") && !valid_library_dir("library-")); } + #[test] + fn restore_plan_refuses_repeated_and_oversized_entries() { + let entry = |name: &str, size: u64| ZipEntry { name: name.into(), crc: 0, size, header: 0, method: 0 }; + let ok = [entry("README.txt", 10), entry("library/catalog.log", 10), entry("originals/abc123/a.jpg", 100)]; + let plan = restore_plan(&ok, "d", 200).unwrap(); + assert_eq!(plan.iter().map(|(k, _)| k.as_str()).collect::>(), ["d/catalog.log", "originals/abc123"]); + // one stored range named 3 times would be read, checked and written 3 times + let twice = [entry("originals/abc123/a.jpg", 100), entry("originals/abc123/b.jpg", 100)]; + assert!(restore_plan(&twice, "d", 1000).unwrap_err().contains("twice")); + let twice = [entry("library/catalog.log", 10), entry("library/catalog.log", 10)]; + assert!(restore_plan(&twice, "d", 1000).is_err()); + // entries claiming more bytes than the file has + let big = [entry("library/catalog.log", 150), entry("originals/abc123/a.jpg", 100)]; + assert!(restore_plan(&big, "d", 200).is_err()); + assert!(restore_plan(&[entry("library/catalog.log", u64::MAX), entry("library/presets.json", 1)], "d", u64::MAX).is_err()); + } + #[test] fn size_limits_are_errors_not_panics() { let mut w = ZipWriter { offset: MAX_BYTES - 10, ..Default::default() }; diff --git a/apps/lightcraft-web/src/safety.rs b/apps/lightcraft-web/src/safety.rs index 137b525039..8bffc818d0 100644 --- a/apps/lightcraft-web/src/safety.rs +++ b/apps/lightcraft-web/src/safety.rs @@ -13,7 +13,7 @@ use wasm_bindgen_futures::JsFuture; use crate::backend::Backend; use crate::backup::{ - ACTIVE_LIBRARY, README, ZipWriter, data_offset, find_central, original_entry, parse_central, restore_key, restored_dir_name, tail_len, + ACTIVE_LIBRARY, README, ZipWriter, data_offset, find_central, original_entry, parse_central, restore_plan, restored_dir_name, tail_len, valid_hash, verify, verify_original, }; use crate::files::{Files, LIBRARY_FILES}; @@ -214,8 +214,7 @@ pub async fn restore(file: web_sys::File, backend: Backend) -> Result>); @@ -244,7 +253,12 @@ impl Workers { return w; } let mut slots = WorkerSlots::default(); - for attempt in 0..n { + let mut failed = 0; + for attempt in 0..n.min(MAX_WORKERS) { + if failed >= MAX_FAILED_STARTS { + log::warn!("render workers: {failed} failed to start in a row; not trying the rest"); + break; + } // The callbacks name their worker by its place in `workers`; a failed attempt takes none. let i = slots.peek(); let opts = web_sys::WorkerOptions::new(); @@ -254,6 +268,7 @@ impl Workers { Ok(x) => x, Err(e) => { log::warn!("render worker {attempt}: {e:?}"); + failed += 1; continue; } }; @@ -274,10 +289,12 @@ impl Workers { worker.set_onmessage(None); worker.set_onerror(None); worker.terminate(); + failed += 1; continue; } w.0.borrow_mut().workers.push(W { worker, ready: false, dead: false, busy: None }); slots.commit(); + failed = 0; } w } From 7b80ffc32b957425eb5cb2682112c5154d3244b0 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 22:59:11 -0500 Subject: [PATCH 47/51] engine: font paths the build embeds stay inside the asset checkout and print as one cargo line (#3670) --- crates/engine/build.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/engine/build.rs b/crates/engine/build.rs index 7f5aa77776..f2573f0197 100644 --- a/crates/engine/build.rs +++ b/crates/engine/build.rs @@ -65,6 +65,7 @@ fn craft_fonts(dir: &Path, required: bool) -> Result { let manifest = dir.join("fonts/manifest.txt"); println!("cargo::rerun-if-changed={}", manifest.display()); let text = std::fs::read_to_string(&manifest).map_err(|e| format!("{}: {e}", manifest.display()))?; + let root = dir.canonicalize().map_err(|e| format!("{}: {e}", dir.display()))?; let wasm = std::env::var("CARGO_CFG_TARGET_ARCH").is_ok_and(|a| a == "wasm32"); let mut out = String::new(); let mut has_japanese_ui = false; @@ -82,7 +83,17 @@ fn craft_fonts(dir: &Path, required: bool) -> Result { continue; } let path = dir.join(file).canonicalize().map_err(|e| format!("{file}: {e}"))?; - println!("cargo::rerun-if-changed={}", path.display()); + // a font is a file of the checkout: a manifest entry or a link reaching outside it would + // embed some other file of the build machine + if !path.starts_with(&root) || !path.is_file() { + return Err(format!("{file}: not a file inside {}", root.display())); + } + let shown = path.display().to_string(); + // one cargo instruction per line: a line break in the path would print a second one + if shown.contains(['\n', '\r']) { + return Err(format!("{file}: the path contains a line break")); + } + println!("cargo::rerun-if-changed={shown}"); let scripts: Vec = scripts.split(',').map(|s| format!("{:?}", s.trim())).collect(); let _ = writeln!( out, From f618fc06a7551156d7c249eda5ab4dae20c99e26 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 22:59:41 -0500 Subject: [PATCH 48/51] security: staging and probe files are created new, never written through an existing entry (#3673) --- crates/denoise-core/src/product.rs | 21 ++++++++++++++++++++- crates/engine/src/camera_profiles.rs | 6 +++--- crates/engine/src/cmd/lut_profiles.rs | 4 +++- crates/engine/src/faces_index.rs | 12 ++++++------ crates/engine/src/slideshow_export.rs | 5 ++++- crates/engine/src/smart.rs | 10 +++++++++- crates/engine/src/smart/tests.rs | 15 +++++++++++++++ crates/preview/src/disk.rs | 6 +++++- 8 files changed, 65 insertions(+), 14 deletions(-) diff --git a/crates/denoise-core/src/product.rs b/crates/denoise-core/src/product.rs index c33b1d4c2a..09cf24245a 100644 --- a/crates/denoise-core/src/product.rs +++ b/crates/denoise-core/src/product.rs @@ -160,7 +160,10 @@ pub fn write(path: &Path, img: &Rgb32f, key: &str) -> Result<(), ProductError> { if let Some(dir) = path.parent() { std::fs::create_dir_all(dir)?; } - let mut f = std::io::BufWriter::new(std::fs::File::create(&tmp)?); + // a new temp file: an entry already under the name (a link planted in the cache folder + // would be written through) is removed, never opened + let _ = std::fs::remove_file(&tmp); + let mut f = std::io::BufWriter::new(std::fs::OpenOptions::new().write(true).create_new(true).open(&tmp)?); f.write_all(MAGIC)?; f.write_all(&[VERSION, 0, 0, 0])?; f.write_all(&(header.len() as u32).to_le_bytes())?; @@ -459,6 +462,22 @@ mod tests { std::env::temp_dir().join(format!("lc-denoise-{name}-{}.lcdn", std::process::id())) } + #[cfg(unix)] + #[test] + fn a_link_planted_under_the_temp_name_is_not_written_through() { + let p = temp("planted"); + let victim = temp("victim"); + std::fs::write(&victim, b"keep").unwrap(); + let _ = std::fs::remove_file(p.with_extension("tmp")); + std::os::unix::fs::symlink(&victim, p.with_extension("tmp")).unwrap(); + write(&p, &picture(16, 4), "k").unwrap(); + assert_eq!(std::fs::read(&victim).unwrap(), b"keep"); + assert!(read_header_at(&p).is_ok()); + for f in [p, victim] { + let _ = std::fs::remove_file(f); + } + } + fn picture(w: usize, h: usize) -> Rgb32f { Rgb32f { width: w, diff --git a/crates/engine/src/camera_profiles.rs b/crates/engine/src/camera_profiles.rs index 5f2c92f175..3c63773c1e 100644 --- a/crates/engine/src/camera_profiles.rs +++ b/crates/engine/src/camera_profiles.rs @@ -116,9 +116,9 @@ pub fn save(profile: &CameraProfile, dir: &Path) -> Result { std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; let path = dir.join(file_name(&profile.model)); let json = serde_json::to_vec_pretty(profile).map_err(|e| e.to_string())?; - let tmp = path.with_extension("json.tmp"); - std::fs::write(&tmp, json).map_err(|e| format!("{}: {e}", tmp.display()))?; - std::fs::rename(&tmp, &path).map_err(|e| format!("{}: {e}", path.display()))?; + // a fresh temp file renamed over the profile: never a fixed `.tmp` name, which a planted link + // would have written through + lightcraft_catalog::safe_file::write_atomic(&path, &json).map_err(|e| format!("{}: {e}", path.display()))?; Ok(path) } diff --git a/crates/engine/src/cmd/lut_profiles.rs b/crates/engine/src/cmd/lut_profiles.rs index b7a0d49928..56a0169a10 100644 --- a/crates/engine/src/cmd/lut_profiles.rs +++ b/crates/engine/src/cmd/lut_profiles.rs @@ -120,7 +120,9 @@ fn import(s: &mut Session, p: &Value) -> Result { let file = match &dir { Some(d) => { let f = d.join(format!("{}.cube", id.trim_start_matches("lut:"))); - std::fs::write(&f, &text).map_err(|e| bad(C, format!("{}: {e}", f.display())))?; + // a new file renamed into place: an entry already under the name (a link planted + // in the Profiles folder) is replaced, never written through + lightcraft_catalog::safe_file::write_atomic(&f, text.as_bytes()).map_err(|e| bad(C, format!("{}: {e}", f.display())))?; f.to_string_lossy().to_string() } // no library on disk: the registered LUT is all this session needs (a copy would only diff --git a/crates/engine/src/faces_index.rs b/crates/engine/src/faces_index.rs index 17bffbc193..5b23c58af4 100644 --- a/crates/engine/src/faces_index.rs +++ b/crates/engine/src/faces_index.rs @@ -214,9 +214,9 @@ impl Index { for (k, e) in &self.entries { buf.extend(self.record(k, e)); } - let part = path.with_extension("part"); - std::fs::write(&part, &buf)?; - std::fs::rename(&part, &path)?; + // a fresh temp file (never a `.part` entry left in the library, which could be a link + // to a file outside it) renamed over the cache + lightcraft_catalog::safe_file::write_atomic_nosync(&path, &buf)?; self.file_matches = true; self.rewrite = false; } @@ -367,9 +367,9 @@ impl Scanned { } else { let mut buf = self.header(); buf.extend(self.set.iter().flat_map(|id| id.to_le_bytes())); - let part = path.with_extension("part"); - std::fs::write(&part, &buf)?; - std::fs::rename(&part, &path)?; + // a fresh temp file (never a `.part` entry left in the library, which could be a link + // to a file outside it) renamed over the cache + lightcraft_catalog::safe_file::write_atomic_nosync(&path, &buf)?; self.file_matches = true; self.rewrite = false; } diff --git a/crates/engine/src/slideshow_export.rs b/crates/engine/src/slideshow_export.rs index e91c1ed346..94a7bc7dd6 100644 --- a/crates/engine/src/slideshow_export.rs +++ b/crates/engine/src/slideshow_export.rs @@ -389,7 +389,10 @@ impl PartFile { let mut name = target.file_name().ok_or_else(|| format!("{}: not a file name", target.display()))?.to_os_string(); name.push(".part"); let part = target.with_file_name(name); - let f = std::fs::File::create(&part).map_err(|e| format!("{}: {e}", part.display()))?; + // an entry already under the staging name (a leftover, or a link planted to point the + // encoder's bytes at another file) is removed, never opened and written through + let _ = std::fs::remove_file(&part); + let f = std::fs::OpenOptions::new().write(true).create_new(true).open(&part).map_err(|e| format!("{}: {e}", part.display()))?; Ok((PartFile { part, target: target.to_path_buf(), done: false }, f)) } diff --git a/crates/engine/src/smart.rs b/crates/engine/src/smart.rs index b936830f83..156f33d42a 100644 --- a/crates/engine/src/smart.rs +++ b/crates/engine/src/smart.rs @@ -268,7 +268,15 @@ pub fn check_writable(dir: &Path) -> Result<(), String> { std::fs::create_dir(dir).map_err(|e| format!("can't create {}: {e}", dir.display()))?; } let probe = dir.join(format!(".lightcraft-write-test-{}", std::process::id())); - std::fs::write(&probe, b"x").map_err(|e| format!("{} is not writable (is the drive full or read-only?): {e}", dir.display()))?; + // the probe is a new entry of ours: an existing one (a link planted under the name would be + // written through) is removed first, never opened + let _ = std::fs::remove_file(&probe); + std::fs::OpenOptions::new() + .write(true) + .create_new(true) + .open(&probe) + .and_then(|mut f| std::io::Write::write_all(&mut f, b"x")) + .map_err(|e| format!("{} is not writable (is the drive full or read-only?): {e}", dir.display()))?; let _ = std::fs::remove_file(&probe); Ok(()) } diff --git a/crates/engine/src/smart/tests.rs b/crates/engine/src/smart/tests.rs index e4bbd548cd..b1eeefc221 100644 --- a/crates/engine/src/smart/tests.rs +++ b/crates/engine/src/smart/tests.rs @@ -18,6 +18,21 @@ fn writable_check_does_not_create_missing_parents() { let _ = std::fs::remove_dir_all(&base); } +#[cfg(unix)] +#[test] +fn writable_probe_never_writes_through_a_planted_link() { + let base = temp("probe-link"); + std::fs::create_dir_all(&base).unwrap(); + let victim = base.join("victim"); + std::fs::write(&victim, b"keep").unwrap(); + let probe = base.join(format!(".lightcraft-write-test-{}", std::process::id())); + std::os::unix::fs::symlink(&victim, &probe).unwrap(); + assert!(check_writable(&base).is_ok()); + assert_eq!(std::fs::read(&victim).unwrap(), b"keep"); + assert!(!probe.exists()); + let _ = std::fs::remove_dir_all(&base); +} + #[test] fn migrate_moves_leaves_or_discards() { let base = temp("migrate"); diff --git a/crates/preview/src/disk.rs b/crates/preview/src/disk.rs index 7510ffa383..ca82870483 100644 --- a/crates/preview/src/disk.rs +++ b/crates/preview/src/disk.rs @@ -191,7 +191,11 @@ impl DiskCache { let used = total.get_or_insert_with(|| self.scan().iter().map(|f| f.1).sum()); let old_len = std::fs::metadata(&p).map_or(0, |m| m.len()); let tmp = p.with_extension(format!("tmp{:?}", std::thread::current().id()).replace(['(', ')'], "")); - if std::fs::write(&tmp, &bytes).is_err() || std::fs::rename(&tmp, &p).is_err() { + // a new temp file: an entry already under the name (a link planted in the cache would be + // written through) is removed, never opened + let _ = std::fs::remove_file(&tmp); + let written = std::fs::OpenOptions::new().write(true).create_new(true).open(&tmp).and_then(|mut f| std::io::Write::write_all(&mut f, &bytes)); + if written.is_err() || std::fs::rename(&tmp, &p).is_err() { let _ = std::fs::remove_file(&tmp); return; } From 1c5cbce83520d36cf77003f8bc1f70e718689645 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 23:00:31 -0500 Subject: [PATCH 49/51] security(engine): the library folder is protected from folder.move however its path is spelled (#3676) --- crates/engine/src/cmd/browse.rs | 8 +++++++- crates/engine/src/tests/folders.rs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/browse.rs b/crates/engine/src/cmd/browse.rs index 9a235306c6..0eb126c40f 100644 --- a/crates/engine/src/cmd/browse.rs +++ b/crates/engine/src/cmd/browse.rs @@ -522,7 +522,13 @@ pub(crate) fn move_folder(s: &mut Session, from: &str, to: &str) -> std::result: if !src.is_dir() { return Err(format!("{from} is not a folder")); } - if s.library.as_ref().is_some_and(|l| l.dir.starts_with(src) || src.starts_with(&l.dir)) { + // compared as the folders are on disk (a differently spelled or linked path to the library + // names the same folder), with the spelling as a fallback where that can't be told + let same_folder = |l: &Path| { + let (lib, chosen) = (l.canonicalize().unwrap_or_else(|_| l.to_path_buf()), src.canonicalize().unwrap_or_else(|_| src.to_path_buf())); + lib.starts_with(&chosen) || chosen.starts_with(&lib) || l.starts_with(src) || src.starts_with(l) + }; + if s.library.as_ref().is_some_and(|l| same_folder(&l.dir)) { return Err("the library's own folders can't be moved here".into()); } // Repairs retain their exact old paths, including those on surviving history entries. diff --git a/crates/engine/src/tests/folders.rs b/crates/engine/src/tests/folders.rs index 34017fc977..6877572768 100644 --- a/crates/engine/src/tests/folders.rs +++ b/crates/engine/src/tests/folders.rs @@ -701,3 +701,18 @@ fn a_destination_without_write_permission_reports_an_error_and_keeps_everything( } std::fs::set_permissions(dir.0.join("ro"), std::fs::Permissions::from_mode(0o755)).unwrap(); } + +#[cfg(unix)] +#[test] +fn the_library_folder_is_protected_however_its_path_is_spelled() { + let dir = Scratch::new("library-link"); + let lib = dir.0.join("library"); + let mut s = Session::new(); + s.open_library(&lib, true).unwrap(); + // a link to the library's folder names the same folder on disk + std::os::unix::fs::symlink(&lib, dir.0.join("alias")).unwrap(); + std::fs::create_dir_all(dir.0.join("elsewhere")).unwrap(); + let err = s.execute("folder.move", &json!({"path": dir.path("alias"), "into": dir.path("elsewhere")})).unwrap_err().to_string(); + assert!(err.contains("library's own folders"), "{err}"); + assert!(lib.is_dir() && !dir.0.join("elsewhere/alias").exists()); +} From 4d42507bbcc180df831435c8ef2c29740b5216aa Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Tue, 3 Nov 2026 23:00:51 -0500 Subject: [PATCH 50/51] security(raster, segment): resample tall narrow images columns first (#3678) --- crates/raster/src/resample.rs | 30 +++++++++++++++++++++++++-- crates/segment/src/mask.rs | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/crates/raster/src/resample.rs b/crates/raster/src/resample.rs index afe0c10d76..4664dfa4b3 100644 --- a/crates/raster/src/resample.rs +++ b/crates/raster/src/resample.rs @@ -115,12 +115,22 @@ impl Pixel for [f32; 4] { } } -/// Resize to `w × h` (separable; horizontal pass then vertical pass). +/// Resize to `w × h` (separable; horizontal pass then vertical pass, or the other way round when +/// that intermediate would be far larger: widening a tall, narrow strip kept every source row at +/// the new width, gigabytes from a small source). pub fn resize(img: &Image, w: usize, h: usize, filter: Filter) -> Image { let (w, h) = (w.max(1), h.max(1)); if img.width == w && img.height == h { return img.clone(); } + if w.saturating_mul(img.height) > img.width.saturating_mul(h).saturating_mul(4) { + return horizontal(&vertical(img, h, filter), w, filter); + } + vertical(&horizontal(img, w, filter), h, filter) +} + +/// Each row resampled to `w` wide. +fn horizontal(img: &Image, w: usize, filter: Filter) -> Image { let wx = weights(img.width, w, filter); let mut tmp = Image::::new(w, img.height); par_rows(&mut tmp.data, w, |y, row| { @@ -134,12 +144,18 @@ pub fn resize(img: &Image, w: usize, h: usize, filter: Filter) -> I *o = acc; } }); + tmp +} + +/// Each column resampled to `h` tall. +fn vertical(img: &Image, h: usize, filter: Filter) -> Image { + let w = img.width; let wy = weights(img.height, h, filter); let mut out = Image::::new(w, h); par_rows(&mut out.data, w, |y, row| { let (lo, ws) = &wy[y]; for (k, wt) in ws.iter().enumerate() { - let src = tmp.row(lo + k); + let src = img.row(lo + k); for (o, s) in row.iter_mut().zip(src) { *o = o.madd(*s, *wt); } @@ -178,6 +194,16 @@ pub fn half(img: &Image) -> Image { mod tests { use super::*; + #[test] + fn widening_a_tall_strip_resamples_its_columns_first() { + // 1 × 400 000 to 4000 × 8: horizontal-first kept 400 000 rows of 4000 (25 GB of RGBA f32) + let mut strip = Image::<[f32; 4]>::new(1, 400_000); + strip.data.iter_mut().for_each(|p| *p = [0.25, 0.5, 0.75, 1.0]); + let out = resize(&strip, 4000, 8, Filter::Mitchell); + assert_eq!((out.width, out.height), (4000, 8)); + assert!(out.data.iter().all(|p| (p[1] - 0.5).abs() < 1e-3), "a flat strip stays flat"); + } + #[test] fn constant_image_stays_constant() { let img = Image::::filled(37, 21, 0.42); diff --git a/crates/segment/src/mask.rs b/crates/segment/src/mask.rs index af349a2625..d884659aab 100644 --- a/crates/segment/src/mask.rs +++ b/crates/segment/src/mask.rs @@ -34,6 +34,31 @@ pub fn preprocess(rgb: &[u8], w: usize, h: usize, side: usize) -> Result w.saturating_mul(4) { + let mut t = vec![0u8; n]; + for y in 0..h { + for x in 0..w { + if let (Some(d), Some(s)) = (t.get_mut((x * h + y) * 3..(x * h + y) * 3 + 3), rgb.get((y * w + x) * 3..(y * w + x) * 3 + 3)) { + d.copy_from_slice(s); + } + } + } + let o = preprocess(&t, h, w, side)?; + let mut out = vec![0f32; o.len()]; + for c in 0..3 { + for oy in 0..side { + for ox in 0..side { + if let (Some(d), Some(v)) = (out.get_mut((c * side + oy) * side + ox), o.get((c * side + ox) * side + oy)) { + *d = *v; + } + } + } + } + return Ok(out); + } let (wx, wy) = (weights(w, side), weights(h, side)); // horizontal pass: h rows × side columns × 3 let mut tmp = vec![0f32; h * side * 3]; @@ -117,6 +142,19 @@ mod tests { #[test] fn short_buffers_are_errors() { assert!(preprocess(&[0; 5], 2, 2, 4).is_err()); + // a strip 1 pixel wide and 200 000 tall: resampled transposed, so the intermediate is + // side × 1 rather than 200 000 × side; it equals the resample of its transposed twin + let h = 200_000usize; + let strip: Vec = (0..h * 3).map(|i| (i / 3 % 251) as u8).collect(); + let a = preprocess(&strip, 1, h, 8).unwrap(); + let b = preprocess(&strip, h, 1, 8).unwrap(); + for c in 0..3 { + for y in 0..8 { + for x in 0..8 { + assert_eq!(a[(c * 8 + y) * 8 + x], b[(c * 8 + x) * 8 + y]); + } + } + } assert!(preprocess(&[], 0, 0, 4).is_err()); } From 1c1eec51796b421d84e8a61ce663b334fdf56ccb Mon Sep 17 00:00:00 2001 From: FredApps Date: Wed, 4 Nov 2026 11:58:39 +0100 Subject: [PATCH 51/51] security(lensdb): refuse a non-regular *.xml before opening it (opening a FIFO blocks) The pathname is checked before File::open as well as the opened file after it: File::open on a FIFO blocks until a writer appears, so the post-open check alone never ran for one. Adds a unix regression test that hung before the fix. --- crates/lensdb/src/lib.rs | 6 +++++- crates/lensdb/src/tests.rs | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs index 91f6723c1c..e34e07f862 100644 --- a/crates/lensdb/src/lib.rs +++ b/crates/lensdb/src/lib.rs @@ -280,7 +280,11 @@ fn exif_focal_range(exif: &[String]) -> Option<(f64, f64)> { fn read_capped(path: &Path) -> Result { use std::io::Read; let io = |e: std::io::Error| Error::Io(e.to_string()); - // the opened file is checked, not the pathname: a link or FIFO swapped in after a pathname check is refused too + // the pathname is checked before opening (opening a FIFO for reading blocks until a writer appears), then the + // opened file again: a link or FIFO swapped in after the pathname check is refused too + if !std::fs::metadata(path).map_err(io)?.is_file() { + return Err(Error::Io(format!("{}: not a regular file", path.display()))); + } let file = std::fs::File::open(path).map_err(io)?; let meta = file.metadata().map_err(io)?; if !meta.is_file() { diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs index e535330a6e..9a25b53b1e 100644 --- a/crates/lensdb/src/tests.rs +++ b/crates/lensdb/src/tests.rs @@ -417,3 +417,21 @@ fn load_dir_refuses_a_non_regular_xml_entry() { assert!(db.is_empty()); assert_eq!(errs.len(), 1, "{errs:?}"); } + +/// A FIFO named `*.xml` is refused without opening it: opening a FIFO for reading blocks until a writer appears. +#[cfg(unix)] +#[test] +fn load_dir_refuses_a_fifo_without_blocking() { + let dir = std::env::temp_dir().join(format!("lensdb-test-fifo-{}", std::process::id())); + std::fs::create_dir(&dir).unwrap(); + let fifo = dir.join("f.xml"); + let made = std::process::Command::new("mkfifo").arg(&fifo).status().is_ok_and(|s| s.success()); + if !made { + std::fs::remove_dir_all(&dir).unwrap(); + return; + } + let (db, errs) = LensDb::load_dir(&dir).unwrap(); + std::fs::remove_dir_all(&dir).unwrap(); + assert!(db.is_empty()); + assert_eq!(errs.len(), 1, "{errs:?}"); +}