LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Merge origin/main into fix/arw-saturated-hues
Harald Hoyer623122c1a935
diff --cc apps/lightcraft-cli/src/main.rsindex 719e5f10b,c1312081d..b9e2641c9--- a/apps/lightcraft-cli/src/main.rs+++ b/apps/lightcraft-cli/src/main.rs@@@ -85,12 -84,6 +85,12 @@@ USAGEWrite synthetic merge inputs (procedural scene; bracketed DNGs or overlapping PNG views).lightcraft-cli commands [--json] list every command id with its parameterslightcraft-cli controls [--json] list every develop control id with its range+ lightcraft-cli calibrate [--max N] [--out DIR] FOLDERS/FILES…+ Fit a colour profile per camera model from raw files and their embedded camera JPEGs- (Sony ARW): up to N files spread over the folders (default 300; 0 = all), pooled per++ (Sony ARW, Nikon NEF): up to N files spread over the folders (default 300; 0 = all), pooled per+ model, written as <model>.json to DIR (default: the profiles folder LightCraft reads,+ <config>/camera-profiles, or $LIGHTCRAFT_CAMERA_PROFILES). Raws of a profiled model then+ take their colour from the profile and only their tone from their own JPEG.lightcraft-cli --version | --help";@@@ -152,114 -144,6 +152,114 @@@ fn main() -> ExitCode}}+/// Raw files below `path` (or `path` itself), skipping hidden and NAS metadata folders.+fn raw_files(path: &Path, out: &mut Vec<std::path::PathBuf>, depth: usize) {- let is_raw = |p: &Path| p.extension().and_then(|e| e.to_str()).is_some_and(|e| e.eq_ignore_ascii_case("arw"));++ let is_raw = |p: &Path| p.extension().and_then(|e| e.to_str()).is_some_and(|e| ["arw", "nef", "nrw"].iter().any(|x| e.eq_ignore_ascii_case(x)));+ if path.is_file() {+ if is_raw(path) {+ out.push(path.to_path_buf());+ }+ return;+ }+ let Ok(entries) = std::fs::read_dir(path) else { return };+ if depth > 32 {+ return;+ }+ for entry in entries.flatten() {+ let name = entry.file_name();+ if name.to_string_lossy().starts_with(['.', '@']) {+ continue;+ }+ raw_files(&entry.path(), out, depth + 1);+ }+}++fn calibrate(args: &[String]) -> Result<(), String> {+ let mut max = 300usize;+ let mut out: Option<std::path::PathBuf> = None;+ let mut inputs = Vec::new();+ let mut i = 0;+ while i < args.len() {+ match args[i].as_str() {+ "--max" => max = take_value(args, &mut i, "--max")?.parse().map_err(|_| "--max needs a number".to_string())?,+ "--out" => out = Some(take_value(args, &mut i, "--out")?.into()),+ a if a.starts_with("--") => return Err(format!("unknown option `{a}`")),+ f => inputs.push(f.to_string()),+ }+ i += 1;+ }+ if inputs.is_empty() {+ return Err("calibrate needs folders or raw files".into());+ }+ let mut files = Vec::new();+ for input in &inputs {+ raw_files(Path::new(input), &mut files, 0);+ }+ files.sort();+ files.dedup();+ let found = files.len();+ if max > 0 && files.len() > max {+ // spread over all folders (dates, scenes) rather than the first N+ files = (0..max).filter_map(|k| files.get(k * found / max).cloned()).collect();+ }+ eprintln!("calibrate: {} of {found} raw files", files.len());+ let dir = out.or_else(lightcraft_engine::camera_profiles::dir).ok_or("no profiles folder: pass --out DIR")?;+ let workers = std::thread::available_parallelism().map_or(4, |n| n.get()).clamp(1, 6);+ let next = std::sync::atomic::AtomicUsize::new(0);+ let done = std::sync::atomic::AtomicUsize::new(0);+ let pools: Vec<lightcraft_engine::camera_profiles::Pool> = std::thread::scope(|scope| {+ let handles: Vec<_> = (0..workers)+ .map(|_| {+ scope.spawn(|| {+ let mut pool = lightcraft_engine::camera_profiles::Pool::default();+ while let Some(path) = files.get(next.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) {+ let result = std::fs::read(path).map_err(|e| e.to_string()).and_then(|bytes| pool.add(&bytes));+ let n = done.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;+ match result {+ Ok(Some(model)) => eprintln!("[{n}/{}] {} ({model})", files.len(), path.display()),+ Ok(None) => eprintln!("[{n}/{}] {}: skipped (no usable camera JPEG or colour)", files.len(), path.display()),+ Err(e) => eprintln!("[{n}/{}] {}: {e}", files.len(), path.display()),+ }+ }+ pool+ })+ })+ .collect();+ handles.into_iter().filter_map(|h| h.join().ok()).collect()+ });+ let mut pool = lightcraft_engine::camera_profiles::Pool::default();+ for p in pools {+ pool.merge(p);+ }+ let mut written = 0;+ for result in pool.fit(5) {+ match result {+ Ok(profile) => {+ let path = lightcraft_engine::camera_profiles::save(&profile, &dir)?;+ println!(+ "{}: {} photos, {} colour pairs, hue/saturation table {} → {}",+ profile.model,+ profile.files,+ profile.samples,+ profile.hue_sat.is_some(),+ path.display()+ );+ written += 1;+ }+ Err(e) => eprintln!("calibrate: {e}"),+ }+ }+ for (model, n) in pool.files() {+ if n < 5 {+ eprintln!("calibrate: {model}: only {n} usable photo(s), at least 5 needed");+ }+ }+ if written == 0 {+ return Err("no profile written".into());+ }+ Ok(())+}+fn mcp(args: &[String]) -> Result<(), String> {let mut connect: Option<String> = None;let mut demo = false;diff --cc crates/engine/src/camera_preview.rsindex da4fbccb9,41bbdca84..f01206411--- a/crates/engine/src/camera_preview.rs+++ b/crates/engine/src/camera_preview.rs@@@ -1,58 -1,34 +1,68 @@@- //! Estimate an ARW starting look from its own JPEG. Colour and luminance are fitted separately;- //! the JPEG supplies correspondences only, never output pixels or a replacement for RAW editing.+ //! Estimate the starting look of a raw without a camera colour matrix (Sony ARW, Nikon NEF) from its+ //! own JPEG. Colour and luminance are fitted separately; the JPEG supplies correspondences only,+ //! never output pixels or a replacement for RAW editing.-use lightcraft_color::{Mat3, luminance_2020};+//! A global matrix can't follow the camera's hue-dependent rendering (the best matrix rendered a+//! lime shirt olive that the camera kept lime): a hue/saturation table fitted to the residuals+//! (applied like a DNG `ProfileHueSatMap`) corrects that when it also improves the held-out pixels.+use lightcraft_color::{D50, D65, Mat3, PROPHOTO, REC2020, bradford, luminance_2020};use lightcraft_pipeline::tone::{CameraTone, ToneMap};use lightcraft_raster::{Rgb32f,resample::{Filter, fit},};- use lightcraft_raw::{RawImage, color::CameraTransform, profile::HsvTable};-use lightcraft_raw::{RawFormat, RawImage, color::CameraTransform};++use lightcraft_raw::{RawFormat, RawImage, color::CameraTransform, profile::HsvTable};-#[derive(Clone, Copy, Debug)]+#[derive(Clone, Debug)]pub(crate) struct CameraLook {pub matrix: Mat3,pub tone: CameraTone,+ /// Hue/saturation correction after `matrix`, in linear ProPhoto RGB (DNG `ProfileHueSatMap`).+ pub hue_sat: Option<HsvTable>,}+/// Long edge of the sensor/JPEG proxy a single photo's look is fitted on (the acceptance gates+/// below were set at this size).+const PROXY: usize = 96;+/// Long edge of the proxies pooled for a camera profile: small objects (a shirt) get 4× the samples.+pub(crate) const PROFILE_PROXY: usize = 192;++ /// Raw formats whose decoder supplies vendor white-balance multipliers but no camera colour matrix:+ /// their starting look is fitted to the file's own JPEG, and white balance is relative to the+ /// as-shot look (`docs/camera-preview-colour.md`). The catalog's `Photo::relative_wb` matches the+ /// same formats by file extension.+ pub(crate) fn file_local_look(format: RawFormat) -> bool {+ matches!(format, RawFormat::Arw | RawFormat::Nef | RawFormat::Nrw)+ }+pub(crate) fn fit_preview(raw: &RawImage, bytes: &[u8], transform: &CameraTransform) -> Option<CameraLook> {- if !transform.matrix_is_fallback || raw.format != lightcraft_raw::RawFormat::Arw {+ if !transform.matrix_is_fallback || !file_local_look(raw.format) {return None;}+ let (sensor, reference) = proxies(raw, bytes, transform, PROXY)?;+ // A camera profile pooled from many photos knows colours this photo shows too little of;+ // only the tone and chroma curves are fitted per photo (DRO and picture styles vary).+ let profile = raw.metadata.model.as_deref().and_then(crate::camera_profiles::get);+ let colour = profile.as_ref().and_then(|p| Some((p.matrix().mul(&transform.matrix.inverse()?), p.hue_sat.clone())));+ let look = fit_pairs_with(&sensor, &reference, colour)?;+ if lightcraft_pipeline::profiling() {+ eprintln!(- "[profile] ARW camera look: {:?}, {:?}, hue/sat table {}, camera profile {}",++ "[profile] {:?} camera look: {:?}, {:?}, hue/sat table {}, camera profile {}",++ raw.format,+ look.matrix.0,+ look.tone,+ look.hue_sat.is_some(),+ profile.is_some()+ );+ }+ Some(look)+}++/// Same-size proxies of the sensor (white-balanced, baseline exposure, through `transform`'s+/// matrix: the generic camera ≈ sRGB model) and of the file's embedded camera JPEG (linear Rec.2020).+fn proxies(raw: &RawImage, bytes: &[u8], transform: &CameraTransform, size: usize) -> Option<(Rgb32f, Rgb32f)> {let jpeg = lightcraft_raw::embedded_preview(bytes)?;- let decoded = lightcraft_codecs::decode(&jpeg, lightcraft_codecs::DecodeOptions { max_size: Some((384, 384)), max_pixels: 64_000_000 }).ok()?;+ let edge = (2 * size).max(384) as u32;+ let decoded = lightcraft_codecs::decode(&jpeg, lightcraft_codecs::DecodeOptions { max_size: Some((edge, edge)), max_pixels: 64_000_000 }).ok()?;let reference = decoded.to_working();let crop = raw.crop.clipped(raw.active_area.width, raw.active_area.height);if crop.width == 0 || crop.height == 0 || reference.width == 0 || reference.height == 0 {@@@ -69,27 -45,11 +79,28 @@@let reference = fit(&reference, sensor.width, sensor.height, Filter::Box);let gain = 2f32.powf(transform.baseline_exposure as f32);sensor.map_in_place(|p| transform.matrix.apply_f32(std::array::from_fn(|i| p[i] * transform.wb[i] * gain)));- let look = fit_pairs(&sensor, &reference)?;- if lightcraft_pipeline::profiling() {- eprintln!("[profile] {:?} camera look: {:?}, {:?}", raw.format, look.matrix.0, look.tone);+ Some((sensor, reference))+}+- /// Colour training pairs of one ARW for a camera profile: white-balanced camera RGB (with the- /// baseline exposure) → its camera JPEG (linear Rec.2020). `None` for other files or unusable previews.++/// Colour training pairs of one raw for a camera profile: white-balanced camera RGB (with the++/// baseline exposure) → its camera JPEG (linear Rec.2020). `None` for formats with their own++/// colour matrices, other files or unusable previews.+pub(crate) fn profile_pairs(raw: &RawImage, bytes: &[u8]) -> Option<Vec<([f64; 3], [f64; 3])>> {- if raw.format != lightcraft_raw::RawFormat::Arw || lightcraft_raw::color::has_matrix(&raw.color) {++ if !file_local_look(raw.format) || lightcraft_raw::color::has_matrix(&raw.color) {+ return None;}- Some(look)+ let transform = lightcraft_raw::color::camera_transform(raw, lightcraft_raw::color::as_shot_white_xy(raw));+ let (sensor, reference) = proxies(raw, bytes, &transform, PROFILE_PROXY)?;+ let to_camera = transform.matrix.inverse()?;+ let (pairs, _) = collect_pairs(&sensor, &reference)?;+ Some(pairs.into_iter().map(|(x, y)| (to_camera.apply(x), y)).collect())+}++/// A camera profile's colour model (matrix from white-balanced camera RGB, hue/saturation table)+/// fitted to pairs pooled from many photos.+pub(crate) fn fit_profile(pairs: &[([f64; 3], [f64; 3])]) -> Option<(Mat3, Option<HsvTable>)> {+ let matrix = fit_matrix(pairs)?;+ Some((matrix, fit_hue_sat(pairs, &matrix)))}/// A fit must cut the held-out squared error to below this share of the fallback's.@@@ -183,270 -122,40 +194,274 @@@ fn fit_matrix(pairs: &[([f64; 3], [f64cross[i][i] += regularization;}let matrix = Mat3(cross).mul(&Mat3(gram).inverse()?);- if !matrix.0.iter().flatten().all(|v| v.is_finite() && v.abs() < 8.0) {- return None;- }- let tone_pairs: Vec<_> =- pairs.iter().enumerate().filter(|(i, _)| i % 3 != 0).map(|(_, (x, y))| (luma(matrix.apply(*x).map(|v| v.max(0.0))), luma(*y))).collect();- let curve = fit_tone(tone_pairs)?;- let tone = ToneMap::camera(&curve, 0.0, 0.0, 0.0);- let original_tone = ToneMap::new(0.0, 0.0, 0.0);- let mut before = 0.0;- let mut after = 0.0;- let mut samples = 0;- for (i, (x, target)) in pairs.iter().enumerate() {- if i % 3 != 0 {- continue;+ matrix.0.iter().flatten().all(|v| v.is_finite() && v.abs() < 8.0).then_some(matrix)+}++/// The photo's look: its own matrix (with and without a hue/saturation table), or the given+/// colour model (a camera profile's, in the sensor proxy's space), each completed with a tone+/// and chroma curve fitted to this photo.+fn fit_pairs_with(sensor: &Rgb32f, reference: &Rgb32f, colour: Option<(Mat3, Option<HsvTable>)>) -> Option<CameraLook> {+ let (pairs, bright) = collect_pairs(sensor, reference)?;+ let candidates = match colour {+ Some(given) => vec![given],+ None => {+ let matrix = fit_matrix(&pairs)?;+ vec![(matrix, fit_hue_sat(&pairs, &matrix)), (matrix, None)]+ }+ };+ // Matrix + table when the table helps the held-out pixels, else the matrix alone. Chosen by+ // error in gamma-encoded display values (closer to what is seen: in linear values a slightly+ // missed bright rock outweighs a clearly wrong dark shirt); the acceptance gates below stay linear.+ let mut best: Option<(f64, f64, usize, CameraLook)> = None;+ for (matrix, hue_sat) in candidates {+ let correction = hue_sat.as_ref().and_then(HueSat::new);+ let colour = |x: [f64; 3]| {+ let p = matrix.apply(x);+ correction.as_ref().map_or(p, |c| c.apply(p.map(|v| v as f32)).map(f64::from))+ };+ let tone_pairs: Vec<_> =+ pairs.iter().enumerate().filter(|(i, _)| i % 3 != 0).map(|(_, (x, y))| (luma(colour(*x).map(|v| v.max(0.0))), luma(*y))).collect();+ let Some(curve) = fit_tone(tone_pairs) else { continue };+ let mut look = CameraLook { matrix, tone: curve, hue_sat: hue_sat.clone() };+ if let Some(tone) = fit_chroma(&bright, &look) {+ look.tone = tone;+ }+ let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);+ let (mut linear, mut perceptual, mut samples) = (0.0, 0.0, 0);+ for (x, target) in pairs.iter().step_by(3) {+ let corrected = displayed(colour(*x), &tone);+ for c in 0..3 {+ linear += (corrected[c] - target[c]).powi(2);+ perceptual += (corrected[c].max(0.0).powf(1.0 / 2.2) - target[c].max(0.0).powf(1.0 / 2.2)).powi(2);+ samples += 1;+ }}- let corrected = displayed(matrix.apply(*x), &tone);- let original = displayed(*x, &original_tone);- for c in 0..3 {- before += (original[c] - target[c]).powi(2);- after += (corrected[c] - target[c]).powi(2);- samples += 1;+ if linear.is_finite() && perceptual.is_finite() && best.as_ref().is_none_or(|b| perceptual < b.0) {+ best = Some((perceptual, linear, samples, look));}}+ let (_, after, samples, look) = best?;+ let original_tone = ToneMap::new(0.0, 0.0, 0.0);+ let before: f64 = pairs+ .iter()+ .step_by(3)+ .map(|(x, target)| {+ let original = displayed(*x, &original_tone);+ (0..3).map(|c| (original[c] - target[c]).powi(2)).sum::<f64>()+ })+ .sum();if lightcraft_pipeline::profiling() {- eprintln!("[profile] ARW holdout RMS {:.5} -> {:.5} ({samples} channels)", (before / samples as f64).sqrt(), (after / samples as f64).sqrt());+ eprintln!(+ "[profile] camera look holdout RMS {:.5} -> {:.5} ({samples} channels)",+ (before / samples as f64).sqrt(),+ (after / samples as f64).sqrt()+ );}- if !after.is_finite() || samples == 0 || after >= before * MIN_IMPROVEMENT || after / samples as f64 > MAX_HOLDOUT_RMS.powi(2) {+ if samples == 0 || after >= before * MIN_IMPROVEMENT || after / samples as f64 > MAX_HOLDOUT_RMS.powi(2) {+ return None;+ }+ Some(look)+}++/// Linear Rec.2020 D65 → linear ProPhoto RGB D50, the space DNG hue/saturation tables work in.+fn to_prophoto() -> Mat3 {+ PROPHOTO.from_xyz().mul(&bradford(D65, D50)).mul(&REC2020.to_xyz())+}++/// A fitted [`CameraLook::hue_sat`] table, ready to apply to linear Rec.2020 pixels. It changes+/// hue and saturation only: luminance is restored, the camera tone curve owns it.+pub(crate) struct HueSat<'a> {+ table: &'a HsvTable,+ to: [[f32; 3]; 3],+ from: [[f32; 3]; 3],+}++impl<'a> HueSat<'a> {+ pub fn new(table: &'a HsvTable) -> Option<HueSat<'a>> {+ let to = to_prophoto();+ Some(HueSat { table, to: to.to_f32(), from: to.inverse()?.to_f32() })+ }++ #[inline]+ pub fn apply(&self, rgb: [f32; 3]) -> [f32; 3] {+ let mul = |m: &[[f32; 3]; 3], v: [f32; 3]| -> [f32; 3] { std::array::from_fn(|i| m[i][0] * v[0] + m[i][1] * v[1] + m[i][2] * v[2]) };+ let out = mul(&self.from, self.table.apply(mul(&self.to, rgb)));+ let (before, after) = (luminance_2020(rgb), luminance_2020(out));+ if before > 0.0 && after > 0.0 && out.iter().all(|v| v.is_finite()) { out.map(|v| v * before / after) } else { rgb }+ }+}++/// HSV hue (degrees) and saturation; `None` for black or non-finite colours.+fn hue_saturation(p: [f64; 3]) -> Option<(f64, f64)> {+ let max = p[0].max(p[1]).max(p[2]);+ let min = p[0].min(p[1]).min(p[2]);+ if !(max.is_finite() && min.is_finite()) || max <= 0.0 {return None;}- Some(CameraLook { matrix, tone: curve })+ let d = max - min;+ if d <= 0.0 {+ return Some((0.0, 0.0));+ }+ let h = if max == p[0] {+ ((p[1] - p[2]) / d).rem_euclid(6.0)+ } else if max == p[1] {+ (p[2] - p[0]) / d + 2.0+ } else {+ (p[0] - p[1]) / d + 4.0+ };+ Some((h * 60.0, d / max))+}++/// Table resolution: 5° hue steps (a coarser grid blurred the lime shirt into neighbouring browns+/// that want the opposite shift), saturation 0, 0.25 … 1. Value is not an axis: tone is fitted separately.+const TABLE_HUES: usize = 72;+const TABLE_SATS: usize = 5;+/// Value levels (sRGB-encoded, as DNG tables with encoding 1): cameras turn dark yellows toward+/// orange but bright yellow-greens toward green, which one shift per hue averages away.+const TABLE_VALS: usize = 5;+/// Kernel widths around each table node (hue in degrees, saturation, encoded value).+const KERNEL_HUE: f64 = 2.5;+const KERNEL_SAT: f64 = 0.15;+const KERNEL_VAL: f64 = 0.12;+/// Kernel weight at which a node keeps half of its estimate; sparse nodes shrink to identity.+const SHRINK_WEIGHT: f64 = 5.0;++/// Fit hue shifts and saturation scales of the training pairs left after `matrix`, per (hue,+/// saturation) node, kernel-weighted and shrunk toward identity where the photo has few samples.+/// Saturation 0 stays identity, so neutrals are never tinted.+fn fit_hue_sat(pairs: &[([f64; 3], [f64; 3])], matrix: &Mat3) -> Option<HsvTable> {+ let to = to_prophoto();+ let samples: Vec<(f64, f64, f64, f64, f64)> = pairs+ .iter()+ .enumerate()+ .filter(|(i, _)| i % 3 != 0)+ .filter_map(|(_, (x, y))| {+ let p = to.apply(matrix.apply(*x));+ let (hp, sp) = hue_saturation(p)?;+ let value = f64::from(lightcraft_color::transfer::linear_to_srgb(p[0].max(p[1]).max(p[2]).min(1.0) as f32));+ let (ht, st) = hue_saturation(to.apply(*y))?;+ // hue is meaningless near neutral+ if sp < 0.08 || st < 0.02 {+ return None;+ }+ let shift = ((ht - hp + 180.0).rem_euclid(360.0) - 180.0).clamp(-30.0, 30.0);+ let log_scale = (st / sp).ln().clamp(-0.7, 0.7);+ Some((hp, sp.min(1.0), value, shift, log_scale))+ })+ .collect();+ if samples.len() < 64 {+ return None;+ }+ // Samples by table hue step: a node only looks at hues within its kernel's reach.+ let step = 360.0 / TABLE_HUES as f64;+ let mut by_hue: Vec<Vec<(f64, f64, f64, f64, f64)>> = vec![Vec::new(); TABLE_HUES];+ for sample in samples {+ if let Some(bin) = by_hue.get_mut(((sample.0.rem_euclid(360.0) / step) as usize).min(TABLE_HUES - 1)) {+ bin.push(sample);+ }+ }+ let reach = (4.0 * KERNEL_HUE / step).ceil() as usize + 1;+ use rayon::prelude::*;+ let data: Vec<[f32; 3]> = (0..TABLE_VALS * TABLE_HUES * TABLE_SATS)+ .into_par_iter()+ .map(|index| {+ let (v, h, s) = (index / (TABLE_HUES * TABLE_SATS), index / TABLE_SATS % TABLE_HUES, index % TABLE_SATS);+ if s == 0 {+ return [0.0, 1.0, 1.0];+ }+ let (val, hue, sat) = (v as f64 / (TABLE_VALS - 1) as f64, h as f64 * step, s as f64 / (TABLE_SATS - 1) as f64);+ let (mut weight, mut shift, mut log_scale) = (0.0, 0.0, 0.0);+ for offset in 0..=2 * reach {+ let Some(bin) = by_hue.get((h + TABLE_HUES * 2 + offset - reach) % TABLE_HUES) else { continue };+ for &(hp, sp, vp, dh, ls) in bin {+ let dhue = (hp - hue + 180.0).rem_euclid(360.0) - 180.0;+ if dhue.abs() > 4.0 * KERNEL_HUE {+ continue;+ }+ let d2 = (dhue / KERNEL_HUE).powi(2) + ((sp - sat) / KERNEL_SAT).powi(2) + ((vp - val) / KERNEL_VAL).powi(2);+ let k = (-0.5 * d2).exp() * sp;+ weight += k;+ shift += k * dh;+ log_scale += k * ls;+ }+ }+ if weight <= 0.0 {+ return [0.0, 1.0, 1.0];+ }+ let shrink = 1.0 / (weight + SHRINK_WEIGHT);+ [(shift * shrink) as f32, (log_scale * shrink).exp() as f32, 1.0]+ })+ .collect();+ data.iter().all(|e| e.iter().all(|v| v.is_finite())).then_some(HsvTable {+ hue_divisions: TABLE_HUES,+ sat_divisions: TABLE_SATS,+ val_divisions: TABLE_VALS,+ data,+ srgb_value: true,+ })+}++/// Kernel width of the chroma curve's nodes (display luminance) and the weight at which a node+/// keeps half of its estimate.+const CHROMA_KERNEL: f64 = 0.08;+const CHROMA_SHRINK: f64 = 2.0;++/// The look's tone curve with a chroma-by-display-luminance curve fitted to `pairs` (two thirds+/// train, one third held out), when that lowers the held-out error. Colourfulness is compared as+/// the distance from neutral of luminance-normalised RGB.+fn fit_chroma(pairs: &[([f64; 3], [f64; 3])], look: &CameraLook) -> Option<CameraTone> {+ let correction = look.hue_sat.as_ref().and_then(HueSat::new);+ let scene = |x: &[f64; 3]| {+ let p = look.matrix.apply(*x);+ correction.as_ref().map_or(p, |c| c.apply(p.map(|v| v as f32)).map(f64::from))+ };+ let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);+ let predict = |x: &[f64; 3]| displayed(scene(x), &tone);+ let chroma = |p: [f64; 3]| {+ let y = luma(p);+ (y > 0.0).then(|| p.iter().map(|v| (v / y - 1.0).powi(2)).sum::<f64>().sqrt())+ };+ let samples: Vec<(f64, f64, f64)> = pairs+ .iter()+ .enumerate()+ .filter(|(i, _)| i % 3 != 0)+ .filter_map(|(_, (x, y))| {+ let p = predict(x);+ let (cp, cy) = (chroma(p)?, chroma(*y)?);+ (cp > 0.05).then(|| (luma(p), cp, (cy.max(1e-3) / cp).ln().clamp(0.05f64.ln(), 2.5f64.ln())))+ })+ .collect();+ if samples.len() < 64 {+ return None;+ }+ let mut curve = [1.0f32; lightcraft_pipeline::tone::CHROMA_N];+ for (j, node) in curve.iter_mut().enumerate() {+ let at = j as f64 / (lightcraft_pipeline::tone::CHROMA_N - 1) as f64;+ let (mut weight, mut sum) = (0.0, 0.0);+ for &(o, cp, log_ratio) in &samples {+ let k = (-0.5 * ((o - at) / CHROMA_KERNEL).powi(2)).exp() * cp;+ weight += k;+ sum += k * log_ratio;+ }+ *node = (sum / (weight + CHROMA_SHRINK)).exp() as f32;+ }+ let fitted = look.tone.with_chroma(curve)?;+ let with = ToneMap::camera(&fitted, 0.0, 0.0, 0.0);+ let error = |map: &ToneMap| -> f64 {+ pairs+ .iter()+ .step_by(3)+ .map(|(x, y)| {+ let p = displayed(scene(x), map);+ (0..3).map(|c| (p[c] - y[c]).powi(2)).sum::<f64>()+ })+ .sum()+ };+ let (before, after) = (error(&tone), error(&with));+ if lightcraft_pipeline::profiling() {+ eprintln!("[profile] ARW chroma curve {curve:?}: held-out error {before:.4} -> {after:.4}");+ }+ (after.is_finite() && after < before).then_some(fitted)}fn median(values: &mut [f64]) -> Option<f64> {@@@ -552,169 -261,29 +567,192 @@@ mod testsassert!(fit_pairs(&sensor, &reference).is_some());}+ /// A camera that rotates saturated yellow-greens toward green (like Sony's "Standard" on a lime+ /// shirt) can't be followed by a matrix alone: the fitted hue/saturation table must close most+ /// of the gap, keep neutrals neutral and keep luminance.+ #[test]+ fn hue_table_follows_a_hue_dependent_camera_rendering() {+ let to = to_prophoto();+ let from = to.inverse().unwrap();+ let mut sensor = Rgb32f::new(96, 64);+ let mut reference = sensor.clone();+ let mut lime = Vec::new();+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ let ev = 0.05 + (i % 29) as f32 * 0.02;+ // mostly greys and mild colours, plus a patch of saturated yellow-green+ *src = if i % 7 == 0 {+ [ev * 0.75, ev, ev * 0.2]+ } else {+ [ev * (0.85 + (i % 11) as f32 * 0.03), ev, ev * (0.85 + (i % 13) as f32 * 0.025)]+ };+ let (h, s) = hue_saturation(to.apply(src.map(f64::from))).unwrap();+ // the camera turns hues in 50°..110° (ProPhoto) by up to +15° and boosts their saturation+ let k = (1.0 - ((h - 80.0) / 30.0).powi(2)).max(0.0) * s.min(1.0);+ let mut p = to.apply(src.map(f64::from)).map(|v| v as f32);+ let table = HsvTable {+ hue_divisions: 1,+ sat_divisions: 2,+ val_divisions: 1,+ data: vec![[0.0, 1.0, 1.0], [15.0 * k as f32, 1.0 + 0.3 * k as f32, 1.0]],+ srgb_value: false,+ };+ p = table.apply(p);+ let p = from.apply(p.map(f64::from)).map(|v| v as f32);+ let scale = luminance_2020(*src) / luminance_2020(p);+ let p = p.map(|v| v * scale);+ let y = luminance_2020(p);+ *dst = p.map(|v| v * (1.0 - (-2.5 * y).exp()) / y);+ if i % 7 == 0 {+ lime.push(i);+ }+ }+ let fit = fit_pairs(&sensor, &reference).unwrap();+ let table = fit.hue_sat.as_ref().expect("a hue/saturation table is fitted");+ let hue_sat = HueSat::new(table).unwrap();+ let hue_error = |with_table: bool| {+ lime.iter()+ .map(|&i| {+ let p = fit.matrix.apply(sensor.data[i].map(f64::from)).map(|v| v as f32);+ let p = if with_table { hue_sat.apply(p) } else { p };+ let (h, _) = hue_saturation(to.apply(p.map(f64::from))).unwrap();+ let (t, _) = hue_saturation(to.apply(reference.data[i].map(f64::from))).unwrap();+ ((h - t + 180.0).rem_euclid(360.0) - 180.0).abs()+ })+ .sum::<f64>()+ / lime.len() as f64+ };+ let (before, after) = (hue_error(false), hue_error(true));+ assert!(after < before * 0.5, "lime hue error {before:.2}° -> {after:.2}°");+ // neutrals pass through and luminance is kept+ let grey = [0.3, 0.3, 0.3];+ assert!(hue_sat.apply(grey).iter().all(|v| (v - 0.3).abs() < 1e-4), "{:?}", hue_sat.apply(grey));+ let green = [0.2, 0.4, 0.05];+ assert!((luminance_2020(hue_sat.apply(green)) - luminance_2020(green)).abs() < 1e-5);+ }++ /// A camera that saturates shadows and bleaches highlights toward white (a per-channel curve),+ /// which a luminance tone curve can't: the fitted chroma curve must follow it, so bright+ /// warm-tinted rock renders white instead of cream.+ #[test]+ fn chroma_curve_follows_highlight_bleaching() {+ let known = Mat3([[1.8, -0.4, -0.1], [-0.2, 1.5, -0.1], [-0.05, -0.3, 1.7]]);+ let camera_chroma = |o: f32| 1.3 - 1.1 * o;+ let mut sensor = Rgb32f::new(96, 64);+ let mut reference = sensor.clone();+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ let ev = 0.02 + (i % 37) as f32 * 0.03;+ *src = [ev * (0.75 + (i % 11) as f32 * 0.05), ev, ev * (0.7 + (i % 13) as f32 * 0.05)];+ let p = known.apply_f32(*src);+ let y = luminance_2020(p);+ let o = 1.0 - (-2.5 * y).exp();+ let k = camera_chroma(o);+ *dst = p.map(|v| (o + (v * o / y - o) * k).clamp(0.0, 0.999));+ }+ let fit = fit_pairs(&sensor, &reference).unwrap();+ // relative to the matrix, which already carries the average colourfulness+ let chroma = fit.tone.chroma();+ assert!(chroma[6] < 0.5 * chroma[1], "highlights bleach relative to shadows: {chroma:?}");+ // and the rendered highlights land on the camera's, far closer than without the curve+ let with = ToneMap::camera(&fit.tone, 0.0, 0.0, 0.0);+ let without = ToneMap::camera(&fit.tone.with_chroma([1.0; lightcraft_pipeline::tone::CHROMA_N]).unwrap(), 0.0, 0.0, 0.0);+ let highlight_error = |map: &ToneMap| -> f64 {+ sensor+ .data+ .iter()+ .zip(&reference.data)+ .filter(|(_, y)| luminance_2020(**y) > 0.7)+ .map(|(x, y)| {+ let p = displayed(fit.matrix.apply(x.map(f64::from)), map);+ (0..3).map(|c| (p[c] - f64::from(y[c])).powi(2)).sum::<f64>()+ })+ .sum()+ };+ let (a, b) = (highlight_error(&without), highlight_error(&with));+ assert!(b < 0.5 * a, "highlight error {a:.4} -> {b:.4}");+ }++ /// Pooled pairs of several "photos" recover the camera's matrix, and a photo given that+ /// colour model (a camera profile) keeps it, fitting only its own tone.+ #[test]+ fn profile_colour_is_pooled_and_then_used_as_given() {+ let known = Mat3([[1.8, -0.4, -0.1], [-0.2, 1.5, -0.1], [-0.05, -0.3, 1.7]]);+ let photo = |seed: usize, strength: f32| {+ let mut sensor = Rgb32f::new(64, 48);+ let mut reference = sensor.clone();+ for (i, (src, dst)) in sensor.data.iter_mut().zip(&mut reference.data).enumerate() {+ let i = i + seed * 7;+ let ev = 0.04 + (i % 23) as f32 * 0.02;+ *src = [ev * (0.7 + (i % 11) as f32 * 0.05), ev, ev * (0.7 + (i % 13) as f32 * 0.05)];+ let p = known.apply_f32(*src);+ let y = luminance_2020(p);+ // each photo has its own tone (DRO, picture style)+ *dst = p.map(|v| v * (1.0 - (-strength * y).exp()) / y);+ }+ (sensor, reference)+ };+ let mut pool = Vec::new();+ for seed in 0..4 {+ let (sensor, reference) = photo(seed, 2.0 + seed as f32 * 0.5);+ pool.extend(collect_pairs(&sensor, &reference).unwrap().0);+ }+ let (matrix, _) = fit_profile(&pool).unwrap();+ // the camera's colours (luminance-normalised: the tone curve sets brightness)+ for x in [[0.8, 1.0, 0.75], [1.1, 1.0, 0.8], [0.75, 1.0, 1.2], [1.0; 3]] {+ let (a, b) = (matrix.apply(x), known.apply(x));+ let (la, lb) = (luma(a), luma(b));+ assert!((0..3).all(|c| (a[c] / la - b[c] / lb).abs() < 0.01), "{x:?}: {a:?} vs {b:?}");+ }+ let (sensor, reference) = photo(9, 3.5);+ let look = fit_pairs_with(&sensor, &reference, Some((matrix, None))).unwrap();+ assert_eq!(look.matrix, matrix);+ assert!(look.hue_sat.is_none());+ let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);+ let error = sensor+ .data+ .iter()+ .zip(&reference.data)+ .map(|(x, y)| {+ let p = displayed(look.matrix.apply(x.map(f64::from)), &tone);+ (0..3).map(|c| (p[c] - f64::from(y[c])).powi(2)).sum::<f64>() / 3.0+ })+ .sum::<f64>()+ / sensor.data.len() as f64;+ assert!(error.sqrt() < 0.02, "this photo's own tone is followed: RMS {}", error.sqrt());+ }++ #[test]+ fn hue_sat_passes_black_and_non_finite_pixels_through() {+ let table = HsvTable { hue_divisions: 4, sat_divisions: 2, val_divisions: 1, data: vec![[10.0, 1.5, 1.0]; 8], srgb_value: false };+ let hue_sat = HueSat::new(&table).unwrap();+ assert_eq!(hue_sat.apply([0.0; 3]), [0.0; 3]);+ let nan = hue_sat.apply([f32::NAN, 0.2, 0.1]);+ assert!(nan[0].is_nan() && nan[1] == 0.2 && nan[2] == 0.1);+ assert!(fit_hue_sat(&[], &Mat3::IDENTITY).is_none(), "too few samples");+ }++ #[test]+ fn sony_and_nikon_raws_get_a_file_local_look() {+ assert!([RawFormat::Arw, RawFormat::Nef, RawFormat::Nrw].into_iter().all(file_local_look));+ assert!(![RawFormat::Dng, RawFormat::Cr2, RawFormat::Raf].into_iter().any(file_local_look));+ }++ /// A public D7500 NEF (skipped without the corpus): its look is fitted to its own JPEG and white+ /// balance is relative to the as-shot look.+ #[test]+ fn corpus_nef_gets_a_camera_look() {+ let path = std::env::var_os("LIGHTCRAFT_CORPUS")+ .map(std::path::PathBuf::from)+ .unwrap_or_else(|| std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus"))+ .join("raw/nef-nikon-d7500-lossless14.nef");+ let Ok(bytes) = std::fs::read(&path) else {+ eprintln!("skip: {} absent", path.display());+ return;+ };+ let (_, info) = crate::files::load_bytes(&bytes, 400).unwrap();+ assert!(info.camera_tone.is_some(), "no camera look fitted");+ assert!(info.relative_wb && info.as_shot_temp == 6500.0 && info.as_shot_tint == 0.0);+ }+#[test]fn rejects_monochrome_invalid_and_unrelated_previews() {let mut sensor = Rgb32f::new(32, 32);diff --cc crates/engine/src/camera_profiles.rsindex a936163ce,000000000..d2a9f16b2mode 100644,000000..100644--- a/crates/engine/src/camera_profiles.rs+++ b/crates/engine/src/camera_profiles.rs@@@ -1,276 -1,0 +1,276 @@@+//! Camera colour profiles of our own, pooled from many photos of one camera model.+//!- //! A raw file without colour matrices (Sony ARW) gets its look fitted to its own embedded camera++//! A raw file without colour matrices (Sony ARW, Nikon NEF) gets its look fitted to its own embedded camera+//! JPEG (`camera_preview`), but one photo shows too little of some colours: a lime shirt covering a+//! few dozen proxy pixels next to a hillside of foliage at the same hue. `lightcraft-cli calibrate`+//! pools the colour pairs of many photos per model and fits one matrix and hue/saturation/value+//! table; photos of that model then only fit their tone and chroma curves.+//!+//! Profiles are JSON files (`<model>.json`) in [`dir`]: `LIGHTCRAFT_CAMERA_PROFILES`, else+//! `<config>/camera-profiles`. They are read once per process; a damaged or hostile file is+//! ignored with a warning. They hold aggregate colour statistics only, never image content.+use lightcraft_color::Mat3;+use lightcraft_raw::profile::HsvTable;+use serde::{Deserialize, Serialize};+use std::collections::HashMap;+use std::path::{Path, PathBuf};+use std::sync::{Arc, Mutex, OnceLock, PoisonError};++/// Format version of the profile files.+const VERSION: u32 = 1;+/// Largest profile file read (a 5 × 72 × 5 table is ~60 KB of JSON).+const MAX_FILE: u64 = 4 << 20;++#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]+pub struct CameraProfile {+ pub version: u32,+ /// The camera model as the files report it (Exif `Model`), e.g. `ILCE-7M4`.+ pub model: String,+ /// Photos and colour pairs the profile was fitted on.+ pub files: usize,+ pub samples: usize,+ /// White-balanced camera RGB (with the baseline exposure) → linear Rec.2020, rows.+ matrix: [[f64; 3]; 3],+ /// Hue/saturation/value correction after `matrix` (linear ProPhoto RGB).+ pub hue_sat: Option<HsvTable>,+}++impl CameraProfile {+ pub fn new(model: &str, files: usize, samples: usize, matrix: Mat3, hue_sat: Option<HsvTable>) -> CameraProfile {+ CameraProfile { version: VERSION, model: model.to_owned(), files, samples, matrix: matrix.0, hue_sat }+ }++ pub fn matrix(&self) -> Mat3 {+ Mat3(self.matrix)+ }++ /// Whether the data is usable (bounded matrix, table shape matching its data).+ fn valid(&self) -> bool {+ let matrix = self.matrix.iter().flatten().all(|v| v.is_finite() && v.abs() < 8.0) && Mat3(self.matrix).inverse().is_some();+ let table = self.hue_sat.as_ref().is_none_or(|t| {+ let dims = (1..=4096).contains(&t.hue_divisions) && (2..=4096).contains(&t.sat_divisions) && (1..=4096).contains(&t.val_divisions);+ let len = t.hue_divisions.checked_mul(t.sat_divisions).and_then(|n| n.checked_mul(t.val_divisions));+ dims && len == Some(t.data.len()) && t.data.iter().flatten().all(|v| v.is_finite()) && t.data.iter().all(|e| e[1] >= 0.0 && e[2] >= 0.0)+ });+ self.version == VERSION && !self.model.is_empty() && matrix && table+ }+}++/// LightCraft's configuration folder (settings, GPU marker, camera profiles).+pub fn config_dir() -> Option<PathBuf> {+ if cfg!(target_os = "macos") {+ std::env::var_os("HOME").map(|h| PathBuf::from(h).join("Library/Application Support/LightCraft"))+ } else if cfg!(windows) {+ std::env::var_os("APPDATA").map(|a| PathBuf::from(a).join("LightCraft"))+ } else {+ std::env::var_os("XDG_CONFIG_HOME")+ .map(PathBuf::from)+ .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".config")))+ .map(|c| c.join("lightcraft"))+ }+}++/// Where camera profiles are read from and written to.+pub fn dir() -> Option<PathBuf> {+ std::env::var_os("LIGHTCRAFT_CAMERA_PROFILES").map(PathBuf::from).or_else(|| config_dir().map(|d| d.join("camera-profiles")))+}++/// File name of `model`'s profile: letters, digits, `-` and `_` kept, anything else `_`.+pub fn file_name(model: &str) -> String {+ let name: String = model.trim().chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '_' }).collect();+ format!("{name}.json")+}++/// Read and validate a profile file.+pub fn load(path: &Path) -> Result<CameraProfile, String> {+ let size = std::fs::metadata(path).map_err(|e| format!("{}: {e}", path.display()))?.len();+ if size > MAX_FILE {+ return Err(format!("{}: {size} bytes is too large for a camera profile", path.display()));+ }+ let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?;+ let profile: CameraProfile = serde_json::from_slice(&bytes).map_err(|e| format!("{}: {e}", path.display()))?;+ if !profile.valid() {+ return Err(format!("{}: not a usable camera profile (version {}, model {:?})", path.display(), profile.version, profile.model));+ }+ Ok(profile)+}++/// Write `profile` to `dir` (created if needed) as `<model>.json`; returns the path.+pub fn save(profile: &CameraProfile, dir: &Path) -> Result<PathBuf, String> {+ std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;+ let path = dir.join(file_name(&profile.model));+ let json = serde_json::to_vec_pretty(profile).map_err(|e| e.to_string())?;+ let tmp = path.with_extension("json.tmp");+ std::fs::write(&tmp, json).map_err(|e| format!("{}: {e}", tmp.display()))?;+ std::fs::rename(&tmp, &path).map_err(|e| format!("{}: {e}", path.display()))?;+ Ok(path)+}++fn cache() -> &'static Mutex<HashMap<String, Option<Arc<CameraProfile>>>> {+ static CACHE: OnceLock<Mutex<HashMap<String, Option<Arc<CameraProfile>>>>> = OnceLock::new();+ CACHE.get_or_init(Default::default)+}++/// The profile for camera `model` from [`dir`], read once per process.+pub fn get(model: &str) -> Option<Arc<CameraProfile>> {+ let model = model.trim();+ if model.is_empty() {+ return None;+ }+ let mut cache = cache().lock().unwrap_or_else(PoisonError::into_inner);+ if let Some(hit) = cache.get(model) {+ return hit.clone();+ }+ let path = dir().map(|d| d.join(file_name(model)));+ let profile = match path {+ Some(path) if path.is_file() => match load(&path) {+ Ok(p) if p.model.trim() == model => Some(Arc::new(p)),+ Ok(p) => {+ eprintln!("lightcraft: ignoring camera profile {}: it is for {:?}, not {model:?}", path.display(), p.model);+ None+ }+ Err(e) => {+ eprintln!("lightcraft: ignoring camera profile {e}");+ None+ }+ },+ _ => None,+ };+ cache.insert(model.to_owned(), profile.clone());+ profile+}++/// Changes when the profiles folder's contents change (per process): part of the render cache+/// keys, so thumbnails rendered before a profile existed are not reused after.+pub fn cache_key() -> u64 {+ static KEY: OnceLock<u64> = OnceLock::new();+ *KEY.get_or_init(|| {+ let Some(entries) = dir().and_then(|d| std::fs::read_dir(d).ok()) else { return 0 };+ let mut files: Vec<(String, u64, u64)> = entries+ .flatten()+ .filter(|e| e.path().extension().is_some_and(|x| x == "json"))+ .filter_map(|e| {+ let meta = e.metadata().ok()?;+ let modified = meta.modified().ok()?.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs();+ Some((e.file_name().to_string_lossy().into_owned(), meta.len(), modified))+ })+ .collect();+ files.sort();+ let mut h = lightcraft_preview::Hasher128::new();+ for (name, len, modified) in &files {+ h.str(name).u64(*len).u64(*modified);+ }+ h.finish().0 as u64+ })+}++/// Per camera model: the photos read and their pooled colour pairs.+#[derive(Default)]+pub struct Pool {+ models: HashMap<String, (usize, Vec<([f64; 3], [f64; 3])>)>,+}++/// Most colour pairs kept per photo, so a few busy photos can't dominate a profile.+const PAIRS_PER_FILE: usize = 4000;++impl Pool {- /// Add one raw file's colour pairs. `Ok(None)` when the file can't contribute (not an ARW- /// without colour matrices, no usable camera JPEG, too little colour).++ /// Add one raw file's colour pairs. `Ok(None)` when the file can't contribute (not an ARW or++ /// NEF without colour matrices, no usable camera JPEG, too little colour).+ pub fn add(&mut self, bytes: &[u8]) -> Result<Option<String>, String> {+ let raw = lightcraft_raw::decode(bytes).map_err(|e| e.to_string())?;+ let Some(model) = raw.metadata.model.as_deref().map(str::trim).filter(|m| !m.is_empty()) else { return Ok(None) };+ let Some(pairs) = crate::camera_preview::profile_pairs(&raw, bytes) else { return Ok(None) };+ let step = pairs.len().div_ceil(PAIRS_PER_FILE).max(1);+ let entry = self.models.entry(model.to_owned()).or_default();+ entry.0 += 1;+ entry.1.extend(pairs.into_iter().step_by(step));+ Ok(Some(model.to_owned()))+ }++ /// Fit a profile per model with at least `min_files` photos.+ pub fn fit(&self, min_files: usize) -> Vec<Result<CameraProfile, String>> {+ let mut models: Vec<_> = self.models.iter().collect();+ models.sort_by(|a, b| a.0.cmp(b.0));+ models+ .into_iter()+ .filter(|(_, (files, _))| *files >= min_files)+ .map(|(model, (files, pairs))| {+ let (matrix, hue_sat) = crate::camera_preview::fit_profile(pairs).ok_or_else(|| format!("{model}: no usable colour fit"))?;+ Ok(CameraProfile::new(model, *files, pairs.len(), matrix, hue_sat))+ })+ .collect()+ }++ /// Add another pool's photos and pairs (pools filled on separate threads).+ pub fn merge(&mut self, other: Pool) {+ for (model, (files, pairs)) in other.models {+ let entry = self.models.entry(model).or_default();+ entry.0 += files;+ entry.1.extend(pairs);+ }+ }++ /// Photos read per model.+ pub fn files(&self) -> Vec<(String, usize)> {+ let mut v: Vec<_> = self.models.iter().map(|(m, (n, _))| (m.clone(), *n)).collect();+ v.sort();+ v+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn profile() -> CameraProfile {+ let table = HsvTable { hue_divisions: 4, sat_divisions: 2, val_divisions: 1, data: vec![[5.0, 1.2, 1.0]; 8], srgb_value: false };+ CameraProfile::new("ILCE-7M4", 12, 3456, Mat3([[1.6, -0.5, -0.1], [-0.2, 1.3, -0.1], [-0.1, -0.2, 1.3]]), Some(table))+ }++ #[test]+ fn profiles_round_trip_and_reject_bad_files() {+ let dir = std::env::temp_dir().join(format!("lc-camera-profiles-{}", std::process::id()));+ let path = save(&profile(), &dir).unwrap();+ assert_eq!(path.file_name().unwrap(), "ILCE-7M4.json");+ assert_eq!(load(&path).unwrap(), profile());+ let bad = |p: CameraProfile| {+ let path = dir.join("bad.json");+ std::fs::write(&path, serde_json::to_vec(&p).unwrap()).unwrap();+ load(&path).is_err()+ };+ let mut p = profile();+ p.matrix[0][0] = f64::NAN;+ assert!(bad(p), "non-finite matrix");+ let mut p = profile();+ p.matrix = [[0.0; 3]; 3];+ assert!(bad(p), "singular matrix");+ let mut p = profile();+ if let Some(t) = p.hue_sat.as_mut() {+ t.data.truncate(3);+ }+ assert!(bad(p), "table data doesn't match its shape");+ let mut p = profile();+ p.version = 99;+ assert!(bad(p), "unknown version");+ std::fs::write(dir.join("junk.json"), b"{not json").unwrap();+ assert!(load(&dir.join("junk.json")).is_err());+ assert!(load(&dir.join("missing.json")).is_err());+ let _ = std::fs::remove_dir_all(&dir);+ }++ #[test]+ fn file_names_are_safe() {+ assert_eq!(file_name("ILCE-7M4"), "ILCE-7M4.json");+ assert_eq!(file_name(" DSC-RX100M3 "), "DSC-RX100M3.json");+ assert_eq!(file_name("../../etc/passwd"), "______etc_passwd.json");+ assert_eq!(file_name("Ω 1/2"), "__1_2.json");+ }++ #[test]+ fn pool_skips_files_that_cannot_contribute() {+ let mut pool = Pool::default();+ assert!(pool.add(b"not a raw file").is_err());+ assert!(pool.fit(1).is_empty());+ assert!(pool.files().is_empty());+ }+}diff --cc crates/engine/src/files.rsindex 6f8d18e15,d2d769c5c..1a34efac3--- a/crates/engine/src/files.rs+++ b/crates/engine/src/files.rs@@@ -288,8 -286,8 +289,8 @@@ fn load_bytes_now(bytes: std::borrow::Ceprintln!("[profile] raw source {}×{} (max {max_edge}, ms after decode): {}", img.width, img.height, parts.join(", "));}let (temp, tint) = xy_to_temp_tint(xy);- let relative = raw.format == lightcraft_raw::RawFormat::Arw && t.matrix_is_fallback;+ let relative = crate::camera_preview::file_local_look(raw.format) && t.matrix_is_fallback;- let camera_tone = camera_look.map(|p| p.tone).or_else(|| raw.color.profile.tone_curve.as_ref().and_then(dng_tone_curve));+ let camera_tone = camera_look.as_ref().map(|p| p.tone).or_else(|| raw.color.profile.tone_curve.as_ref().and_then(dng_tone_curve));let (temp, tint) = if relative { (6500.0, 0.0) } else { (temp.round(), tint.round()) };return Ok((img, SourceInfo { raw: true, as_shot_temp: temp, as_shot_tint: tint, lens, relative_wb: relative, camera_tone }));}diff --cc crates/engine/src/media.rsindex 27c9ca325,c5860c495..86b346e75--- a/crates/engine/src/media.rs+++ b/crates/engine/src/media.rs@@@ -26,7 -26,7 +26,7 @@@ use lightcraft_raster::{Histogram, Rgb3use serde::{Deserialize, Serialize};/// Bump when the pipeline's output changes, to invalidate cached thumbnails.- pub const RENDER_CACHE_VERSION: u64 = 10;-pub const RENDER_CACHE_VERSION: u64 = 9;++pub const RENDER_CACHE_VERSION: u64 = 11;/// 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];@@@ -718,16 -705,51 +718,58 @@@ impl crate::Session})}+ /// A square close-up of `face` (normalized, in the photo's upright frame) for a People card: the+ /// photo's own look with its crop replaced by a square around the face (room for hair and chin),+ /// `edge` pixels across (≤ 512). The source level is the smallest that keeps the face sharp, up+ /// to the preview. Cached like [`Self::variant_job`], and `key` likewise follows the content.+ pub fn face_job(&mut self, id: PhotoId, face: lightcraft_geom::Rect, edge: usize) -> Option<RenderJob> {+ let p = self.catalog.photo(id)?.clone();+ // `face` is on the upright (EXIF-oriented) photo; the crop below is in the frame after the+ // user's Rotate Left/Right, so carry the box (and the photo's size) over to it.+ let orient = p.develop.orientation;+ let face = orient.map_norm_rect(face);+ let (w, h) = (f64::from(p.width.max(1)), f64::from(p.height.max(1)));+ let (w, h) = if orient.swaps_axes() { (h, w) } else { (w, h) };+ let side = ((face.x1 - face.x0) * w).max((face.y1 - face.y0) * h) * 1.7;+ // finite, within the photo, and never wider than its short side (so the clamps below are valid)+ let side = if side.is_finite() { side.clamp(1.0, w.min(h)) } else { w.min(h) };+ let (cx, cy) = ((face.x0 + face.x1) / 2.0 * w, (face.y0 + face.y1) / 2.0 * h);+ let (cx, cy) = (if cx.is_finite() { cx } else { w / 2.0 }, if cy.is_finite() { cy } else { h / 2.0 });+ let (x0, y0) = (cx.clamp(side / 2.0, w - side / 2.0) - side / 2.0, cy.clamp(side / 2.0, h - side / 2.0) - side / 2.0);+ let rect = lightcraft_geom::Rect { x0: x0 / w, y0: y0 / h, x1: (x0 + side) / w, y1: (y0 + side) / h };+ let mut settings = (*p.develop).clone();+ settings.crop = lightcraft_develop::Crop { geometry: lightcraft_geom::CropGeometry { rect, angle: 0.0 }, ..Default::default() };+ let edge = edge.clamp(16, SourceLevel::Thumb.max_edge());+ // pixels along the long edge that leave `edge` across the crop+ let needed = (edge as f64 * w.max(h) / side).ceil().min(SourceLevel::Preview.max_edge() as f64);+ let level = SourceLevel::for_size(needed as usize);+ let source = self.media.source_ref(&p, level);+ let ck = Self::variant_key(&p, &settings, edge);+ Some(RenderJob {+ photo: id,+ level,+ source,+ origin: p.source.clone(),+ info: source_info(&p),+ settings: Arc::new(settings),+ request: RenderRequest { apply_crop: true, ..RenderRequest::fit(edge, edge) },+ key: (ck.0 as u64) ^ ((ck.0 >> 64) as u64),+ cache: Some((self.media.rendered.clone(), ck)),+ stages: None,+ view_cache: None,+ })+ }+/// Size-independent cache key of a photo's view render (loupe) for its current settings.fn view_key(p: &Photo, apply_crop: bool) -> Hash128 {- Hasher128::new().str(&content_key(p)).str("view").u64(p.develop.hash64()).u64(apply_crop as u64).u64(RENDER_CACHE_VERSION).finish()+ Hasher128::new()+ .str(&content_key(p))+ .str("view")+ .u64(p.develop.hash64())+ .u64(apply_crop as u64)+ .u64(RENDER_CACHE_VERSION)+ .u64(crate::camera_profiles::cache_key())+ .finish()}/// The loupe's render job: like [`Self::render_job`], and a full-quality result is kept as thediff --cc docs/camera-preview-colour.mdindex c996e6cd3,6c847c890..f1c6924d1--- a/docs/camera-preview-colour.md+++ b/docs/camera-preview-colour.md@@@ -30,15 -26,14 +30,21 @@@ Synthetic tests cover colour with a nonFive local Sony ILCE-6700 ARWs were accepted. Held-out per-channel linear-display RMS values were 0.04244, 0.03349, 0.04552, 0.02871 and 0.03100. All five unedited supplied JPEG exports stayed pixel-identical to the installed pre-change renderer at equal size/quality. Four supplied JPEGs use Display P3 and two have a narrower crop than the RAW, so they are visual references rather than aligned pixel ground truth.-Public CC0 samples (raw.pixls.us; 10 files, ILCE-6000, -6400 ×2, -6700, -7M3, -7M4 ×2, -7RM4, -7SM3, DSC-RX100M3), rendered at 1200 px and compared with each file's embedded JPEG (mean CIE76 ΔE at 200 px): 7 fits accepted, ΔE 17–26 → 3–10 with matching mean lightness. (The RX100M3 was rejected at the time because the decoder read neither its white balance nor its black level and its render was green; since issue #148 its fit is accepted, held-out RMS 0.153 → 0.087.) Rejected and unchanged: an ILCE-7RM4 dusk scene whose fit was worse than the fallback (ΔE 7.6), and a reduced-size lossless ARW (no fit attempted). Saturated hues can still differ (an orange-red hull renders yellow).+Public CC0 samples (raw.pixls.us; 10 files, ILCE-6000, -6400 ×2, -6700, -7M3, -7M4 ×2, -7RM4, -7SM3, DSC-RX100M3), rendered at 1200 px and compared with each file's embedded JPEG (mean CIE76 ΔE at 200 px): 7 fits accepted, ΔE 17–26 → 3–10 with matching mean lightness. (The RX100M3 was rejected at the time because the decoder read neither its white balance nor its black level and its render was green; since issue #148 its fit is accepted, held-out RMS 0.153 → 0.087.) Rejected and unchanged: an ILCE-7RM4 dusk scene whose fit was worse than the fallback (ΔE 7.6), and a reduced-size lossless ARW (no fit attempted). Saturated hues can still differ (an orange-red hull renders yellow). With the hue/saturation table, 28 local ILCE-7M4 ARWs (one month of photos, one in 24 picked) went from mean ΔE 4.74 to 4.27 against their camera JPEGs (200 px, CIE76); 27 accepted a table, none got worse overall by more than 0.1, and a few saturated regions moved away (12.4 → 15.2 on one). The lime shirt's hue error dropped from 16° to 6–8°. With the value axis, the chroma curve and gamma-domain selection, the same set plus the shirt photo (29 files) went from mean ΔE 4.23 to 3.54 (4.74 before the table), every file closer than before, saturated areas 7.65 → 6.41; all 29 now use a table. A small object stays hard: in a second shot of the lime shirt only 7 of the 6144 proxy pixels show it, so the table can't learn its colour and the shirt still renders yellower than the JPEG.+ ### Nikon NEF++ The same fit, gates and relative WB apply to NEF/NRW (issue #150: NEFs rendered muted and greenish). Public CC0 samples from raw.pixls.us (D750 12/14-bit, D780 12-bit, D850 12/14-bit, D7500 12/14-bit lossless and lossy, Z 50 12-bit) plus five local D7500 shots of an indoor event, rendered at 1200 px and compared with each file's embedded JPEG (mean CIE76 ΔE at 200 px): all 13 decodable files accepted, held-out RMS 0.11–0.24 → 0.01–0.09, ΔE 13–46 → 2.9–7.8 with matching mean lightness. The exception is a mixed-light D7500 scene (faces lit by a purple screen): accepted (RMS 0.156 → 0.057) but only ΔE 12.7 → 12.0, rendered warmer and lighter than the camera JPEG; the linear-display error the gate measures is dominated by the bright screen. Files that still open as the embedded preview (lossy-after-split, uncompressed data labelled compressed) are not fitted.++ Measuring this found a decoder bug: 12-bit NEFs store maker note `0x003d` BlackLevel in 14-bit units (D750 600, D780/Z 50 1008, D850/D7500 400, while their darkest samples are 150 / ~252 / 99), so 12-bit files rendered nearly black or with crushed shadows before any fit. The decoder now scales it to the sample depth; the fits for the same D7500 scene at 12 and 14 bits then agree (held-out RMS 0.042–0.049 vs 0.050–0.054; before, the 12-bit fits had to desaturate to compensate).+On one sample, Exposure +1 EV, Saturation +100, Contrast +50, warm/cool WB, tint, highlights and shadows all changed the output. The same matrix and curve were selected for 400-pixel and full-resolution renders. Headless UI checks wait until the loupe reports `source: render` and no jobs remain; the camera JPEG stand-in does not count as verification.- This is a per-file camera-look estimate, **not measured spectral calibration or Lightroom parity**. Camera local tone, picture styles, noise reduction and lens warps cannot all be reproduced by a global matrix/curve; same-aspect crops may evade the aspect check. Skin, mixed-light scenes and saturated highlights can still differ from a supplied JPEG. Broader Sony model/lighting coverage, chart-based calibration, absolute WB and other manufacturers remain unverified. Rejected fits and other RAW formats retain their existing behaviour.+ This is a per-file camera-look estimate, **not measured spectral calibration or Lightroom parity**. Camera local tone, picture styles, noise reduction and lens warps cannot all be reproduced by a global matrix/curve; same-aspect crops may evade the aspect check. Skin, mixed-light scenes and saturated highlights can still differ from a supplied JPEG. Broader Sony and Nikon model/lighting coverage, chart-based calibration, absolute WB and other manufacturers remain unverified. Rejected fits and other RAW formats retain their existing behaviour.++## Camera profiles (pooled per model)++One photo can show too little of a colour for its own fit to learn it: in a second shot of the lime shirt only a few dozen proxy pixels show it, next to a hillside of foliage at the same hue that the camera renders differently, and the shirt stayed yellow (hue 68° against the JPEG's 82°; a 192-pixel proxy only reached 74°). `lightcraft-cli calibrate [--max N] [--out DIR] FOLDERS…` therefore pools the colour pairs of many ARWs per camera model — each against its own embedded JPEG, at a 192-pixel proxy, at most 4000 pairs per photo, files spread evenly over the folders — and fits one matrix (from white-balanced camera RGB, so no single photo's white point is baked in) and one hue/saturation/value table. Models need at least 5 usable photos.++Profiles are JSON files `<model>.json` in `$LIGHTCRAFT_CAMERA_PROFILES`, else `<config>/camera-profiles` (macOS `~/Library/Application Support/LightCraft/camera-profiles`). They hold aggregate colour statistics only. A photo of a profiled model takes its colour from the profile and fits only its own tone and chroma curves (DRO and picture styles vary per shot); the acceptance gates still apply, and a rejected fit falls back as before. Files are read once per process and validated (version, bounded invertible matrix, table shape and finite data); a damaged file is ignored with a warning. The profiles folder's contents are part of the render cache keys, so thumbnails rendered before a profile existed are redone; smart previews built before keep their colour until rebuilt.+diff --cc docs/parity.mdindex 678d22329,135b6ba12..38d768af5--- a/docs/parity.md+++ b/docs/parity.md@@@ -59,7 -59,7 +59,7 @@@ Ordered by user impact, then tier, thethat decide whether a photographer can switch (see the honest assessment in [ROADMAP.md](../ROADMAP.md#where-we-stand)).Take the first one nobody is working on.- 1. **LR-PROF-CAMERACOLOR** (P0): our own camera colour calibration. Sony ARW now gets a guarded file-local fit to its own embedded JPEG (colour matrix + hue/saturation/value table + tone and chroma curves, relative WB; 7 of 10 public samples accepted, mean ΔE vs the camera JPEG 17–26 → 3–10); measured calibration and fidelity remain missing. Other non-DNG raws and rejected fits still use a neutral matrix. Expand validated preview fitting and use matrices the files carry themselves; never Adobe data.-1. **LR-PROF-CAMERACOLOR** (P0): our own camera colour calibration. Sony ARW and Nikon NEF now get a guarded file-local fit to their own embedded JPEG (colour matrix + tone curve, relative WB; ARW: 7 of 10 public samples accepted, mean ΔE vs the camera JPEG 17–26 → 3–10; NEF: 13 of 13 decodable samples from 6 bodies accepted, ΔE 13–46 → 3–8, one mixed-light scene 13 → 12); measured calibration and fidelity remain missing. Other non-DNG raws and rejected fits still use a neutral matrix. Expand validated preview fitting and use matrices the files carry themselves; never Adobe data.++1. **LR-PROF-CAMERACOLOR** (P0): our own camera colour calibration. Sony ARW and Nikon NEF now get a guarded file-local fit to their own embedded JPEG (colour matrix + hue/saturation/value table + tone and chroma curves, relative WB; per-model profiles pooled from many photos via `lightcraft-cli calibrate`; ARW: 7 of 10 public samples accepted, mean ΔE vs the camera JPEG 17–26 → 3–10; NEF: 13 of 13 decodable samples from 6 bodies accepted, ΔE 13–46 → 3–8, one mixed-light scene 13 → 12); measured calibration and fidelity remain missing. Other non-DNG raws and rejected fits still use a neutral matrix. Expand validated preview fitting and use matrices the files carry themselves; never Adobe data.2. **LR-IMP-FORMATS** (P0): **CR3** first (every Canon body since ~2018), then compressed RAF / ORF, RW2 v4, Nikon"lossy after split" NEF, Canon sRAW; HEIC/AVIF decode. Clean-room, from prose descriptions only (see`crates/raw/src/vendor/nefc.rs` for how compressed NEF was done). Until decoded, such photos are `preview_only`.@@@ -274,7 -274,7 +274,7 @@@ under the UI command that wraps it (a n| LR-PROF-ADOBE | Standard raw looks (own equivalents) | P0 | ✅ | `crates/engine/src/presets.rs` (`PROFILES`), `crates/pipeline/src/profiles.rs` | six own looks: Color, Neutral, Vivid, Landscape, Portrait, Monochrome || LR-PROF-ADAPTIVE | Adaptive profiles | P2 | ⬜ | | || LR-PROF-CAMERA | Camera-matching looks | P2 | ⬜ | | |- | LR-PROF-CAMERACOLOR | Camera colour calibration (own) | P0 | 🟡 | `crates/raw/src/color.rs`, `crates/raw/src/profile.rs`, `crates/engine/src/camera_preview.rs` | DNG files use the colour matrices and the camera-profile look they carry (`ProfileHueSatMap`, `ProfileLookTable`, `ProfileToneCurve`, read from the file at run time per DNG spec ch. 6 — Lightroom-converted DNGs rendered flat and muted without them, issue #138); Sony ARW gets a guarded file-local fit to its own embedded JPEG — chromaticity matrix + hue/saturation/value table + tone and chroma curves, relative WB — or, with a local per-model profile pooled from many photos (`lightcraft-cli calibrate`, `crates/engine/src/camera_profiles.rs`), the profile's colour and only its own tone/chroma curves (on 29 held-out ILCE-7M4 photos mean ΔE vs the camera JPEG 3.54 → 3.10), kept with smart previews (see docs/camera-preview-colour.md); on 10 public raw.pixls.us samples from 8 bodies 7 fits were accepted, mean ΔE vs the camera JPEG 17–26 → 3–10; this is not measured camera calibration; other raws and rejected fits use a neutral fallback (camera RGB ≈ linear sRGB, `matrix_is_fallback`) with as-shot white balance, so colours are muted and not accurate. Needs our own per-camera calibration: matrices the files carry themselves (e.g. Olympus `ColorMatrix`), fitting each camera to its own embedded JPEG, then chart shots. Adobe matrices / DCPs are never used. Biggest image-quality gap today |-| LR-PROF-CAMERACOLOR | Camera colour calibration (own) | P0 | 🟡 | `crates/raw/src/color.rs`, `crates/raw/src/profile.rs`, `crates/engine/src/camera_preview.rs` | DNG files use the colour matrices and the camera-profile look they carry (`ProfileHueSatMap`, `ProfileLookTable`, `ProfileToneCurve`, read from the file at run time per DNG spec ch. 6 — Lightroom-converted DNGs rendered flat and muted without them, issue #138); Sony ARW and Nikon NEF get a guarded file-local fit to their own embedded JPEG — chromaticity matrix + tone curve, relative WB, kept with smart previews (see docs/camera-preview-colour.md); ARW: on 10 public raw.pixls.us samples from 8 bodies 7 fits were accepted, mean ΔE vs the camera JPEG 17–26 → 3–10; NEF: 13 of 13 decodable samples (D750, D780, D850, D7500, Z 50 and local D7500 shots) accepted, ΔE 13–46 → 3–8 (issue #150); this is not measured camera calibration; other raws and rejected fits use a neutral fallback (camera RGB ≈ linear sRGB, `matrix_is_fallback`) with as-shot white balance, so colours are muted and not accurate. Needs our own per-camera calibration: matrices the files carry themselves (e.g. Olympus `ColorMatrix`), fitting each camera to its own embedded JPEG, then chart shots. Adobe matrices / DCPs are never used. Biggest image-quality gap today |++| LR-PROF-CAMERACOLOR | Camera colour calibration (own) | P0 | 🟡 | `crates/raw/src/color.rs`, `crates/raw/src/profile.rs`, `crates/engine/src/camera_preview.rs` | DNG files use the colour matrices and the camera-profile look they carry (`ProfileHueSatMap`, `ProfileLookTable`, `ProfileToneCurve`, read from the file at run time per DNG spec ch. 6 — Lightroom-converted DNGs rendered flat and muted without them, issue #138); Sony ARW and Nikon NEF get a guarded file-local fit to their own embedded JPEG — chromaticity matrix + hue/saturation/value table + tone and chroma curves, relative WB — or, with a local per-model profile pooled from many photos (`lightcraft-cli calibrate`, `crates/engine/src/camera_profiles.rs`), the profile's colour and only their own tone/chroma curves (on 29 held-out ILCE-7M4 photos mean ΔE vs the camera JPEG 3.54 → 3.10), kept with smart previews (see docs/camera-preview-colour.md); ARW: on 10 public raw.pixls.us samples from 8 bodies 7 fits were accepted, mean ΔE vs the camera JPEG 17–26 → 3–10; NEF: 13 of 13 decodable samples (D750, D780, D850, D7500, Z 50 and local D7500 shots) accepted, ΔE 13–46 → 3–8 (issue #150); this is not measured camera calibration; other raws and rejected fits use a neutral fallback (camera RGB ≈ linear sRGB, `matrix_is_fallback`) with as-shot white balance, so colours are muted and not accurate. Needs our own per-camera calibration: matrices the files carry themselves (e.g. Olympus `ColorMatrix`), fitting each camera to its own embedded JPEG, then chart shots. Adobe matrices / DCPs are never used. Biggest image-quality gap today || LR-PROF-CREATIVE | Creative profiles (own) | P2 | ✅ | `cmd:develop.profile`, `crates/pipeline/src/profiles.rs` | 16 own looks in Film / Cinematic / Muted / B&W (tone + point-curve fades, colour grading, mixer / B&W mix); scale with `ctl:profile.amount`; sliders untouched || LR-PROF-LEGACY | Legacy profiles | P2 | ⬜ | | || LR-PROF-NONRAW | Profiles for non-raw files | P0 | ✅ | `cmd:develop.profile` | same looks apply to JPEG/TIFF |