Skip to content
32 changes: 30 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-l
# Canon CRW decoder shared with the other Craft apps
craft-crw = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" }
craft-cms = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3" }
craft-color-transform = { git = "https://github.com/storytold/craft-libs", rev = "d7fc79387dadb8bd666e0ae4fbfc4b3fc6710373" }
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3", version = "0.1.0" }
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3", features = ["frames"] }
craft-jxl = { git = "https://github.com/storytold/craft-libs", rev = "23acc704933a9a5619a5cd1cddc9bb1fb5a9b2d3", default-features = false, features = ["decode", "encode"] }
Expand Down
1 change: 1 addition & 0 deletions crates/color/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description = "LightCraft color (L0)"
workspace = true

[dependencies]
craft-color-transform = { workspace = true }
serde = { workspace = true }
lightcraft-geom = { workspace = true }

Expand Down
14 changes: 14 additions & 0 deletions crates/color/src/cct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ pub fn wb_matrix(space: &RgbSpace, src: Xy) -> Mat3 {
space.from_xyz().mul(&adapt).mul(&space.to_xyz())
}

/// Checked Bradford white-balance matrix from `craft_color_transform::cct::wb_matrix`:
/// returns `Mat3::IDENTITY` adaptation when the source illuminant's Bradford blue cone response
/// is `<= 0.0` (warm, strongly green illuminants).
pub fn wb_matrix_checked(space: &RgbSpace, src: Xy) -> Mat3 {
let shared_space = craft_color_transform::RgbSpace {
r: craft_color_transform::Xy::new(space.r.x, space.r.y),
g: craft_color_transform::Xy::new(space.g.x, space.g.y),
b: craft_color_transform::Xy::new(space.b.x, space.b.y),
white: craft_color_transform::Xy::new(space.white.x, space.white.y),
};
let m = craft_color_transform::cct::wb_matrix(&shared_space, craft_color_transform::Xy::new(src.x, src.y));
Mat3(m.0)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
9 changes: 7 additions & 2 deletions crates/develop/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ pub enum Process {
/// V7, with the Highlights/Shadows base taken of log luminance without its fine detail (clamped
/// between a fine and a 3× wider median), so gaps in dark lettering aren't lifted into bright blocks.
V8,
/// V8, with the checked Bradford white-balance fallback (`Mat3::IDENTITY` when a source
/// illuminant's Bradford blue cone response is `<= 0.0`, `craft-libs#587`).
V9,
}

impl Process {
/// Every process, oldest first.
pub const ALL: [Process; 8] = [Process::V1, Process::V2, Process::V3, Process::V4, Process::V5, Process::V6, Process::V7, Process::V8];
pub const LATEST: Process = Process::V8;
pub const ALL: [Process; 9] =
[Process::V1, Process::V2, Process::V3, Process::V4, Process::V5, Process::V6, Process::V7, Process::V8, Process::V9];
pub const LATEST: Process = Process::V9;

pub const fn version(self) -> ProcessVersion {
match self {
Expand All @@ -119,6 +123,7 @@ impl Process {
Process::V6 => ProcessVersion(6),
Process::V7 => ProcessVersion(7),
Process::V8 => ProcessVersion(8),
Process::V9 => ProcessVersion(9),
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions crates/gpu/tests/it/equivalence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ fn v3_luminance_cutoff_chroma_4() {
check_v3_luminance_cutoff(4.0, [0.035_338_085, 0.000_048_468_723, 0.010_193_758], [47, 0, 19, 255]);
}

#[test]
fn warm_green_wb_zero_crossing_gpu_matches_cpu_on_v9() {
if !gpu() {
return;
}
let src = scene(2, 160, 120);
let info = SourceInfo { as_shot_temp: 5500.0, as_shot_tint: 0.0, ..Default::default() };
let req = RenderRequest::fit(160, 120);
// Test across the Bradford blue-cone s[2] <= 0.0 zero crossing on Process::V9:
// (2000 K, 0) has s[2] > 0; (2000 K, 30) and (3000 K, 100) have s[2] <= 0.
for (temp, tint) in [(2000.0, 0.0), (2000.0, 30.0), (3000.0, 100.0)] {
let mut s = DevelopSettings::default();
assert_eq!(s.process.process(), Process::V9);
(s.wb.mode, s.wb.temp, s.wb.tint) = (WbMode::Custom, temp, tint);
let cpu = render(&src, &info, &s, &req).image;
let gpu = lightcraft_gpu::try_render(&src, &info, &s, &req, None).expect("GPU render without CPU fallback").image;
let (mean, max, _) = diff(&cpu, &gpu);
assert!(mean < MEAN_LSB && max <= MAX_LSB, "{temp} K tint {tint}: mean {mean:.3}, max {max}");
}
}

#[test]
fn camera_tone_and_relative_wb() {
if !gpu() {
Expand Down
6 changes: 3 additions & 3 deletions crates/pipeline/src/finish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ impl GrainModel {
seed: g.seed,
})
}),
Process::V7 | Process::V8 if draft => crate::crystal::CrystalGrain::draft(g, px_per_long).map(GrainModel::Crystal),
Process::V7 | Process::V8 => crate::crystal::CrystalGrain::new(g, px_per_long).map(GrainModel::Crystal),
Process::V7 | Process::V8 | Process::V9 if draft => crate::crystal::CrystalGrain::draft(g, px_per_long).map(GrainModel::Crystal),
Process::V7 | Process::V8 | Process::V9 => crate::crystal::CrystalGrain::new(g, px_per_long).map(GrainModel::Crystal),
}
}
}
Expand Down Expand Up @@ -407,7 +407,7 @@ pub fn base_tone(s: &DevelopSettings, info: &SourceInfo, peak: f32) -> ToneMap {
},
// 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 | Process::V6 | Process::V7 | Process::V8 => match info.camera_tone.as_ref().filter(|_| info.raw) {
Process::V5 | Process::V6 | Process::V7 | Process::V8 | Process::V9 => 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
2 changes: 1 addition & 1 deletion crates/pipeline/src/light_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ 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 | Process::V5 | Process::V6 | Process::V7 | Process::V8 => camera_look,
Process::V4 | Process::V5 | Process::V6 | Process::V7 | Process::V8 | Process::V9 => camera_look,
}
}

