LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Merge PR #590
Brandon Thomas2bd974d583b0
diff --cc crates/engine/src/cmd/previews.rsindex 54f8d0529,8ebbfc59f..50ccb4bd6--- a/crates/engine/src/cmd/previews.rs+++ b/crates/engine/src/cmd/previews.rs@@@ -162,10 -170,11 +187,11 @@@ fn smart_runstd::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;}}- for (id, path, source) in jobs {+ for (id, path, source, look) in jobs {- if state.is_some_and(|st| st.cancel.load(Ordering::Relaxed)) {+ if state.is_some_and(|(st, _)| st.cancel.load(Ordering::Relaxed)) {break;}+ let _one = SMART_WRITE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);if discard {if std::fs::remove_file(&path).is_ok() {n.removed += 1;@@@ -188,16 -202,24 +219,24 @@@match r {Ok(()) => {n.built += 1;- if damaged {+ if stale {+ n.refreshed += 1;+ } else if damaged {n.repaired += 1;- if let Some(st) = state {+ if let Some((st, _)) = state {st.repaired.fetch_add(1, Ordering::Relaxed);}}}+ Err(e) if stale => {+ // offline (or unreadable) original: the proxy is all there is, keep its look+ log::info!("smart preview {} keeps its older look: {e}", path.display());+ n.built += 1;+ n.stale_kept += 1;+ }Err(e) => {n.failed.push(json!([id.0, e]));- if let Some(st) = state {+ if let Some((st, _)) = state {st.failed.fetch_add(1, Ordering::Relaxed);}}diff --cc crates/engine/src/lib.rsindex c507f0a0a,c61f60167..66acfe4a7--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@@ -14,9 -14,8 +14,9 @@@#[cfg(not(feature = "denoise"))]extern crate lightcraft_denoise_core as lightcraft_denoise;+pub mod activity;pub mod availability;- mod camera_preview;+ pub(crate) mod camera_preview;pub mod camera_profiles;pub mod cmd;pub mod config;diff --cc docs/camera-preview-colour.mdindex afa48dfa6,83ce9d8d6..e933c64f7--- a/docs/camera-preview-colour.md+++ b/docs/camera-preview-colour.md@@@ -99,35 -99,20 +99,49 @@@ Measured before → after (mean L* andPicture styles other than Standard are lowered to the Standard curve too where it is darker; Lightroom ignores them as well. Nikon Active D-Lighting, Canon Auto Lighting Optimizer and Fujifilm D-Range settings are likely to have the same effect and are not handled yet. Render cache version 23 redraws thumbnails; smart previews built before keep the brightened tone curve in their header until rebuilt.+## Spectral camera matrices and the order of precedence++For 52 camera models LightCraft has colour matrices fitted to the camera's measured spectral sensitivities+(`crates/raw/src/spectral.rs`, generated into `spectral_table.rs` by `tools/spectral_camera_table.py` from+[rawtoaces-data](https://github.com/AcademySoftwareFoundation/rawtoaces-data) v1.1.0, Apache-2.0): 22 Canon, 11 Nikon,+11 Sony, 3 Fujifilm and 5 others, plus the other names rawtoaces-data lists for them (EOS Rebel T3i and Kiss X5 for+the EOS 600D; bodies it lists as sharing a sensor, such as the GFX100S with the GFX 100). Per model the generator fits,+under CIE Standard Illuminant A and under D65, a white-preserving 3×3 from white-balanced camera RGB to XYZ by the+least mean CIE76 error over rawtoaces-data's 190 training reflectances, and writes DNG `ColorMatrix1/2` and+`ForwardMatrix1/2`. The existing DNG colour model then interpolates them for the file's as-shot white, which the+vendor white-balance multipliers give through the same matrices. No number comes from Adobe or from a raw decoder.++A raw starts from the first of these that is available (`camera_preview::starting_colour`):++1. the file's own colour matrices (DNG);+2. a camera profile for its model (bundled or local), with tone and chroma fitted to the file's JPEG;+3. the file-local fit to its own JPEG described above;+4. the model's spectral matrices, with LightCraft's default tone curve and no JPEG fit;+5. the neutral fallback.++The spectral matrices only replace the neutral fallback: a photo whose JPEG fit is accepted (with or without a+profile) renders exactly as before, and a covered model takes its spectral matrices only when that fit fails the+gates or the file has no usable JPEG. White balance stays relative to the as-shot look for every raw without matrices+of its own, as before, so the catalog's `Photo::relative_wb` and the controls are unchanged. `lightcraft-cli+calibrate` still pools the uncorrected camera colour of covered models, so a profile can be fitted for them.++No colour-difference measurement of the spectral matrices against the camera JPEG, or against the file-local fit,+is recorded here yet. Whether they should come before the file-local fit for these models is open until one is.++ ## Smart previews and the look version++ A smart preview keeps the result of the fit twice: the colour matrix is baked into its pixels and the tone and chroma curves are stored in its header. When the fit changes, a preview built earlier keeps the old look until it is rebuilt, and the render cache version does not reach it (renders are keyed on the library's files, proxies are files of their own).++ The fit therefore has a version, `LOOK_VERSION` in `crates/engine/src/camera_preview.rs`, and every smart preview records the version that wrote it in its header line (`look_version`). **Bump the constant in any change that makes the fit give a different result for the same file**: the matrix, hue/saturation, tone or chroma fit, their gates and fallbacks, or how a camera profile feeds them. Changes that leave the fitted look alone don't need it. A header without the field (everything built before it existed) counts as version 0, and so does a value that is not a non-negative integer; a value from a newer build is left alone.++ A proxy older than the constant is brought up to date by the existing Build Smart Previews path (`smart_run`), which also repairs damaged proxies:++ - only proxies of raws that take the per-file look (`file_local_look`: ARW, NEF, RW2, RAF, CR3, CR2, PEF, SRW, ORF…) go stale; the fit never reaches JPEG, PNG, TIFF or DNG proxies, which are never rebuilt for it;+ - when the library is opened and its smart previews folder has not been checked at this `LOOK_VERSION` (the `look-version` marker file in the folder), a background thread looks at those proxies, rebuilds the stale ones from their originals and then writes the marker, so the scan runs once per version bump, not at every opening (no proxy is created, nothing is shown, the UI never waits on a drive);+ - Build Smart Previews does the same for the photos it is run on (`refreshed` in its result), whatever the marker says;+ - when the original can't be read (offline drive), the proxy and its curve are kept exactly as they are (`staleKept`). The old version in its header is the mark: the next Build Smart Previews with the original online rebuilds it. A rebuilt proxy carries the current version, so nothing is rebuilt twice. A proxy is only ever rebuilt from the original, never from another proxy;+ - the two never write the same proxy at once: each proxy is checked and written under one lock.+## Nikon crop and preview colour metadataNikon maker-note `CropArea` (0x0045) supplies the default `[left, top, width, height]` crop. The decoder validates the rectangle against the active sensor area and falls back to that area for missing or invalid values. The CFA origin is unchanged; cropping follows demosaicing.