Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions crates/develop/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ impl ProcessVersion {
/// makes the compiler point at every stage whose behaviour depends on it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Process {
/// The rendering of LightCraft before process versions existed (2026-10).
/// The rendering of LightCraft before process versions existed (2026-10). Frozen.
V1,
/// Highlights and Shadows keep the local contrast of the tones they move: their edge-aware base
/// is taken of a fine median of log luminance (issue #632).
/// The measured rendering (2026-10), shipped as one batch. **Highlights and Shadows** keep the
/// local contrast of the tones they move: their edge-aware base is taken of a fine median of
/// log luminance (issue #632). The **tone model** — the default raw tone curve, Blacks and
/// Whites — is fitted to the reference developer's observed output instead of inherited from a
/// neutral curve, the raw curve acts per channel and positive Whites is per-photo
/// (`crates/pipeline/src/tone.rs`). A photo that updates to V2 gets the whole measured look in
/// one step, so the pieces land together (`docs/process-versions.md`).
V2,
}

Expand All @@ -97,6 +102,18 @@ impl Process {
Process::V2 => ProcessVersion(2),
}
}

/// Whether this process renders the tone model measured against the reference developer's
/// output (V2 on): the raw tone curve acts per channel, positive Whites is per-photo, and
/// Blacks is a separate per-channel curve. The tone stages branch on this — `finish::base_tone`
/// in one exhaustive `match`, and the two places that only have a yes/no question to ask
/// (whether to measure the frame's highlights, whether to build the Blacks curve).
pub const fn is_measured_tone(self) -> bool {
match self {
Process::V1 => false,
Process::V2 => true,
}
}
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
56 changes: 33 additions & 23 deletions crates/engine/src/camera_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ fn with_dro_off_tone(look: CameraLook) -> CameraLook {
}

