mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
fix(algo): HDR Toning stays finite when its tone overflows f32 (#14723)
A bright pixel between dark ones with a large Detail and a low Gamma toned past f32 before the shadow/highlight cubic, which then gave 0 * inf = NaN. The brightest-base anchor made that easier to reach. The direct path now takes the checked replay's wide-range evaluation, shared as wide_range_tone, so both paths give the same finite colour.
This commit is contained in:
1 parent
d1bf7129be
commit
79335e9df9
3 files changed
+32
-8
No files matched your search
@@ -250,6 +250,19 @@ fn curve_eval(pts: &[(f32, f32)], x: f32) -> f32 {
|
||||
pts[pts.len() - 1].1
|
||||
}
|
||||
|
||||
/// The tone of a sample whose f32 exposure, gamma and shadow/highlight arithmetic overflowed (a
|
||||
/// low Gamma or a strong local contrast): the same curve evaluated in f64. A cap beyond every
|
||||
/// control's polynomial crossover keeps cubing finite; those values saturate at the display
|
||||
/// clamp. Both HDR Toning paths use it, so they stay equal.
|
||||
pub(crate) fn wide_range_tone(new_log: f32, exposure: f32, gamma: f32, shadow: f32, highlight: f32) -> f32 {
|
||||
let log_value = (f64::from(new_log) + f64::from(exposure) * std::f64::consts::LN_2) / f64::from(gamma);
|
||||
let value = log_value.exp().min(1e100);
|
||||
let shadow = f64::from(shadow) / 200.;
|
||||
let highlight = f64::from(highlight) / 200.;
|
||||
let mapped = ((-(shadow + highlight) * value + 3. * shadow) * value + (1. - 3. * shadow)) * value + shadow;
|
||||
mapped.clamp(0., 1.) as f32
|
||||
}
|
||||
|
||||
/// Local adaptation tone mapping (Durand–Dorsey style base/detail split in log luminance): the
|
||||
/// blurred log luminance (radius) is the base layer, compressed by `strength` toward its brightest
|
||||
/// value, which becomes white, so an ordinary image is stretched to the full range and its dark
|
||||
@@ -292,6 +305,9 @@ pub fn hdr_toning(px: &mut [[f32; 4]], w: usize, h: usize, p: &HdrToning) {
|
||||
// the exponent, matching the crate's other gamma controls (hdr.rs, proof_sim.rs).
|
||||
let mut l1 = (new_log.exp() * exposure).max(0.0).powf(1.0 / gamma);
|
||||
l1 += p.shadow / 100.0 * (1.0 - l1).powi(3) * 0.5 - p.highlight / 100.0 * l1.powi(3) * 0.5;
|
||||
if !l1.is_finite() {
|
||||
l1 = wide_range_tone(new_log, p.exposure, gamma, p.shadow, p.highlight);
|
||||
}
|
||||
l1 = curve_eval(&sorted, l1.clamp(0.0, 1.0));
|
||||
let ratio = l1 / l0;
|
||||
let mut out = c.map(|v| v * ratio);
|
||||
|
||||
@@ -205,14 +205,8 @@ pub fn process_with(px: &mut [[f32; 4]], w: usize, h: usize, p: &HdrToning, ctl:
|
||||
l1 += p.shadow / 100. * (1. - l1).powi(3) * 0.5 - p.highlight / 100. * l1.powi(3) * 0.5;
|
||||
if !l1.is_finite() {
|
||||
// Preserve ordinary native arithmetic, but evaluate extreme valid HDR in a
|
||||
// wider domain. A cap beyond every f32 control's polynomial crossover keeps
|
||||
// cubing finite; those values already saturate at the final display clamp.
|
||||
let log_value = (f64::from(new_log) + f64::from(p.exposure) * std::f64::consts::LN_2) / f64::from(p.gamma);
|
||||
let value = log_value.exp().min(1e100);
|
||||
let shadow = f64::from(p.shadow) / 200.;
|
||||
let highlight = f64::from(p.highlight) / 200.;
|
||||
let mapped = ((-(shadow + highlight) * value + 3. * shadow) * value + (1. - 3. * shadow)) * value + shadow;
|
||||
l1 = mapped.clamp(0., 1.) as f32;
|
||||
// wider domain.
|
||||
l1 = super::wide_range_tone(new_log, p.exposure, p.gamma, p.shadow, p.highlight);
|
||||
if !l1.is_finite() {
|
||||
return Err(Error::NonFiniteSample);
|
||||
}
|
||||
|
||||
@@ -149,3 +149,17 @@ fn merge_to_hdr_local_adaptation_reaches_the_reference_from_the_reference_radian
|
||||
assert!((got - want).abs() <= 0.08 * 65535.0, "{name}: {got:.0}, the reference gives {want}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_strong_local_contrast_at_a_low_gamma_stays_finite() {
|
||||
// A bright pixel between dark ones with Detail 300 and Gamma 0.1 tones far past f32 before the
|
||||
// shadow and highlight curve; the result is still a colour, and the checked replay agrees.
|
||||
let p = HdrToning { radius: 100.0, strength: 0.1, gamma: 0.1, detail: 300.0, saturation: 0.0, ..Default::default() };
|
||||
let input = vec![[0.01, 0.01, 0.01, 1.0], [0.9, 0.9, 0.9, 1.0], [0.01, 0.01, 0.01, 1.0]];
|
||||
let mut px = input.clone();
|
||||
hdr_toning(&mut px, 3, 1, &p);
|
||||
assert!(px.iter().all(|q| q.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v))), "{px:?}");
|
||||
let mut checked = input;
|
||||
checked_hdr::process_with(&mut checked, 3, 1, &p, &photocraft_raster::Interrupt::NONE).unwrap();
|
||||
assert!(px.iter().flatten().zip(checked.iter().flatten()).all(|(a, b)| (a - b).abs() < 1e-6), "{px:?} vs {checked:?}");
|
||||
}
|
||||
Reference in new issue
Block a user