Dodge: tone a 32-bit document's linear values with the reference's curve

A 32-bit RGB document holds linear values, and Dodge ran them through the
8-bit gamma tone curve with Protect Tones' luma scaling, so one default click
was far too strong (grey 128 -> 152, red 200,40,40 -> 236,49,49 read as
8-bit) and gamut compression clipped light above white to 1.0. Midtones Dodge
with Protect Tones in 32-bit RGB now uses its own per-channel curve,
v + 1.34*e*v^2*(1-v)^1.5, fitted to the reference's measurements, which leaves
the shadows alone and values outside 0..1 untouched. 8- and 16-bit results
are pinned unchanged. Fixes #12687.
This commit is contained in:
qvest-ssels committed 2026-11-22 20:48:27 +01:00
1 parent 406ac677e1
commit d35d05dacc
2 files changed
+146

No files matched your search

+28
View File
@@ -185,6 +185,24 @@ fn tone_stroke(fmt: PixelFormat, toned: impl Fn([f32; 3]) -> [f32; 3] + Send + '
})
}
/// Strength of [`linear_midtones`] per unit of Exposure.
const LINEAR_MIDTONES_STRENGTH: f32 = 1.34;
/// The Midtones Dodge (`burn = false`) or Burn curve for one linear value of a 32-bit document at
/// exposure `e` (0..1): `v ± 1.34·e·v²·(1 − v)^1.5`, each channel on its own. It is fitted to the
/// reference's 32-bit results (#12687), which tone the stored linear values: much gentler on the
/// mid tones than the 8-bit curve on gamma-encoded values, and the shadows are left alone. Black
/// and white stay put, values outside 0..1 (light above white) are kept as they are, and tones
/// stay in order at every exposure.
#[inline]
fn linear_midtones(v: f32, e: f32, burn: bool) -> f32 {
if !(v > 0.0 && v < 1.0) {
return v;
}
let d = LINEAR_MIDTONES_STRENGTH * e.clamp(0.0, 1.0) * v * v * (1.0 - v).powf(1.5);
if burn { v - d } else { v + d }
}
fn tone_range(p: &Value, cmd: &str) -> Result<ToneRange> {
match string(p, "range", "midtones") {
"shadows" => Ok(ToneRange::Shadows),
@@ -305,6 +323,12 @@ impl DabTool {
pub(super) fn effect(&self, fmt: PixelFormat, spacing: f32) -> DabEffect {
let sp = spacing;
match self.clone() {
// A 32-bit RGB document holds linear values, which the reference's default Dodge tones
// with its own curve. Grayscale, the other ranges and Protect Tones off weren't measured
// there and keep the gamma curve.
Self::Tone { burn: false, range: ToneRange::Midtones, exposure, protect: true } if fmt.sample == SampleType::F32 && fmt.mode == ColorMode::Rgb => {
tone_stroke(fmt, move |c| c.map(|v| linear_midtones(v, exposure, false)))
}
Self::Tone { burn, range, exposure, protect } => tone_stroke(fmt, move |c| dodge_burn(c, exposure, range, burn, protect)),
// Flow is already in the dab's coverage. The reference RGB response applies to integer
// RGB surfaces, including 16-bit. Float RGB and the other models use the legacy curve.
@@ -828,3 +852,7 @@ mod smudge_refuses_indexed_tests;
#[cfg(test)]
#[path = "smudge_mode_tests.rs"]
mod smudge_mode_tests;
#[cfg(test)]
#[path = "dodge_32bit_linear_tests.rs"]
mod dodge_32bit_linear_tests;
@@ -0,0 +1,118 @@
//! Dodge in a 32-bit document tones the linear values it holds, with the reference's curve
//! (#12687); 8- and 16-bit documents keep their results.
use crate::Session;
use photocraft_color::convert::linear_to_srgb;
use serde_json::{Value, json};
/// The reference's test colours (8-bit RGB): grey 32, 128 and 224, red, skin and pastel.
const PATCHES: [[u8; 3]; 6] = [[32, 32, 32], [128, 128, 128], [224, 224, 224], [200, 40, 40], [220, 170, 140], [180, 200, 230]];
const SIDE: i32 = 60;
/// An 8-bit RGB document of `PATCHES` side by side, converted to `depth` bits.
fn patches(depth: u32) -> Session {
let mut s = Session::new();
s.execute("file.new", json!({"width": SIDE * PATCHES.len() as i32, "height": SIDE, "depth": 8, "mode": "rgb"})).unwrap();
s.edit("patches", |doc, active| {
let b = doc.bounds();
let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
let fmt = surf.format();
let mut data = Vec::new();
for _ in b.y0..b.y1 {
for x in b.x0..b.x1 {
let c = PATCHES[(x / SIDE) as usize].map(|v| f32::from(v) / 255.0);
data.extend(photocraft_raster::from_rgba(&fmt, [c[0], c[1], c[2], 1.0]));
}
}
surf.write_region(b, &data);
Ok(())
})
.unwrap();
if depth != 8 {
s.execute(&format!("image.mode.bits{depth}"), json!({})).unwrap();
}
s
}
fn rgb(s: &Session, x: i32, y: i32) -> [f32; 3] {
let d = s.active().unwrap();
let p = d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y);
[p[0], p[1], p[2]]
}
/// One default Dodge click (Midtones, Protect Tones, 50 %) with a hard 40 px tip on each patch's
/// centre; the stored values there.
fn dodge_each(s: &mut Session, extra: Value) -> Vec<[f32; 3]> {
(0..PATCHES.len() as i32)
.map(|i| {
let (x, y) = (i * SIDE + SIDE / 2, SIDE / 2);
let mut p = json!({"points": [[x, y]], "size": 40, "hardness": 100});
if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) {
p.extend(e.clone());
}
s.execute("paint.dodge", p).unwrap();
rgb(s, x, y)
})
.collect()
}
/// A 32-bit document's linear values as 8-bit RGB, as the reference reads them.
fn as_8bit(c: [f32; 3]) -> [i32; 3] {
c.map(|v| (linear_to_srgb(v).clamp(0.0, 1.0) * 255.0).round() as i32)
}
#[test]
fn dodge_tones_linear_values_in_32_bit() {
// Measured in the reference: one default click in a 32-bit RGB document, read as 8-bit.
let expected = [[32, 32, 32], [134, 134, 134], [230, 230, 230], [210, 40, 40], [227, 177, 147], [191, 209, 235]];
let mut s = patches(32);
let got: Vec<_> = dodge_each(&mut s, json!({})).into_iter().map(as_8bit).collect();
for ((g, e), p) in got.iter().zip(expected).zip(PATCHES) {
assert!(g.iter().zip(e).all(|(g, e)| (g - e).abs() <= 2), "32-bit Dodge of {p:?}: got {g:?}, expected {e:?} (+-2); all: {got:?}");
}
}
#[test]
fn dodge_keeps_8_and_16_bit_results() {
// Pinned from main 406ac677e1: this change only adds the 32-bit path.
let mut s = patches(8);
let got: Vec<_> = dodge_each(&mut s, json!({})).into_iter().map(|c| c.map(|v| (v * 255.0).round() as u16)).collect();
assert_eq!(got, [[46, 46, 46], [161, 161, 161], [228, 228, 228], [255, 65, 65], [242, 187, 154], [191, 212, 244]]);
let mut s = patches(16);
let got: Vec<_> = dodge_each(&mut s, json!({})).into_iter().map(|c| c.map(|v| (v * 65535.0).round() as u16)).collect();
assert_eq!(got, [[11825, 11825, 11825], [41392, 41392, 41392], [58654, 58654, 58654], [65535, 16658, 16658], [62104, 47989, 39521], [49139, 54599, 62789]]);
}
#[test]
fn dodge_keeps_light_above_white_in_32_bit() {
let mut s = patches(32);
s.edit("bright", |doc, active| {
let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
let r = photocraft_geom::Rect::new(0, 0, SIDE, SIDE);
let px = photocraft_raster::from_rgba(&surf.format(), [4.0, 1.5, 0.2, 1.0]);
surf.write_region(r, &px.repeat((SIDE * SIDE) as usize));
Ok(())
})
.unwrap();
let c = dodge_each(&mut s, json!({}))[0];
assert_eq!([c[0], c[1]], [4.0, 1.5], "light above white must stay as it was: {c:?}");
assert!(c[2] > 0.2, "the in-range channel is still dodged: {c:?}");
}
#[test]
fn dodge_keeps_tones_in_order_in_32_bit_at_full_exposure() {
let mut s = Session::new();
s.execute("file.new", json!({"width": 256, "height": 8, "depth": 32, "mode": "rgb"})).unwrap();
s.edit("ramp", |doc, active| {
let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
let fmt = surf.format();
let data: Vec<f32> = (0..8).flat_map(|_| (0..256).flat_map(|x| photocraft_raster::from_rgba(&fmt, [x as f32 / 255.0; 4]))).collect();
surf.write_region(photocraft_geom::Rect::new(0, 0, 256, 8), &data);
Ok(())
})
.unwrap();
s.execute("paint.dodge", json!({"points": [[-20, 4], [276, 4]], "size": 40, "hardness": 100, "exposure": 100})).unwrap();
let row: Vec<f32> = (0..256).map(|x| rgb(&s, x, 4)[0]).collect();
assert!(row.windows(2).all(|w| w[1] >= w[0]), "{row:?}");
assert!(row.iter().all(|v| (0.0..=1.0).contains(v)) && row[0] == 0.0 && row[255] == 1.0, "{row:?}");
}