mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-17 23:46:10 +00:00
raw: block-wise final encode stage for the default rendering
When neither Dehaze nor a local tone map is active (neutral tone and no black point; only the Nikon D4 profile sets one), the matrix is applied into a block of 768 samples and the gamma encode runs over the block: clamp and square root first (vectorises), then the table with [value, step] pairs. Same arithmetic, same output; other renderings keep the old per-sample loop. A test compares the block encode with the per-sample encode. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b0622d20ca
commit
df23873ccf
1 file changed
+54
-2
@@ -375,9 +375,23 @@ pub fn develop_sensor(s: &Sensor, opts: &DevelopOptions) -> Result<Developed> {
|
||||
Some(l) => GammaLut::with_tone(l.tone.as_ref()),
|
||||
None => GammaLut::with_curve(&s.tone_curve),
|
||||
};
|
||||
// The default rendering (no Dehaze, no local tone): matrix then encode, in blocks so the
|
||||
// encode's clamp and square root run over whole slices.
|
||||
let plain = map.is_none() && haze.is_none();
|
||||
par::chunks_mut(&mut out, band * w * 3, |b, chunk| {
|
||||
let start = b * band * w * 3;
|
||||
let src = &rgb[start..start + chunk.len()];
|
||||
if plain {
|
||||
let mut buf = [0.0f32; ENCODE_BLOCK];
|
||||
for (oc, sc) in chunk.chunks_mut(ENCODE_BLOCK).zip(src.chunks(ENCODE_BLOCK)) {
|
||||
let n = sc.len();
|
||||
for (t, p) in buf[..n].as_chunks_mut::<3>().0.iter_mut().zip(sc.as_chunks::<3>().0) {
|
||||
*t = pro(p);
|
||||
}
|
||||
lut.encode_into(&buf[..n], oc);
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (i, (o, p)) in chunk.as_chunks_mut::<3>().0.iter_mut().zip(src.as_chunks::<3>().0).enumerate() {
|
||||
let mut v = linear(b * band * w + i, p);
|
||||
if let Some(m) = &map {
|
||||
@@ -476,9 +490,13 @@ fn gain_row(s: &Sensor, y: usize, x0: usize, w: usize, k: usize) -> Option<Vec<f
|
||||
/// (error below 0.2 code values).
|
||||
struct GammaLut {
|
||||
table: Vec<f32>,
|
||||
/// `[table[i], table[i + 1] - table[i]]`: the same interpolation from one load.
|
||||
pairs: Vec<[f32; 2]>,
|
||||
}
|
||||
|
||||
const GAMMA_STEPS: usize = 4096;
|
||||
/// Samples per block of [`GammaLut::encode_into`] (whole pixels).
|
||||
const ENCODE_BLOCK: usize = 3 * 256;
|
||||
|
||||
impl GammaLut {
|
||||
#[cfg(test)]
|
||||
@@ -494,7 +512,7 @@ impl GammaLut {
|
||||
tone(curve, s * s).clamp(0.0, 1.0).powf(1.0 / 1.8) * 65535.0
|
||||
})
|
||||
.collect();
|
||||
GammaLut { table }
|
||||
Self::from_table(table)
|
||||
}
|
||||
|
||||
/// The gamma-1.8 encoding after a fitted [`ToneCurve`] (`None`: none, plain gamma 1.8).
|
||||
@@ -508,7 +526,30 @@ impl GammaLut {
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
GammaLut { table }
|
||||
Self::from_table(table)
|
||||
}
|
||||
|
||||
fn from_table(table: Vec<f32>) -> Self {
|
||||
let pairs = table.windows(2).map(|p| [p[0], p[1] - p[0]]).collect();
|
||||
GammaLut { table, pairs }
|
||||
}
|
||||
|
||||
/// [`Self::encode`] of every sample of `v` into `out` (at most [`ENCODE_BLOCK`] samples):
|
||||
/// the clamp and square root first over the whole block, then the table.
|
||||
#[inline]
|
||||
fn encode_into(&self, v: &[f32], out: &mut [u16]) {
|
||||
let mut xs = [0.0f32; ENCODE_BLOCK];
|
||||
let n = v.len().min(ENCODE_BLOCK).min(out.len());
|
||||
for (x, &v) in xs[..n].iter_mut().zip(&v[..n]) {
|
||||
let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) };
|
||||
*x = v.sqrt() * GAMMA_STEPS as f32;
|
||||
}
|
||||
for (o, &x) in out[..n].iter_mut().zip(&xs[..n]) {
|
||||
let i = (x as usize).min(GAMMA_STEPS);
|
||||
let f = x - i as f32;
|
||||
let [a, step] = self.pairs[i];
|
||||
*o = (a + step * f + 0.5) as u16;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -589,6 +630,17 @@ pub(crate) fn orient(src: Vec<u16>, w: usize, h: usize, o: u16) -> (Vec<u16>, us
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn block_encode_matches_per_sample_encode() {
|
||||
let lut = GammaLut::with_curve(&[[0.25, 0.5], [1.0, 1.0]]);
|
||||
let v: Vec<f32> = (0..2000).map(|i| (i as f32 - 300.0) / 1500.0).chain([f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 0.0, 1.0]).collect();
|
||||
for n in [0, 1, 3, 255, ENCODE_BLOCK, v.len().min(ENCODE_BLOCK)] {
|
||||
let mut out = vec![0u16; n];
|
||||
lut.encode_into(&v[..n], &mut out);
|
||||
assert_eq!(out, v[..n].iter().map(|&x| lut.encode(x)).collect::<Vec<_>>(), "{n}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hundred_megapixel_bayer_fits_without_raising_default_limits() {
|
||||
let mut opts = DevelopOptions::default();
|
||||
|
||||
Reference in new issue
Block a user