Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 6 additions & 2 deletions crates/develop/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,23 @@ pub enum Process {
/// camera curve keeps V2 Whites and fitted pointwise operators follow it
/// (`lightcraft_pipeline::light_v3`, `docs/light-v3.md`).
V4,
/// V4, with a raw's tone curve rolling off into white in stops past its last fitted knot, so
/// highlights the sensor recorded keep their detail when Exposure lifts them (issue #1632).
V5,
}

impl Process {
/// Every process, oldest first.
pub const ALL: [Process; 4] = [Process::V1, Process::V2, Process::V3, Process::V4];
pub const LATEST: Process = Process::V4;
pub const ALL: [Process; 5] = [Process::V1, Process::V2, Process::V3, Process::V4, Process::V5];
pub const LATEST: Process = Process::V5;

pub const fn version(self) -> ProcessVersion {
match self {
Process::V1 => ProcessVersion(1),
Process::V2 => ProcessVersion(2),
Process::V3 => ProcessVersion(3),
Process::V4 => ProcessVersion(4),
Process::V5 => ProcessVersion(5),
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion crates/engine/src/camera_preview/look_fit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ pub(super) fn dro_off(own_tone: &CameraTone) -> CameraTone {
// Past its last knot a curve goes on as a shoulder whose rate comes from its last two knots
// (`CameraTone::apply`): lowering the second-last knot more than the last steepens it, and the
// extended highlights would end up above the fitted curve. Raise the second-last knot (never
// above the fitted curve) until the rate is at most the fitted curve's.
// above the fitted curve) until the rate is at most the fitted curve's. Process V5's shoulder
// (`CameraTone::apply_rolloff`) is not part of this bound or of `at_most`: V1 to V4 read the
// same curve, and a V5 bound here would change their renders. It stays below the fitted
// curve's too when the last segment's slope over the squared gap to white is at most the fitted
// curve's; a lowered curve with a nearly flat last segment can end up above it far past the last
// knot under V5 (issue #1632).
let ([a, b], last) = ([own[30], own[31]], knots[31]);
let rate = ((b[1] - a[1]) / (b[0] - a[0])).clamp(0.1, 16.0) / (1.0 - b[1]).max(0.01);
let floor = last[1] - rate * (1.0 - last[1]).max(0.01) * (b[0] - a[0]);
Expand Down
26 changes: 26 additions & 0 deletions crates/engine/src/camera_preview/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,30 @@ fn assert_lowered(fitted: &CameraLook, fixed: &CameraLook, what: &str) {
let x = 0.001 * 1.005f32.powi(i);
assert!(fixed.tone.apply(x) <= fitted.tone.apply(x) + 1e-5, "{what} at {x}: {} > {}", fixed.tone.apply(x), fitted.tone.apply(x));
}
// V5's shoulder stays below too whenever the lowered last segment's slope over its squared gap to
// white is at most the fitted one's (`dro_off` doesn't enforce it, so V1 to V4 keep their curve;
// issue #1632)
if v5_bounded(&fixed.tone, &fitted.tone) {
assert!(v5_below(&fixed.tone, &fitted.tone), "{what}: brighter than the fitted curve under V5");
}
}

/// Whether the last segment of `a`, over its squared gap to white, is at most `b`'s: then `a`'s V5
/// shoulder ([`CameraTone::apply_rolloff`]) is nowhere above `b`'s (issue #1632).
fn v5_bounded(a: &CameraTone, b: &CameraTone) -> bool {
let rate = |t: &CameraTone| {
let k = t.knots();
((k[31][1] - k[30][1]) / (k[31][0] - k[30][0])).max(0.0) / (1.0 - k[31][1]).powi(2)
};
rate(a) <= rate(b)
}

/// Whether `a`'s V5 curve is nowhere brighter than `b`'s, from deep shadows to far past the last knot.
fn v5_below(a: &CameraTone, b: &CameraTone) -> bool {
(0..=2400).all(|i| {
let x = 0.001 * 1.005f32.powi(i);
a.apply_rolloff(x) <= b.apply_rolloff(x) + 1e-5
})
}

/// Issue #244: Sony's Dynamic Range Optimizer lifts the camera JPEG's shadows, not the raw's. A
Expand Down Expand Up @@ -1423,6 +1447,8 @@ fn corpus_arw_with_dro_is_lowered_to_the_dro_off_tone() {
let fitted = fit_look(&sensor, &reference, &clipped, colour).unwrap();
if dro {
assert_lowered(&fitted, &look, name);
// the Sony DRO samples' lowered curves stay below their fitted ones under V5 as well
assert!(v5_below(&look.tone, &fitted.tone), "{name}: brighter than the fitted curve under V5");
for x in lowered {
assert!(look.tone.apply(x) < 0.95 * fitted.tone.apply(x), "{name} at {x}: {} vs {}", look.tone.apply(x), fitted.tone.apply(x));
}
Expand Down
32 changes: 32 additions & 0 deletions crates/engine/src/files/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,3 +1844,35 @@ fn existing_photo_settings_are_not_rewritten() {
drop(s);
let _ = std::fs::remove_dir_all(&dir);
}

/// Issue #1632: a DNG's `ProfileToneCurve` is the finish stage's camera curve, so process V5 extends
/// it past its last point (the sensor's white, scene 1.0) with the stop roll-off as well. A curve
/// that reaches white there renders as under V4 up to it and white past it; one that ends below
/// white (here 0.9) keeps what Exposure lifts past the sensor's white apart instead of reaching
/// 8-bit 255 within a stop.
#[test]
fn v5_extends_a_dng_tone_curve_past_white_in_stops() {
use lightcraft_color::transfer::linear_to_srgb;
use lightcraft_develop::{DevelopSettings, Process};
use lightcraft_pipeline::SourceInfo;
let code = |curve: &[f64], process: Process, y: f32| {
let tone = lightcraft_raw::profile::ToneCurve::from_tag(curve).and_then(|c| super::dng_tone_curve(&c));
let info = SourceInfo { raw: true, camera_tone: Some(tone.expect("a valid curve")), ..SourceInfo::default() };
let s = DevelopSettings { process: process.version(), ..DevelopSettings::default() };
let o = lightcraft_pipeline::finish::base_tone(&s, &info, 1.0).apply(y);
(linear_to_srgb(o.clamp(0.0, 1.0)) * 255.0).round()
};
let to_white = [0.0, 0.0, 0.03, 0.09, 0.18, 0.4, 0.5, 0.78, 1.0, 1.0];
for y in [0.01, 0.05, 0.18, 0.5, 0.9] {
assert_eq!(code(&to_white, Process::V5, y), code(&to_white, Process::V4, y), "{y}");
}
for y in [1.0, 2.0, 4.0] {
assert_eq!((code(&to_white, Process::V4, y), code(&to_white, Process::V5, y)), (255.0, 255.0), "{y}");
}
let below_white = [0.0, 0.0, 0.03, 0.09, 0.18, 0.4, 0.5, 0.72, 1.0, 0.9];
let at = |process, ev: f32| code(&below_white, process, ev.exp2());
assert_eq!(at(Process::V5, 0.0), at(Process::V4, 0.0));
assert_eq!(at(Process::V4, 1.0), 255.0, "V4 is frozen");
let (white, beyond) = (at(Process::V5, 0.0), at(Process::V5, 1.0));
assert!(beyond < 254.0 && beyond - white >= 3.0, "{white} then {beyond} a stop past the sensor's white");
}
11 changes: 8 additions & 3 deletions crates/gpu/tests/it/equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ fn camera_tone_and_relative_wb() {
s.wb.mode = WbMode::Custom;
s.wb.temp = 8000.0;
check("camera tone edited", &src, &info, &s, &RenderRequest::fit(320, 240));
// each process's shoulder past the last knot, with highlights lifted into it (issue #1632)
for p in Process::ALL {
let mut lifted = DevelopSettings { process: p.version(), ..DevelopSettings::default() };
lifted.light.exposure = 2.0;
check(&format!("camera tone {p:?} exposure +2"), &src, &info, &lifted, &RenderRequest::fit(320, 240));
}
// a camera chroma curve: richer shadows, highlights bleached toward white
let curve = curve.with_chroma([1.4, 1.3, 1.1, 1.0, 0.7, 0.4, 0.25, 0.2]).unwrap();
let info = SourceInfo { camera_tone: Some(curve), ..info };
Expand Down Expand Up @@ -709,8 +715,7 @@ fn light_v3_operators_match() {
let req = RenderRequest::fit(720, 720);
for id in ["light.contrast", "light.whites", "light.blacks"] {
for v in [-100.0, -50.0, 50.0, 100.0] {
let mut s = DevelopSettings::default();
assert_eq!(s.process.process(), Process::V4);
let mut s = DevelopSettings { process: Process::V4.version(), ..Default::default() };
lightcraft_develop::controls::set(&mut s, id, v);
check(&format!("V4 {id} {v}"), &src, &info, &s, &req);
if id == "light.whites" {
Expand All @@ -723,7 +728,7 @@ fn light_v3_operators_match() {
}
}
}
let mut s = DevelopSettings::default();
let mut s = DevelopSettings { process: Process::V4.version(), ..Default::default() };
typical(&mut s);
s.light.contrast = 35.0;
s.light.whites = 40.0;
Expand Down
7 changes: 7 additions & 0 deletions crates/pipeline/src/finish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ pub fn base_tone(s: &DevelopSettings, info: &SourceInfo, peak: f32) -> ToneMap {
None if info.raw => ToneMap::hdr(l.contrast, l.whites, l.blacks, peak),
None => ToneMap::display_hdr(l.contrast, l.whites, l.blacks, peak),
},
// V4, with a raw's tone curve rolling off into white in stops: a camera curve past its last
// knot, the generic curve from half a stop below its white point instead of clipping (issue #1632)
Process::V5 => match info.camera_tone.as_ref().filter(|_| info.raw) {
Some(curve) => ToneMap::camera_rolloff_hdr(curve, 0.0, l.whites, 0.0, peak),
None if info.raw => ToneMap::rolloff_hdr(l.contrast, l.whites, l.blacks, peak),
None => ToneMap::display_hdr(l.contrast, l.whites, l.blacks, peak),
},
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/pipeline/src/light_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,13 @@ pub struct LightOps {
/// The fits of Contrast and Blacks, in the order they apply.
pub const FITS: [&Fit; 2] = [&fitted::CONTRAST, &fitted::BLACKS];

/// Whether `s` renders `info` with the V4 operators: process V4 and a raw with its camera's tone
/// Whether `s` renders `info` with the V4 operators: process V4 or later and a raw with its camera's tone
/// curve (other sources keep the tone map's own Contrast and Blacks).
pub fn applies(s: &DevelopSettings, info: &SourceInfo) -> bool {
let camera_look = info.raw && info.camera_tone.is_some();
match s.process.process() {
Process::V1 | Process::V2 | Process::V3 => false,
Process::V4 => camera_look,
Process::V4 | Process::V5 => camera_look,
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/pipeline/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ pub fn base_detail_radius(px_per_long: f64) -> usize {
pub fn tone_base_detail(s: &DevelopSettings, px_per_long: f64) -> usize {
match s.process.process() {
Process::V1 => 0,
Process::V2 | Process::V3 | Process::V4 => base_detail_radius(px_per_long),
Process::V2 | Process::V3 | Process::V4 | Process::V5 => base_detail_radius(px_per_long),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/pipeline/src/tests_light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ fn v3_sliders_move_a_grey_ramp_their_way() {
let ramp = Rgb32f::from_fn(256, 8, |x, _| [0.0005 * 1.035f32.powi(x as i32); 3]);
let shot = |id: &str, v: f64| {
let mut s = DevelopSettings::default();
assert_eq!(s.process.process(), Process::V4);
assert!(s.process.process() >= Process::V4, "the default process has the V4 sliders");
controls::set(&mut s, id, v);
let r = render(&ramp, &camera_look(), &s, &RenderRequest::fit(256, 8)).image;
(0..256).map(|x| r.get(x, 4)).collect::<Vec<[u8; 4]>>()
Expand Down
64 changes: 64 additions & 0 deletions crates/pipeline/src/tests_tone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,67 @@ fn the_base_plane_is_cached_per_process() {
assert_ne!(local::base_key(&s1, sg, 0), local::base_key(&s2, sg, 0), "the process is part of the key");
assert_ne!(local::base_key(&s2, sg, 0), local::base_key(&s2, sg, sig.base_detail));
}

/// Issue #1632: past its last fitted knot (here 1.0 stop below the sensor's white, scene 1.0, at
/// 8-bit 233, as on the CC0 ILCE-7CR file) a raw's camera tone curve closed on white exponentially
/// in linear light, so Exposure +1 rendered the sensor's white at 255 and Exposure +2 left the top
/// stop it recorded (scene 0.5..1.0) under one 8-bit code. The latest process rolls off in stops
/// and keeps both apart; V1 to V4 keep their look.
#[test]
fn exposure_keeps_the_highlights_a_camera_curve_recorded() {
use lightcraft_color::transfer::linear_to_srgb;
let knots = std::array::from_fn(|i| {
let x = 0.003 * 1.18f32.powi(i as i32);
[x, 1.0 - (-3.3 * x).exp()]
});
let info = SourceInfo { raw: true, camera_tone: crate::tone::CameraTone::new(knots), ..SourceInfo::default() };
let at = |process: Process, ev: f32, y: f32| {
let o = crate::finish::base_tone(&on(process), &info, 1.0).apply(y * ev.exp2());
(linear_to_srgb(o.clamp(0.0, 1.0)) * 255.0).round()
};
for ev in [0.0, 1.0, 2.0] {
let (white, half) = (at(Process::LATEST, ev, 1.0), at(Process::LATEST, ev, 0.5));
assert!(white <= 254.0, "Exposure {ev:+}: the sensor's white renders at {white}");
assert!(white - half >= 1.5, "Exposure {ev:+}: the top stop spans {half}..{white}");
}
// the earlier processes are frozen: the sensor's white at 255 from Exposure +1
for p in [Process::V1, Process::V2, Process::V3, Process::V4] {
assert_eq!(at(p, 1.0, 1.0), 255.0, "{p:?}");
}
// up to the last knot every process renders the camera's curve
for y in [0.003, 0.02, 0.18, 0.4, 0.5] {
let (v2, v3) = (crate::finish::base_tone(&on(Process::V2), &info, 1.0), crate::finish::base_tone(&on(Process::LATEST), &info, 1.0));
assert!((v2.apply(y) - v3.apply(y)).abs() < 1e-6, "{y}");
}
}

/// Issue #1632: a raw without a camera curve takes the generic curve, which reached white about ¼
/// stop past the sensor's white (scene 1.0) and clipped there, so Exposure +1 rendered the top stop
/// the sensor recorded (scene 0.5..1.0) at 8-bit 255 from its upper half. The latest process rolls
/// off in stops from half a stop below the white point; V1 to V4 keep their look.
#[test]
fn exposure_keeps_the_highlights_the_generic_curve_recorded() {
use lightcraft_color::transfer::linear_to_srgb;
let info = SourceInfo { raw: true, ..SourceInfo::default() };
let at = |process: Process, ev: f32, y: f32| {
let o = crate::finish::base_tone(&on(process), &info, 1.0).apply(y * ev.exp2());
(linear_to_srgb(o.clamp(0.0, 1.0)) * 255.0).round()
};
for ev in [0.0, 1.0, 2.0] {
let (white, half) = (at(Process::LATEST, ev, 1.0), at(Process::LATEST, ev, 0.5));
assert!(white <= 253.0, "Exposure {ev:+}: the sensor's white renders at {white}");
assert!(white - half >= 3.0, "Exposure {ev:+}: the top stop spans {half}..{white}");
}
// the curve still reaches white, 5 stops past the sensor's
assert_eq!(at(Process::LATEST, 5.0, 1.0), 255.0);
// the earlier processes are frozen: the sensor's white at 255 from Exposure +1
for p in [Process::V1, Process::V2, Process::V3, Process::V4] {
assert_eq!(at(p, 1.0, 1.0), 255.0, "{p:?}");
}
assert_eq!(at(Process::V2, 0.0, 1.0), at(Process::V1, 0.0, 1.0));
// below the knee every process renders the same curve
let (v2, v3) = (crate::finish::base_tone(&on(Process::V2), &info, 1.0), crate::finish::base_tone(&on(Process::LATEST), &info, 1.0));
for y in [0.003, 0.02, 0.18, 0.4, 0.6] {
assert_eq!(v2.apply(y), v3.apply(y), "{y}");
}
}
Loading
Loading