LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
//! 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::{REC2020, luminance_2020};
use lightcraft_develop::{DevelopSettings, Process};
use lightcraft_raster::blur::gaussian;
use std::sync::Arc;
use lightcraft_raster::{Plane, Rgb32f, par_join};
use crate::geometry::Frame;
use crate::{Prepared, Quality, SourceInfo, for_rows, masks, timed};
/// White balance (relative to the source's as-shot white) and exposure, in place.
pub fn scene_linear_pre(img: &mut Rgb32f, info: &SourceInfo, s: &DevelopSettings) {
wb_gain(img, info, s, 2f32.powf(s.light.exposure as f32));
}
/// White balance only (exposure is applied by the per-pixel stage, see [`crate::Prepared`]).
pub fn white_balance(img: &mut Rgb32f, info: &SourceInfo, s: &DevelopSettings) {
wb_gain(img, info, s, 1.0);
}
/// The white-balance matrix (linear Rec.2020) for the settings, or `None` when the as-shot white
/// is kept. A raw source with its camera colour model ([`crate::CameraColor`]) is re-developed for
/// the new white in camera space, as Lightroom does (so a neutral under the chosen white renders
/// neutral and saturated colours move as the camera's matrices say); other sources are adapted
/// with Bradford, luminance-preserving.
pub fn wb_matrix_for(info: &SourceInfo, s: &DevelopSettings) -> 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(cc) = info.camera_color.as_deref().filter(|_| info.raw && !info.relative_wb)
&& let Some(m) = lightcraft_raw::color::rebalance(&cc.tags, cc.developed_for, temp_tint_to_xy(t, tint))
{
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 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]);
let y = g[0] * 0.2627 + g[1] * 0.6780 + g[2] * 0.0593;
Some(m.mul(&lightcraft_color::Mat3::diag(1.0 / y, 1.0 / y, 1.0 / y)).to_f32())
}
fn wb_gain(img: &mut Rgb32f, info: &SourceInfo, s: &DevelopSettings, gain: f32) {
let m = wb_matrix_for(info, s);
let w = img.width;
for_rows(&mut img.data, w, |_, row| {
for p in row.iter_mut() {
let mut c = *p;
if let Some(m) = &m {
c = [
m[0][0] * c[0] + m[0][1] * c[1] + m[0][2] * c[2],
m[1][0] * c[0] + m[1][1] * c[1] + m[1][2] * c[2],
m[2][0] * c[0] + m[2][1] * c[1] + m[2][2] * c[2],
];
}
*p = [(c[0] * gain).max(0.0), (c[1] * gain).max(0.0), (c[2] * gain).max(0.0)];
}
});
}
/// The white balance actually in effect (presets resolve to their Kelvin values for raw files; settings written on
/// the relative scale before a raw's camera matrices reached its white balance give the same colour change on the
/// Kelvin scale, see [`legacy_to_kelvin`]).
pub fn effective_wb(info: &SourceInfo, s: &DevelopSettings) -> (f64, f64) {
use lightcraft_develop::WbMode;
match s.wb.mode {
WbMode::AsShot => (info.as_shot_temp, info.as_shot_tint),
_ if legacy_wb(info, s) => legacy_to_kelvin(s.wb.temp, s.wb.tint, (info.as_shot_temp, info.as_shot_tint)),
m if info.raw && !info.relative_wb => m.preset().unwrap_or((s.wb.temp, s.wb.tint)),
_ => (s.wb.temp, s.wb.tint),
}
}
/// The source white the render adapts from the as-shot white to: [`effective_wb`] as a chromaticity, except that a
/// white balance still on the relative scale is composed exactly ([`legacy_white`]) rather than read back through
/// its rounded Kelvin / tint.
fn effective_white(info: &SourceInfo, s: &DevelopSettings) -> lightcraft_color::Xy {
if legacy_wb(info, s)
&& s.wb.mode != lightcraft_develop::WbMode::AsShot
&& let Some(xy) = legacy_white(s.wb.temp, s.wb.tint, (info.as_shot_temp, info.as_shot_tint))
{
return xy;
}
let (t, tint) = effective_wb(info, s);
temp_tint_to_xy(t, tint)
}
/// Whether `s` holds a white balance written on the relative scale for a source that is edited in Kelvin today.
fn legacy_wb(info: &SourceInfo, s: &DevelopSettings) -> bool {
info.raw && !info.relative_wb && info.legacy_relative_wb && s.wb.scale == lightcraft_develop::WbScale::Legacy
}
/// The white that renders exactly as `(temp, tint)` did on the relative scale, where 6500 K / 0 meant *as shot* and
/// the source's real as-shot white is `as_shot`.
///
/// The relative render adapted the picture from 6500 K / 0 to `(temp, tint)` (Bradford, [`wb_matrix_for`]); the
/// Kelvin render adapts it from `as_shot` to the result. Bradford adaptations are von Kries scalings in one cone
/// space, so they compose exactly: the new white's cone response is the old target's times the as-shot white's
/// over the old reference's, and the two renders give the same matrix (up to the scale the luminance
/// normalisation removes). `None` when the arithmetic has nowhere to go (a non-finite input, a white outside the
/// chromaticity plane).
pub fn legacy_white(temp: f64, tint: f64, as_shot: (f64, f64)) -> Option<lightcraft_color::Xy> {
use lightcraft_color::{BRADFORD, Xy};
if !(temp.is_finite() && tint.is_finite() && as_shot.0.is_finite() && as_shot.1.is_finite()) {
return None;
}
let cones = |t: f64, tint: f64| BRADFORD.apply(temp_tint_to_xy(t, tint).to_xyz());
let (old, reference, shot) = (cones(temp, tint), cones(6500.0, 0.0), cones(as_shot.0, as_shot.1));
let inverse = BRADFORD.inverse()?;
let mut new = [0.0; 3];
for i in 0..3 {
if reference[i].abs() < 1e-12 {
return None;
}
new[i] = old[i] * shot[i] / reference[i];
}
let xy = Xy::from_xyz(inverse.apply(new));
(xy.x.is_finite() && xy.y.is_finite() && xy.y > 0.0).then_some(xy)
}
/// The Kelvin / tint of [`legacy_white`]: what a white balance written on the relative scale reads as, and becomes
/// when it is next edited ([`normalize_wb`]). Exact wherever the white has a Kelvin value (1000–50000 K after the
/// composition; the relative slider reached 2000–13550 K around 6500 K, so only a far-blue as-shot white pushed
/// to the slider's blue end lands beyond, and clamps), to the precision of the Kelvin / tint parametrisation
/// (better than 0.01 % away from its 4000 K seam). Unchanged when the arithmetic has nowhere to go.
pub fn legacy_to_kelvin(temp: f64, tint: f64, as_shot: (f64, f64)) -> (f64, f64) {
if temp == 6500.0 && tint == 0.0 && as_shot.0.is_finite() && as_shot.1.is_finite() {
// the relative scale's as-shot reference, exactly (a Custom value left where As Shot was stays as shot)
return as_shot;
}
match legacy_white(temp, tint, as_shot).map(xy_to_temp_tint) {
Some((t, tint)) if t.is_finite() && tint.is_finite() => (t, tint),
_ => (temp, tint),
}
}
/// The relative-scale `(temp, tint)` that [`legacy_to_kelvin`] would read as the Kelvin `(temp, tint)` on a source
/// whose real as-shot white is `as_shot`: the inverse of [`legacy_white`]'s composition. It runs the Bradford step
/// backwards — the old target's cone response times the old reference's over the as-shot white's — and reads the
/// result back through [`xy_to_temp_tint`]. `(6500, 0)` is the relative scale's as-shot reference, so the Kelvin
/// white of the source itself maps exactly to it. `None` when the arithmetic has nowhere to go (a non-finite input,
/// a white outside the chromaticity plane, or an as-shot cone response of zero, where the forward step discarded it).
pub fn kelvin_to_legacy(temp: f64, tint: f64, as_shot: (f64, f64)) -> Option<(f64, f64)> {
use lightcraft_color::{BRADFORD, Xy};
if !(temp.is_finite() && tint.is_finite() && as_shot.0.is_finite() && as_shot.1.is_finite()) {
return None;
}
if (temp - as_shot.0).abs() < 1e-9 && (tint - as_shot.1).abs() < 1e-9 {
// the source's own white: the relative scale's as-shot reference, exactly
return Some((6500.0, 0.0));
}
let cones = |t: f64, tint: f64| BRADFORD.apply(temp_tint_to_xy(t, tint).to_xyz());
let (target, reference, shot) = (cones(temp, tint), cones(6500.0, 0.0), cones(as_shot.0, as_shot.1));
let inverse = BRADFORD.inverse()?;
let mut old = [0.0; 3];
for i in 0..3 {
if shot[i].abs() < 1e-12 {
return None;
}
old[i] = target[i] * reference[i] / shot[i];
}
let xy = Xy::from_xyz(inverse.apply(old));
if !(xy.x.is_finite() && xy.y.is_finite() && xy.y > 0.0) {
return None;
}
let (t, tint) = xy_to_temp_tint(xy);
(t.is_finite() && tint.is_finite()).then_some((t, tint))
}
/// Bring the white balance of `d` onto the scale `info` is edited on, before a new value is written into it: on a
/// raw with a Kelvin scale the values are marked Kelvin, and ones still on the relative scale become the Custom
/// Kelvin / tint that renders the same ([`legacy_to_kelvin`]; a preset mode's look is kept as that custom
/// value, since the preset's Kelvin value would be a different look). Elsewhere the scale is cleared, so values
/// edited on a rendered photo or a raw without matrices never pass for Kelvin when pasted onto one that has them.
pub fn normalize_wb(d: &mut DevelopSettings, info: &SourceInfo) {
use lightcraft_develop::{WbMode, WbScale};
if !info.raw || info.relative_wb {
d.wb.scale = WbScale::Legacy;
return;
}
if legacy_wb(info, d) && d.wb.mode != WbMode::AsShot {
let (t, tint) = legacy_to_kelvin(d.wb.temp, d.wb.tint, (info.as_shot_temp, info.as_shot_tint));
d.wb.temp = t;
d.wb.tint = tint;
d.wb.mode = WbMode::Custom;
}
d.wb.scale = WbScale::Kelvin;
}
/// Self-guided filter (He et al.) on a single plane with Gaussian windows of `sigma` px.
pub fn guided(p: &Plane, sigma: f32, eps: f32) -> Plane {
let (ma, mb) = guided_coeffs(p, sigma, eps);
guided_apply(p, &ma, &mb)
}
/// Blurred guided-filter coefficients (mean a, mean b) of `p`; every pass row-parallel, and the
/// two independent blurs of each step run side by side.
fn guided_coeffs(p: &Plane, sigma: f32, eps: f32) -> (Plane, Plane) {
let (mean, corr) = par_join(|| gaussian(p, sigma), || gaussian(&p.map(|v| v * v), sigma));
let a = corr.zip_map(&mean, |c, m| {
let var = (c - m * m).max(0.0);
var / (var + eps)
});
let b = mean.zip_map(&a, |m, a| m - a * m);
par_join(|| gaussian(&a, sigma), || gaussian(&b, sigma))
}
/// `q = a·p + b` per pixel. Reduced coefficients are sampled with the same separable
/// weights and accumulation order as a bilinear resize, without full-size coefficient planes.
fn guided_apply(p: &Plane, a: &Plane, b: &Plane) -> Plane {
let mut q = Plane::new(p.width, p.height);
let w = p.width;
if (a.width, a.height) != (p.width, p.height) {
use lightcraft_raster::resample::{Filter, weights};
let wx = weights(a.width, w, Filter::Bilinear);
let wy = weights(a.height, p.height, Filter::Bilinear);
// Bilinear upsampling has at most four taps (including zero-weight boundary taps).
// Keep only those horizontally sampled rows per band, reusing them for neighbouring
// output rows. Scratch scales with width and active bands, not image area.
const BAND: usize = 32;
for_rows(&mut q.data, w.saturating_mul(BAND), |band, rows| {
let mut cache: [(usize, Vec<f32>, Vec<f32>); 4] = std::array::from_fn(|_| (usize::MAX, vec![0.0; w], vec![0.0; w]));
let mut bias = vec![0.0; w];
for (dy, row) in rows.chunks_mut(w).enumerate() {
let y = band * BAND + dy;
let (top, ys) = &wy[y];
bias.fill(0.0);
for (dy, yw) in ys.iter().enumerate() {
let cy = top + dy;
let (cached_y, aa, bb) = &mut cache[cy % 4];
if *cached_y != cy {
let (ar, br) = (a.row(cy), b.row(cy));
for ((av, bv), (left, xs)) in aa.iter_mut().zip(bb.iter_mut()).zip(&wx) {
*av = 0.0;
*bv = 0.0;
for (dx, xw) in xs.iter().enumerate() {
*av += ar[left + dx] * xw;
*bv += br[left + dx] * xw;
}
}
*cached_y = cy;
}
for (av, ax) in row.iter_mut().zip(aa) {
*av += *ax * yw;
}
for (bv, bx) in bias.iter_mut().zip(bb) {
*bv += *bx * yw;
}
}
for ((v, pv), bv) in row.iter_mut().zip(p.row(y)).zip(&bias) {
*v = *v * pv + bv;
}
}
});
return q;
}
for_rows(&mut q.data, w, |y, row| {
for ((v, pv), (av, bv)) in row.iter_mut().zip(p.row(y)).zip(a.row(y).iter().zip(b.row(y))) {
*v = av * pv + bv;
}
});
q
}
/// Fast guided filter (He & Sun 2015): coefficients computed on a `s×` subsampled plane and
/// sampled bilinearly at full resolution; the output keeps full-resolution edges. Equivalent to [`guided`] for
/// large windows at a fraction of the cost.
pub fn guided_fast(p: &Plane, sigma: f32, eps: f32) -> Plane {
use lightcraft_raster::resample::{Filter, resize};
let s = guided_fast_step(sigma);
if s <= 1 {
return guided(p, sigma, eps);
}
let (lw, lh) = (p.width.div_ceil(s).max(1), p.height.div_ceil(s).max(1));
let lo = resize(p, lw, lh, Filter::Box);
let (ma, mb) = guided_coeffs(&lo, sigma / s as f32, eps);
guided_apply(p, &ma, &mb)
}
/// Separable median (`2r+1` px along each row, then each column; clamped edges). It keeps the
/// steps between regions where they are and removes features thinner than about `r` px.
pub fn median_hv(p: &Plane, r: usize) -> Plane {
if r == 0 || p.data.is_empty() {
return p.clone();
}
let rows = |src: &Plane| {
let mut out = Plane::new(src.width, src.height);
for_rows(&mut out.data, src.width, |y, row| median_row(src.row(y), row, r));
out
};
transpose(&rows(&transpose(&rows(p))))
}
/// Running median of `src` over `2r+1` taps (clamped edges) into `out`: a sorted window that
/// drops the leaving value and inserts the entering one.
fn median_row(src: &[f32], out: &mut [f32], r: usize) {
let n = src.len();
if n == 0 {
return;
}
let at = |i: isize| total_key(src[i.clamp(0, n as isize - 1) as usize]);
let r_ = r as isize;
let mut win: Vec<i32> = (-r_..=r_).map(at).collect();
win.sort_unstable();
for (x, o) in out.iter_mut().enumerate() {
*o = f32::from_bits(total_key_inv(win[r]));
let (old, new) = (at(x as isize - r_), at(x as isize + r_ + 1));
if old != new {
// Ranks by counting the smaller keys in one branch-free pass the compiler vectorizes.
// Binary searches here mispredicted on most steps and, with `remove` + `insert`, cost
// 2.4 s per 24 MP at r = 9 on one core.
let (mut i, mut j) = (0u32, 0u32);
for &v in &win {
i += u32::from(v < old);
j += u32::from(v < new);
}
let (i, j) = (i as usize, j as usize);
// `old` sits at `i`; shift the keys between the two ranks by one over it
if j > i {
win.copy_within(i + 1..j, i);
win[j - 1] = new;
} else {
win.copy_within(j..i, j + 1);
win[j] = new;
}
}
}
}
/// `v`'s bits as an `i32` that orders like [`f32::total_cmp`] (the same transform it applies), so
/// equal keys are equal bits.
fn total_key(v: f32) -> i32 {
let b = v.to_bits() as i32;
b ^ (((b >> 31) as u32) >> 1) as i32
}
/// The bits [`total_key`] came from (the transform is its own inverse: it keeps the sign bit).
fn total_key_inv(k: i32) -> u32 {
(k ^ (((k >> 31) as u32) >> 1) as i32) as u32
}
fn transpose(p: &Plane) -> Plane {
let (w, h) = (p.width, p.height);
let mut t = Plane::new(h, w);
for_rows(&mut t.data, h, |x, row| {
for (y, v) in row.iter_mut().enumerate() {
*v = p.data[y * w + x];
}
});
t
}
/// Detail scale of the highlights/shadows base from process V2, as a fraction of the long edge:
/// the base is the edge-aware filter of the [`median_hv`] of log luminance at this radius, so
/// twigs, lines and fine texture brighter or darker than their surroundings take their
/// surroundings' shift (and keep their contrast, as with Exposure) while region boundaries stay
/// sharp (no halos). V1's base is the edge-aware filter of log luminance itself (issue #632).
pub const BASE_DETAIL: f64 = 0.0015;
/// Largest [`base_detail_radius`] (px).
pub const BASE_DETAIL_MAX: usize = 24;
/// Median radius (px) of the highlights/shadows base at `px_per_long` (see [`BASE_DETAIL`]).
pub fn base_detail_radius(px_per_long: f64) -> usize {
((BASE_DETAIL * px_per_long).round().max(0.0) as usize).min(BASE_DETAIL_MAX)
}
/// Median radius (px) of the highlights/shadows base of `s`'s rendering process at `px_per_long`:
/// 0 (no median) under V1, [`base_detail_radius`] from V2.
pub fn tone_base_detail(s: &DevelopSettings, px_per_long: f64) -> usize {
match s.process.process() {
Process::V1 => 0,
Process::V2 => base_detail_radius(px_per_long),
}
}
/// The highlights/shadows base layer of log luminance `l`: the fast guided filter ([`BASE_EPS`])
/// of its [`median_hv`] at `detail` px ([`tone_base_detail`]); at 0, of `l` itself (V1).
pub fn tone_base(l: &Plane, sigma: f32, detail: usize) -> Plane {
if detail == 0 {
return guided_fast(l, sigma, BASE_EPS);
}
guided_fast(&median_hv(l, detail), sigma, BASE_EPS)
}
/// Subsampling step of [`guided_fast`] for `sigma` (1 = no subsampling: the plain guided filter).
pub fn guided_fast_step(sigma: f32) -> usize {
(sigma / 3.0).floor().clamp(1.0, 16.0) as usize
}
/// Luminance noise reduction: guided filter of log luminance (`sigma` px, `eps`), blended by `k`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct NrLum {
pub sigma: f32,
pub eps: f32,
pub k: f32,
}
/// Colour noise reduction: chromaticity blurred by `sigma` px, mixed in by `t`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct NrColor {
pub sigma: f32,
pub t: f32,
}
/// Output pixels per pixel of the original photo for an output of `px_per_long` pixels per unit
/// of the long edge of an original `native_long` pixels long, at most 1 ([`crate::Plan::detail_scale`]).
pub fn detail_scale(px_per_long: f64, native_long: usize) -> f32 {
let k = (px_per_long / native_long.max(1) as f64) as f32;
if k.is_finite() { k.clamp(0.0, 1.0) } else { 1.0 }
}
/// Smallest radius (px) noise reduction filters a preview with: [`gaussian`] makes no blur below
/// about 0.58 px and one 3-px box from there ([`lightcraft_raster::blur::box_sigma`]).
const NR_MIN_SIGMA: f32 = 1.0;
/// Below this fade a preview's noise reduction is not drawn at all (less than 1 % of a 1-px blur).
const NR_MIN_BLEND: f32 = 0.01;
/// Radius and blend weight that stand in for a blur of `sigma` px: below [`NR_MIN_SIGMA`] the
/// smallest blur the filter makes, blended in by the ratio of their variances, which keeps the
/// smoothing's second moment (how much it softens the finest detail) that of `sigma`.
fn nr_radius(sigma: f32) -> (f32, f32) {
if sigma >= NR_MIN_SIGMA {
return (sigma, 1.0);
}
let made = lightcraft_raster::blur::box_sigma(NR_MIN_SIGMA);
(NR_MIN_SIGMA, (sigma / made).powi(2).min(1.0))
}
/// Noise-reduction parameters at `scale` output pixels per pixel of the original
/// ([`crate::Plan::detail_scale`]; see [`denoise`]). Noise lives at the original's pixels, so the
/// radii are the full-size radii times `scale`, and the guided filter's `eps` (the variance it
/// treats as noise) follows the noise variance, which averaging `1/scale × 1/scale` pixels into one
/// multiplies by `scale²`. A radius below [`NR_MIN_SIGMA`] fades instead ([`nr_radius`]): previews look like
/// the full-size render scaled down (issue #960). At `scale` 1 these are the full-size parameters.
pub fn nr_params(s: &DevelopSettings, scale: f32) -> (Option<NrLum>, Option<NrColor>) {
let lum = (s.detail.nr_luminance / 100.0) as f32;
let col = (s.detail.nr_color / 100.0) as f32;
let scale = if scale.is_finite() { scale.clamp(0.0, 1.0) } else { 1.0 };
let l = (lum > 0.0).then(|| nr_radius((1.0 + 2.5 * lum) * scale)).filter(|&(_, w)| w >= NR_MIN_BLEND).map(|(sigma, w)| {
let detail = (s.detail.nr_detail / 100.0) as f32;
let eps = ((0.002 + lum * lum * 0.25 * (1.0 - 0.7 * detail)) * scale * scale).max(1e-7);
NrLum { sigma, eps, k: lum.sqrt() * w }
});
let smooth = 1.0 + (s.detail.nr_color_smoothness / 100.0) as f32;
let c = (col > 0.0).then(|| nr_radius((1.5 + 6.0 * col) * scale * smooth)).filter(|&(_, w)| w >= NR_MIN_BLEND).map(|(sigma, w)| {
let keep = (s.detail.nr_color_detail / 100.0) as f32 * 0.5;
NrColor { sigma, t: col * (1.0 - keep) * w }
});
(l, c)
}
/// Noise reduction at output resolution: luminance via an edge-aware self-guided filter on
/// log-luminance, colour by blurring chromaticity (rgb / Y) and re-applying the original luminance.
/// `scale`: output pixels per pixel of the original ([`nr_params`]).
pub fn denoise(img: &mut Rgb32f, s: &DevelopSettings, scale: f32) {
let (lum, col) = nr_params(s, scale);
apply_nr(img, lum, col);
}
/// Noise reduction with explicit parameters (see [`denoise`]).
pub fn apply_nr(img: &mut Rgb32f, lum: Option<NrLum>, col: Option<NrColor>) {
let w = img.width;
if let Some(nr) = lum {
let _t = crate::profiling().then(std::time::Instant::now);
let l = img.map(log_lum);
let f = guided(&l, nr.sigma, nr.eps);
for_rows(&mut img.data, w, |y, row| {
for (x, p) in row.iter_mut().enumerate() {
let i = y * w + x;
let d = (f.data[i] - l.data[i]) * nr.k;
let g = d.exp2();
*p = p.map(|v| v * g);
}
});
if let Some(t) = _t {
eprintln!(" nr luminance: {:.1} ms", t.elapsed().as_secs_f64() * 1e3);
}
}
if let Some(nr) = col {
let _t = crate::profiling().then(std::time::Instant::now);
let chroma = img.map(|c| {
let y = luminance_2020(c).max(1e-6);
[c[0] / y, c[1] / y, c[2] / y]
});
let b = gaussian(&chroma, nr.sigma);
for_rows(&mut img.data, w, |y, row| {
for (x, p) in row.iter_mut().enumerate() {
let i = y * w + x;
let yl = luminance_2020(*p);
let c0 = chroma.data[i];
let cb = b.data[i];
*p = [0, 1, 2].map(|k| ((c0[k] + (cb[k] - c0[k]) * nr.t) * yl).max(0.0));
}
});
if let Some(t) = _t {
eprintln!(" nr colour: {:.1} ms", t.elapsed().as_secs_f64() * 1e3);
}
}
}
pub fn log_lum(c: [f32; 3]) -> f32 {
(luminance_2020(c).max(1e-7) / crate::tone::GREY).log2()
}
/// Spatial planes of one (pre-exposure) image, each tagged with the radius it was computed at.
/// `key` identifies the image; see [`crate::StageCache`].
#[derive(Clone, Default)]
pub(crate) struct Planes {
pub key: u64,
/// The image's key without its noise reduction (`Plan::pre_nr_key`).
pub pre_nr: u64,
pub log_l: Option<Arc<Plane>>,
/// Keyed by its radius, process and median radius ([`BaseKey`]).
pub base: Option<(BaseKey, Arc<Plane>)>,
pub clarity: Option<(u32, Arc<Plane>)>,
pub texture: Option<(u32, Arc<Plane>)>,
/// Dark channel and its airlight.
pub dark: Option<(u32, Arc<Plane>, f32)>,
/// Blurred chromaticity (local Moiré / Noise).
pub chroma: Option<(u32, Arc<Rgb32f>)>,
}
impl Planes {
/// The planes that still fit an image differing from this one only in noise reduction, close
/// enough for a draft: the wide blurs (base, clarity, dehaze, chromaticity). Its `key` matches
/// no image, so only another draft takes them.
pub fn for_other_nr(self) -> Planes {
Planes { key: 0, pre_nr: self.pre_nr, log_l: None, texture: None, ..self }
}
}
/// What the highlights/shadows base plane was computed with: its radius (`f32` bits), the
/// rendering process and the median radius ([`tone_base_detail`]). The base differs between
/// processes, so a photo moved to another one (Update to Current Process, undo) recomputes it.
pub type BaseKey = (u32, Process, usize);
/// The [`BaseKey`] of a base at `sigma` for `s` (`detail`: [`PlaneSigmas::base_detail`]).
pub fn base_key(s: &DevelopSettings, sigma: f32, detail: usize) -> BaseKey {
(sigma.to_bits(), s.process.process(), detail)
}
/// Reuse `slot` if it was computed with `key` (a radius's `f32` bits, or a [`BaseKey`]), else
/// compute and store it.
pub fn plane_at<K: Copy + PartialEq, P>(slot: &mut Option<(K, Arc<P>)>, key: K, f: impl FnOnce() -> P) -> Arc<P> {
match slot {
Some((k, p)) if *k == key => p.clone(),
_ => {
let p = Arc::new(f());
*slot = Some((key, p.clone()));
p
}
}
}
/// Guided-filter epsilons (EV²) of the highlights/shadows base and the clarity band.
pub const BASE_EPS: f32 = 0.35;
pub const CLARITY_EPS: f32 = 0.8;
/// Dark channel of a pixel (dehaze).
#[inline]
pub fn dark_of(c: [f32; 3]) -> f32 {
c[0].min(c[1]).min(c[2])
}
/// Radius of the local Moiré / colour-noise chromaticity blur, as a fraction of the long edge.
pub const CHROMA_SIGMA: f32 = 0.004;
/// Radii (px) of the spatial planes the settings need (`None` = not needed).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct PlaneSigmas {
/// Edge-aware base for highlights/shadows ([`tone_base`]: fast guided filter, [`BASE_EPS`]; from
/// process V2 of the [`BASE_DETAIL`] median).
pub base: Option<f32>,
/// Median radius (px) of that base ([`tone_base_detail`]: 0 under V1).
pub base_detail: usize,
/// Clarity band (fast guided filter, [`CLARITY_EPS`]).
pub clarity: Option<f32>,
/// Texture / sharpening band (Gaussian).
pub texture: Option<f32>,
/// Dehaze dark channel (Gaussian).
pub dark: Option<f32>,
/// Chromaticity blur for local Moiré / colour noise (Gaussian).
pub chroma: Option<f32>,
}
/// Gaussian sigma (pixels) of the texture / sharpening band at `px_per_long`.
pub fn texture_sigma(px_per_long: f64) -> f32 {
(0.0018 * px_per_long as f32).max(0.6)
}
pub fn plane_sigmas(s: &DevelopSettings, px_per_long: f64, q: Quality) -> PlaneSigmas {
let ppl = px_per_long as f32;
// A hidden mask, or one with no components, does not change the picture. Its sliders must
// not pick the tone base Clarity reads (#1214).
let live = |m: &&lightcraft_develop::Mask| m.visible && !m.components.is_empty();
let local_any = |f: fn(&lightcraft_develop::LocalAdjustments) -> f64| s.masks.iter().filter(live).any(|m| f(&m.adjust) != 0.0);
let tone_active = s.light.highlights != 0.0
|| s.light.shadows != 0.0
|| s.masks.iter().filter(live).any(|m| m.adjust.highlights != 0.0 || m.adjust.shadows != 0.0);
// Edge-aware base at ~1.5% of the long edge (EV² epsilon: edges of > ~0.6 EV are preserved)
// From V2 it is taken of the median at 0.15%: lines and texture finer than that are detail,
// not tone (#632).
let base = tone_active.then(|| {
let sigma = (0.015 * ppl).max(1.0);
if q == Quality::Draft { sigma.min(24.0) } else { sigma }
});
let clarity = (s.effects.clarity != 0.0 || local_any(|a| a.clarity)).then(|| (0.012 * ppl).max(1.0));
// local Noise and Defringe read the fine detail band too
let texture = (s.effects.texture != 0.0
|| s.detail.sharpen_amount != 0.0
|| local_any(|a| a.texture)
|| local_any(|a| a.sharpness)
|| local_any(|a| a.noise)
|| s.masks.iter().filter(live).any(|m| m.adjust.defringe > 0.0))
.then(|| texture_sigma(px_per_long));
let dark = (s.effects.dehaze != 0.0 || local_any(|a| a.dehaze)).then(|| (0.02 * ppl).max(1.0));
let chroma = (local_any(|a| a.moire) || s.masks.iter().filter(live).any(|m| m.adjust.noise > 0.0)).then(|| (CHROMA_SIGMA * ppl).max(1.0));
let base_detail = if base.is_some() { tone_base_detail(s, px_per_long) } else { 0 };
PlaneSigmas { base, base_detail, clarity, texture, dark, chroma }
}
/// The spatial planes the per-pixel stage needs for `img` (white-balanced, before exposure),
/// reusing whatever `planes` already holds for it. Missing planes are computed side by side (each
/// one alone scales poorly: the guided filters work on small subsampled grids).
#[allow(clippy::too_many_arguments)]
pub(crate) fn prepare(
img: Arc<Rgb32f>,
s: &DevelopSettings,
frame: &Frame,
px_per_long: f64,
q: Quality,
planes: &mut Planes,
mattes: Option<&masks::Mattes>,
) -> Prepared {
let log_l = planes.log_l.get_or_insert_with(|| Arc::new(timed("log_l", || img.map(log_lum)))).clone();
let PlaneSigmas { base: base_sigma, base_detail, clarity: clarity_sigma, texture: texture_sigma, dark: dark_sigma, chroma: chroma_sigma } =
plane_sigmas(s, px_per_long, q);
let Planes { base: sb, clarity: sc, texture: st, dark: sd, chroma: sch, .. } = planes;
let chroma_blur = chroma_sigma.map(|sg| match sch {
Some((k, c)) if *k == sg.to_bits() => c.clone(),
_ => {
let c = timed("chroma", || Arc::new(gaussian(&img.map(masks::chromaticity), sg)));
*sch = Some((sg.to_bits(), c.clone()));
c
}
});
let l = &log_l;
let (base, (clarity_blur, (texture_blur, dark))) = par_join(
|| base_sigma.map(|sg| plane_at(sb, base_key(s, sg, base_detail), || timed("base", || tone_base(l, sg, base_detail)))),
|| {
par_join(
|| clarity_sigma.map(|sg| plane_at(sc, sg.to_bits(), || timed("clarity", || guided_fast(l, sg, CLARITY_EPS)))),
|| {
par_join(
|| texture_sigma.map(|sg| plane_at(st, sg.to_bits(), || timed("texture", || gaussian(l, sg)))),
|| {
dark_sigma.map(|sg| match sd {
Some((k, d, air)) if *k == sg.to_bits() => (d.clone(), *air),
_ => {
let d = timed("dehaze", || Arc::new(gaussian(&img.map(dark_of), sg)));
let air = airlight(&d);
*sd = Some((sg.to_bits(), d.clone(), air));
(d, air)
}
})
},
)
},
)
},
);
let base = base.unwrap_or_else(|| log_l.clone());
let (dark, air) = match dark {
Some((d, air)) => (Some(d), air),
None => (None, 1.0),
};
let ev = s.light.exposure as f32;
let masks = timed("masks", || masks::evaluate(&s.masks, frame, img.width, img.height, &img, &log_l, ev, mattes));
Prepared { img, log_l, base, clarity_blur, texture_blur, dark, chroma_blur, air, masks, px_per_long }
}
/// The airlight of the whole output frame, estimated on a small render of it (`proxy_w` px wide):
/// a windowed render must dehaze with the frame's airlight, not that of the pixels it holds.
pub(crate) fn frame_airlight(src: &Rgb32f, info: &SourceInfo, s: &DevelopSettings, frame: &Frame, proxy_w: usize, proxy_h: usize) -> f32 {
let mut img = Rgb32f::clone(&frame_proxy(src, frame, proxy_w, proxy_h));
white_balance(&mut img, info, s);
let sigma = (0.02 * frame.px_per_long(proxy_w)).max(1.0) as f32;
airlight(&gaussian(&img.map(dark_of), sigma))
}
/// How many airlight proxies [`frame_proxy`] keeps (one per photo and frame being zoomed into; each ≤ 1.8 MB).
const FRAME_PROXIES_KEPT: usize = 4;
/// `frame.sample(src, w, h)`, kept for the last few (source, frame, size): sampling a 64 MP original down to the
/// airlight proxy reads all of it (~190 ms of CPU), and every window of a zoomed view with Dehaze asked for it again,
/// on each pan and slider tick. The proxy is taken before white balance, so a WB change still hits.
fn frame_proxy(src: &Rgb32f, frame: &Frame, w: usize, h: usize) -> Arc<Rgb32f> {
type Entry = (u64, Frame, usize, usize, Arc<Rgb32f>);
static CACHE: std::sync::Mutex<Vec<Entry>> = std::sync::Mutex::new(Vec::new());
let key = crate::optics::fingerprint(src);
let found = |c: &[Entry]| c.iter().find(|e| e.0 == key && e.1 == *frame && (e.2, e.3) == (w, h)).map(|e| e.4.clone());
if let Some(img) = found(&CACHE.lock().unwrap_or_else(std::sync::PoisonError::into_inner)) {
return img;
}
let img = Arc::new(frame.sample(src, w, h));
let mut c = CACHE.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
c.push((key, frame.clone(), w, h, img.clone()));
if c.len() > FRAME_PROXIES_KEPT {
c.remove(0);
}
img
}
/// The airlight is estimated from every `AIRLIGHT_STEP`-th value of the dark channel.
pub const AIRLIGHT_STEP: usize = 7;
/// Airlight estimate: bright end of the dark channel.
pub fn airlight(dark: &Plane) -> f32 {
airlight_of(dark.data.iter().step_by(AIRLIGHT_STEP).copied().collect())
}
/// Airlight from the sampled dark-channel values (see [`airlight`]): their 99.5th percentile.
pub fn airlight_of(mut v: Vec<f32>) -> f32 {
if v.is_empty() {
return 1.0;
}
let k = ((v.len() as f32) * 0.995) as usize;
let k = k.min(v.len() - 1);
v.select_nth_unstable_by(k, |a, b| a.total_cmp(b));
v[k].max(0.05)
}
#[cfg(test)]
mod tests {
use super::*;
use lightcraft_color::cct::xy_to_uv;
#[test]
fn guided_preserves_step_edges_and_flattens_texture() {
let p = Plane::from_fn(80, 20, |x, y| if x < 40 { -2.0 } else { 2.0 } + if (x + y) % 2 == 0 { 0.05 } else { -0.05 });
let q = guided(&p, 4.0, 0.3);
// edge kept
assert!(q.get(35, 10) < -1.5 && q.get(45, 10) > 1.5);
// checkerboard texture removed away from the edge
assert!((q.get(10, 10) - q.get(11, 10)).abs() < 0.02);
}
#[test]
fn wb_as_shot_is_identity_and_warming_adds_red() {
let s = DevelopSettings::default();
let info = SourceInfo::default();
let mut img = Rgb32f::filled(4, 4, [0.3, 0.3, 0.3]);
scene_linear_pre(&mut img, &info, &s);
assert!((img.get(0, 0)[0] - 0.3).abs() < 1e-6);
let mut warm = s.clone();
warm.wb.mode = lightcraft_develop::WbMode::Custom;
warm.wb.temp = 9000.0;
let mut img2 = Rgb32f::filled(4, 4, [0.3, 0.3, 0.3]);
scene_linear_pre(&mut img2, &info, &warm);
let c = img2.get(0, 0);
assert!(c[0] > c[2], "{c:?}");
// luminance preserved
assert!((luminance_2020(c) - 0.3).abs() < 0.01);
}
// A windowed render's airlight proxy is kept per (source, frame, size): the airlight is the one sampling anew gives,
// and another source or frame of the same size gets its own, never the proxy kept for the last one
#[test]
fn the_kept_airlight_proxy_matches_a_fresh_one_and_misses_on_another_source_or_frame() {
let (info, s) = (SourceInfo::default(), DevelopSettings::default());
let hazy = Rgb32f::from_fn(300, 200, |x, y| {
let v = 0.05 + 0.6 * x as f32 / 300.0;
[v, v * 0.95, v * (0.8 + 0.2 * y as f32 / 200.0)]
});
let other = Rgb32f::from_fn(300, 200, |x, y| [0.02 + 0.1 * ((x + y) % 7) as f32 / 7.0; 3]);
let fresh = |src: &Rgb32f, frame: &Frame| {
let mut img = frame.sample(src, 48, 32);
white_balance(&mut img, &info, &s);
airlight(&gaussian(&img.map(dark_of), (0.02 * frame.px_per_long(48)).max(1.0) as f32))
};
let frame = crate::frame_for(&hazy, &info, &s, true);
let kept = frame_proxy(&hazy, &frame, 48, 32);
assert!(Arc::ptr_eq(&kept, &frame_proxy(&hazy, &frame, 48, 32)), "the second ask is served from the cache");
assert_eq!(frame_airlight(&hazy, &info, &s, &frame, 48, 32).to_bits(), fresh(&hazy, &frame).to_bits());
assert_eq!(frame_airlight(&hazy, &info, &s, &frame, 48, 32).to_bits(), fresh(&hazy, &frame).to_bits());
// another source of the same size
assert_ne!(fresh(&other, &frame), fresh(&hazy, &frame));
assert_eq!(frame_airlight(&other, &info, &s, &frame, 48, 32).to_bits(), fresh(&other, &frame).to_bits());
// the same source, cropped to its darker half
let mut cropped = frame.clone();
cropped.crop.rect.x1 = 0.5;
assert_ne!(fresh(&hazy, &cropped), fresh(&hazy, &frame));
assert_eq!(frame_airlight(&hazy, &info, &s, &cropped, 48, 32).to_bits(), fresh(&hazy, &cropped).to_bits());
}
#[test]
fn a_hidden_or_empty_mask_does_not_request_the_tone_base() {
use lightcraft_develop::{Mask, MaskComponent, MaskOp, MaskShape};
use lightcraft_geom::Point;
let shape = MaskShape::Linear { start: Point::new(0.0, 0.0), end: Point::new(1.0, 0.0) };
let component = MaskComponent { name: None, op: MaskOp::Add, invert: false, shape };
let mut s = DevelopSettings::default();
s.masks.push(Mask {
visible: false,
components: vec![component],
adjust: lightcraft_develop::LocalAdjustments { highlights: 40.0, ..Default::default() },
..Default::default()
});
assert!(plane_sigmas(&s, 2000.0, Quality::Full).base.is_none());
s.masks[0].visible = true;
s.masks[0].components.clear();
assert!(plane_sigmas(&s, 2000.0, Quality::Full).base.is_none());
s.masks[0].components.push(MaskComponent {
name: None,
op: MaskOp::Add,
invert: false,
shape: MaskShape::Linear { start: Point::new(0.0, 0.0), end: Point::new(1.0, 0.0) },
});
assert!(plane_sigmas(&s, 2000.0, Quality::Full).base.is_some());
}
#[test]
fn exposure_doubles() {
let mut s = DevelopSettings::default();
s.light.exposure = 1.0;
let mut img = Rgb32f::filled(2, 2, [0.1, 0.2, 0.3]);
scene_linear_pre(&mut img, &SourceInfo::default(), &s);
assert!((img.get(1, 1)[2] - 0.6).abs() < 1e-5);
}
/// [`kelvin_to_legacy`] inverts [`legacy_to_kelvin`] over the whole relative slider range and back: the
/// round trip returns the start, in both directions, to the precision of the Kelvin / tint parametrisation.
#[test]
fn kelvin_to_legacy_inverts_legacy_to_kelvin() {
for as_shot in [(4986.0, -2.0), (6500.0, 0.0), (3200.0, 14.0), (9500.0, -12.0)] {
// the relative slider's whole reach: temp 0..100 (2000–13550 K around 6500), tint −150..150
for pct in [0.0, 12.5, 25.0, 50.0, 75.0, 100.0] {
for tint in [-150.0, -40.0, 0.0, 33.0, 150.0] {
let (lt, ltint) = controls_relative(pct, tint);
let (kt, ktint) = legacy_to_kelvin(lt, ltint, as_shot);
// past the Kelvin parametrisation's own reach (its search runs 1000–50000 K) the forward
// conversion has already clamped, so the pair is not expected to come back
if !(1050.0..=49_000.0).contains(&kt) {
continue;
}
let back = kelvin_to_legacy(kt, ktint, as_shot).unwrap();
let (x1, y1) = xy_to_uv(temp_tint_to_xy(lt, ltint));
let (x2, y2) = xy_to_uv(temp_tint_to_xy(back.0, back.1));
// the pair may differ by more than the slider's own step at the extreme corners (the Kelvin
// parametrisation is read back through a search), but never by much, and the white it renders
// is the same
assert!(
(back.0 - lt).abs() <= lt.abs() * 0.01 && (back.1 - ltint).abs() < 0.5,
"as shot {as_shot:?}: relative ({lt}, {ltint}) -> kelvin ({kt}, {ktint}) -> {back:?}"
);
assert!((x1 - x2).hypot(y1 - y2) < 1e-3, "as shot {as_shot:?}: relative ({lt}, {ltint}) -> kelvin ({kt}, {ktint}) -> {back:?}");
}
}
// the other direction: every white the relative scale can hold maps back to a pair that renders the same
for kt in [as_shot.0, 2500.0, 4200.0, 6500.0, 12000.0] {
for ktint in [-60.0, 0.0, 45.0] {
let Some((lt, ltint)) = kelvin_to_legacy(kt, ktint, as_shot) else { continue };
// outside the slider's reach the relative scale has no value for this white: the forward
// conversion would clamp, so the round trip is not expected to close there
if !(2000.0..=13550.0).contains(<) || ltint.abs() > 150.0 {
continue;
}
let (kt2, ktint2) = legacy_to_kelvin(lt, ltint, as_shot);
let (x1, y1) = xy_to_uv(temp_tint_to_xy(kt, ktint));
let (x2, y2) = xy_to_uv(temp_tint_to_xy(kt2, ktint2));
assert!(
(x1 - x2).hypot(y1 - y2) < 1e-3,
"as shot {as_shot:?}: kelvin ({kt}, {ktint}) -> relative ({lt}, {ltint}) -> ({kt2}, {ktint2})"
);
}
}
// the source's own white is the relative scale's as-shot reference, exactly
assert_eq!(kelvin_to_legacy(as_shot.0, as_shot.1, as_shot), Some((6500.0, 0.0)));
}
// the exact inverse of the special case inside legacy_to_kelvin
assert_eq!(kelvin_to_legacy(4986.0, -2.0, (4986.0, -2.0)), Some((6500.0, 0.0)));
assert_eq!(legacy_to_kelvin(6500.0, 0.0, (4986.0, -2.0)), (4986.0, -2.0));
// hostile numbers answer None, never a panic or a plausible lie
assert_eq!(kelvin_to_legacy(f64::NAN, 0.0, (5000.0, 0.0)), None);
assert_eq!(kelvin_to_legacy(5000.0, f64::INFINITY, (5000.0, 0.0)), None);
assert_eq!(kelvin_to_legacy(5000.0, 0.0, (f64::NAN, 0.0)), None);
}
/// The relative scale's `(temp, tint)` for a slider temperature at `pct` % (2000–7000 K fill the left half) and tint.
fn controls_relative(pct: f64, tint: f64) -> (f64, f64) {
let t = if pct <= 50.0 { 2000.0 + pct * 100.0 } else { 7000.0 + (pct - 50.0) * 131.0 };
(t, tint)
}
}
#[cfg(test)]
mod fast_tests {
use super::*;
/// The previous fast path, including its two materialized coefficient resizes.
fn resized_reference(p: &Plane, sigma: f32, eps: f32) -> Plane {
use lightcraft_raster::resample::{Filter, resize};
let s = guided_fast_step(sigma);
if s <= 1 {
return guided(p, sigma, eps);
}
let lo = resize(p, p.width.div_ceil(s).max(1), p.height.div_ceil(s).max(1), Filter::Box);
let (a, b) = guided_coeffs(&lo, sigma / s as f32, eps);
let (a, b) = (resize(&a, p.width, p.height, Filter::Bilinear), resize(&b, p.width, p.height, Filter::Bilinear));
Plane::from_fn(p.width, p.height, |x, y| a.get(x, y) * p.get(x, y) + b.get(x, y))
}
#[test]
fn sampled_coefficients_match_materialized_resizes() {
for (w, h) in [(0, 0), (1, 1), (1, 19), (23, 1), (2, 3), (17, 13), (64, 48), (161, 119)] {
let planes = [
Plane::filled(w, h, -3.25),
Plane::from_fn(w, h, |x, y| ((x * 7919 + y * 104729) % 97) as f32 / 7.0 - 8.0),
Plane::from_fn(w, h, |x, y| {
if x == w / 2 && y == h / 2 {
9.0
} else if x < w / 2 {
-5.0
} else {
2.0
}
}),
];
// Step 1 fallback, every small step, Draft's cap, and the maximum step (16).
for sigma in [0.0, 2.99, 5.99, 6.0, 9.0, 12.0, 24.0, 48.0, 200.0] {
for eps in [BASE_EPS, CLARITY_EPS, 0.0001] {
for p in &planes {
let expected = resized_reference(p, sigma, eps);
let actual = guided_fast(p, sigma, eps);
assert_eq!((actual.width, actual.height), (w, h));
assert_eq!(actual.data, expected.data, "{w}x{h}, sigma {sigma}, eps {eps}");
}
}
}
}
}
#[test]
fn sampled_tone_base_keeps_the_median_before_the_guided_filter() {
let p = Plane::from_fn(73, 51, |x, y| if x % 9 == 0 { 3.0 } else { -4.0 + ((x + y) % 7) as f32 * 0.03 });
for sigma in [6.0, 24.0, 120.0] {
for detail in [0, 1, 3, BASE_DETAIL_MAX] {
let median = median_hv(&p, detail);
assert_eq!(tone_base(&p, sigma, detail).data, resized_reference(&median, sigma, BASE_EPS).data, "{sigma}, {detail}");
}
}
}
#[test]
fn fast_guided_close_to_reference() {
let p = Plane::from_fn(160, 120, |x, y| ((x as f32 * 0.13).sin() + (y as f32 * 0.07).cos()) * 2.0 + if x > 80 { 1.5 } else { 0.0 });
let a = guided(&p, 12.0, 0.3);
let b = guided_fast(&p, 12.0, 0.3);
let err: f32 = a.data.iter().zip(&b.data).map(|(u, v)| (u - v).abs()).sum::<f32>() / a.len() as f32;
assert!(err < 0.08, "{err}");
}
}
#[cfg(test)]
mod nr_tests {
use super::*;
#[test]
fn luminance_nr_reduces_noise_keeps_mean() {
let mut img = Rgb32f::from_fn(64, 64, |x, y| {
let n = (((x * 7919 + y * 104729) % 97) as f32 / 97.0 - 0.5) * 0.06;
[0.2 + n; 3]
});
let var = |im: &Rgb32f| {
let m = im.data.iter().map(|p| p[1]).sum::<f32>() / im.len() as f32;
(im.data.iter().map(|p| (p[1] - m).powi(2)).sum::<f32>() / im.len() as f32, m)
};
let (v0, m0) = var(&img);
let mut s = DevelopSettings::default();
s.detail.nr_luminance = 80.0;
denoise(&mut img, &s, 1.0);
let (v1, m1) = var(&img);
assert!(v1 < v0 * 0.5, "{v0} -> {v1}");
assert!((m1 - m0).abs() < 0.01);
}
}