Skip to content

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

LR-EDIT-OPTICS-PROFILE: fixed-lens lookup only by name-free lookups; no-make models and infinite focals

Andrew Frahn0b91a1de87c2
diff --git a/crates/lensdb/src/lib.rs b/crates/lensdb/src/lib.rs
index b1358a3fa..cf6b2ec14 100644
--- a/crates/lensdb/src/lib.rs
+++ b/crates/lensdb/src/lib.rs
@@ -160,12 +160,15 @@ impl LensDb {
/// The camera with this EXIF make and model.
pub fn find_camera(&self, make: &str, model: &str) -> Option<&Camera> {
let mk = tokens(make);
- let md = strip_maker(tokens(model), &mk);
+ let raw = tokens(model);
+ let md = strip_maker(raw.clone(), &mk);
if md.is_empty() {
return None;
}
self.cameras.iter().find(|c| {
let cm = tokens(&c.maker);
+ // with no EXIF make, the model may still start with the maker's word
+ let md = if mk.is_empty() { strip_maker(raw.clone(), &cm) } else { md.clone() };
maker_matches(&cm, &mk) && strip_maker(tokens(&c.model), &cm) == md
})
}
@@ -173,7 +176,7 @@ impl LensDb {
/// The best profile for an EXIF lens model (and lens make, when the file has one), preferring lenses for the
/// camera's mount. `None` when no name is a convincing match.
pub fn find_lens(&self, lens_make: Option<&str>, lens_model: &str, camera: Option<&Camera>) -> Option<&Profile> {
- self.best_lens(lens_make, lens_model, camera, |_| true)
+ self.best_lens(lens_make, lens_model, camera, |_| true).or_else(|| self.find_fixed_lens(&tokens(lens_model), camera?))
}
/// [`find_lens`](Self::find_lens) among the profiles one of whose names is exactly `name`
@@ -206,10 +209,7 @@ impl LensDb {
best = Some((score, lens));
}
}
- match best {
- Some((_, l)) => Some(l),
- None => self.find_fixed_lens(&exif, camera?),
- }
+ best.map(|(_, l)| l)
}
/// Compact cameras with a built-in lens: lensfun names the lens after the camera and gives it the camera's own
@@ -233,6 +233,9 @@ impl LensDb {
return (cands.len() == 1).then(|| cands[0]);
};
let (lo, hi) = (lo * 0.9, hi * 1.1);
+ if !lo.is_finite() || !hi.is_finite() {
+ return None;
+ }
let mut best: Option<(f64, &Profile)> = None;
for p in cands {
let focals: Vec<f64> =
@@ -262,7 +265,7 @@ fn strip_maker(model: Vec<String>, maker: &[String]) -> Vec<String> {
/// The numbers before the first "mm" in tokenised EXIF lens text: (shortest, longest) focal length.
fn exif_focal_range(exif: &[String]) -> Option<(f64, f64)> {
let end = exif.iter().position(|t| t == "mm")?;
- let nums: Vec<f64> = exif.get(..end)?.iter().filter_map(|t| t.parse::<f64>().ok()).filter(|v| *v > 0.0).collect();
+ let nums: Vec<f64> = exif.get(..end)?.iter().filter_map(|t| t.parse::<f64>().ok()).filter(|v| v.is_finite() && *v > 0.0).collect();
let lo = nums.iter().copied().fold(f64::INFINITY, f64::min);
let hi = nums.iter().copied().fold(0.0, f64::max);
(lo.is_finite() && hi > 0.0).then_some((lo, hi))
diff --git a/crates/lensdb/src/tests.rs b/crates/lensdb/src/tests.rs
index d63680883..1da57a9d2 100644
--- a/crates/lensdb/src/tests.rs
+++ b/crates/lensdb/src/tests.rs
@@ -304,6 +304,14 @@ fn built_in_lens_matches_by_camera_mount_and_focal_range() {
let l = db.find_lens(None, "6.0-72.0 mm f/2.7-3.5", cam).unwrap();
assert_eq!(l.mounts, ["canonS5"]);
assert!(db.find_lens(None, "6.0-72.0 mm f/2.7-3.5", None).is_none());
+ // a file with no make still finds the camera by its full model name
+ assert!(db.find_camera("", "Canon PowerShot S5 IS").is_some());
+ // the exact-name lookup never falls back to the camera's mount
+ assert!(db.find_lens_named(None, "6.0-72.0 mm f/2.7-3.5", cam).is_none());
+ // a focal length that overflows to infinity is ignored, not taken as an unbounded range
+ let huge = format!("6.0-{}9 mm f/2.7-3.5", "9".repeat(400));
+ assert_eq!(db.find_lens(None, &huge, cam).unwrap().mounts, ["canonS5"]);
+ assert_eq!(exif_focal_range(&tokens(&huge)), Some((6.0, 6.0)));
}
/// A hand-edited or corrupt database may carry any number: infinities, NaN, enormous or negative