From 11689fb14f3a4d0291e2ccc05d6cb3556e4ea1ab Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Wed, 28 Oct 2026 09:23:46 +0100 Subject: core: fix focal-length-to-scale constant (36 mm film width, not 35) The 35mm film frame is 36 mm wide, so a lens with 35mm-equivalent focal length f projects with focal length f*w/36 in pixels. The original C++ code divided by 35, making the EXIF-derived initial scale ~2.9% too large (and the fitted focal readout ~2.9% low). - EXIF init and the focal slider/readout in both apps now use /36 - the gipfel JPEG comment KEEPS its historical scale*35/w encoding (focal_from_comment flag), so annotated JPEGs written by any older gipfel version still reproduce the identical view, and files saved by this version remain readable by them - new regression test focal_scale_exif_vs_comment; golden tests untouched (they pin the comment path) --- crates/gipfel-app/src/main.rs | 9 ++++++--- crates/gipfel-core/src/metadata.rs | 36 +++++++++++++++++++++++++++++++++--- crates/wipfel/src/app.rs | 4 ++-- dist/wipfel/wipfel_bg.wasm | Bin 519140 -> 519240 bytes 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/crates/gipfel-app/src/main.rs b/crates/gipfel-app/src/main.rs index 12e2845..9ef046a 100644 --- a/crates/gipfel-app/src/main.rs +++ b/crates/gipfel-app/src/main.rs @@ -363,11 +363,11 @@ impl Gipfel { } fn focal_35mm(&self) -> f64 { - self.pan.get_scale() * 35.0 / self.img_max() + self.pan.get_scale() * 36.0 / self.img_max() } fn set_focal_35mm(&mut self, f: f64) { - self.pan.set_scale(f * self.img_max() / 35.0); + self.pan.set_scale(f * self.img_max() / 36.0); } fn sync_inputs(&mut self) { @@ -476,7 +476,10 @@ impl Gipfel { md.direction = self.pan.get_center_angle(); md.pitch = self.pan.get_pitch_angle(); md.roll = self.pan.get_roll_angle(); - md.focal_length_35mm = self.focal_35mm(); + // the comment keeps its historical scale * 35 / w encoding so + // annotated JPEGs stay interchangeable with older gipfel + // versions (see apply_image_metadata) + md.focal_length_35mm = self.pan.get_scale() * 35.0 / self.img_max(); md.projection_type = self.pan.projection_type as i32; md.k0 = self.pan.parms.k0; md.k1 = self.pan.parms.k1; diff --git a/crates/gipfel-core/src/metadata.rs b/crates/gipfel-core/src/metadata.rs index f55bf1c..9d73a8d 100644 --- a/crates/gipfel-core/src/metadata.rs +++ b/crates/gipfel-core/src/metadata.rs @@ -25,6 +25,10 @@ pub struct ImageMetaData { pub x0: f64, pub focal_length: f64, pub focal_length_35mm: f64, + /// true when focal_length_35mm came from a gipfel JPEG comment rather + /// than EXIF; the comment uses a legacy scale encoding (see + /// apply_image_metadata) + pub focal_from_comment: bool, pub projection_type: i32, } @@ -44,6 +48,7 @@ impl Default for ImageMetaData { x0: NAN, focal_length: NAN, focal_length_35mm: NAN, + focal_from_comment: false, projection_type: 0, } } @@ -147,6 +152,7 @@ impl ImageMetaData { self.pitch = pit.unwrap(); self.roll = rol.unwrap(); self.focal_length_35mm = fr.unwrap(); + self.focal_from_comment = true; self.projection_type = pt; // n >= 10: k0 and k1 were matched; x0 defaults to 0.0 when only 10 @@ -548,13 +554,20 @@ pub fn apply_image_metadata( pan.set_roll_angle(md.roll); } - // set_focal_length_35mm: scale = fl * max(w, h) / 35 + // scale = f35 * w / 36: the 35mm film frame is 36 mm wide, so a lens + // with 35mm-equivalent focal length f projects with focal length + // f * w / 36 in pixels. Exception: the gipfel JPEG comment has always + // encoded the focal as scale * 35 / w, so that legacy encoding is + // kept for comments to reproduce identical views with annotated + // JPEGs written by older gipfel versions. let w = img_w.max(img_h); if md.focal_length_35mm.is_nan() || md.focal_length_35mm == 0.0 { - pan.set_scale(35.0 * w as f64 / 35.0); + pan.set_scale(35.0 * w as f64 / 36.0); have_info = false; - } else { + } else if md.focal_from_comment { pan.set_scale(md.focal_length_35mm * w as f64 / 35.0); + } else { + pan.set_scale(md.focal_length_35mm * w as f64 / 36.0); } // distortion parameters, C ordering: @@ -721,4 +734,21 @@ mod tests { } let _ = std::fs::remove_dir_all(&dir); } + + #[test] + fn focal_scale_exif_vs_comment() { + // EXIF 35mm-equivalent focal: scale = f * w / 36 (film width) + let mut md = ImageMetaData::new(); + md.focal_length_35mm = 50.0; + let mut pan = crate::panorama::Panorama::new(); + apply_image_metadata(&mut pan, &md, 1000, 800); + assert!((pan.get_scale() - 50.0 * 1000.0 / 36.0).abs() < 1e-9); + + // gipfel comment: legacy encoding scale = f * w / 35, kept so + // annotated JPEGs from older versions reproduce the same view + md.focal_from_comment = true; + let mut pan = crate::panorama::Panorama::new(); + apply_image_metadata(&mut pan, &md, 1000, 800); + assert!((pan.get_scale() - 50.0 * 1000.0 / 35.0).abs() < 1e-9); + } } diff --git a/crates/wipfel/src/app.rs b/crates/wipfel/src/app.rs index 38c220e..a7af089 100644 --- a/crates/wipfel/src/app.rs +++ b/crates/wipfel/src/app.rs @@ -391,7 +391,7 @@ pub fn app() -> Html { lon: p.get_view_long(), height: p.get_view_height(), az: p.get_center_angle().rem_euclid(360.0), - focal: (p.get_scale() * 35.0 / w as f64).clamp(1.0, 300.0), + focal: (p.get_scale() * 36.0 / w as f64).clamp(1.0, 300.0), pitch: p.get_pitch_angle(), roll: p.get_roll_angle(), vis: (0.11 - p.height_dist_ratio).clamp(0.01, 0.1), @@ -596,7 +596,7 @@ pub fn app() -> Html { vp.set(u); let w = img_size.0 as f64; if w > 0.0 { - pan.borrow_mut().set_scale(v * w / 35.0); + pan.borrow_mut().set_scale(v * w / 36.0); } }) }; diff --git a/dist/wipfel/wipfel_bg.wasm b/dist/wipfel/wipfel_bg.wasm index 3f5974a..d35f0ae 100644 Binary files a/dist/wipfel/wipfel_bg.wasm and b/dist/wipfel/wipfel_bg.wasm differ -- cgit v1.2.3