Skip to content
Open
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
77 changes: 71 additions & 6 deletions crates/develop/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,71 @@ pub fn wb_matrix_for(info: &WbSource, s: &DevelopSettings) -> Option<[[f32; 3];

/// [`wb_matrix_for`] with an optional camera-space white-balance callback (`rebalance(target_xy)`).
pub fn wb_matrix_for_with(info: &WbSource, s: &DevelopSettings, rebalance: Option<&dyn Fn(Xy) -> Option<Mat3>>) -> Option<[[f32; 3]; 3]> {
wb_matrix_for_impl(info, s, rebalance, false)
}

/// White balance with the checked Bradford source white limited before a cone response reaches zero.
///
/// Opt in from a new rendering process so existing edits retain their white balance. Camera-space
/// rebalancing is unchanged; only the fallback adaptation needs a cone floor.
pub fn wb_matrix_for_with_smooth_tint(info: &WbSource, s: &DevelopSettings, rebalance: Option<&dyn Fn(Xy) -> Option<Mat3>>) -> Option<[[f32; 3]; 3]> {
wb_matrix_for_impl(info, s, rebalance, true)
}

fn wb_matrix_for_impl(
info: &WbSource,
s: &DevelopSettings,
rebalance: Option<&dyn Fn(Xy) -> Option<Mat3>>,
smooth_tint: bool,
) -> Option<[[f32; 3]; 3]> {
let (t, tint) = effective_wb(info, s);
if (t - info.as_shot_temp).abs() < 1e-6 && (tint - info.as_shot_tint).abs() < 1e-6 {
return None;
}
if let Some(m) = rebalance.filter(|_| info.raw && !info.relative_wb).and_then(|f| f(temp_tint_to_xy(t, tint))) {
return Some(m.to_f32());
}
let checked = checked_wb(s);
let set = if checked { wb_matrix_checked(&REC2020, effective_white(info, s)) } else { wb_matrix_legacy(&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_legacy(&REC2020, temp_tint_to_xy(info.as_shot_temp, info.as_shot_tint))
let matrix = |white, temperature| {
if smooth_tint {
wb_matrix_checked(&REC2020, bounded_wb_white(white, temperature))
} else if checked_wb(s) {
wb_matrix_checked(&REC2020, white)
} else {
wb_matrix_legacy(&REC2020, white)
}
};
let set = matrix(effective_white(info, s), t);
let shot = matrix(temp_tint_to_xy(info.as_shot_temp, info.as_shot_tint), info.as_shot_temp);
let m = set.mul(&shot.inverse().unwrap_or(Mat3::IDENTITY));
// Normalize so neutral luminance is preserved (WB shouldn't change exposure).
let g = m.apply([1.0, 1.0, 1.0]);
let y = g[0] * 0.2627 + g[1] * 0.6780 + g[2] * 0.0593;
Some(m.mul(&Mat3::diag(1.0 / y, 1.0 / y, 1.0 / y)).to_f32())
}

/// Keep each Bradford cone response at least 5% of the Rec.2020 white's response, or at the
/// untinted locus white's own response when it is lower. Interpolating in unit-luminance XYZ
/// changes each cone response linearly, so the exact limit needs no numerical search.
fn bounded_wb_white(white: Xy, temperature: f64) -> Xy {
let neutral = REC2020.white;
let locus = temp_tint_to_xy(temperature, 0.0);
let (Some(xyz), Some(anchor), Some(reference)) = (white.to_xyz(), locus.to_xyz(), neutral.to_xyz()) else {
return neutral;
};
let (requested, base, reference) = (BRADFORD.apply(xyz), BRADFORD.apply(anchor), BRADFORD.apply(reference));
if base.iter().any(|v| !v.is_finite() || *v <= 0.0) || requested.iter().any(|v| !v.is_finite()) {
return neutral;
}
let mut share = 1.0_f64;
for i in 0..3 {
let floor = (reference[i] * 0.05).min(base[i]);
if requested[i] < floor {
share = share.min((base[i] - floor) / (base[i] - requested[i]));
}
}
if share == 1.0 { white } else { Xy::from_xyz(std::array::from_fn(|i| anchor[i] + share * (xyz[i] - anchor[i]))).unwrap_or(neutral) }
}

fn wb_gain(img: &mut Rgb32f, info: &WbSource, s: &DevelopSettings, gain: f32) {
let m = wb_matrix_for(info, s);
let w = img.width;
Expand Down Expand Up @@ -1162,6 +1206,27 @@ mod tests {
assert!((luminance_2020(c) - 0.3).abs() < 0.01);
}

#[test]
fn smooth_tint_avoids_the_warm_green_bradford_jump() {
let info = WbSource { raw: true, as_shot_temp: 4919.0, as_shot_tint: -2.0, ..Default::default() };
let gains = |temp, tint, smooth| {
let mut s = DevelopSettings { process: craft_develop_model::ProcessVersion(10), ..Default::default() };
(s.wb.mode, s.wb.temp, s.wb.tint, s.wb.scale) = (WbMode::Custom, temp, tint, WbScale::Kelvin);
let matrix = wb_matrix_for_impl(&info, &s, None, smooth).unwrap();
matrix.map(|row| row.iter().sum::<f32>())
};
assert_eq!(gains(5200.0, 15.0, true), gains(5200.0, 15.0, false), "ordinary whites are unchanged");
for points in
[(0..=300).map(|i| (2000.0, f64::from(i) * 0.5)).collect::<Vec<_>>(), (0..=240).map(|i| (3300.0 + f64::from(i) * 5.0, 150.0)).collect()]
{
for w in points.windows(2) {
let (a, b) = (gains(w[0].0, w[0].1, true), gains(w[1].0, w[1].1, true));
assert!(a.into_iter().chain(b).all(|v| v.is_finite() && v > 0.0), "{w:?}: {a:?} -> {b:?}");
assert!(a.into_iter().zip(b).all(|(a, b)| (b / a).ln().abs() < 0.05), "{w:?}: {a:?} -> {b:?}");
}
}
}

#[test]
fn process_v10_uses_checked_wb() {
let mut s = DevelopSettings::default();
Expand Down
Loading