mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-04 21:13:07 -05:00
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 to001a58e9"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), as001a58e9landed. - **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.