Expand Down
15 changes: 11 additions & 4 deletions crates/pipeline/src/local.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Scene-linear preparation: white balance + exposure, and the spatial planes the per-pixel stage
//! needs (edge-aware base layer for highlights/shadows, clarity/texture bands, dehaze veil).

use lightcraft_color::cct::{temp_tint_to_xy, wb_matrix, xy_to_temp_tint};
use lightcraft_color::cct::{temp_tint_to_xy, wb_matrix, wb_matrix_checked, xy_to_temp_tint};
use lightcraft_color::{REC2020, luminance_2020};
use lightcraft_develop::{DevelopSettings, Process};
use lightcraft_raster::blur::{gauss_taps, gaussian, gaussian_taps};
Expand Down Expand Up @@ -37,8 +37,13 @@ pub fn wb_matrix_for(info: &SourceInfo, s: &DevelopSettings) -> Option<[[f32; 3]
{
return Some(m.to_f32());
}
let set = wb_matrix(&REC2020, effective_white(info, s));
let shot = wb_matrix(&REC2020, temp_tint_to_xy(info.as_shot_temp, info.as_shot_tint));
let checked = s.process.process() >= Process::V9;
let set = if checked { wb_matrix_checked(&REC2020, effective_white(info, s)) } else { wb_matrix(&REC2020, effective_white(info, s)) };
let shot = if checked {
wb_matrix_checked(&REC2020, temp_tint_to_xy(info.as_shot_temp, info.as_shot_tint))
} else {
wb_matrix(&REC2020, temp_tint_to_xy(info.as_shot_temp, info.as_shot_tint))
};
let m = set.mul(&shot.inverse().unwrap_or(lightcraft_color::Mat3::IDENTITY));
// Normalize so neutral luminance is preserved (WB shouldn't change exposure).
let g = m.apply([1.0, 1.0, 1.0]);
Expand Down Expand Up @@ -375,7 +380,9 @@ 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 | Process::V5 | Process::V6 | Process::V7 | Process::V8 => base_detail_radius(px_per_long),
Process::V2 | Process::V3 | Process::V4 | Process::V5 | Process::V6 | Process::V7 | Process::V8 | Process::V9 => {
base_detail_radius(px_per_long)
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions crates/pipeline/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,48 @@ fn custom_white_balance_redevelops_in_camera_space() {
assert!(wb_matrix_for(&info, &s).is_none());
}

#[test]
fn saved_warm_green_wb_preserves_legacy_bradford_while_v9_uses_checked_fallback() {
use crate::local::wb_matrix_for;
use lightcraft_color::REC2020;
use lightcraft_color::cct::{temp_tint_to_xy, wb_matrix, wb_matrix_checked};
use lightcraft_develop::{Process, ProcessVersion, WbMode};

let info = SourceInfo { as_shot_temp: 5500.0, as_shot_tint: 0.0, ..Default::default() };
// Saved V8 edit in the reachable warm-green region (Bradford blue cone response <= 0.0)
// must reopen without upgrading its process version and must keep the legacy Bradford result.
let saved_json = serde_json::json!({
"process": 8,
"wb": { "mode": "Custom", "temp": 2000.0, "tint": 30.0 }
});
let saved = DevelopSettings::from_json(&saved_json).unwrap();
assert_eq!(saved.process, ProcessVersion(8));
assert_eq!(saved.process.process(), Process::V8);
let legacy_m = wb_matrix_for(&info, &saved).unwrap();

// New V9 edit at the same warm-green WB uses the shared checked Mat3::IDENTITY fallback.
let mut v9 = DevelopSettings::default();
assert_eq!(v9.process.process(), Process::V9);
(v9.wb.mode, v9.wb.temp, v9.wb.tint) = (WbMode::Custom, 2000.0, 30.0);
let v9_m = wb_matrix_for(&info, &v9).unwrap();

assert_ne!(legacy_m, v9_m, "saved V8 edit must not silently adopt V9's checked fallback");

// Verify exact correspondence to legacy wb_matrix (V8) vs checked wb_matrix_checked (V9).
let expected_shot = wb_matrix(&REC2020, temp_tint_to_xy(5500.0, 0.0)).inverse().unwrap();
let legacy_raw = wb_matrix(&REC2020, temp_tint_to_xy(2000.0, 30.0)).mul(&expected_shot);
let legacy_g = legacy_raw.apply([1.0, 1.0, 1.0]);
let legacy_y = legacy_g[0] * 0.2627 + legacy_g[1] * 0.6780 + legacy_g[2] * 0.0593;
let legacy_expected = legacy_raw.mul(&lightcraft_color::Mat3::diag(1.0 / legacy_y, 1.0 / legacy_y, 1.0 / legacy_y)).to_f32();
assert_eq!(legacy_m, legacy_expected);

let v9_raw = wb_matrix_checked(&REC2020, temp_tint_to_xy(2000.0, 30.0)).mul(&expected_shot);
let v9_g = v9_raw.apply([1.0, 1.0, 1.0]);
let v9_y = v9_g[0] * 0.2627 + v9_g[1] * 0.6780 + v9_g[2] * 0.0593;
let v9_expected = v9_raw.mul(&lightcraft_color::Mat3::diag(1.0 / v9_y, 1.0 / v9_y, 1.0 / v9_y)).to_f32();
assert_eq!(v9_m, v9_expected);
}

/// Every stored process number renders with a process this build knows: settings saved before
/// process versions existed exactly as V1, and a number from a newer LightCraft exactly as the
/// latest process here (`docs/process-versions.md`). Raw (base tone curve), camera-tone and
Expand Down
2 changes: 1 addition & 1 deletion crates/pipeline/src/tests_grain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn processes_before_v7_keep_the_value_noise() {
Process::V1 | Process::V2 | Process::V3 | Process::V4 | Process::V5 | Process::V6 => {
assert!(matches!(model, Some(GrainModel::Value(_))), "{p:?}")
}
Process::V7 | Process::V8 => assert!(matches!(model, Some(GrainModel::Crystal(_))), "{p:?}"),
Process::V7 | Process::V8 | Process::V9 => assert!(matches!(model, Some(GrainModel::Crystal(_))), "{p:?}"),
}
assert!(GrainModel::new(&on(p, 0.0), 2000.0, false).is_none(), "{p:?}: no grain at Amount 0");
}
Expand Down
Loading