diff --git a/crates/catalog/src/lib.rs b/crates/catalog/src/lib.rs index 8c873a6b92..1d33d3f029 100644 --- a/crates/catalog/src/lib.rs +++ b/crates/catalog/src/lib.rs @@ -1153,7 +1153,7 @@ impl Catalog { } Op::SetEmbeddedLens { id, lens } => { let p = self.photo_mut(id)?; - Op::SetEmbeddedLens { id, lens: std::mem::replace(&mut p.embedded_lens, lens.map(|l| *l)).map(Box::new) } + Op::SetEmbeddedLens { id, lens: std::mem::replace(&mut p.embedded_lens, lens) } } Op::SetAsShotWb { id, wb } => { if wb.is_some_and(|(t, tint)| !(t.is_finite() && tint.is_finite())) { diff --git a/crates/catalog/src/model.rs b/crates/catalog/src/model.rs index f09096e76d..86bd67676f 100644 --- a/crates/catalog/src/model.rs +++ b/crates/catalog/src/model.rs @@ -310,7 +310,7 @@ pub struct Photo { /// Lens corrections embedded in the file (DNG `WarpRectilinear` / `FixVignetteRadial`), applied when /// "Enable Profile Corrections" is on. #[serde(default, skip_serializing_if = "Option::is_none")] - pub embedded_lens: Option, + pub embedded_lens: Option>, /// A virtual copy: the photo it was copied from (it shares that photo's file but has its own /// settings, metadata and history). #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/crates/catalog/src/tests.rs b/crates/catalog/src/tests.rs index ed9c49a12e..e14818eaa1 100644 --- a/crates/catalog/src/tests.rs +++ b/crates/catalog/src/tests.rs @@ -661,7 +661,7 @@ fn set_embedded_lens_is_undoable_and_journaled() { 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(); - assert_eq!(c.photo(a).unwrap().embedded_lens, Some(lens)); + assert_eq!(c.photo(a).unwrap().embedded_lens, Some(Box::new(lens))); let inv: Op = serde_json::from_str(&serde_json::to_string(&inv).unwrap()).unwrap(); c.apply(inv).unwrap(); assert_eq!(c.photo(a).unwrap().embedded_lens, None); @@ -1187,3 +1187,18 @@ fn a_batch_that_cannot_be_rolled_back_whole_makes_earlier_changes_unknown() { assert_eq!(c.touched_since(start), None, "so nothing is said of what changed"); assert_eq!(c.touched_since(c.revision), Some(vec![]), "from after it, it is known again"); } + +/// `Photo::embedded_lens` is boxed to keep photo records small; the box doesn't change the serialized form: +/// a photo written with the lens reads back with it, and its JSON is the bare lens, as before the box. +#[test] +fn boxed_embedded_lens_serializes_like_the_bare_lens() { + let lens = lightcraft_develop::EmbeddedLens { warp: Some(Default::default()), vignette: None, stages: Default::default(), ..Default::default() }; + let mut p = Photo::new(PhotoId(1), Source::File { path: "/x.dng".into() }, "x.dng", "DNG", 4000, 3000, "2026-10-06T00:00:00"); + p.embedded_lens = Some(Box::new(lens)); + let json = serde_json::to_value(&p).unwrap(); + assert_eq!(json["embedded_lens"], serde_json::to_value(lens).unwrap()); + let back: Photo = serde_json::from_value(json).unwrap(); + assert_eq!(back.embedded_lens.as_deref(), Some(&lens)); + p.embedded_lens = None; + assert!(serde_json::to_value(&p).unwrap().get("embedded_lens").is_none(), "no lens, no field"); +} diff --git a/crates/engine/src/cmd/convert.rs b/crates/engine/src/cmd/convert.rs index af81d3cae4..cf0991f4e3 100644 --- a/crates/engine/src/cmd/convert.rs +++ b/crates/engine/src/cmd/convert.rs @@ -294,7 +294,7 @@ fn legacy_shutter_text(t: f64) -> Option { /// what import gave it, the lens correction is turned on as import would; an edited photo keeps /// its settings. pub(crate) fn lens_ops(id: lightcraft_catalog::PhotoId, ph: &lightcraft_catalog::Photo, info: &crate::media::ProbeInfo) -> Vec { - if info.embedded_lens == ph.embedded_lens { + if info.embedded_lens.as_ref() == ph.embedded_lens.as_deref() { return Vec::new(); } let mut ops = vec![Op::SetEmbeddedLens { id, lens: info.embedded_lens.map(Box::new) }]; @@ -601,7 +601,7 @@ mod tests { c.apply(op).unwrap(); } let after = c.photo(lightcraft_catalog::PhotoId(1)).unwrap(); - assert_eq!(after.embedded_lens, Some(lens())); + assert_eq!(after.embedded_lens, Some(Box::new(lens()))); assert!(!after.is_edited()); assert_eq!(*after.develop, after.camera_defaults()); assert!(lens_ops(after.id, after, &info).is_empty(), "nothing more to do the second time"); @@ -622,7 +622,7 @@ mod tests { #[test] fn reload_leaves_a_photo_whose_user_switched_the_correction_off_alone() { let mut ph = old_rw2(); - ph.embedded_lens = Some(lens()); + ph.embedded_lens = Some(Box::new(lens())); let mut d = ph.camera_defaults(); d.optics.lens_profile = false; ph.develop = std::sync::Arc::new(d); diff --git a/crates/engine/src/cmd/lens_refresh.rs b/crates/engine/src/cmd/lens_refresh.rs index a84ff15ab9..c5b03ad834 100644 --- a/crates/engine/src/cmd/lens_refresh.rs +++ b/crates/engine/src/cmd/lens_refresh.rs @@ -33,7 +33,7 @@ pub struct Missing { 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); + let from_database = |p: &Photo| p.embedded_lens.as_deref().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)) @@ -82,7 +82,7 @@ pub fn apply(s: &mut Session, results: Vec<(Missing, std::result::Result SourceInfo { raw: true, as_shot_temp: temp, as_shot_tint: tint, - lens: p.embedded_lens, + lens: p.embedded_lens.as_deref().copied(), relative_wb: relative, // (a fitted-look raw edited in Kelvin: its camera's matrices give the scale, see `files::WbScaleOf`) legacy_relative_wb: p.fitted_look() && !relative, diff --git a/crates/engine/src/media/session.rs b/crates/engine/src/media/session.rs index 4bf40bbf57..9b0b0509fd 100644 --- a/crates/engine/src/media/session.rs +++ b/crates/engine/src/media/session.rs @@ -367,7 +367,7 @@ impl crate::Session { p.height.max(1) as usize, &job.settings, apply_crop, - p.embedded_lens.as_ref(), + p.embedded_lens.as_deref(), ); let ppl = frame.px_per_long(full_w); lightcraft_pipeline::spots::window_for_reads_checked(&job.settings, &frame, full_w, full_h, ppl, window.clamped(full_w, full_h))?; diff --git a/crates/engine/src/slide.rs b/crates/engine/src/slide.rs index 009a2e2ac3..4ddf6cca6e 100644 --- a/crates/engine/src/slide.rs +++ b/crates/engine/src/slide.rs @@ -87,7 +87,7 @@ impl crate::Session { p.height.max(1) as usize, &p.develop, true, - p.embedded_lens.as_ref(), + p.embedded_lens.as_deref(), ); let a = frame.aspect(); Some(if a.is_finite() && a > 0.0 { a } else { 1.5 }) diff --git a/crates/engine/src/tests/constrain_crop.rs b/crates/engine/src/tests/constrain_crop.rs index 9f01ed05d6..a07ad2fea7 100644 --- a/crates/engine/src/tests/constrain_crop.rs +++ b/crates/engine/src/tests/constrain_crop.rs @@ -17,7 +17,7 @@ fn fits(s: &Session) -> bool { let p = s.catalog.photo(s.active().unwrap()).unwrap(); let mut d = (*p.develop).clone(); d.geometry.constrain_crop = true; - let f = Frame::with_lens(p.width as usize, p.height as usize, &d, true, p.embedded_lens.as_ref()); + let f = Frame::with_lens(p.width as usize, p.height as usize, &d, true, p.embedded_lens.as_deref()); let (a, b) = (f.crop.rect, p.develop.crop.geometry.rect); [a.x0 - b.x0, a.y0 - b.y0, a.x1 - b.x1, a.y1 - b.y1].iter().all(|d| d.abs() < 1e-9) } diff --git a/crates/engine/src/tests/lens_refresh.rs b/crates/engine/src/tests/lens_refresh.rs index d3bf3a3574..a00406a910 100644 --- a/crates/engine/src/tests/lens_refresh.rs +++ b/crates/engine/src/tests/lens_refresh.rs @@ -19,7 +19,7 @@ fn lens(k: f64) -> EmbeddedLens { } fn lens_of(s: &Session, id: PhotoId) -> Option { - s.catalog.photo(id).and_then(|p| p.embedded_lens) + s.catalog.photo(id).and_then(|p| p.embedded_lens.as_deref().copied()) } /// Two demo photos "taken with different lenses". @@ -157,7 +157,7 @@ fn photos_get_their_profile_when_the_database_arrives() { } fn with_lens(mut p: Photo, l: Option, on: bool) -> Photo { - p.embedded_lens = l; + p.embedded_lens = l.map(Box::new); let mut d = (*p.develop).clone(); d.optics.lens_profile = on; p.develop = Arc::new(d); diff --git a/crates/engine/src/tests/merge.rs b/crates/engine/src/tests/merge.rs index ed524ae3d2..432ad14ca1 100644 --- a/crates/engine/src/tests/merge.rs +++ b/crates/engine/src/tests/merge.rs @@ -100,8 +100,8 @@ fn hdr_merge_keeps_the_reference_frames_lens_corrections() { let reference = s.catalog.photo(lightcraft_catalog::PhotoId(ids[1])).unwrap().clone(); let expect = if orientation == Orientation::Normal { (452, 304) } else { (304, 452) }; assert_eq!(((merged.width, merged.height), (reference.width, reference.height)), (expect, expect)); - let a = reference.embedded_lens.expect("the source's correction"); - let b = merged.embedded_lens.expect("the merged DNG carries the reference frame's correction"); + let a = reference.embedded_lens.as_deref().copied().expect("the source's correction"); + let b = merged.embedded_lens.as_deref().copied().expect("the merged DNG carries the reference frame's correction"); let (wa, wb) = (a.warp.unwrap(), b.warp.unwrap()); let (va, vb) = (a.vignette.unwrap(), b.vignette.unwrap()); let (w, h) = (expect.0 as f64, expect.1 as f64); diff --git a/crates/ui-egui/examples/thumb_request_bench.rs b/crates/ui-egui/examples/thumb_request_bench.rs index 728059b0e3..091482eda0 100644 --- a/crates/ui-egui/examples/thumb_request_bench.rs +++ b/crates/ui-egui/examples/thumb_request_bench.rs @@ -207,7 +207,7 @@ fn real() { p.kind = probe.kind; p.meta = probe.meta; p.as_shot_wb = probe.as_shot_wb; - p.embedded_lens = probe.embedded_lens; + p.embedded_lens = probe.embedded_lens.map(Box::new); p.content_hash = probe.content_hash; p.preview_only = probe.preview_only; lightcraft_engine::import::apply_import_defaults(&session, &mut p); diff --git a/crates/ui-egui/src/lens_db.rs b/crates/ui-egui/src/lens_db.rs index 67933e7041..8ee309436e 100644 --- a/crates/ui-egui/src/lens_db.rs +++ b/crates/ui-egui/src/lens_db.rs @@ -185,7 +185,7 @@ fn database_name(camera: &str, lens: &str) -> Option { /// none. pub fn readout(app: &LightcraftApp, p: &lightcraft_catalog::Photo) -> String { use lightcraft_develop::LensSource; - match p.embedded_lens.map(|l| l.source) { + match p.embedded_lens.as_deref().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()); @@ -219,10 +219,10 @@ mod tests { 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()); + p.embedded_lens = Some(Box::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() }); + p.embedded_lens = Some(Box::new(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/panels/compare.rs b/crates/ui-egui/src/panels/compare.rs index f3cd9821b3..4e25376485 100644 --- a/crates/ui-egui/src/panels/compare.rs +++ b/crates/ui-egui/src/panels/compare.rs @@ -222,7 +222,7 @@ fn frame_of(photo: &lightcraft_catalog::Photo) -> lightcraft_pipeline::geometry: photo.height.max(1) as usize, &photo.develop, true, - photo.embedded_lens.as_ref(), + photo.embedded_lens.as_deref(), ) } diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs index ed32f1e3d9..b437ad7ebf 100644 --- a/crates/ui-egui/src/panels/detail.rs +++ b/crates/ui-egui/src/panels/detail.rs @@ -164,7 +164,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) { // the full-screen preview shows the photo only: no tool overlays let right = if fullscreen { RightPanel::None } else { app.ui.right }; let crop_tool = right == RightPanel::Crop; - let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &d, !crop_tool, photo.embedded_lens.as_ref()); + let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &d, !crop_tool, photo.embedded_lens.as_deref()); let aspect = frame.aspect() as f32; let ppp = ui.ctx().pixels_per_point(); let area = canvas.shrink(loupe_margin(fullscreen, crop_tool)); @@ -538,7 +538,7 @@ fn neighbour_job( with_crop: bool, ) -> Option { let np = app.session.catalog.photo(nid)?; - let nf = Frame::with_lens(np.width.max(1) as usize, np.height.max(1) as usize, &np.develop, with_crop, np.embedded_lens.as_ref()); + let nf = Frame::with_lens(np.width.max(1) as usize, np.height.max(1) as usize, &np.develop, with_crop, np.embedded_lens.as_deref()); let na = nf.aspect() as f32; let nr = fit_rect(main_area, na, app.ui.zoom, output_px(&nf), ppp, app.ui.pan); let nw = app.ui.settings.prefetch_edge(nr.width().max(nr.height()) * ppp, output_px(&nf).into_iter().max().unwrap_or(1), texture_side); diff --git a/crates/ui-egui/src/panels/detail/geometry.rs b/crates/ui-egui/src/panels/detail/geometry.rs index 6e5edd1ce0..320bef86d8 100644 --- a/crates/ui-egui/src/panels/detail/geometry.rs +++ b/crates/ui-egui/src/panels/detail/geometry.rs @@ -253,7 +253,7 @@ pub(crate) fn output_px(frame: &Frame) -> [usize; 2] { pub(crate) fn focus_box(photo: &lightcraft_catalog::Photo) -> Option { let f = photo.meta.focus.as_ref()?; let d = &photo.develop; - let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, d, true, photo.embedded_lens.as_ref()); + let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, d, true, photo.embedded_lens.as_deref()); let [w, h] = output_px(&frame); let to_out = frame.out_to_norm(w, h).inverse()?; // stored on the upright photo: the user's Rotate / Flip on top, then the crop diff --git a/crates/ui-egui/src/panels/slideshow.rs b/crates/ui-egui/src/panels/slideshow.rs index 351b8d531c..48db76c54e 100644 --- a/crates/ui-egui/src/panels/slideshow.rs +++ b/crates/ui-egui/src/panels/slideshow.rs @@ -213,7 +213,7 @@ pub fn tick(app: &mut LightcraftApp, ctx: &egui::Context, now: f64) { /// [`SlideSettings::is_plain`]: lightcraft_engine::slide::SlideSettings::is_plain fn slide_job(app: &mut LightcraftApp, id: PhotoId, area: Rect, ppp: f32, texture_side: usize) -> Option { let photo = app.session.catalog.photo(id)?.clone(); - let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &photo.develop, true, photo.embedded_lens.as_ref()); + let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &photo.develop, true, photo.embedded_lens.as_deref()); let native = super::detail::output_px(&frame); if !app.session.slides_plain() { let (w, h) = slide_px(area, ppp, texture_side); diff --git a/crates/ui-egui/src/tests/compare.rs b/crates/ui-egui/src/tests/compare.rs index 23c15efe58..d71c537080 100644 --- a/crates/ui-egui/src/tests/compare.rs +++ b/crates/ui-egui/src/tests/compare.rs @@ -306,7 +306,7 @@ fn compare_zoom_window_tracks_the_zoomed_pane() { photo.height.max(1) as usize, &photo.develop, true, - photo.embedded_lens.as_ref(), + photo.embedded_lens.as_deref(), ); let aspect = frame.aspect() as f32; let native = crate::panels::detail::output_px(&frame);