From aec2dd1bde0034acaa602f2b688f06737f5b32c5 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Sat, 7 Nov 2026 21:32:30 +0100 Subject: [PATCH 1/6] LR-PROF-CAMERACOLOR: lensfun vignetting no longer darkens and skews a raw's camera look (#3941) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3941 ## Problem With the downloaded lensfun database, raws whose look LightCraft fits to their own camera JPEG (Sony ARW and the other makes without a colour matrix) render too dark, with colour skewed, when lensfun has a vignetting profile for their lens: in the loupe, the thumbnails and exports alike. Without the database the same files match their camera JPEG more closely. On the 11 affected photos measured below, the bug darkened the render by 0.4 to 17 L* (one unchanged); its colour shifts are scene-dependent, mostly a little lost chroma. On the ILCE-7M3 / FE 28mm F2 photo from #3941 (f/2, the worst case) the yellow CRT text turns orange and the frame 17 L* darker. `git bisect` (rendering each commit with and without the lens database) points to 001a58e9 "RAW.colour: fit the per-file look on the vignetting-corrected sensor" (LOOK_VERSION 13). Since then every per-file fit multiplies the sensor proxy by the whole embedded vignetting correction, on the assumption that the camera JPEG has it applied. That holds for the file's own data (Fujifilm maker notes, DNG opcodes), but `lens_profiles::for_raw` hands the fit a lensfun profile the same way, and a lens database describes the lens, not what this camera did to its JPEG relative to the raw. The #3941 JPEG shows none of the profile's correction relative to its raw, although its maker note says vignetting correction Auto (as with the A7CR's shading Auto JPEGs in docs/sony-lens-corrections.md). So the fit saw corners far brighter than the JPEG's: its held-out RMS went from 0.027 (without lensfun) to 0.062, accepted only on the "away from edges" retry, the tone curve darkened to compensate and the colour matrix bent. How far depends on how much of the profile the JPEG lacks and what the scene puts in the corners. ## Change `look_proxies` (crates/engine/src/camera_preview/look_fit.rs) keeps the whole correction for the file's own data (`LensSource::File`, unchanged). For a `LensSource::Database` profile it measures the share of the correction the camera JPEG shows (`camera_vignetting_amount`) and applies that share to the sensor proxy, scaled as the Vignetting amount slider scales it (`1 + (gain - 1) * amount`). The camera's tone curve is monotone, so with the share the camera applied, the corrected sensor orders its pixels by luminance as the JPEG does: of 0 to 100 % in 5 % steps, the share with the highest rank correlation (`rank_correlation_ties`) wins. Clipped and non-finite pixels are left out; with fewer than 256 usable pixels, or with a lens warp on the proxy (which a file with a database profile doesn't have), the whole correction is kept, as before. LOOK_VERSION 14, RENDER_CACHE_VERSION 77. Docs: camera-preview-colour.md, lens-profiles.md, raw-parity.md (with a revision row). **Not in this PR:** - The render still applies a lensfun profile's vignetting correction in full while Enable Profile Corrections is on, so the #3941 photo's corners come out much brighter than in its camera JPEG (lensfun: about 2.6 EV at the extreme corner of this lens at f/2; the Vignetting amount slider lowers it). If Sony applies part of its shading correction to the raw samples (an open question in docs/sony-lens-corrections.md), that is a double correction of its own; it needs flat-field measurements, and the ARW's recorded shading setting and table (`0x7031`/`0x7032`) could then decide it. - Sony's DRO brightens the camera JPEG's shadows locally, which could bias the measured share; CR3 proxies are measured in the oriented frame without their small framing alignment. ## How it was checked - [x] `cargo xtask ci` on this branch, rebased on current `main` (macOS aarch64): fmt, clippy, heif, parity, layers, assets, roots and wasm pass; the tests pass except two that fail identically on unchanged `main` on this machine: `tests_light::v1_and_v2_render_as_before_v3` (#3788) and `tests::titlebar::double_click_on_the_empty_bar_zooms_then_restores`. The engine's corpus tests pass with the files above. - [x] A test that fails without this change: `a_lens_database_vignetting_reaches_the_fit_as_far_as_the_camera_jpeg_shows_it` (synthetic ARW and camera JPEG, `grey_raw_with_jpeg`): with an uncorrected JPEG the grey and the colour fit keep the curve fitted without the profile, and with a fully corrected JPEG the whole correction applies. Without the change it fails: the curve at 0.25 is 0.460 instead of the camera's 0.528. Plus `camera_vignetting_amount_measures_the_share_the_jpeg_shows` (0, 40 and 100 % measured within 5 %). - [x] User-visible change: `lightcraft-cli snapshot` of the loupe and `lightcraft-cli render`, with `LIGHTCRAFT_LENSFUN_DIR` set to the downloaded database and to an empty folder; `LIGHTCRAFT_PROFILE=1` prints the measured share. Against the camera JPEG, with the downloaded lensfun database: the look fit's held-out RMS, and the mean CIEDE2000 of the central half of the render (profile distortion off so the two line up): | Photo | share | RMS main → this | ΔE00 main → this | |---|---|---|---| | #3941: ILCE-7M3, FE 28mm F2, f/2 | 0 % | 0.0625 → 0.0268 | 7.47 → 5.11 | | corpus: ILCE-9M2 | 0 % | 0.0452 → 0.0149 | 6.84 → 3.39 | | corpus: ILCE-7RM2 | 45 % | 0.0561 → 0.0556 | 9.14 → 8.96 | | corpus: SLT-A33 | 0 % | 0.0614 → 0.0341 | 4.70 → 4.82 | | corpus: ILCE-7SM2 | 0 % | 0.0483 → 0.0449 | 5.29 → 5.28 | | corpus: ZV-E10M2 | 0 % | 0.0413 → 0.0230 | 7.24 → 5.45 | | corpus: DSC-RX1RM3 | 0 % | 0.0480 → 0.0396 | 3.91 → 2.90 | | corpus: ILCE-6400 | 10 % | 0.0350 → 0.0322 | 3.38 → 2.80 | | corpus: E-M5 Mark II | 30 % | 0.0185 → 0.0178 | 1.76 → 1.74 | | corpus: PEN-F | 10 % | 0.0218 → 0.0219 | 1.69 → 1.66 | | corpus: D780 | 35 % | 0.0287 → 0.0188 | 2.93 → 2.57 | The corpus rows are the CC0 raw.pixls.us files `cargo xtask corpus --download` pins: of 91 checked (all 37 ARWs, 19 ORFs, 15 PEFs, the first 20 NEFs), these 10 get a lens database vignetting profile into their fit; the other 81 take the same code path as before. Corpus mean ΔE00 4.69 → 3.96: 6 better, 3 within 0.05, one 0.12 worse; held-out RMS lower on 9 of 10 (PEN-F +0.0001). Script: render each file with both builds under `LIGHTCRAFT_PROFILE=1` (`--set optics.profileDistortion=0`), read the measured share and the accepted fit's held-out RMS from stderr, and compare the central half with the largest embedded JPEG. ## Merging - **Overlap:** none found fixing #3941. Open pull requests that also touch `camera_preview` and bump the shared numbers: #3587 and #2092 (also `look_fit.rs`, other functions), #3902, #3635, #2188, #2854. - **Existing edits and libraries:** photos with a lens database vignetting profile get a new camera look (LOOK_VERSION 14) and render closer to their camera JPEG; edits and settings are unchanged. A bug fix in the camera colour a file gets, which changes the source, not how settings are interpreted (docs/process-versions.md), as 001a58e9 landed. - **Shared numbers taken:** LOOK_VERSION 14, RENDER_CACHE_VERSION 77. - **Where it came from:** own measurements on the #3941 photo and the CC0 raw.pixls.us corpus (`cargo xtask corpus`); no lens or camera data added. - **New dependencies, `unsafe`, build scripts, network access, spawned processes, deleted files, CI or `xtask` gate changes:** none. --- crates/engine/src/camera_preview.rs | 8 +- crates/engine/src/camera_preview/look_fit.rs | 95 ++++++++++++++++++-- crates/engine/src/camera_preview/tests.rs | 83 +++++++++++++++++ crates/engine/src/media/source.rs | 2 +- docs/camera-preview-colour.md | 13 ++- docs/lens-profiles.md | 7 ++ docs/raw-parity.md | 6 +- 7 files changed, 199 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/camera_preview.rs b/crates/engine/src/camera_preview.rs index 014a744080..dcaa30e47b 100644 --- a/crates/engine/src/camera_preview.rs +++ b/crates/engine/src/camera_preview.rs @@ -36,8 +36,8 @@ use hue_sat::{MIN_LUMINANCE_KEPT, NO_LUMINANCE_KEPT, fit_hue_sat, hue_saturation pub(crate) use look_fit::fit_preview; #[cfg(test)] use look_fit::{ - FIT_PREVIEW_MAX_PIXELS, SENSOR_CLIP, at_most, clips_more, correct_vignetting, fit_look, fit_look_with, sony_dro_off_tone, with_dro_off_tone, - without_clipped, + FIT_PREVIEW_MAX_PIXELS, SENSOR_CLIP, at_most, camera_vignetting_amount, clips_more, correct_vignetting, fit_look, fit_look_with, + sony_dro_off_tone, with_dro_off_tone, without_clipped, }; #[cfg(test)] use mono::{fit_dark_tone, fit_tone_only, monochrome, monochrome_look, to_prophoto}; @@ -84,7 +84,9 @@ use tone::{HighlightCheck, ToneFit, fit_tone, fit_tone_conditional, highlights}; /// 13: the sensor proxy of every per-file fit gets the embedded vignetting correction the render applies (Fujifilm /// RAF; other makes with a lens database), so the render no longer applies it twice, at the position a lens warp samples; /// the warped candidate's alignment leaves out the pixels outside the sensor instead of reading them as black. -pub const LOOK_VERSION: u32 = 13; +/// 14: a lens database's vignetting correction reaches the sensor proxy only in the share the camera JPEG shows +/// (`camera_vignetting_amount`), not whole: JPEGs that show less of it fitted looks too dark, with skewed colour. +pub const LOOK_VERSION: u32 = 14; #[derive(Clone, Debug)] pub(crate) struct CameraLook { diff --git a/crates/engine/src/camera_preview/look_fit.rs b/crates/engine/src/camera_preview/look_fit.rs index fac4e2df40..3cde7752fd 100644 --- a/crates/engine/src/camera_preview/look_fit.rs +++ b/crates/engine/src/camera_preview/look_fit.rs @@ -282,7 +282,91 @@ pub(super) fn look_proxies( lens: Option<&LensGeometry>, embedded: Option<&lightcraft_develop::EmbeddedLens>, ) -> Option<(Rgb32f, Rgb32f, Vec)> { - proxies(raw, bytes, transform, PROXY, 0.0, lens, embedded) + // The file's own vignetting data is what its camera applied to the JPEG. A lens database profile describes the + // lens, not what this camera did: an ILCE-7M3 JPEG with an FE 28mm F2 at f/2 (maker note: vignetting correction + // Auto) shows none of lensfun's correction relative to its raw (the A7CR's shading Auto JPEGs show no consistent + // peripheral brightening either, docs/sony-lens-corrections.md). With the whole profile on the sensor that fit + // learned corners the JPEG doesn't have: 0.062 held-out RMS instead of 0.027, a darker, orange render. So the share + // of the profile the JPEG shows is measured. (Only without a lens warp: the gain goes on the returned proxy, and a + // warped one no longer has each pixel at its source position. A warp comes from the file's own opcodes, and a file + // with its own correction never gets a database profile, `lens_profiles::for_raw`.) + let database = embedded.filter(|e| e.source == lightcraft_develop::LensSource::Database && lens.is_none()); + let Some(vignette) = database.and_then(|e| e.vignette) else { + return proxies(raw, bytes, transform, PROXY, 0.0, lens, embedded); + }; + let (mut sensor, reference, clipped) = proxies(raw, bytes, transform, PROXY, 0.0, lens, None)?; + // CR3 proxies come back oriented like the JPEG (their framing aligned to it), the others in sensor orientation + let orientation = if raw.format == lightcraft_raw::RawFormat::Cr3 { lightcraft_geom::Orientation::Normal } else { raw.orientation }; + let amount = camera_vignetting_amount(&sensor, &reference, &clipped, &vignette, orientation); + apply_vignetting(&mut sensor, &vignette, orientation, amount); + if lightcraft_pipeline::profiling() { + eprintln!("[profile] camera look: the camera JPEG shows {:.0} % of the lens database's vignetting correction", amount * 100.0); + } + Some((sensor, reference, clipped)) +} + +/// Steps between none and all of a lens database's vignetting correction that [`camera_vignetting_amount`] tries. +const VIGNETTING_STEPS: u32 = 20; + +/// Fewest usable proxy pixels [`camera_vignetting_amount`] measures on; with fewer it keeps the whole correction. +const MIN_VIGNETTING_PIXELS: usize = 256; + +/// The share (0..=1, as the Vignetting amount slider scales it: `1 + (gain - 1) · amount`) of `vignette` that the +/// camera JPEG `reference` shows relative to the uncorrected `sensor` proxy. The camera's tone curve is monotone, so +/// with the share the camera applied, the corrected sensor orders its pixels by brightness as the JPEG does: the +/// share with the highest rank correlation of luminance. Clipped and non-finite pixels are left out. +pub(super) fn camera_vignetting_amount( + sensor: &Rgb32f, + reference: &Rgb32f, + clipped: &[bool], + vignette: &lightcraft_develop::EmbeddedVignette, + orientation: lightcraft_geom::Orientation, +) -> f64 { + if sensor.data.len() != reference.data.len() { + return 1.0; + } + let gains = vignetting_gains(sensor, vignette, orientation); + let usable = |p: &[f32; 3]| p.iter().all(|v| v.is_finite()) && luminance_2020(*p) > 0.0; + let pixels: Vec<(f64, [f64; 3], [f64; 3])> = (sensor.data.iter().zip(&reference.data).zip(gains).enumerate()) + .filter(|(i, ((s, r), _))| !clipped.get(*i).copied().unwrap_or(false) && usable(s) && usable(r)) + .map(|(_, ((s, r), g))| (g, s.map(f64::from), r.map(f64::from))) + .collect(); + if pixels.len() < MIN_VIGNETTING_PIXELS { + return 1.0; + } + let agreement = |amount: f64| { + let pairs: Vec<([f64; 3], [f64; 3])> = pixels.iter().map(|(g, s, r)| (s.map(|v| v * (1.0 + (g - 1.0) * amount)), *r)).collect(); + super::pairs::rank_correlation_ties(&pairs) + }; + // ties keep the larger share, the correction a camera JPEG usually has (the file's own data always gets all of it) + (0..=VIGNETTING_STEPS) + .map(|s| f64::from(s) / f64::from(VIGNETTING_STEPS)) + .map(|a| (a, agreement(a))) + .fold((1.0, f64::MIN), |best, (a, r)| if r > best.1 + 1e-9 || ((r - best.1).abs() <= 1e-9 && a > best.0) { (a, r) } else { best }) + .0 +} + +/// The render's vignetting gain (`optics::embedded_vignette_gain`) at each pixel of `sensor` (in the frame +/// `orientation` maps to the oriented, cropped frame `vignette` is in). +fn vignetting_gains(sensor: &Rgb32f, vignette: &lightcraft_develop::EmbeddedVignette, orientation: lightcraft_geom::Orientation) -> Vec { + let (w, h) = (sensor.width as f64, sensor.height as f64); + let (ow, oh) = if orientation.swaps_axes() { (h, w) } else { (w, h) }; + let width = sensor.width.max(1); + (0..sensor.data.len()) + .map(|i| { + let (x, y) = orientation.map((i % width) as f64 + 0.5, (i / width) as f64 + 0.5, w, h); + lightcraft_pipeline::optics::embedded_vignette_gain(vignette, lightcraft_geom::Point::new(x, y), ow, oh) + }) + .collect() +} + +/// Multiply `sensor` by `amount` of `vignette`'s correction, as the Vignetting amount slider scales it. +fn apply_vignetting(sensor: &mut Rgb32f, vignette: &lightcraft_develop::EmbeddedVignette, orientation: lightcraft_geom::Orientation, amount: f64) { + let gains = vignetting_gains(sensor, vignette, orientation); + for (p, g) in sensor.data.iter_mut().zip(gains) { + let gain = (1.0 + (g - 1.0) * amount) as f32; + *p = p.map(|v| v * gain); + } } /// Multiply the sensor proxy (sensor orientation, default crop) by the embedded vignetting correction, the gain the @@ -290,12 +374,5 @@ pub(super) fn look_proxies( /// camera JPEG has it applied, so a tone fitted without it brightens the render's corners twice. pub(super) fn correct_vignetting(sensor: &mut Rgb32f, lens: Option<&lightcraft_develop::EmbeddedLens>, orientation: lightcraft_geom::Orientation) { let Some(vignette) = lens.and_then(|l| l.vignette) else { return }; - let (w, h) = (sensor.width as f64, sensor.height as f64); - let (ow, oh) = if orientation.swaps_axes() { (h, w) } else { (w, h) }; - let width = sensor.width.max(1); - for (i, p) in sensor.data.iter_mut().enumerate() { - let (x, y) = orientation.map((i % width) as f64 + 0.5, (i / width) as f64 + 0.5, w, h); - let gain = lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, lightcraft_geom::Point::new(x, y), ow, oh) as f32; - *p = p.map(|v| v * gain); - } + apply_vignetting(sensor, &vignette, orientation, 1.0); } diff --git a/crates/engine/src/camera_preview/tests.rs b/crates/engine/src/camera_preview/tests.rs index 733b576f6f..ea4d86940c 100644 --- a/crates/engine/src/camera_preview/tests.rs +++ b/crates/engine/src/camera_preview/tests.rs @@ -2195,6 +2195,52 @@ fn the_colour_look_is_fitted_on_the_vignetting_corrected_sensor() { assert!(uncorrected > corrected + 0.03, "without the correction the curve brightens: {uncorrected} vs {corrected}"); } +/// A lens database profile reaches the fit only as far as the camera JPEG shows it. A camera that left its JPEG +/// uncorrected relative to its raw keeps the curve fitted without any correction: with the whole +/// profile on the sensor the curve darkened and the colour fit bent (an ILCE-7M3 with an FE 28mm F2 at f/2 rendered +/// darker and orange). A camera that corrected its JPEG fully gets the whole correction, as with the file's own data. +#[test] +fn a_lens_database_vignetting_reaches_the_fit_as_far_as_the_camera_jpeg_shows_it() { + use lightcraft_develop::{EmbeddedLens, EmbeddedVignette, LensSource}; + let (w, h) = (192usize, 128usize); + let vignette = EmbeddedVignette { k: [1.0, 0.0, 0.0, 0.0, 0.0], center: lightcraft_geom::Point::new(0.5, 0.5), radius: 0.6 }; + let gain = |x: usize, y: usize| { + let point = lightcraft_geom::Point::new(x as f64 + 0.5, y as f64 + 0.5); + lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, point, w as f64, h as f64) as f32 + }; + let scene = |x: usize, y: usize| 0.01 + 0.5 * (x as f32 / w as f32) * (0.3 + 0.7 * y as f32 / h as f32); + let database = EmbeddedLens { vignette: Some(vignette), source: LensSource::Database, ..Default::default() }; + let camera = 1.0 - (-0.75f32).exp(); + let at_quarter = |raw: &RawImage, bytes: &[u8], embedded: Option<&EmbeddedLens>| { + let t = lightcraft_raw::color::camera_transform(raw, lightcraft_raw::color::as_shot_white_xy(raw)); + let look = monochrome_look(raw, bytes, &t, None, embedded).expect("a tone curve"); + ToneMap::camera(&look.tone, 0.0, 0.0, 0.0).apply(0.25) + }; + + // the camera JPEG of the vignetted sensor, uncorrected + let vignetted = |x: usize, y: usize| scene(x, y) / gain(x, y); + let (raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, vignetted, [1.0; 3]); + let (with_profile, without) = (at_quarter(&raw, &bytes, Some(&database)), at_quarter(&raw, &bytes, None)); + assert!((with_profile - camera).abs() < 0.03, "the camera's curve at 0.25: {with_profile} vs {camera}"); + assert!((with_profile - without).abs() < 0.01, "as without the profile: {with_profile} vs {without}"); + + // the same in colour: the tinted JPEG takes the colour fit, whose curve the whole profile darkened + let (raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, vignetted, [1.0, 0.9, 0.8]); + let colour_at_quarter = |embedded: Option<&EmbeddedLens>| { + let (_, _, look) = starting_colour(&mut raw.clone(), &bytes, None, embedded); + let look = look.expect("a colour look"); + assert!(look.matrix != Mat3::IDENTITY || look.hue_sat.is_some(), "the colour fit, not the black-and-white one"); + ToneMap::camera(&look.tone, 0.0, 0.0, 0.0).apply(0.25) + }; + let (with_profile, without) = (colour_at_quarter(Some(&database)), colour_at_quarter(None)); + assert!((with_profile - without).abs() < 0.01, "the colour fit's curve as without the profile: {with_profile} vs {without}"); + + // a camera JPEG with the whole correction applied: the whole correction reaches the fit + let (raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, scene, [1.0; 3]); + let corrected = at_quarter(&raw, &bytes, Some(&database)); + assert!((corrected - camera).abs() < 0.03, "the camera's curve at 0.25: {corrected} vs {camera}"); +} + /// A lens-warped sensor proxy gets the vignetting gain of the position it samples, as the render applies it: the /// vignetted sensor, corrected, gives the proxy of the same sensor without vignetting. #[test] @@ -2632,3 +2678,40 @@ fn a_narrow_black_and_white_curve_that_darkens_the_bright_areas_is_refused() { assert!(sky > 0.8, "an accepted curve must not darken what the camera renders at 0.93: {sky}"); } } + +/// A lens database's vignetting correction reaches the fit in the share the camera JPEG shows: a JPEG with none, part +/// or all of it is measured as such, and a JPEG without enough usable pixels keeps the whole correction. +#[test] +fn camera_vignetting_amount_measures_the_share_the_jpeg_shows() { + use lightcraft_develop::EmbeddedVignette; + let vignette = EmbeddedVignette { k: [2.0, 1.0, 0.0, 0.0, 0.0], center: lightcraft_geom::Point::new(0.5, 0.5), radius: 0.6 }; + let (w, h) = (96, 64); + let gain = |i: usize| { + lightcraft_pipeline::optics::embedded_vignette_gain( + &vignette, + lightcraft_geom::Point::new((i % w) as f64 + 0.5, (i / w) as f64 + 0.5), + w as f64, + h as f64, + ) as f32 + }; + // a scene of unrelated brightnesses (no gradient for the vignetting to hide in), darkened by the lens + let mut seed = 12345u32; + let mut sensor = Rgb32f::new(w, h); + for (i, p) in sensor.data.iter_mut().enumerate() { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let v = 0.02 + 0.4 * (seed >> 8) as f32 / (1u32 << 24) as f32; + *p = [v, v * 0.9, v * 0.7].map(|c| c / gain(i)); + } + let clipped = vec![false; w * h]; + for shown in [0.0, 0.4, 1.0] { + let mut reference = sensor.clone(); + for (i, p) in reference.data.iter_mut().enumerate() { + let g = 1.0 + (gain(i) - 1.0) * shown; + *p = p.map(|c| (c * g).powf(1.0 / 2.2).min(1.0)); + } + let amount = camera_vignetting_amount(&sensor, &reference, &clipped, &vignette, lightcraft_geom::Orientation::Normal); + assert!((amount - f64::from(shown)).abs() < 0.051, "the JPEG shows {shown}, measured {amount}"); + } + let tiny = Rgb32f::new(8, 8); + assert_eq!(camera_vignetting_amount(&tiny, &tiny, &[false; 64], &vignette, lightcraft_geom::Orientation::Normal), 1.0); +} diff --git a/crates/engine/src/media/source.rs b/crates/engine/src/media/source.rs index 0cfa5eb162..248eb3f54d 100644 --- a/crates/engine/src/media/source.rs +++ b/crates/engine/src/media/source.rs @@ -1,7 +1,7 @@ use super::{Arc, DcpProfile, Deserialize, DevelopSettings, MediaKind, Photo, Rgb32f, Rgba8, Serialize, SourceInfo}; /// Bump when the pipeline's output changes, to invalidate cached thumbnails. -pub const RENDER_CACHE_VERSION: u64 = 76; +pub const RENDER_CACHE_VERSION: u64 = 77; /// 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/docs/camera-preview-colour.md b/docs/camera-preview-colour.md index 41f9af0c1f..e04a9cbd7a 100644 --- a/docs/camera-preview-colour.md +++ b/docs/camera-preview-colour.md @@ -136,7 +136,18 @@ it (Enable Profile Corrections). The camera applied the same correction to its J (`fit_preview`, `monochrome_look`, the near-black tone fallback) therefore takes its sensor proxy through `look_proxies`, which multiplies it by that gain as `fit_dng_tone` already did (`correct_vignetting`). Before `LOOK_VERSION` 13 the RAF fit saw the uncorrected sensor: the tone curve learned the JPEG's corrected brightness, and -the render added the correction again. Lens-database corrections (`LIGHTCRAFT_LENSFUN_DIR`) reach the fit the same way. +the render added the correction again. A lens-database profile (`LIGHTCRAFT_LENSFUN_DIR`, the downloaded lensfun data) +describes the lens, not what the camera did to its JPEG, so since `LOOK_VERSION` 14 it reaches the fit only in the +share the JPEG shows (`camera_vignetting_amount`): the camera's tone curve is monotone, so the share whose corrected +sensor ranks its pixels' luminance most like the JPEG wins (0 to 100 % in 5 % steps). With the whole profile, an +ILCE-7M3 frame at f/2 with an FE 28mm F2 (lensfun: corners at 16 % of the centre) fitted 0.062 held-out RMS instead +of 0.027 and rendered darker and orange. Its camera JPEG, with vignetting correction Auto in the maker note, shows +none of that correction relative to the raw (rank correlation 0.991 at 0 % down to 0.903 at 100 %), like the A7CR +JPEGs in [sony-lens-corrections.md](sony-lens-corrections.md). Of 91 CC0 corpus raws (all 37 ARWs, 19 ORFs, 15 +PEFs, 20 NEFs), 10 get a lens database vignetting profile into their fit (7 Sony, 2 Olympus, 1 Nikon); the shares +measured were 0 % on six, 10–45 % on four, and the mean CIEDE2000 of the central half of the render against the +camera JPEG went from 4.69 to 3.96 (6 better, 3 within 0.05, an SLT-A33 0.12 worse). The whole profile had made 9 of +the 10 darker (0.4 to 8.5 L*), with small, scene-dependent colour shifts (mostly lost chroma). `proxies` applies the gain before it moves the sensor into the JPEG's lens geometry, so each pixel gets the gain of the position it samples, as in the render. The choice between the warped and the plain sensor (`alignment`) leaves out the pixels the warp finds no sensor for. It used to read them as black, a false edge along the border that made the diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index 4e72251b14..2817cd791e 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -82,6 +82,13 @@ corrections: **Enable Profile Corrections** and its Distortion / Vignetting amou stage (one resample), orientation handling and DNG export. A conversion whose radial error exceeds 0.001 of the half diagonal, or that would fold the image, is not applied. +A raw whose look is fitted to its own camera JPEG (Sony, Nikon, Pentax, Olympus and others without a colour +matrix, [camera-preview-colour.md](camera-preview-colour.md)) has that fit made on the sensor with only the +share of a profile's vignetting correction its camera JPEG shows, measured per photo: the profile describes the +lens, not what the camera did to its JPEG relative to the raw (a Sony JPEG with shading compensation Auto can show +none of it). The render +still applies the profile at the Vignetting amount. + 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). Missing or invalid crop factors remain unknown; diff --git a/docs/raw-parity.md b/docs/raw-parity.md index 67d50bfa6c..f3d4d7c3e4 100644 --- a/docs/raw-parity.md +++ b/docs/raw-parity.md @@ -222,7 +222,10 @@ Neither names ORF or specifies its file layout. These references do not establis bodies, median ΔE 5.1, ΔL\* +0.2; rejected fits fall back to a neutral matrix at median ΔE 18.5, ΔL\* −11, chroma ×0.39. RW2 140 of 174 files accepted, median 5.0. NEF 13 of 13, 2.9–7.8. RAF 23 of 24, pooled X-H2S 1.62 / X-T4 2.34 (measured before `LOOK_VERSION` 13 stopped applying the embedded vignetting - correction twice); X-S10, 60 photos not used to find that bug, mean ΔE00 2.71 → 2.55 with it fixed. SRW 2.2–7.6. + correction twice); X-S10, 60 photos not used to find that bug, mean ΔE00 2.71 → 2.55 with it fixed. Since + `LOOK_VERSION` 14 a lens database's vignetting reaches the fit only in the share the camera JPEG shows (an + ILCE-7M3 with an FE 28mm F2 at f/2, whose JPEG shows none of it, had rendered darker and orange with the whole + profile). SRW 2.2–7.6. - Against Lightroom itself (few): Sony DRO median ΔE76 7.5 → 3.3; Olympus E-1 vs Lightroom Classic ΔE00 11.25 → 8.30; ProRAW with its gain map ΔE00 19 (why the map is off). No measurement against Adobe Standard across models exists. @@ -294,6 +297,7 @@ Beyond decoding: | Date | Change | Summary | |---|---|---| +| 2026-11-07 | patch | Camera look: a lens database's vignetting reaches the per-file fit only in the share the camera JPEG shows (`LOOK_VERSION` 14), so raws whose JPEG shows little or none of it (mostly Sony) no longer render too dark, with skewed colour, with lensfun profiles. | | 2026-11-07 | correction | Compressed ORF sensor support withdrawn pending provenance review; embedded preview fallback remains. Coverage regenerated without the removed decoder: 557 verified, 665 unverified, 187 preview-only, 37 unsupported; 94% use-weighted coverage. | | 2026-11-04 | minor | Compressed ORF support was introduced in #3775; subsequently withdrawn pending provenance review. Earlier clean-room and verification claims are withdrawn; compressed files use embedded previews when available. See [ORF status](orf-compression.md). | | 2026-10-26 | patch | Bundled camera profile count 5 → 9 (Nikon Z 6II, Z 8 and D5300, ILCE-7RM5); counts only | From a6705ac0fe19d8a04a63e53623a4d71bdec0d59c Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Sat, 7 Nov 2026 22:35:36 +0100 Subject: [PATCH 2/6] LR-PROF-CAMERACOLOR: measure the lens database vignetting share per hue class, for CR3 framing and DNG tone fits Review follow-ups for #3941: - camera_vignetting_amount ranks luminance within classes of similar sensor chromaticity (near-neutral and eight hue sectors), weighted by pixel count, so a camera that renders some hues brighter, gathered toward the corners, no longer reads as vignetting. - A CR3 proxy moved to its JPEG's framing gets the vignetting gain of the position each pixel shows (framed_proxies returns the framing). - fit_dng_tone takes a lens database profile in the share the preview shows, as the other raws' fits do; a file's own correction stays whole. - Tests: hue classes, CR3 framing gains, the DNG share, a tinted fully corrected JPEG with and without the profile, and an informative tiny image for the too-few-pixels fallback. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/camera_preview.rs | 7 +- crates/engine/src/camera_preview/look_fit.rs | 132 +++++++++++++++---- crates/engine/src/camera_preview/proxy.rs | 28 +++- crates/engine/src/camera_preview/tests.rs | 121 ++++++++++++++++- docs/camera-preview-colour.md | 10 +- docs/lens-profiles.md | 2 +- 6 files changed, 257 insertions(+), 43 deletions(-) diff --git a/crates/engine/src/camera_preview.rs b/crates/engine/src/camera_preview.rs index dcaa30e47b..7226747684 100644 --- a/crates/engine/src/camera_preview.rs +++ b/crates/engine/src/camera_preview.rs @@ -36,8 +36,8 @@ use hue_sat::{MIN_LUMINANCE_KEPT, NO_LUMINANCE_KEPT, fit_hue_sat, hue_saturation pub(crate) use look_fit::fit_preview; #[cfg(test)] use look_fit::{ - FIT_PREVIEW_MAX_PIXELS, SENSOR_CLIP, at_most, camera_vignetting_amount, clips_more, correct_vignetting, fit_look, fit_look_with, - sony_dro_off_tone, with_dro_off_tone, without_clipped, + FIT_PREVIEW_MAX_PIXELS, ProxyFrame, SENSOR_CLIP, at_most, camera_vignetting_amount, clips_more, correct_vignetting, fit_dng_tone, fit_look, + fit_look_with, sony_dro_off_tone, vignetting_gains, with_dro_off_tone, without_clipped, }; #[cfg(test)] use mono::{fit_dark_tone, fit_tone_only, monochrome, monochrome_look, to_prophoto}; @@ -85,7 +85,8 @@ use tone::{HighlightCheck, ToneFit, fit_tone, fit_tone_conditional, highlights}; /// RAF; other makes with a lens database), so the render no longer applies it twice, at the position a lens warp samples; /// the warped candidate's alignment leaves out the pixels outside the sensor instead of reading them as black. /// 14: a lens database's vignetting correction reaches the sensor proxy only in the share the camera JPEG shows -/// (`camera_vignetting_amount`), not whole: JPEGs that show less of it fitted looks too dark, with skewed colour. +/// (`camera_vignetting_amount`, ranked within chromaticity classes), not whole: JPEGs that show less of it fitted looks too +/// dark, with skewed colour. The same for a DNG's tone fit; a CR3 proxy gets the gain where its framed pixels come from. pub const LOOK_VERSION: u32 = 14; #[derive(Clone, Debug)] diff --git a/crates/engine/src/camera_preview/look_fit.rs b/crates/engine/src/camera_preview/look_fit.rs index 3cde7752fd..b4d0e5f842 100644 --- a/crates/engine/src/camera_preview/look_fit.rs +++ b/crates/engine/src/camera_preview/look_fit.rs @@ -1,7 +1,7 @@ use super::colour::{PROXY, file_local_look}; use super::mono::{fit_tone_only_with, monochrome}; use super::pairs::{Attempt, TONE_FITS, fit_attempt, fit_partial, search_ordered}; -use super::proxy::{CLIP_TOLERANCE, LensGeometry, MIN_PREVIEW_FRACTION, proxies, structure_agrees}; +use super::proxy::{CLIP_TOLERANCE, Cr3Framing, LensGeometry, MIN_PREVIEW_FRACTION, framed_proxies, proxies, structure_agrees}; use super::tone::{HighlightCheck, highlights}; use super::{CameraLook, CameraTone, CameraTransform, HsvTable, Mat3, RawImage, Rgb32f, ToneMap, luminance_2020}; @@ -228,10 +228,14 @@ pub(super) fn fit_dng_tone( let gain = 2f32.powf(transform.baseline_exposure as f32); sensor.map_in_place(|p| tables.apply(p.map(|v| v / gain), gain)); } - correct_vignetting(&mut sensor, lens, raw.orientation); if (sensor.width, sensor.height) != (reference.width, reference.height) { return refuse("preview"); } + // a lens database's correction in the share the camera JPEG shows, as for the other raws (`look_proxies`) + match lens.filter(|l| l.source == lightcraft_develop::LensSource::Database).and_then(|l| l.vignette) { + Some(vignette) => database_vignetting(&mut sensor, &reference, &clipped, &vignette, ProxyFrame::new(raw.orientation)), + None => correct_vignetting(&mut sensor, lens, raw.orientation), + } if !structure_agrees(&sensor, &reference) { return refuse("structure"); } @@ -294,15 +298,43 @@ pub(super) fn look_proxies( let Some(vignette) = database.and_then(|e| e.vignette) else { return proxies(raw, bytes, transform, PROXY, 0.0, lens, embedded); }; - let (mut sensor, reference, clipped) = proxies(raw, bytes, transform, PROXY, 0.0, lens, None)?; + let (mut sensor, reference, clipped, framing) = framed_proxies(raw, bytes, transform, PROXY, 0.0, lens, None)?; // CR3 proxies come back oriented like the JPEG (their framing aligned to it), the others in sensor orientation let orientation = if raw.format == lightcraft_raw::RawFormat::Cr3 { lightcraft_geom::Orientation::Normal } else { raw.orientation }; - let amount = camera_vignetting_amount(&sensor, &reference, &clipped, &vignette, orientation); - apply_vignetting(&mut sensor, &vignette, orientation, amount); + database_vignetting(&mut sensor, &reference, &clipped, &vignette, ProxyFrame { orientation, framing }); + Some((sensor, reference, clipped)) +} + +/// Multiply `sensor` by the share of a lens database's `vignette` correction that the camera JPEG `reference` shows +/// ([`camera_vignetting_amount`]). +fn database_vignetting( + sensor: &mut Rgb32f, + reference: &Rgb32f, + clipped: &[bool], + vignette: &lightcraft_develop::EmbeddedVignette, + frame: ProxyFrame, +) { + let amount = camera_vignetting_amount(sensor, reference, clipped, vignette, frame); + apply_vignetting(sensor, vignette, frame, amount); if lightcraft_pipeline::profiling() { eprintln!("[profile] camera look: the camera JPEG shows {:.0} % of the lens database's vignetting correction", amount * 100.0); } - Some((sensor, reference, clipped)) +} + +/// Where a sensor proxy's pixels sit in the oriented, cropped frame a lens's vignetting is in: `framing` (a CR3 proxy +/// moved to its camera JPEG's framing, [`framed_proxies`]) takes each pixel to the position it shows, and +/// `orientation` maps that position to the oriented frame. +#[derive(Clone, Copy, Debug)] +pub(super) struct ProxyFrame { + pub(super) orientation: lightcraft_geom::Orientation, + pub(super) framing: Option, +} + +impl ProxyFrame { + /// A proxy whose pixels are where they were sampled, in the frame `orientation` maps to the oriented one. + pub(super) fn new(orientation: lightcraft_geom::Orientation) -> Self { + Self { orientation, framing: None } + } } /// Steps between none and all of a lens database's vignetting correction that [`camera_vignetting_amount`] tries. @@ -311,32 +343,76 @@ const VIGNETTING_STEPS: u32 = 20; /// Fewest usable proxy pixels [`camera_vignetting_amount`] measures on; with fewer it keeps the whole correction. const MIN_VIGNETTING_PIXELS: usize = 256; +/// Hue sectors of the sensor's chromaticity that [`camera_vignetting_amount`] ranks within, besides the near-neutral +/// class ([`NEUTRAL_CHROMA`]). +const HUE_SECTORS: usize = 8; + +/// Distance from grey (in `r - g`, `(r + g) / 2 - b` of the sensor's rgb chromaticity) below which a pixel counts as +/// near-neutral in [`camera_vignetting_amount`]. +const NEUTRAL_CHROMA: f64 = 0.03; + +/// Fewest pixels a chromaticity class needs to take part in [`camera_vignetting_amount`]. +const MIN_CLASS_PIXELS: usize = 32; + +/// The chromaticity class of a sensor pixel in [`camera_vignetting_amount`]: 0 near-neutral, else 1 + its hue sector. +fn chromaticity_class(p: [f64; 3]) -> usize { + let p = p.map(|v| v.max(0.0)); + let sum = p[0] + p[1] + p[2]; + if sum.is_nan() || sum <= 0.0 { + return 0; + } + let [r, g, b] = p.map(|v| v / sum); + let (a, yb) = (r - g, (r + g) * 0.5 - b); + if a.hypot(yb) < NEUTRAL_CHROMA { + return 0; + } + let sector = ((yb.atan2(a) + std::f64::consts::PI) / std::f64::consts::TAU * HUE_SECTORS as f64) as usize; + 1 + sector.min(HUE_SECTORS - 1) +} + /// The share (0..=1, as the Vignetting amount slider scales it: `1 + (gain - 1) · amount`) of `vignette` that the /// camera JPEG `reference` shows relative to the uncorrected `sensor` proxy. The camera's tone curve is monotone, so /// with the share the camera applied, the corrected sensor orders its pixels by brightness as the JPEG does: the -/// share with the highest rank correlation of luminance. Clipped and non-finite pixels are left out. +/// share with the highest rank correlation of luminance. A camera's rendering also brightens or darkens some hues +/// more than others, and where those hues gather toward the corners (a blue sky above, a darker foreground), the +/// ranks across hues would read that as vignetting: the correlation is taken within classes of similar sensor +/// chromaticity ([`chromaticity_class`]), averaged by their pixel counts. Clipped and non-finite pixels are left out. pub(super) fn camera_vignetting_amount( sensor: &Rgb32f, reference: &Rgb32f, clipped: &[bool], vignette: &lightcraft_develop::EmbeddedVignette, - orientation: lightcraft_geom::Orientation, + frame: ProxyFrame, ) -> f64 { if sensor.data.len() != reference.data.len() { return 1.0; } - let gains = vignetting_gains(sensor, vignette, orientation); + let gains = vignetting_gains(sensor, vignette, frame); let usable = |p: &[f32; 3]| p.iter().all(|v| v.is_finite()) && luminance_2020(*p) > 0.0; - let pixels: Vec<(f64, [f64; 3], [f64; 3])> = (sensor.data.iter().zip(&reference.data).zip(gains).enumerate()) - .filter(|(i, ((s, r), _))| !clipped.get(*i).copied().unwrap_or(false) && usable(s) && usable(r)) - .map(|(_, ((s, r), g))| (g, s.map(f64::from), r.map(f64::from))) - .collect(); - if pixels.len() < MIN_VIGNETTING_PIXELS { + let mut classes: Vec> = vec![Vec::new(); HUE_SECTORS + 1]; + for (i, ((s, r), g)) in sensor.data.iter().zip(&reference.data).zip(gains).enumerate() { + if clipped.get(i).copied().unwrap_or(false) || !usable(s) || !usable(r) { + continue; + } + let s = s.map(f64::from); + if let Some(class) = classes.get_mut(chromaticity_class(s)) { + class.push((g, s, r.map(f64::from))); + } + } + classes.retain(|c| c.len() >= MIN_CLASS_PIXELS); + let total: usize = classes.iter().map(Vec::len).sum(); + if total < MIN_VIGNETTING_PIXELS { return 1.0; } let agreement = |amount: f64| { - let pairs: Vec<([f64; 3], [f64; 3])> = pixels.iter().map(|(g, s, r)| (s.map(|v| v * (1.0 + (g - 1.0) * amount)), *r)).collect(); - super::pairs::rank_correlation_ties(&pairs) + let weighted: f64 = classes + .iter() + .map(|class| { + let pairs: Vec<([f64; 3], [f64; 3])> = class.iter().map(|(g, s, r)| (s.map(|v| v * (1.0 + (g - 1.0) * amount)), *r)).collect(); + super::pairs::rank_correlation_ties(&pairs) * class.len() as f64 + }) + .sum(); + weighted / total as f64 }; // ties keep the larger share, the correction a camera JPEG usually has (the file's own data always gets all of it) (0..=VIGNETTING_STEPS) @@ -346,23 +422,31 @@ pub(super) fn camera_vignetting_amount( .0 } -/// The render's vignetting gain (`optics::embedded_vignette_gain`) at each pixel of `sensor` (in the frame -/// `orientation` maps to the oriented, cropped frame `vignette` is in). -fn vignetting_gains(sensor: &Rgb32f, vignette: &lightcraft_develop::EmbeddedVignette, orientation: lightcraft_geom::Orientation) -> Vec { +/// The render's vignetting gain (`optics::embedded_vignette_gain`) at each pixel of `sensor`, at the position `frame` +/// gives it in the oriented, cropped frame `vignette` is in. +pub(super) fn vignetting_gains(sensor: &Rgb32f, vignette: &lightcraft_develop::EmbeddedVignette, frame: ProxyFrame) -> Vec { let (w, h) = (sensor.width as f64, sensor.height as f64); - let (ow, oh) = if orientation.swaps_axes() { (h, w) } else { (w, h) }; + let (ow, oh) = if frame.orientation.swaps_axes() { (h, w) } else { (w, h) }; let width = sensor.width.max(1); (0..sensor.data.len()) .map(|i| { - let (x, y) = orientation.map((i % width) as f64 + 0.5, (i / width) as f64 + 0.5, w, h); + let (px, py) = ((i % width) as f64 + 0.5, (i / width) as f64 + 0.5); + let (px, py) = match frame.framing { + Some(framing) => { + let (x, y) = framing.source(px as f32, py as f32, sensor.width, sensor.height); + (f64::from(x), f64::from(y)) + } + None => (px, py), + }; + let (x, y) = frame.orientation.map(px, py, w, h); lightcraft_pipeline::optics::embedded_vignette_gain(vignette, lightcraft_geom::Point::new(x, y), ow, oh) }) .collect() } /// Multiply `sensor` by `amount` of `vignette`'s correction, as the Vignetting amount slider scales it. -fn apply_vignetting(sensor: &mut Rgb32f, vignette: &lightcraft_develop::EmbeddedVignette, orientation: lightcraft_geom::Orientation, amount: f64) { - let gains = vignetting_gains(sensor, vignette, orientation); +fn apply_vignetting(sensor: &mut Rgb32f, vignette: &lightcraft_develop::EmbeddedVignette, frame: ProxyFrame, amount: f64) { + let gains = vignetting_gains(sensor, vignette, frame); for (p, g) in sensor.data.iter_mut().zip(gains) { let gain = (1.0 + (g - 1.0) * amount) as f32; *p = p.map(|v| v * gain); @@ -374,5 +458,5 @@ fn apply_vignetting(sensor: &mut Rgb32f, vignette: &lightcraft_develop::Embedded /// camera JPEG has it applied, so a tone fitted without it brightens the render's corners twice. pub(super) fn correct_vignetting(sensor: &mut Rgb32f, lens: Option<&lightcraft_develop::EmbeddedLens>, orientation: lightcraft_geom::Orientation) { let Some(vignette) = lens.and_then(|l| l.vignette) else { return }; - apply_vignetting(sensor, &vignette, orientation, 1.0); + apply_vignetting(sensor, &vignette, ProxyFrame::new(orientation), 1.0); } diff --git a/crates/engine/src/camera_preview/proxy.rs b/crates/engine/src/camera_preview/proxy.rs index e5a9cf06cb..a6ccefce7d 100644 --- a/crates/engine/src/camera_preview/proxy.rs +++ b/crates/engine/src/camera_preview/proxy.rs @@ -93,6 +93,20 @@ pub(super) fn proxies( lens: Option<&LensGeometry>, embedded: Option<&lightcraft_develop::EmbeddedLens>, ) -> Option<(Rgb32f, Rgb32f, Vec)> { + framed_proxies(raw, bytes, transform, size, min_fraction, lens, embedded).map(|(sensor, reference, clipped, _)| (sensor, reference, clipped)) +} + +/// [`proxies`], and the CR3 framing the sensor proxy was moved to ([`estimate_cr3_framing`]; `None` when it was not +/// moved): a pixel of the returned CR3 sensor proxy shows the oriented sensor at `framing.source` of its position. +pub(super) fn framed_proxies( + raw: &RawImage, + bytes: &[u8], + transform: &CameraTransform, + size: usize, + min_fraction: f64, + lens: Option<&LensGeometry>, + embedded: Option<&lightcraft_develop::EmbeddedLens>, +) -> Option<(Rgb32f, Rgb32f, Vec, Option)> { let edge = (2 * size).max(384) as u32; let decoded = crate::files::decode_raw_preview( bytes, @@ -127,6 +141,7 @@ pub(super) fn proxies( let k = (crop.width.max(crop.height).div_ceil(edge as usize).max(2)).div_ceil(2) * 2; let mut sensor = sensor_proxy(raw, k, edge as usize)?; let mut clipped = clip_mask(&sensor); + let mut framing = None; correct_vignetting(&mut sensor, embedded, raw.orientation); // The camera may have corrected its JPEG with the lens warp the file carries (Nikon Z lenses always, at up to 9 % // zoom; other cameras when their correction setting is on): the sensor moved into that geometry is a candidate, @@ -144,7 +159,7 @@ pub(super) fn proxies( sensor.map_in_place(to_working); // Canon's JPEG can be cropped differently from the sensor. Estimate only that common // framing from edge directions, independently of the subsequent colour fit. - (sensor, clipped) = align_cr3_framing(sensor, clipped, &reference); + (sensor, clipped, framing) = align_cr3_framing(sensor, clipped, &reference); } let mut sensor = fit(&sensor, size, size, Filter::Box); let mut reference_small = reference_at(&reference, &sensor); @@ -163,7 +178,7 @@ pub(super) fn proxies( if raw.format != RawFormat::Cr3 { sensor.map_in_place(to_working); } - Some((sensor, reference, clipped)) + Some((sensor, reference, clipped, framing)) } /// The camera JPEG at exactly the sensor proxy's size, so the two pair pixel for pixel. Fitting it @@ -299,9 +314,10 @@ pub(super) fn estimate_cr3_framing(sensor: &Rgb32f, reference: &Rgb32f) -> Optio Some(best.1) } -/// The sensor proxy and its clip mask (same size), both moved to the camera JPEG's framing. -fn align_cr3_framing(sensor: Rgb32f, clipped: Rgb32f, reference: &Rgb32f) -> (Rgb32f, Rgb32f) { - let Some(framing) = estimate_cr3_framing(&sensor, reference) else { return (sensor, clipped) }; +/// The sensor proxy and its clip mask (same size), both moved to the camera JPEG's framing, and that framing (`None` +/// when they stay as they are). +fn align_cr3_framing(sensor: Rgb32f, clipped: Rgb32f, reference: &Rgb32f) -> (Rgb32f, Rgb32f, Option) { + let Some(framing) = estimate_cr3_framing(&sensor, reference) else { return (sensor, clipped, None) }; let apply = |image: &Rgb32f| { let (w, h) = (image.width, image.height); let mut aligned = Rgb32f::new(w, h); @@ -311,7 +327,7 @@ fn align_cr3_framing(sensor: Rgb32f, clipped: Rgb32f, reference: &Rgb32f) -> (Rg } aligned }; - (apply(&sensor), apply(&clipped)) + (apply(&sensor), apply(&clipped), Some(framing)) } /// Colour training pairs of one raw for a camera profile: white-balanced camera RGB (with the diff --git a/crates/engine/src/camera_preview/tests.rs b/crates/engine/src/camera_preview/tests.rs index ea4d86940c..da3cde71ee 100644 --- a/crates/engine/src/camera_preview/tests.rs +++ b/crates/engine/src/camera_preview/tests.rs @@ -2225,20 +2225,26 @@ fn a_lens_database_vignetting_reaches_the_fit_as_far_as_the_camera_jpeg_shows_it assert!((with_profile - without).abs() < 0.01, "as without the profile: {with_profile} vs {without}"); // the same in colour: the tinted JPEG takes the colour fit, whose curve the whole profile darkened - let (raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, vignetted, [1.0, 0.9, 0.8]); - let colour_at_quarter = |embedded: Option<&EmbeddedLens>| { - let (_, _, look) = starting_colour(&mut raw.clone(), &bytes, None, embedded); + let tint = [1.0, 0.9, 0.8]; + let (raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, vignetted, tint); + let colour_at_quarter = |raw: &RawImage, bytes: &[u8], embedded: Option<&EmbeddedLens>| { + let (_, _, look) = starting_colour(&mut raw.clone(), bytes, None, embedded); let look = look.expect("a colour look"); assert!(look.matrix != Mat3::IDENTITY || look.hue_sat.is_some(), "the colour fit, not the black-and-white one"); ToneMap::camera(&look.tone, 0.0, 0.0, 0.0).apply(0.25) }; - let (with_profile, without) = (colour_at_quarter(Some(&database)), colour_at_quarter(None)); + let (with_profile, without) = (colour_at_quarter(&raw, &bytes, Some(&database)), colour_at_quarter(&raw, &bytes, None)); assert!((with_profile - without).abs() < 0.01, "the colour fit's curve as without the profile: {with_profile} vs {without}"); // a camera JPEG with the whole correction applied: the whole correction reaches the fit let (raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, scene, [1.0; 3]); let corrected = at_quarter(&raw, &bytes, Some(&database)); assert!((corrected - camera).abs() < 0.03, "the camera's curve at 0.25: {corrected} vs {camera}"); + + // and in colour: the colour fit keeps the database profile, whose correction it would otherwise learn into the curve + let (raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, scene, tint); + let (with_profile, without) = (colour_at_quarter(&raw, &bytes, Some(&database)), colour_at_quarter(&raw, &bytes, None)); + assert!(without > with_profile + 0.03, "without the profile the colour fit's curve brightens: {without} vs {with_profile}"); } /// A lens-warped sensor proxy gets the vignetting gain of the position it samples, as the render applies it: the @@ -2493,6 +2499,40 @@ fn dng_tone_gates_follow_the_files_own_tags() { assert!(looked.profile.look_table.is_some() && eligible(RawFormat::Dng, looked), "a look table defines colour, not tone"); } +/// A DNG's tone fit takes a lens database's vignetting correction in the share its camera JPEG shows, as the other +/// raws' fits do: none for a JPEG left uncorrected (the curve as without the profile), all of it for a corrected one. +#[test] +fn dng_tone_fit_takes_a_lens_database_vignetting_as_far_as_the_camera_jpeg_shows_it() { + use lightcraft_develop::{EmbeddedLens, EmbeddedVignette, LensSource}; + let (w, h) = (384usize, 256usize); + let vignette = EmbeddedVignette { k: [1.0, 0.0, 0.0, 0.0, 0.0], center: lightcraft_geom::Point::new(0.5, 0.5), radius: 0.6 }; + let gain = |x: usize, y: usize| { + let point = lightcraft_geom::Point::new(x as f64 + 0.5, y as f64 + 0.5); + lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, point, w as f64, h as f64) as f32 + }; + // a gradient with blocks of texture, so the preview shows the same picture (`structure_agrees`) + let texture = |x: usize, y: usize| ((x / 6).wrapping_mul(2_654_435_761) ^ (y / 6).wrapping_mul(40_503)) % 7; + let scene = |x: usize, y: usize| (0.01 + 0.5 * (x as f32 / w as f32) * (0.3 + 0.7 * y as f32 / h as f32)) * (0.55 + 0.075 * texture(x, y) as f32); + let vignetted = |x: usize, y: usize| scene(x, y) / gain(x, y); + let database = EmbeddedLens { vignette: Some(vignette), source: LensSource::Database, ..Default::default() }; + let at_quarter = |jpeg_scene: &dyn Fn(usize, usize) -> f32, embedded: Option<&EmbeddedLens>| { + // tinted: a fit's pairs need some colour (`collect_pairs`) + let (mut raw, bytes) = grey_raw_with_jpeg((w, h), vignetted, jpeg_scene, [1.0, 0.95, 0.9]); + raw.format = RawFormat::Dng; + // a camera whose RGB is linear sRGB (XYZ to sRGB under D65): the grey sensor stays grey + let xyz_to_camera = Mat3([[3.2406, -1.5372, -0.4986], [-0.9689, 1.8758, 0.0415], [0.0557, -0.2040, 1.0570]]); + raw.color = lightcraft_raw::ColorData { illuminant: [21, 0], color_matrix: [Some(xyz_to_camera), None], ..Default::default() }; + let t = lightcraft_raw::color::camera_transform(&raw, lightcraft_raw::color::as_shot_white_xy(&raw)); + assert!(dng_tone_eligible(&raw, &t), "a DNG whose tone the fit defines"); + let look = fit_dng_tone(&raw, &bytes, &t, embedded).expect("a DNG tone fit"); + ToneMap::camera(&look.tone, 0.0, 0.0, 0.0).apply(0.25) + }; + let (with_profile, without) = (at_quarter(&vignetted, Some(&database)), at_quarter(&vignetted, None)); + assert!((with_profile - without).abs() < 0.01, "an uncorrected JPEG, as without the profile: {with_profile} vs {without}"); + let (with_profile, without) = (at_quarter(&scene, Some(&database)), at_quarter(&scene, None)); + assert!(without > with_profile + 0.03, "a corrected JPEG: without the profile the curve brightens: {without} vs {with_profile}"); +} + /// The 1024 px preview Lightroom Classic writes into a 7008 px DNG is fitted; a 256 px thumbnail is not. #[test] fn dng_tone_fit_takes_lightroom_sized_previews() { @@ -2709,9 +2749,76 @@ fn camera_vignetting_amount_measures_the_share_the_jpeg_shows() { let g = 1.0 + (gain(i) - 1.0) * shown; *p = p.map(|c| (c * g).powf(1.0 / 2.2).min(1.0)); } - let amount = camera_vignetting_amount(&sensor, &reference, &clipped, &vignette, lightcraft_geom::Orientation::Normal); + let amount = camera_vignetting_amount(&sensor, &reference, &clipped, &vignette, ProxyFrame::new(lightcraft_geom::Orientation::Normal)); assert!((amount - f64::from(shown)).abs() < 0.051, "the JPEG shows {shown}, measured {amount}"); } - let tiny = Rgb32f::new(8, 8); - assert_eq!(camera_vignetting_amount(&tiny, &tiny, &[false; 64], &vignette, lightcraft_geom::Orientation::Normal), 1.0); + // too few pixels to measure on: the whole correction, although this JPEG shows none of it + let (tw, th) = (8, 8); + let mut tiny = Rgb32f::new(tw, th); + for (i, p) in tiny.data.iter_mut().enumerate() { + let g = lightcraft_pipeline::optics::embedded_vignette_gain( + &vignette, + lightcraft_geom::Point::new((i % tw) as f64 + 0.5, (i / tw) as f64 + 0.5), + tw as f64, + th as f64, + ) as f32; + let v = 0.05 + 0.05 * ((i * 7) % 8) as f32; + *p = [v, v * 0.9, v * 0.7].map(|c| c / g); + } + let mut tiny_reference = tiny.clone(); + tiny_reference.map_in_place(|p| p.map(|c| c.powf(1.0 / 2.2))); + let frame = ProxyFrame::new(lightcraft_geom::Orientation::Normal); + assert_eq!(camera_vignetting_amount(&tiny, &tiny_reference, &[false; 64], &vignette, frame), 1.0); +} + +/// A camera that renders some hues brighter than others doesn't read as vignetting where those hues gather toward +/// the corners: the JPEG without any correction, whose blue pixels (more of them toward the corners) the camera +/// renders brighter, is measured as showing none of it. +#[test] +fn camera_vignetting_amount_is_not_misled_by_hues_gathered_in_the_corners() { + use lightcraft_develop::EmbeddedVignette; + let vignette = EmbeddedVignette { k: [2.0, 1.0, 0.0, 0.0, 0.0], center: lightcraft_geom::Point::new(0.5, 0.5), radius: 0.6 }; + let (w, h) = (96, 64); + let mut seed = 54321u32; + let mut random = || { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + (seed >> 8) as f32 / (1u32 << 24) as f32 + }; + let mut sensor = Rgb32f::new(w, h); + let mut reference = Rgb32f::new(w, h); + for (i, (s, r)) in sensor.data.iter_mut().zip(reference.data.iter_mut()).enumerate() { + let point = lightcraft_geom::Point::new((i % w) as f64 + 0.5, (i / w) as f64 + 0.5); + let gain = lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, point, w as f64, h as f64) as f32; + let radius = ((point.x / w as f64 - 0.5).hypot(point.y / h as f64 - 0.5) / 0.5f64.hypot(0.5)) as f32; + let v = (0.02 + 0.4 * random()) / gain; + let blue = random() < radius; + *s = if blue { [0.4 * v, 0.6 * v, v] } else { [v, 0.6 * v, 0.4 * v] }; + let brightness = if blue { 1.6 } else { 1.0 }; + *r = s.map(|c| (c * brightness).powf(1.0 / 2.2).min(1.0)); + } + let clipped = vec![false; w * h]; + let amount = camera_vignetting_amount(&sensor, &reference, &clipped, &vignette, ProxyFrame::new(lightcraft_geom::Orientation::Normal)); + assert!(amount < 0.051, "the JPEG shows no correction, measured {amount}"); +} + +/// A CR3 sensor proxy moved to its camera JPEG's framing gets the vignetting gain of the position each pixel shows, +/// not of where it lands. +#[test] +fn the_vignetting_gain_of_a_framed_proxy_is_taken_where_its_pixels_come_from() { + use lightcraft_develop::EmbeddedVignette; + let vignette = EmbeddedVignette { k: [2.0, 1.0, 0.0, 0.0, 0.0], center: lightcraft_geom::Point::new(0.5, 0.5), radius: 0.6 }; + let (w, h) = (96, 64); + let framing = Cr3Framing { scale: 0.92, offset: [0.02, -0.015] }; + let frame = ProxyFrame { orientation: lightcraft_geom::Orientation::Normal, framing: Some(framing) }; + let gains = vignetting_gains(&Rgb32f::new(w, h), &vignette, frame); + for i in [0, w - 1, w * h / 2 + w / 3, w * h - 1] { + let (x, y) = framing.source((i % w) as f32 + 0.5, (i / w) as f32 + 0.5, w, h); + let expected = lightcraft_pipeline::optics::embedded_vignette_gain( + &vignette, + lightcraft_geom::Point::new(f64::from(x), f64::from(y)), + w as f64, + h as f64, + ); + assert!((gains[i] - expected).abs() < 1e-9, "pixel {i}: {} vs {expected}", gains[i]); + } } diff --git a/docs/camera-preview-colour.md b/docs/camera-preview-colour.md index e04a9cbd7a..e73d433a13 100644 --- a/docs/camera-preview-colour.md +++ b/docs/camera-preview-colour.md @@ -139,7 +139,11 @@ it (Enable Profile Corrections). The camera applied the same correction to its J the render added the correction again. A lens-database profile (`LIGHTCRAFT_LENSFUN_DIR`, the downloaded lensfun data) describes the lens, not what the camera did to its JPEG, so since `LOOK_VERSION` 14 it reaches the fit only in the share the JPEG shows (`camera_vignetting_amount`): the camera's tone curve is monotone, so the share whose corrected -sensor ranks its pixels' luminance most like the JPEG wins (0 to 100 % in 5 % steps). With the whole profile, an +sensor ranks its pixels' luminance most like the JPEG wins (0 to 100 % in 5 % steps). The ranks are compared within +classes of similar sensor chromaticity (near-neutral and eight hue sectors), weighted by their pixel counts: a camera +that renders some hues brighter than others would otherwise read as vignetting where those hues gather toward the +corners. A CR3 proxy moved to its JPEG's framing gets the gain of the position each pixel shows, and a DNG's tone fit +(`fit_dng_tone`) takes a database profile in the measured share too. With the whole profile, an ILCE-7M3 frame at f/2 with an FE 28mm F2 (lensfun: corners at 16 % of the centre) fitted 0.062 held-out RMS instead of 0.027 and rendered darker and orange. Its camera JPEG, with vignetting correction Auto in the maker note, shows none of that correction relative to the raw (rank correlation 0.991 at 0 % down to 0.903 at 100 %), like the A7CR @@ -147,7 +151,9 @@ JPEGs in [sony-lens-corrections.md](sony-lens-corrections.md). Of 91 CC0 corpus PEFs, 20 NEFs), 10 get a lens database vignetting profile into their fit (7 Sony, 2 Olympus, 1 Nikon); the shares measured were 0 % on six, 10–45 % on four, and the mean CIEDE2000 of the central half of the render against the camera JPEG went from 4.69 to 3.96 (6 better, 3 within 0.05, an SLT-A33 0.12 worse). The whole profile had made 9 of -the 10 darker (0.4 to 8.5 L*), with small, scene-dependent colour shifts (mostly lost chroma). +the 10 darker (0.4 to 8.5 L*), with small, scene-dependent colour shifts (mostly lost chroma). These figures predate +the chromaticity classes, which moved four of the ten shares by one step: three of the six at 0 % to 5 % (SLT-A33, +ILCE-7SM2, DSC-RX1RM3) and the PEN-F from 10 to 15 %. `proxies` applies the gain before it moves the sensor into the JPEG's lens geometry, so each pixel gets the gain of the position it samples, as in the render. The choice between the warped and the plain sensor (`alignment`) leaves out the pixels the warp finds no sensor for. It used to read them as black, a false edge along the border that made the diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index 2817cd791e..060eb96046 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -83,7 +83,7 @@ stage (one resample), orientation handling and DNG export. A conversion whose ra half diagonal, or that would fold the image, is not applied. A raw whose look is fitted to its own camera JPEG (Sony, Nikon, Pentax, Olympus and others without a colour -matrix, [camera-preview-colour.md](camera-preview-colour.md)) has that fit made on the sensor with only the +matrix, and a DNG whose tone is fitted to its preview, [camera-preview-colour.md](camera-preview-colour.md)) has that fit made on the sensor with only the share of a profile's vignetting correction its camera JPEG shows, measured per photo: the profile describes the lens, not what the camera did to its JPEG relative to the raw (a Sony JPEG with shading compensation Auto can show none of it). The render From ca64d3c171a28d6cdf8d8b6fe203a6c2756db6e7 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Sun, 8 Nov 2026 11:44:49 +0100 Subject: [PATCH 3/6] Review fixes --- crates/engine/src/camera_preview/look_fit.rs | 5 +- crates/engine/src/camera_preview/tests.rs | 147 +++++++++++++++++++ docs/camera-preview-colour.md | 25 ++-- docs/lens-profiles.md | 4 +- 4 files changed, 167 insertions(+), 14 deletions(-) diff --git a/crates/engine/src/camera_preview/look_fit.rs b/crates/engine/src/camera_preview/look_fit.rs index b4d0e5f842..6d2d21c041 100644 --- a/crates/engine/src/camera_preview/look_fit.rs +++ b/crates/engine/src/camera_preview/look_fit.rs @@ -376,7 +376,10 @@ fn chromaticity_class(p: [f64; 3]) -> usize { /// share with the highest rank correlation of luminance. A camera's rendering also brightens or darkens some hues /// more than others, and where those hues gather toward the corners (a blue sky above, a darker foreground), the /// ranks across hues would read that as vignetting: the correlation is taken within classes of similar sensor -/// chromaticity ([`chromaticity_class`]), averaged by their pixel counts. Clipped and non-finite pixels are left out. +/// chromaticity ([`chromaticity_class`]), averaged by their pixel counts. Within a class the camera's rendering is +/// taken as alike: two saturations of one hue that it renders differently, one gathered toward the corners, can still +/// read as vignetting. Clipped, black and non-finite pixels are left out. Proxies of different sizes, or fewer than +/// [`MIN_VIGNETTING_PIXELS`] usable pixels, keep the whole correction. pub(super) fn camera_vignetting_amount( sensor: &Rgb32f, reference: &Rgb32f, diff --git a/crates/engine/src/camera_preview/tests.rs b/crates/engine/src/camera_preview/tests.rs index da3cde71ee..6395b4fc1b 100644 --- a/crates/engine/src/camera_preview/tests.rs +++ b/crates/engine/src/camera_preview/tests.rs @@ -2501,6 +2501,7 @@ fn dng_tone_gates_follow_the_files_own_tags() { /// A DNG's tone fit takes a lens database's vignetting correction in the share its camera JPEG shows, as the other /// raws' fits do: none for a JPEG left uncorrected (the curve as without the profile), all of it for a corrected one. +/// The file's own vignetting data still reaches it whole. #[test] fn dng_tone_fit_takes_a_lens_database_vignetting_as_far_as_the_camera_jpeg_shows_it() { use lightcraft_develop::{EmbeddedLens, EmbeddedVignette, LensSource}; @@ -2531,6 +2532,13 @@ fn dng_tone_fit_takes_a_lens_database_vignetting_as_far_as_the_camera_jpeg_shows assert!((with_profile - without).abs() < 0.01, "an uncorrected JPEG, as without the profile: {with_profile} vs {without}"); let (with_profile, without) = (at_quarter(&scene, Some(&database)), at_quarter(&scene, None)); assert!(without > with_profile + 0.03, "a corrected JPEG: without the profile the curve brightens: {without} vs {with_profile}"); + + // the file's own vignetting data is what its camera applied: it reaches the fit whole, whatever the JPEG shows + let file = EmbeddedLens { source: LensSource::File, ..database }; + let with_file = at_quarter(&scene, Some(&file)); + assert!((with_file - with_profile).abs() < 0.01, "a corrected JPEG, as with the measured share: {with_file} vs {with_profile}"); + let (with_file, without) = (at_quarter(&vignetted, Some(&file)), at_quarter(&vignetted, None)); + assert!(without > with_file + 0.03, "an uncorrected JPEG still gets the file's whole correction: {without} vs {with_file}"); } /// The 1024 px preview Lightroom Classic writes into a 7008 px DNG is fitted; a 256 px thumbnail is not. @@ -2822,3 +2830,142 @@ fn the_vignetting_gain_of_a_framed_proxy_is_taken_where_its_pixels_come_from() { assert!((gains[i] - expected).abs() < 1e-9, "pixel {i}: {} vs {expected}", gains[i]); } } + +/// What `camera_vignetting_amount` can't measure on keeps the whole correction: proxies of different sizes, and a flat +/// scene, where every share ranks alike (a tie keeps the larger share, the correction a camera JPEG usually has). +/// Clipped and black pixels are left out: here they sit where the correction would brighten or darken them most and +/// would pull the share of a JPEG without any correction up. Non-finite pixels don't spoil the measurement. +#[test] +fn camera_vignetting_amount_falls_back_and_leaves_out_what_it_cannot_measure() { + use lightcraft_develop::EmbeddedVignette; + let vignette = EmbeddedVignette { k: [2.0, 1.0, 0.0, 0.0, 0.0], center: lightcraft_geom::Point::new(0.5, 0.5), radius: 0.6 }; + let (w, h) = (96, 64); + let gains: Vec = (0..w * h) + .map(|i| { + let point = lightcraft_geom::Point::new((i % w) as f64 + 0.5, (i / w) as f64 + 0.5); + lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, point, w as f64, h as f64) as f32 + }) + .collect(); + let measure = |sensor: &Rgb32f, reference: &Rgb32f, clipped: &[bool]| { + camera_vignetting_amount(sensor, reference, clipped, &vignette, ProxyFrame::new(lightcraft_geom::Orientation::Normal)) + }; + let unclipped = vec![false; w * h]; + + let mut flat = Rgb32f::new(w, h); + flat.data.fill([0.2, 0.18, 0.14]); + assert_eq!(measure(&flat, &flat, &unclipped), 1.0, "a tie keeps the whole correction"); + assert_eq!(measure(&flat, &Rgb32f::new(w, h / 2), &unclipped), 1.0, "proxies of different sizes"); + + // a scene of unrelated brightnesses, darkened by the lens, and its JPEG without any correction + let mut seed = 777u32; + let mut sensor = Rgb32f::new(w, h); + for (p, g) in sensor.data.iter_mut().zip(&gains) { + seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); + let v = 0.02 + 0.4 * (seed >> 8) as f32 / (1u32 << 24) as f32; + *p = [v, v * 0.9, v * 0.7].map(|c| c / g); + } + let mut reference = sensor.clone(); + reference.data.iter_mut().for_each(|p| *p = p.map(|c| c.powf(1.0 / 2.2))); + assert!(measure(&sensor, &reference, &unclipped) < 0.051); + let mut sorted = gains.clone(); + sorted.sort_by(f32::total_cmp); + let (low, high) = (sorted[w * h / 4], sorted[w * h * 3 / 4]); + + // the quarter the correction brightens most, clipped white in the JPEG + let corners: Vec = gains.iter().map(|g| *g >= high).collect(); + let mut clipped_reference = reference.clone(); + clipped_reference.data.iter_mut().zip(&corners).filter(|(_, c)| **c).for_each(|(p, _)| *p = [1.0; 3]); + let amount = measure(&sensor, &clipped_reference, &corners); + assert!(amount < 0.051, "clipped pixels are left out: measured {amount}"); + let amount = measure(&sensor, &clipped_reference, &unclipped); + assert!(amount > 0.1, "the clipped pixels would have pulled the share up: {amount}"); + + // the quarter it brightens least, black in the JPEG + let mut dark_reference = reference.clone(); + let darkened = |level: f32| { + let mut r = dark_reference.clone(); + r.data.iter_mut().zip(&gains).filter(|(_, g)| **g <= low).for_each(|(p, _)| *p = [level; 3]); + r + }; + let amount = measure(&sensor, &darkened(0.0), &unclipped); + assert!(amount < 0.051, "black pixels are left out: measured {amount}"); + let amount = measure(&sensor, &darkened(1e-3), &unclipped); + assert!(amount > 0.1, "the same pixels, measurable, would have pulled the share up: {amount}"); + + // non-finite pixels in either proxy + for i in (0..w * h).step_by(37) { + sensor.data[i] = [f32::NAN, 0.1, 0.1]; + dark_reference.data[i + 1] = [f32::INFINITY; 3]; + } + let amount = measure(&sensor, &dark_reference, &unclipped); + assert!(amount < 0.051, "non-finite pixels are left out: measured {amount}"); +} + +/// A sensor proxy of a photo turned upright gets the gain of where each pixel lands in the upright frame the lens +/// profile describes: with the optical centre off the middle, pixel (x, y) of a 96 × 64 sensor turned a quarter +/// clockwise takes the gain at (64 - y, x) of the 64 × 96 upright frame, and turned anticlockwise at (y, 96 - x). +#[test] +fn the_vignetting_gain_of_a_turned_proxy_is_taken_in_the_upright_frame() { + use lightcraft_develop::EmbeddedVignette; + use lightcraft_geom::{Orientation, Point}; + let vignette = EmbeddedVignette { k: [2.0, 1.0, 0.0, 0.0, 0.0], center: Point::new(0.3, 0.6), radius: 0.6 }; + let (w, h) = (96usize, 64usize); + let (wf, hf) = (w as f64, h as f64); + let normal = vignetting_gains(&Rgb32f::new(w, h), &vignette, ProxyFrame::new(Orientation::Normal)); + for (orientation, upright) in [ + (Orientation::Rotate90, (|x: f64, y: f64, _: f64, h: f64| (h - y, x)) as fn(f64, f64, f64, f64) -> (f64, f64)), + (Orientation::Rotate270, |x, y, w, _| (y, w - x)), + ] { + let gains = vignetting_gains(&Rgb32f::new(w, h), &vignette, ProxyFrame::new(orientation)); + let mut differ = 0; + for i in 0..w * h { + let (x, y) = upright((i % w) as f64 + 0.5, (i / w) as f64 + 0.5, wf, hf); + let expected = lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, Point::new(x, y), hf, wf); + assert!((gains[i] - expected).abs() < 1e-9, "{orientation:?}, pixel {i}: {} vs {expected}", gains[i]); + differ += usize::from((gains[i] - normal[i]).abs() > 1e-3); + } + assert!(differ > w * h / 2, "{orientation:?}: the turn matters for this lens ({differ} pixels differ)"); + } +} + +/// A CR3 sensor proxy moved to its camera JPEG's framing is measured with the gain of where its pixels come from: the +/// share a JPEG with the whole correction shows is found with the framing, and missed without it. +#[test] +fn camera_vignetting_amount_follows_a_cr3_proxys_framing() { + use lightcraft_develop::EmbeddedVignette; + use lightcraft_geom::{Orientation, Point}; + let vignette = EmbeddedVignette { k: [2.0, 1.0, 0.0, 0.0, 0.0], center: Point::new(0.5, 0.5), radius: 0.6 }; + let (w, h) = (96usize, 64usize); + let framing = Cr3Framing { scale: 0.8, offset: [0.08, -0.06] }; + // a scene of unrelated brightnesses, one per sensor pixel + let scene = |x: f32, y: f32| { + let seed = (x.floor() as u32).wrapping_mul(73_856_093) ^ (y.floor() as u32).wrapping_mul(19_349_663); + 0.02 + 0.4 * (seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223) >> 8) as f32 / (1u32 << 24) as f32 + }; + // the framed proxy: pixel q shows the vignetted sensor at `framing.source(q)` + let mut sensor = Rgb32f::new(w, h); + let mut source_gains = Vec::with_capacity(w * h); + for (i, p) in sensor.data.iter_mut().enumerate() { + let (x, y) = framing.source((i % w) as f32 + 0.5, (i / w) as f32 + 0.5, w, h); + let g = lightcraft_pipeline::optics::embedded_vignette_gain(&vignette, Point::new(f64::from(x), f64::from(y)), w as f64, h as f64) as f32; + let v = scene(x, y) / g; + *p = [v, v * 0.9, v * 0.7]; + source_gains.push(g); + } + let clipped = vec![false; w * h]; + let measure = |reference: &Rgb32f, framing: Option| { + camera_vignetting_amount(&sensor, reference, &clipped, &vignette, ProxyFrame { orientation: Orientation::Normal, framing }) + }; + for shown in [0.0f32, 1.0] { + let mut reference = sensor.clone(); + for (p, g) in reference.data.iter_mut().zip(&source_gains) { + *p = p.map(|c| (c * (1.0 + (g - 1.0) * shown)).powf(1.0 / 2.2)); + } + let framed = measure(&reference, Some(framing)); + assert!((framed - f64::from(shown)).abs() < 0.051, "the JPEG shows {shown}, measured {framed} with the framing"); + if shown == 1.0 { + let unframed = measure(&reference, None); + assert!(unframed < 0.9, "without the framing the gains are taken at the wrong places: {unframed}"); + } + } +} diff --git a/docs/camera-preview-colour.md b/docs/camera-preview-colour.md index e73d433a13..a10b8dac03 100644 --- a/docs/camera-preview-colour.md +++ b/docs/camera-preview-colour.md @@ -143,17 +143,20 @@ sensor ranks its pixels' luminance most like the JPEG wins (0 to 100 % in 5 % st classes of similar sensor chromaticity (near-neutral and eight hue sectors), weighted by their pixel counts: a camera that renders some hues brighter than others would otherwise read as vignetting where those hues gather toward the corners. A CR3 proxy moved to its JPEG's framing gets the gain of the position each pixel shows, and a DNG's tone fit -(`fit_dng_tone`) takes a database profile in the measured share too. With the whole profile, an -ILCE-7M3 frame at f/2 with an FE 28mm F2 (lensfun: corners at 16 % of the centre) fitted 0.062 held-out RMS instead -of 0.027 and rendered darker and orange. Its camera JPEG, with vignetting correction Auto in the maker note, shows -none of that correction relative to the raw (rank correlation 0.991 at 0 % down to 0.903 at 100 %), like the A7CR -JPEGs in [sony-lens-corrections.md](sony-lens-corrections.md). Of 91 CC0 corpus raws (all 37 ARWs, 19 ORFs, 15 -PEFs, 20 NEFs), 10 get a lens database vignetting profile into their fit (7 Sony, 2 Olympus, 1 Nikon); the shares -measured were 0 % on six, 10–45 % on four, and the mean CIEDE2000 of the central half of the render against the -camera JPEG went from 4.69 to 3.96 (6 better, 3 within 0.05, an SLT-A33 0.12 worse). The whole profile had made 9 of -the 10 darker (0.4 to 8.5 L*), with small, scene-dependent colour shifts (mostly lost chroma). These figures predate -the chromaticity classes, which moved four of the ten shares by one step: three of the six at 0 % to 5 % (SLT-A33, -ILCE-7SM2, DSC-RX1RM3) and the PEN-F from 10 to 15 %. +(`fit_dng_tone`) takes a database profile in the measured share too. The whole correction is kept when nothing can +be measured: proxies of different sizes, fewer than 256 usable pixels (clipped, black and non-finite ones are left +out, as are chromaticity classes under 32 pixels), a lens-warped sensor proxy (the gain would no longer be at each +pixel's source position); shares that rank alike keep the larger one. Within a class the camera's rendering is taken as alike, so two +saturations of one hue that it renders differently, one gathered toward the corners, can still read as vignetting. +With the whole profile, an ILCE-7M3 frame at f/2 with an FE 28mm F2 (lensfun: corners at 16 % of the centre) fitted +0.062 held-out RMS instead of 0.027 and rendered darker and orange. Its camera JPEG, with vignetting correction Auto +in the maker note, is measured as showing none of that correction relative to the raw, like the A7CR JPEGs in +[sony-lens-corrections.md](sony-lens-corrections.md). Of 91 CC0 corpus raws (all 37 ARWs, 19 ORFs, 15 PEFs, 20 +NEFs), 10 get a lens database vignetting profile into their fit (7 Sony, 2 Olympus, 1 Nikon). The shares measured +are 0 % on two, 5 % on three (SLT-A33, ILCE-7SM2, DSC-RX1RM3) and 10–45 % on five, and the mean CIEDE2000 of the +central half of the render against the camera JPEG goes from 4.69 to 3.96 (6 better, 3 within 0.05, the SLT-A33 0.12 +worse). The whole profile had made 9 of the 10 darker (0.4 to 8.5 L*), with small, scene-dependent colour shifts +(mostly lost chroma). `proxies` applies the gain before it moves the sensor into the JPEG's lens geometry, so each pixel gets the gain of the position it samples, as in the render. The choice between the warped and the plain sensor (`alignment`) leaves out the pixels the warp finds no sensor for. It used to read them as black, a false edge along the border that made the diff --git a/docs/lens-profiles.md b/docs/lens-profiles.md index 060eb96046..facb067bdb 100644 --- a/docs/lens-profiles.md +++ b/docs/lens-profiles.md @@ -86,8 +86,8 @@ A raw whose look is fitted to its own camera JPEG (Sony, Nikon, Pentax, Olympus matrix, and a DNG whose tone is fitted to its preview, [camera-preview-colour.md](camera-preview-colour.md)) has that fit made on the sensor with only the share of a profile's vignetting correction its camera JPEG shows, measured per photo: the profile describes the lens, not what the camera did to its JPEG relative to the raw (a Sony JPEG with shading compensation Auto can show -none of it). The render -still applies the profile at the Vignetting amount. +none of it). When the share can't be measured (too few usable pixels, or a lens-warped sensor proxy) the fit keeps +the whole correction. The render still applies the profile at the Vignetting amount. 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 From b21ecbb18aea66e8921956b3866d0ac271d911b7 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Mon, 9 Nov 2026 09:52:04 +0100 Subject: [PATCH 4/6] LR-PROF-CAMERACOLOR: check the CR3 framing vignetting estimate at partial correction too Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/camera_preview/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/engine/src/camera_preview/tests.rs b/crates/engine/src/camera_preview/tests.rs index 6395b4fc1b..90e95916d8 100644 --- a/crates/engine/src/camera_preview/tests.rs +++ b/crates/engine/src/camera_preview/tests.rs @@ -2956,7 +2956,7 @@ fn camera_vignetting_amount_follows_a_cr3_proxys_framing() { let measure = |reference: &Rgb32f, framing: Option| { camera_vignetting_amount(&sensor, reference, &clipped, &vignette, ProxyFrame { orientation: Orientation::Normal, framing }) }; - for shown in [0.0f32, 1.0] { + for shown in [0.0f32, 0.75, 1.0] { let mut reference = sensor.clone(); for (p, g) in reference.data.iter_mut().zip(&source_gains) { *p = p.map(|c| (c * (1.0 + (g - 1.0) * shown)).powf(1.0 / 2.2)); From 09e3840f1f4abcddcc54ec3890a777badad5c802 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Wed, 11 Nov 2026 11:49:13 +0100 Subject: [PATCH 5/6] LR-PROF-CAMERACOLOR: next free shared numbers, CR3 framing note and measurements - LOOK_VERSION 26 and RENDER_CACHE_VERSION 83: open PRs already take 14/77 (#758, #3902), up to 25/82 (#1206). - look_proxies: say why a CR3's framing is now estimated on the uncorrected sensor, and that it came out the same as on the corrected one on all 22 corpus CR3s with a database profile. - camera-preview-colour.md: the 22 corpus CR3s this changes, measured against their camera JPEGs. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/camera_preview.rs | 4 ++-- crates/engine/src/camera_preview/look_fit.rs | 3 +++ crates/engine/src/media/source.rs | 2 +- docs/camera-preview-colour.md | 7 +++++-- docs/raw-parity.md | 4 ++-- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/camera_preview.rs b/crates/engine/src/camera_preview.rs index 7226747684..3b85f9b016 100644 --- a/crates/engine/src/camera_preview.rs +++ b/crates/engine/src/camera_preview.rs @@ -84,10 +84,10 @@ use tone::{HighlightCheck, ToneFit, fit_tone, fit_tone_conditional, highlights}; /// 13: the sensor proxy of every per-file fit gets the embedded vignetting correction the render applies (Fujifilm /// RAF; other makes with a lens database), so the render no longer applies it twice, at the position a lens warp samples; /// the warped candidate's alignment leaves out the pixels outside the sensor instead of reading them as black. -/// 14: a lens database's vignetting correction reaches the sensor proxy only in the share the camera JPEG shows +/// 26: a lens database's vignetting correction reaches the sensor proxy only in the share the camera JPEG shows /// (`camera_vignetting_amount`, ranked within chromaticity classes), not whole: JPEGs that show less of it fitted looks too /// dark, with skewed colour. The same for a DNG's tone fit; a CR3 proxy gets the gain where its framed pixels come from. -pub const LOOK_VERSION: u32 = 14; +pub const LOOK_VERSION: u32 = 26; #[derive(Clone, Debug)] pub(crate) struct CameraLook { diff --git a/crates/engine/src/camera_preview/look_fit.rs b/crates/engine/src/camera_preview/look_fit.rs index 6d2d21c041..f2477c87ff 100644 --- a/crates/engine/src/camera_preview/look_fit.rs +++ b/crates/engine/src/camera_preview/look_fit.rs @@ -298,6 +298,9 @@ pub(super) fn look_proxies( let Some(vignette) = database.and_then(|e| e.vignette) else { return proxies(raw, bytes, transform, PROXY, 0.0, lens, embedded); }; + // A CR3's framing (`estimate_cr3_framing`) is estimated here on the uncorrected sensor, not the corrected one as + // before: it compares edge directions, which a smooth vignetting gain hardly moves. On the 22 corpus CR3s with a + // database profile it came out the same both ways (21 unmoved, the R50 V at scale 0.986). let (mut sensor, reference, clipped, framing) = framed_proxies(raw, bytes, transform, PROXY, 0.0, lens, None)?; // CR3 proxies come back oriented like the JPEG (their framing aligned to it), the others in sensor orientation let orientation = if raw.format == lightcraft_raw::RawFormat::Cr3 { lightcraft_geom::Orientation::Normal } else { raw.orientation }; diff --git a/crates/engine/src/media/source.rs b/crates/engine/src/media/source.rs index 248eb3f54d..d58cfd2861 100644 --- a/crates/engine/src/media/source.rs +++ b/crates/engine/src/media/source.rs @@ -1,7 +1,7 @@ use super::{Arc, DcpProfile, Deserialize, DevelopSettings, MediaKind, Photo, Rgb32f, Rgba8, Serialize, SourceInfo}; /// Bump when the pipeline's output changes, to invalidate cached thumbnails. -pub const RENDER_CACHE_VERSION: u64 = 77; +pub const RENDER_CACHE_VERSION: u64 = 83; /// 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/docs/camera-preview-colour.md b/docs/camera-preview-colour.md index a10b8dac03..1d0897196b 100644 --- a/docs/camera-preview-colour.md +++ b/docs/camera-preview-colour.md @@ -137,7 +137,7 @@ it (Enable Profile Corrections). The camera applied the same correction to its J `look_proxies`, which multiplies it by that gain as `fit_dng_tone` already did (`correct_vignetting`). Before `LOOK_VERSION` 13 the RAF fit saw the uncorrected sensor: the tone curve learned the JPEG's corrected brightness, and the render added the correction again. A lens-database profile (`LIGHTCRAFT_LENSFUN_DIR`, the downloaded lensfun data) -describes the lens, not what the camera did to its JPEG, so since `LOOK_VERSION` 14 it reaches the fit only in the +describes the lens, not what the camera did to its JPEG, so since `LOOK_VERSION` 26 it reaches the fit only in the share the JPEG shows (`camera_vignetting_amount`): the camera's tone curve is monotone, so the share whose corrected sensor ranks its pixels' luminance most like the JPEG wins (0 to 100 % in 5 % steps). The ranks are compared within classes of similar sensor chromaticity (near-neutral and eight hue sectors), weighted by their pixel counts: a camera @@ -156,7 +156,10 @@ NEFs), 10 get a lens database vignetting profile into their fit (7 Sony, 2 Olymp are 0 % on two, 5 % on three (SLT-A33, ILCE-7SM2, DSC-RX1RM3) and 10–45 % on five, and the mean CIEDE2000 of the central half of the render against the camera JPEG goes from 4.69 to 3.96 (6 better, 3 within 0.05, the SLT-A33 0.12 worse). The whole profile had made 9 of the 10 darker (0.4 to 8.5 L*), with small, scene-dependent colour shifts -(mostly lost chroma). +(mostly lost chroma). Of the 35 corpus CR3s, 22 get a lens database vignetting profile into their fit; their camera +JPEGs show 0 to 100 % of it (median 45 %), the held-out RMS is lower on 21, and the mean CIEDE2000 goes from 2.86 to +2.62 (13 better, 5 within 0.05, 4 worse by up to 0.22). Their framing is estimated on the uncorrected sensor and came +out the same as on the corrected one for all 22. `proxies` applies the gain before it moves the sensor into the JPEG's lens geometry, so each pixel gets the gain of the position it samples, as in the render. The choice between the warped and the plain sensor (`alignment`) leaves out the pixels the warp finds no sensor for. It used to read them as black, a false edge along the border that made the diff --git a/docs/raw-parity.md b/docs/raw-parity.md index f3d4d7c3e4..e268339c46 100644 --- a/docs/raw-parity.md +++ b/docs/raw-parity.md @@ -223,7 +223,7 @@ Neither names ORF or specifies its file layout. These references do not establis ΔL\* −11, chroma ×0.39. RW2 140 of 174 files accepted, median 5.0. NEF 13 of 13, 2.9–7.8. RAF 23 of 24, pooled X-H2S 1.62 / X-T4 2.34 (measured before `LOOK_VERSION` 13 stopped applying the embedded vignetting correction twice); X-S10, 60 photos not used to find that bug, mean ΔE00 2.71 → 2.55 with it fixed. Since - `LOOK_VERSION` 14 a lens database's vignetting reaches the fit only in the share the camera JPEG shows (an + `LOOK_VERSION` 26 a lens database's vignetting reaches the fit only in the share the camera JPEG shows (an ILCE-7M3 with an FE 28mm F2 at f/2, whose JPEG shows none of it, had rendered darker and orange with the whole profile). SRW 2.2–7.6. - Against Lightroom itself (few): Sony DRO median ΔE76 7.5 → 3.3; Olympus E-1 vs Lightroom Classic @@ -297,7 +297,7 @@ Beyond decoding: | Date | Change | Summary | |---|---|---| -| 2026-11-07 | patch | Camera look: a lens database's vignetting reaches the per-file fit only in the share the camera JPEG shows (`LOOK_VERSION` 14), so raws whose JPEG shows little or none of it (mostly Sony) no longer render too dark, with skewed colour, with lensfun profiles. | +| 2026-11-07 | patch | Camera look: a lens database's vignetting reaches the per-file fit only in the share the camera JPEG shows (`LOOK_VERSION` 26), so raws whose JPEG shows little or none of it (mostly Sony) no longer render too dark, with skewed colour, with lensfun profiles. | | 2026-11-07 | correction | Compressed ORF sensor support withdrawn pending provenance review; embedded preview fallback remains. Coverage regenerated without the removed decoder: 557 verified, 665 unverified, 187 preview-only, 37 unsupported; 94% use-weighted coverage. | | 2026-11-04 | minor | Compressed ORF support was introduced in #3775; subsequently withdrawn pending provenance review. Earlier clean-room and verification claims are withdrawn; compressed files use embedded previews when available. See [ORF status](orf-compression.md). | | 2026-10-26 | patch | Bundled camera profile count 5 → 9 (Nikon Z 6II, Z 8 and D5300, ILCE-7RM5); counts only | From 33cdc22af595877f85eb492e82439a4c19c7f843 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Wed, 11 Nov 2026 14:48:05 +0100 Subject: [PATCH 6/6] LR-PROF-CAMERACOLOR: leave pixels clipped in the camera JPEG out of the vignetting share The sensor clip mask misses highlights the JPEG clips and the raw doesn't; their JPEG brightness no longer follows the scene, so they skewed the measured share. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/camera_preview/look_fit.rs | 10 ++++++++-- crates/engine/src/camera_preview/tests.rs | 9 +++++++-- docs/camera-preview-colour.md | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/camera_preview/look_fit.rs b/crates/engine/src/camera_preview/look_fit.rs index f2477c87ff..a11af04335 100644 --- a/crates/engine/src/camera_preview/look_fit.rs +++ b/crates/engine/src/camera_preview/look_fit.rs @@ -357,6 +357,11 @@ const NEUTRAL_CHROMA: f64 = 0.03; /// Fewest pixels a chromaticity class needs to take part in [`camera_vignetting_amount`]. const MIN_CLASS_PIXELS: usize = 32; +/// Camera JPEG level at or above which a channel counts as clipped in [`camera_vignetting_amount`], as for the colour +/// pairs (`pairs::colour_pair`): the JPEG's brightness no longer follows the scene there, whether or not the sensor +/// clipped too ([`SENSOR_CLIP`]). +const JPEG_CLIP: f32 = 0.98; + /// The chromaticity class of a sensor pixel in [`camera_vignetting_amount`]: 0 near-neutral, else 1 + its hue sector. fn chromaticity_class(p: [f64; 3]) -> usize { let p = p.map(|v| v.max(0.0)); @@ -381,7 +386,8 @@ fn chromaticity_class(p: [f64; 3]) -> usize { /// ranks across hues would read that as vignetting: the correlation is taken within classes of similar sensor /// chromaticity ([`chromaticity_class`]), averaged by their pixel counts. Within a class the camera's rendering is /// taken as alike: two saturations of one hue that it renders differently, one gathered toward the corners, can still -/// read as vignetting. Clipped, black and non-finite pixels are left out. Proxies of different sizes, or fewer than +/// read as vignetting. Pixels clipped on the sensor (`clipped`) or in the JPEG ([`JPEG_CLIP`]), black and non-finite +/// pixels are left out. Proxies of different sizes, or fewer than /// [`MIN_VIGNETTING_PIXELS`] usable pixels, keep the whole correction. pub(super) fn camera_vignetting_amount( sensor: &Rgb32f, @@ -397,7 +403,7 @@ pub(super) fn camera_vignetting_amount( let usable = |p: &[f32; 3]| p.iter().all(|v| v.is_finite()) && luminance_2020(*p) > 0.0; let mut classes: Vec> = vec![Vec::new(); HUE_SECTORS + 1]; for (i, ((s, r), g)) in sensor.data.iter().zip(&reference.data).zip(gains).enumerate() { - if clipped.get(i).copied().unwrap_or(false) || !usable(s) || !usable(r) { + if clipped.get(i).copied().unwrap_or(false) || !usable(s) || !usable(r) || r.iter().any(|v| *v >= JPEG_CLIP) { continue; } let s = s.map(f64::from); diff --git a/crates/engine/src/camera_preview/tests.rs b/crates/engine/src/camera_preview/tests.rs index 90e95916d8..54331bdde9 100644 --- a/crates/engine/src/camera_preview/tests.rs +++ b/crates/engine/src/camera_preview/tests.rs @@ -2834,7 +2834,8 @@ fn the_vignetting_gain_of_a_framed_proxy_is_taken_where_its_pixels_come_from() { /// What `camera_vignetting_amount` can't measure on keeps the whole correction: proxies of different sizes, and a flat /// scene, where every share ranks alike (a tie keeps the larger share, the correction a camera JPEG usually has). /// Clipped and black pixels are left out: here they sit where the correction would brighten or darken them most and -/// would pull the share of a JPEG without any correction up. Non-finite pixels don't spoil the measurement. +/// would pull the share of a JPEG without any correction up. A JPEG clips highlights the sensor doesn't, so pixels +/// clipped in the JPEG are left out without the sensor's clip mask too. Non-finite pixels don't spoil the measurement. #[test] fn camera_vignetting_amount_falls_back_and_leaves_out_what_it_cannot_measure() { use lightcraft_develop::EmbeddedVignette; @@ -2878,7 +2879,11 @@ fn camera_vignetting_amount_falls_back_and_leaves_out_what_it_cannot_measure() { let amount = measure(&sensor, &clipped_reference, &corners); assert!(amount < 0.051, "clipped pixels are left out: measured {amount}"); let amount = measure(&sensor, &clipped_reference, &unclipped); - assert!(amount > 0.1, "the clipped pixels would have pulled the share up: {amount}"); + assert!(amount < 0.051, "pixels clipped in the JPEG alone are left out: measured {amount}"); + let mut bright_reference = reference.clone(); + bright_reference.data.iter_mut().zip(&corners).filter(|(_, c)| **c).for_each(|(p, _)| *p = [0.97; 3]); + let amount = measure(&sensor, &bright_reference, &unclipped); + assert!(amount > 0.1, "the same pixels, just below the clip, would have pulled the share up: {amount}"); // the quarter it brightens least, black in the JPEG let mut dark_reference = reference.clone(); diff --git a/docs/camera-preview-colour.md b/docs/camera-preview-colour.md index 1d0897196b..b0836b02d6 100644 --- a/docs/camera-preview-colour.md +++ b/docs/camera-preview-colour.md @@ -144,8 +144,8 @@ classes of similar sensor chromaticity (near-neutral and eight hue sectors), wei that renders some hues brighter than others would otherwise read as vignetting where those hues gather toward the corners. A CR3 proxy moved to its JPEG's framing gets the gain of the position each pixel shows, and a DNG's tone fit (`fit_dng_tone`) takes a database profile in the measured share too. The whole correction is kept when nothing can -be measured: proxies of different sizes, fewer than 256 usable pixels (clipped, black and non-finite ones are left -out, as are chromaticity classes under 32 pixels), a lens-warped sensor proxy (the gain would no longer be at each +be measured: proxies of different sizes, fewer than 256 usable pixels (pixels clipped on the sensor or in the camera +JPEG, black and non-finite ones are left out, as are chromaticity classes under 32 pixels), a lens-warped sensor proxy (the gain would no longer be at each pixel's source position); shares that rank alike keep the larger one. Within a class the camera's rendering is taken as alike, so two saturations of one hue that it renders differently, one gathered toward the corners, can still read as vignetting. With the whole profile, an ILCE-7M3 frame at f/2 with an FE 28mm F2 (lensfun: corners at 16 % of the centre) fitted