/// Whether curve `a` is nowhere brighter than `b` over the scene luminances the finish stage's tone
/// table covers ([`ToneMap::camera`]).
/// table covers ([`ToneMap::camera_v1`]).
fn at_most(a: &CameraTone, b: &CameraTone) -> bool {
use lightcraft_pipeline::tone::{GREY, LUT_MAX_EV, LUT_MIN_EV, LUT_N};
(0..LUT_N).all(|i| {
Expand Down Expand Up @@ -673,6 +673,16 @@ fn search_ordered(sensor: &Rgb32f, reference: &Rgb32f, colour: Option<(Mat3, Opt
})
}

/// One candidate fit, scored with the **frozen V1 tone map** ([`ToneMap::camera_v1`] /
/// [`ToneMap::hdr_v1`], through `fit_pairs_on` and `fit_partial_on`).
///
/// The model a fit is *scored* with is deliberately not the one V2 renders: the acceptance gates
/// below decide which files get a look at all, so moving them is a change to how a file is decoded
/// and coloured — the source stage, which the process version does not reach
/// (`docs/process-versions.md`). What the fit produces is process-independent anyway: it is a
/// [`CameraTone`] and a colour matrix, which `finish::base_tone` composes with whichever process
/// the photo is on. At neutral settings the two models agree exactly, and neutral is where the
/// candidate looks are built.
fn fit_attempt(sensor: &Rgb32f, reference: &Rgb32f, colour: &Option<(Mat3, Option<HsvTable>)>, attempt: Attempt) -> Option<CameraLook> {
let colour = if attempt.profile { colour.clone() } else { None };
fit_pairs_on(sensor, reference, colour, attempt.away_from_edges.then_some(EDGE_CONTRAST), attempt.tone)
Expand Down Expand Up @@ -712,7 +722,7 @@ fn fit_pairs_on(
if let Some(tone) = fit_chroma(&bright, &look) {
look.tone = tone;
}
let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);
let tone = ToneMap::camera_v1(&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);
Expand All @@ -727,7 +737,7 @@ fn fit_pairs_on(
}
}
let (_, after, samples, look) = best?;
let original_tone = ToneMap::new(0.0, 0.0, 0.0);
let original_tone = ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0);
let before: f64 = pairs
.iter()
.step_by(3)
Expand Down Expand Up @@ -855,7 +865,7 @@ fn fit_partial_on(sensor: &Rgb32f, reference: &Rgb32f, edge_limit: Option<f32>)
if let Some(tone) = fit_chroma(&bright, &look) {
look.tone = tone;
}
let (tone, neutral) = (ToneMap::camera(&look.tone, 0.0, 0.0, 0.0), ToneMap::new(0.0, 0.0, 0.0));
let (tone, neutral) = (ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0), ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0));
let encoded = |v: f64| v.max(0.0).powf(1.0 / 2.2);
let (mut before, mut after, mut before_encoded, mut after_encoded, mut samples) = (0.0, 0.0, 0.0, 0.0, 0);
for (x, target) in pairs.iter().step_by(3) {
Expand Down Expand Up @@ -960,7 +970,7 @@ fn fit_tone_only_on(sensor: &Rgb32f, reference: &Rgb32f, colour: &Option<(Mat3,
let tone_pairs: Vec<_> =
pairs.iter().enumerate().filter(|(i, _)| i % 3 != 0).map(|(_, (x, y))| (luma(coloured(*x).map(|v| v.max(0.0))), luma(*y))).collect();
let look = CameraLook { matrix, tone: fit_tone(tone_pairs)?, hue_sat: hue_sat.clone() };
let (tone, neutral) = (ToneMap::camera(&look.tone, 0.0, 0.0, 0.0), ToneMap::new(0.0, 0.0, 0.0));
let (tone, neutral) = (ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0), ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0));
let encoded = |v: f64| v.max(0.0).powf(1.0 / 2.2);
let (mut before, mut after, mut before_encoded, mut after_encoded, mut samples) = (0.0, 0.0, 0.0, 0.0, 0);
for (x, target) in pairs.iter().step_by(3) {
Expand Down Expand Up @@ -1164,7 +1174,7 @@ fn fit_chroma(pairs: &[([f64; 3], [f64; 3])], look: &CameraLook) -> Option<Camer
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 tone = ToneMap::camera_v1(&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);
Expand Down Expand Up @@ -1195,7 +1205,7 @@ fn fit_chroma(pairs: &[([f64; 3], [f64; 3])], look: &CameraLook) -> Option<Camer
*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 with = ToneMap::camera_v1(&fitted, 0.0, 0.0, 0.0);
let error = |map: &ToneMap| -> f64 {
pairs
.iter()
Expand Down Expand Up @@ -1377,7 +1387,7 @@ mod tests {
let original = sensor.clone();
let fit = fit_pairs(&sensor, &reference).unwrap();
assert_eq!(sensor.data, original.data);
let tone = ToneMap::camera(&fit.tone, 0.0, 0.0, 0.0);
let tone = ToneMap::camera_v1(&fit.tone, 0.0, 0.0, 0.0);
let error: f64 = sensor
.data
.iter()
Expand Down Expand Up @@ -1504,8 +1514,8 @@ mod tests {
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 with = ToneMap::camera_v1(&fit.tone, 0.0, 0.0, 0.0);
let without = ToneMap::camera_v1(&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
Expand Down Expand Up @@ -1557,7 +1567,7 @@ mod tests {
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 tone = ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0);
let error = sensor
.data
.iter()
Expand Down Expand Up @@ -2270,7 +2280,7 @@ mod tests {
}
let fit = fit_pairs(&sensor, &reference).expect("look fitted away from the misaligned edges");
// The look itself is right: on the aligned scene it reproduces the camera.
let tone = ToneMap::camera(&fit.tone, 0.0, 0.0, 0.0);
let tone = ToneMap::camera_v1(&fit.tone, 0.0, 0.0, 0.0);
let (mut error, mut n) = (0.0, 0);
for y in 0..h {
for x in 0..w {
Expand Down Expand Up @@ -2438,7 +2448,7 @@ mod tests {
let y = luma(p);
p.iter().map(|v| (v / y - 1.0).powi(2)).sum::<f64>().sqrt()
};
let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);
let tone = ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0);
let (mut rendered, mut camera) = (0.0, 0.0);
for ((x, y), c) in sensor.data.iter().zip(&reference.data).zip(clipped) {
let y = y.map(f64::from);
Expand Down Expand Up @@ -2568,8 +2578,8 @@ mod tests {
assert!(look.hue_sat.is_none());
assert_ne!(look.matrix, Mat3::IDENTITY, "a colourful scene gets the damped matrix");
// on the registered scene, against what the camera renders
let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);
let neutral = ToneMap::new(0.0, 0.0, 0.0);
let tone = ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0);
let neutral = ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0);
let partial = mean_error(&sensor, bright_camera, |x| displayed(look.matrix.apply(x), &tone));
let fallback = mean_error(&sensor, bright_camera, |x| displayed(x, &neutral));
assert!(partial < fallback * 0.25, "partial {partial:.5}, neutral fallback {fallback:.5}");
Expand Down Expand Up @@ -2597,8 +2607,8 @@ mod tests {
assert!(fit_pairs(&sensor, &reference).is_none(), "too little colour for the full look");
let look = fit_look(&sensor, &reference, &vec![false; w * h], None).expect("a partial look");
assert_eq!(look.matrix, Mat3::IDENTITY, "no colour matrix from a colourless scene");
let tone = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);
let neutral = ToneMap::new(0.0, 0.0, 0.0);
let tone = ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0);
let neutral = ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0);
let lightness =
|tone: &ToneMap| sensor.data.iter().map(|p| f64::from(tone.apply(luminance_2020(*p)))).sum::<f64>() / sensor.data.len() as f64;
let target = reference.data.iter().map(|p| f64::from(luminance_2020(*p))).sum::<f64>() / reference.data.len() as f64;
Expand All @@ -2618,7 +2628,7 @@ mod tests {
}
let none = vec![false; w * h];
// the camera renders exactly as the neutral fallback: nothing to gain
let neutral = ToneMap::new(0.0, 0.0, 0.0);
let neutral = ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0);
let mut reference = sensor.clone();
reference.map_in_place(|p| displayed(p.map(f64::from), &neutral).map(|v| v as f32));
assert!(fit_partial(&sensor, &reference).is_none(), "no clear gain over the fallback");
Expand Down Expand Up @@ -2687,9 +2697,9 @@ mod tests {
let n = sensor.data.len() as f64;
let mean = |tone: &ToneMap| sensor.data.iter().map(|p| luma(displayed(p.map(f64::from), tone))).sum::<f64>() / n;
let target = reference.data.iter().map(|p| f64::from(luminance_2020(*p))).sum::<f64>() / n;
let toned = mean(&ToneMap::camera(&look.tone, 0.0, 0.0, 0.0));
let toned = mean(&ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0));
assert!((toned / target - 1.0).abs() < 0.05, "{toned} vs {target}");
assert!(mean(&ToneMap::new(0.0, 0.0, 0.0)) < target * 0.8, "the default curve is darker");
assert!(mean(&ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0)) < target * 0.8, "the default curve is darker");
// with a camera profile's colour, the look carries it
let matrix = Mat3([[1.2, -0.15, -0.05], [-0.1, 1.15, -0.05], [-0.02, -0.1, 1.12]]);
let look = fit_tone_only(&sensor, &reference, Some((matrix, None))).expect("a tone curve with the profile's colour");
Expand All @@ -2708,7 +2718,7 @@ mod tests {
*p = hues[i % 6].map(|c| c * ev);
}
// a JPEG a little brighter than the default curve, so a curve has to fit closely to be clearly better
let neutral = ToneMap::new(0.0, 0.0, 0.0);
let neutral = ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0);
let mut reference = sensor.clone();
reference.map_in_place(|p| [(1.15 * luma(displayed(p.map(f64::from), &neutral))) as f32; 3]);
let swapped = Mat3([[0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [1.0, 0.0, 0.0]]);
Expand Down Expand Up @@ -2784,7 +2794,7 @@ mod tests {
let t = lightcraft_raw::color::camera_transform(&raw, lightcraft_raw::color::as_shot_white_xy(&raw));
let look = monochrome_look(&raw, &file([1.0; 3]), &t).expect("a tone curve from the black-and-white JPEG");
assert!(look.matrix == Mat3::IDENTITY && look.hue_sat.is_none(), "on the transform's colour");
let curve = ToneMap::camera(&look.tone, 0.0, 0.0, 0.0);
let curve = ToneMap::camera_v1(&look.tone, 0.0, 0.0, 0.0);
let mid = curve.apply(0.25);
assert!((mid - (1.0 - (-0.75f32).exp())).abs() < 0.05, "the camera's curve at 0.25: {mid}");
assert!(monochrome_look(&raw, &file([1.0, 0.9, 0.8]), &t).is_none(), "a tinted JPEG is not black and white");
Expand All @@ -2795,7 +2805,7 @@ mod tests {
#[test]
fn a_black_and_white_jpeg_gives_a_tone_curve_only_when_clearly_better() {
let (sensor, mut reference) = black_and_white_scene();
let neutral = ToneMap::new(0.0, 0.0, 0.0);
let neutral = ToneMap::hdr_v1(0.0, 0.0, 0.0, 1.0);
reference.data.iter_mut().zip(&sensor.data).for_each(|(r, s)| *r = [luma(displayed(s.map(f64::from), &neutral)) as f32; 3]);
assert!(monochrome(&reference));
assert!(fit_tone_only(&sensor, &reference, None).is_none(), "no clear gain over the default curve");
Expand Down
14 changes: 14 additions & 0 deletions crates/engine/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,14 @@ fn matte_kind(name: &str) -> Option<lightcraft_pipeline::masks::MatteKind> {
/// A DNG `ProfileToneCurve` (linear in, linear out, 1.0 = white after exposure compensation) as the
/// finish stage's camera tone curve: 32 knots, log-spaced over 12 stops below white; above white
/// the camera tone's shoulder continues it.
///
/// The curve carries **no chroma curve**. It is the file's own and acts on the reference's
/// *channels*, so mapping luminance here does lose colourfulness the reference keeps — but both
/// ways of putting it back overshoot on measured files. On `ProfileToneCurve` DNGs at neutral
/// neutral (Adobe Standard, 1200 px) the bare luminance map reads ΔE 5.5 and 4.9; compositing the
/// default curve's chroma curve over it reads 7.8 and 7.9, and applying the curve per channel
/// instead reads 7.9 and 8.4. The reference sits *under* a luminance map on these files, so the
/// residual is not in the tone stage and no constant fitted to the default curve can repair it.
pub(crate) fn dng_tone_curve(curve: &lightcraft_raw::profile::ToneCurve) -> Option<lightcraft_pipeline::tone::CameraTone> {
let knots: [[f32; 2]; 32] = std::array::from_fn(|i| {
let x = 2f32.powf(-12.0 + 12.0 * i as f32 / 31.0);
Expand Down Expand Up @@ -1103,6 +1111,12 @@ mod tests {
assert!((0.8..1.1).contains(&(mean(&after) / mean(&before))), "{} vs {}", mean(&after), mean(&before));
let tone = info.camera_tone.expect("the DNG tone curve becomes the camera tone");
assert!((tone.apply(0.18) - 0.3).abs() < 0.01, "{}", tone.apply(0.18));
// The profile curve is the reference's own and acts on its channels, but this path maps
// luminance, and every chroma curve we can put back over it overshoots the reference on
// measured files (see `dng_tone_curve`) — so the file's tone carries none, and it must not
// silently grow one again. A chroma curve here cost two measured files 1.9 and
// 2.9 ΔE at neutral against Lightroom.
assert!(tone.chroma().iter().all(|k| *k == 1.0), "camera tone has a chroma curve: {:?}", tone.chroma());
// a look table alone changes the render too (applied after exposure)
raw.color.profile = ProfileLook {
look_table: Some(HsvTable { hue_divisions: 1, sat_divisions: 2, val_divisions: 1, data: vec![[0.0, 1.0, 0.5]; 2], srgb_value: false }),
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl SettingsHashes {
}

/// Bump when the pipeline's output changes, to invalidate cached thumbnails.
pub const RENDER_CACHE_VERSION: u64 = 60;
pub const RENDER_CACHE_VERSION: u64 = 61;

/// 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];
Expand Down
12 changes: 12 additions & 0 deletions crates/gpu/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const FIELDS: &[(&str, usize)] = &[
("AIR_PRE", 1),
("HL", 1),
("SH", 1),
// the finish stage's Blacks curve (`lightcraft_pipeline::tone::Blacks`)
("BLACKS", 1),
("BLACKS_T", 1),
("BLACKS_P", 1),
("BLACKS_A", 1),
// the default raw curve is applied per channel, a camera look to luminance (see ToneMap)
("PER_CHANNEL", 1),
("CLAR", 1),
("TEX", 1),
("DEHAZE", 1),
Expand Down Expand Up @@ -172,6 +179,11 @@ pub fn finish_block(fp: &FinishParams, masks: &[[f32; MASK_TERMS]], present: &Pr
p.f("AIR_PRE", fp.air_pre);
p.f("HL", fp.hl);
p.f("SH", fp.sh);
p.f("BLACKS_T", fp.blacks.black_point());
p.f("BLACKS_P", fp.blacks.power());
p.f("BLACKS_A", fp.blacks.share());
p.b("BLACKS", !fp.blacks.is_identity());
p.b("PER_CHANNEL", fp.tone.per_channel());
p.f("CLAR", fp.clar);
p.f("TEX", fp.tex);
p.f("DEHAZE", fp.dehaze);
Expand Down
2 changes: 1 addition & 1 deletion crates/gpu/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ pub fn render(
lap("masks", &mut t, &mut cx);

// 5. per-pixel stage
let mut fp = FinishParams::new(s, &plan.frame, info, w, h, plan.px_per_long, prep.air, req.space);
let mut fp = FinishParams::new(s, &plan.frame, info, w, h, plan.px_per_long, prep.air, plan.whites_scale, req.space);
if let Some(d) = &req.display {
fp.for_display(d, None);
}
Expand Down
Loading