Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

LR-EDIT-LIGHT-HIGHLIGHTS: merge main; binned previews' clip mask on top of #607's reconstruction (#548)

Brandon Thomasec39ca1ad644
diff --cc crates/engine/src/files.rs
index 512c31d32,e1c54b342..93c214f8e
--- a/crates/engine/src/files.rs
+++ b/crates/engine/src/files.rs
@@@ -200,6 -202,86 +202,92 @@@ fn preview_reason(bytes: &[u8], e: ligh
/// Sensor clip level (normalised) for highlight reconstruction.
const HIGHLIGHT_CLIP: f32 = 0.99;
+ /// How a raw's camera RGB becomes the linear Rec.2020 the pipeline takes.
+ pub(crate) struct CameraColourModel<'a> {
+ /// White-balance multipliers (as shot).
+ pub wb: [f32; 3],
+ /// White-balanced camera RGB → linear Rec.2020: the camera matrix, or a look fitted to the camera's JPEG on top.
+ pub matrix: lightcraft_color::Mat3,
+ /// Baseline exposure, as a gain.
+ pub gain: f32,
+ /// A DNG profile's hue/saturation map and look table.
+ pub tables: Option<&'a lightcraft_raw::profile::ProfileTables>,
+ /// A fitted camera look's hue/saturation table.
+ pub hue_sat: Option<&'a crate::camera_preview::HueSat<'a>>,
+ }
+
+ impl CameraColourModel<'_> {
+ /// Rebuild sensor-clipped channels; fully clipped highlights become [`clipped_white`] of the matrix, so they
+ /// come out of [`Self::apply`] neutral.
+ pub fn rebuild_highlights(&self, img: &mut Rgb32f) {
+ lightcraft_raw::highlight::reconstruct_with(img, self.wb, HIGHLIGHT_CLIP, clipped_white(&self.matrix));
+ }
+
++ /// [`Self::rebuild_highlights`] for a binned image whose clipped channels `mask` tells
++ /// ([`lightcraft_raw::RawImage::develop_binned_masked`]).
++ pub fn rebuild_highlights_masked(&self, img: &mut Rgb32f, mask: &[u8]) {
++ lightcraft_raw::highlight::reconstruct_masked(img, self.wb, HIGHLIGHT_CLIP, clipped_white(&self.matrix), mask);
++ }
++
+ /// White balance, matrix, tables and baseline exposure, in place.
+ pub fn apply(&self, img: &mut Rgb32f) {
+ let (wb, m, gain) = (self.wb, self.matrix.to_f32(), self.gain);
+ img.map_in_place(|p| {
+ let c = [p[0] * wb[0], p[1] * wb[1], p[2] * wb[2]];
+ let rgb = [
+ 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],
+ ];
+ let rgb = match self.tables {
+ Some(tables) => tables.apply(rgb, gain),
+ None => rgb.map(|v| v * gain),
+ };
+ self.hue_sat.map_or(rgb, |h| h.apply(rgb)).map(|v| v.max(0.0))
+ });
+ }
+ }
+
+ /// The white-balanced camera RGB that `model` (white-balanced camera RGB → linear Rec.2020) renders
+ /// neutral, as bright as it renders `[1, 1, 1]`: what fully clipped highlights are rebuilt to, so they come out
+ /// neutral whatever the colour model does to the camera's own neutral (a look fitted to the camera's JPEG or a
+ /// camera profile often tints it by a few per cent, which any darkening of the highlights would show).
+ /// `[1, 1, 1]` (the camera's own neutral, as before this existed) when the model can't be trusted with it: not
+ /// invertible or nearly so, an answer far from the camera's neutral (a sane colour model moves white by a few per
+ /// cent), or one that isn't neutral through the `f32` matrix the decode applies.
+ pub(crate) fn clipped_white(model: &lightcraft_color::Mat3) -> [f32; 3] {
+ const CAMERA: [f32; 3] = [1.0; 3];
+ /// Largest condition number (max row sum norm) of a model trusted for this.
+ const CONDITION: f64 = 1e3;
+ /// Largest factor between the answer and the camera's neutral, per channel.
+ const SPREAD: f64 = 4.0;
+ let norm = |m: &lightcraft_color::Mat3| m.0.iter().map(|row| row.iter().map(|v| v.abs()).sum::<f64>()).fold(0.0, f64::max);
+ let Some(inverse) = model.inverse() else { return CAMERA };
+ let condition = norm(model) * norm(&inverse);
+ if !condition.is_finite() || condition > CONDITION {
+ return CAMERA;
+ }
+ let luma = |v: [f64; 3]| {
+ let c = model.apply(v);
+ (0..3).map(|i| c[i] * f64::from(lightcraft_color::LUMA_2020[i])).sum::<f64>()
+ };
+ let n = inverse.apply([1.0; 3]);
+ let (target, own) = (luma([1.0; 3]), luma(n));
+ if !(target > 0.0 && own > 0.0 && target.is_finite() && own.is_finite()) {
+ return CAMERA;
+ }
+ let white = n.map(|v| v * target / own);
+ if !white.iter().all(|v| v.is_finite() && (1.0 / SPREAD..=SPREAD).contains(v)) {
+ return CAMERA;
+ }
+ let white = white.map(|v| v as f32);
+ // neutral through the matrix as the decode applies it
+ let m = model.to_f32();
+ let out: [f32; 3] = std::array::from_fn(|i| m[i][0] * white[0] + m[i][1] * white[1] + m[i][2] * white[2]);
+ let (lo, hi) = (out[0].min(out[1]).min(out[2]), out[0].max(out[1]).max(out[2]));
+ if lo > 0.0 && hi.is_finite() && hi - lo <= 1e-3 * hi { white } else { CAMERA }
+ }
+
/// The largest block size to bin a raw's mosaic by for a source of at most `max_edge` pixels:
/// the binned image must keep at least 90 % of `max_edge` (a 16 MP sensor still bins 2× for the
/// 2560 px preview). X-Trans can only bin 3× (its 6×6 pattern), and its full demosaic is ~5× the
@@@ -315,24 -399,9 +407,12 @@@ fn load_bytes_now
let finish = |mut img: Rgb32f| {
let mut stages = vec![("develop", t0.elapsed())];
stages.push(("transform", t0.elapsed()));
- colour.rebuild_highlights(&mut img);
+ match &clip_mask {
- Some(mask) => lightcraft_raw::highlight::reconstruct_masked(&mut img, wb, HIGHLIGHT_CLIP, mask),
- None => lightcraft_raw::highlight::reconstruct(&mut img, wb, HIGHLIGHT_CLIP),
- };
++ Some(mask) => colour.rebuild_highlights_masked(&mut img, mask),
++ None => colour.rebuild_highlights(&mut img),
++ }
stages.push(("highlights", t0.elapsed()));
- img.map_in_place(|p| {
- let c = [p[0] * wb[0], p[1] * wb[1], p[2] * wb[2]];
- let rgb = [
- 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],
- ];
- let rgb = match &tables {
- Some(tables) => tables.apply(rgb, gain),
- None => rgb.map(|v| v * gain),
- };
- hue_sat.as_ref().map_or(rgb, |h| h.apply(rgb)).map(|v| v.max(0.0))
- });
+ colour.apply(&mut img);
stages.push(("colour", t0.elapsed()));
let img = fit(&img, max_edge, max_edge, Filter::Box);
stages.push(("fit", t0.elapsed()));
diff --cc crates/engine/src/media.rs
index 81e900ae7,1e2a7d3e5..a14093de8
--- a/crates/engine/src/media.rs
+++ b/crates/engine/src/media.rs
@@@ -62,7 -62,7 +62,7 @@@ impl SettingsHashes
}
/// Bump when the pipeline's output changes, to invalidate cached thumbnails.
- pub const RENDER_CACHE_VERSION: u64 = 26;
-pub const RENDER_CACHE_VERSION: u64 = 37;
++pub const RENDER_CACHE_VERSION: u64 = 38;
/// Thumbnails render at one of these long edges (so window/cell size changes reuse the cache).
pub const THUMB_SIZES: [usize; 4] = [128, 256, 384, 512];
diff --cc crates/raw/src/binned.rs
index 1be08089a,fde6c2c0b..674651c90
--- a/crates/raw/src/binned.rs
+++ b/crates/raw/src/binned.rs
@@@ -197,44 -177,6 +197,44 @@@ mod tests
assert!((xb.get(4, 3)[2] - 0.3).abs() < 2e-3);
}
+ /// Issue #548: specular points that clip one green sample in some 8 × 8 blocks of a grey surface.
+ /// Taking the block's maximum makes those blocks' green the clip level, which reconstruction then
+ /// keeps (a green speckle); the masked binning keeps the mean and marks the green as clipped.
+ #[test]
+ fn clipping_is_decided_before_averaging() {
+ let img = Rgb32f::filled(64, 64, [0.3, 0.3, 0.3]);
+ let mut raw = raw_from(&mosaic_from_rgb(&img, &Cfa::bayer("RGGB").unwrap()), 0.0, 4095.0);
+ if let RawData::U16(d) = &mut raw.data {
+ // one green sample (RGGB: odd column of an even row) in every other block
+ for by in 0..8 {
+ for bx in (by % 2..8).step_by(2) {
+ d[(by * 8 + 2) * 64 + bx * 8 + 3] = 4095;
+ }
+ }
+ }
+ let wb = [1.0f32; 3];
+ let (mut masked, mask) = raw.develop_binned_masked(8, 0.99).unwrap().unwrap();
+ assert_eq!(mask.len(), 64);
+ assert_eq!(mask.iter().filter(|&&m| m == highlight::CLIPPED_G).count(), 32);
+ assert!(mask.iter().all(|&m| m & (highlight::CLIPPED_R | highlight::CLIPPED_B) == 0));
+ // the clipped block's green is its mean: (31 · 0.3 + 1) / 32
+ assert!((masked.get(0, 0)[1] - (31.0 * 0.3 + 1.0) / 32.0).abs() < 1e-3, "{:?}", masked.get(0, 0));
- assert_eq!(highlight::reconstruct_masked(&mut masked, wb, 0.99, &mask), 32);
++ assert_eq!(highlight::reconstruct_masked(&mut masked, wb, 0.99, [1.0; 3], &mask), 32);
+ let green = |img: &Rgb32f| img.data.iter().map(|p| p[1] / p[0]).fold(0.0f32, f32::max);
+ // (no more than the mean itself: the clipped sample counts at the clip level)
+ assert!(green(&masked) < 1.08, "green excess {}", green(&masked));
+ // the maximum rule: a green speckle at the clip level
+ let mut max = raw.develop_binned(8, 0.99).unwrap().unwrap();
+ assert!(max.get(0, 0)[1] >= 0.99);
+ highlight::reconstruct(&mut max, wb, 0.99);
+ assert!(green(&max) > 3.0, "{}", green(&max));
+ // without clipped samples both are the same plain means, and the mask is empty
+ let clean = raw_from(&mosaic_from_rgb(&img, &Cfa::bayer("RGGB").unwrap()), 0.0, 4095.0);
+ let (a, m) = clean.develop_binned_masked(8, 0.99).unwrap().unwrap();
+ assert!(m.iter().all(|&v| v == 0));
+ assert!(a.data == clean.develop_binned(8, 0.99).unwrap().unwrap().data);
+ }
+
#[test]
fn binning_matches_demosaic_downscaled_on_smooth_images() {
let img = Rgb32f::from_fn(64, 48, |x, y| [0.2 + 0.3 * (x as f32 / 64.0), 0.4 - 0.2 * (y as f32 / 48.0), 0.25]);
diff --cc crates/raw/src/highlight.rs
index b79733664,7fc056600..bfebfe814
--- a/crates/raw/src/highlight.rs
+++ b/crates/raw/src/highlight.rs
@@@ -2,24 -2,22 +2,24 @@@
//!
//! - [`clip_neutral`]: white-balance-aware clipping so that sensor-clipped areas render neutral instead of
//! magenta/cyan (each channel limited to the smallest white-balanced clip level).
- //! - [`reconstruct`]: where only some channels are clipped, rebuild them from the unclipped channels using the
- //! local chromaticity of nearby unclipped pixels (diffused into the clipped region with a coarse-to-fine
- //! push-pull fill). Fully clipped pixels become neutral at the brightest plausible level.
+ //! - [`reconstruct`] / [`reconstruct_with`]: where only some channels are clipped, rebuild them from the
+ //! unclipped channels using the chromaticity of reliable unclipped content nearby (bright, away from edges and
+ //! from the clipped pixels themselves; diffused into the clipped region coarse to fine) unless both the pixel's
+ //! own channels and the clipped surface's colour just below its clip (the rim) contradict it, and neutral where
+ //! there is none within reach. Right past the rim, the rim's colour carries on. Fully clipped pixels become
+ //! neutral at the brightest plausible level; partly clipped ones fade into it on their way from the first channel
+ //! clipping to the last, pale ones from the start.
++//! - [`reconstruct_masked`]: the same for a binned image, whose clipped channels a mask tells (block means with a
++//! clipped sample among them).
//!
- //! Issue #548 (a bright cloud turning cyan once Highlights pulled it down, green speckles in binned
- //! previews) set three rules for the colour of partly clipped pixels:
- //! - an unclipped pixel's chromaticity counts with a confidence that grows with its brightness
- //! (`confidence`): the colour of a clipped highlight is told by the bright pixels around it, not by
- //! dark ones in front of it (wires and branches across a cloud, with their own colour and demosaicing
- //! fringes, were the nearest colour inside the cloud and tinted all of it). A dim pixel fills in only as
- //! much as its confidence allows, the rest comes from the coarser level, i.e. from brighter pixels
- //! further out; where there are none, dim pixels still decide, pooled over a larger area;
- //! - pixels within [`DEMOSAIC_REACH`] of a clipped one were demosaiced partly from clipped samples and
- //! give no colour (unless nothing else does);
- //! - the more channels of a pixel clip, the nearer to neutral its rebuilt colour (`rebuild`).
- //!
- //! A binned image ([`crate::RawImage::develop_binned_masked`]) says which channels are clipped with a
- //! mask instead of by value ([`reconstruct_masked`]).
+ //! Why the care: the white-balance gains push clipped channels apart (green clips first, red and blue keep
+ //! rising), so whatever colour the rebuilt pixels carry is hidden at Neutral only by the tone map's roll-off to
+ //! white. Any darkening (Highlights, negative Exposure or Whites) brings it out. Three things used to leak colour
+ //! in: the chromaticity of whatever unclipped pixel happened to be nearest (dark foliage, purple and green
+ //! fringes along branches, pixels demosaiced from clipped neighbours); a colour model that does not render the
+ //! camera's white-balanced neutral as neutral (a look fitted to the camera's JPEG, a camera profile); and, in a
+ //! pale partly clipped sky, the colour of another reliable surface (bright foliage) and the sky's own faint
+ //! colour, which darkening and a camera profile turned teal.
use lightcraft_raster::Rgb32f;
use rayon::prelude::*;
@@@ -35,240 -33,375 +35,412 @@@ pub fn clip_neutral(img: &mut Rgb32f, w
}
}
- /// A chromaticity premultiplied by its confidence, and the confidence (0..=1): `[w·r, w·g, w·b, w]`.
- /// `w = 0` is an empty entry (a clipped pixel, or nothing known yet).
- type Weighted = [f32; 4];
+ /// Unclipped pixels lend their chromaticity in proportion to how close they come to clipping (their largest
+ /// channel, relative to the clip level): from nothing below `SOURCE_LO` to fully above `SOURCE_HI`. Dark content
+ /// (foliage, branches, buildings against a clipped sky) says nothing about the sky's colour.
+ const SOURCE_LO: f32 = 0.25;
+ const SOURCE_HI: f32 = 0.6;
+ /// Sources on a strong edge (a 3 × 3 half-resolution neighbour brighter or darker by this many stops) are
+ /// left out, fading from `EDGE_LO` to `EDGE_HI`: edge pixels mix two surfaces and carry lens fringes.
+ const EDGE_LO: f32 = 0.15;
+ const EDGE_HI: f32 = 0.4;
+ /// Source cells within this many half-resolution cells of a clipped pixel are left out: their channels were
+ /// demosaiced partly from clipped samples. (At least 1: the same neighbourhood tells which cells clipped
+ /// pixels sample.)
+ const MARGIN: usize = 1;
+ /// Weight of the coarser estimate against a cell's own sources when the pyramid is pushed back down (in units
+ /// of source density: a cell half covered by full-weight sources counts 0.5).
+ const PRIOR: f32 = 0.01;
+ /// The rebuilt colour fades to neutral where reliable content covers less than `SUPPORT` of the surroundings at
+ /// every scale up to `REACH` of the long edge (so previews and exports agree), gradually, as the coverage of the
+ /// widest one falls: a clipped sky between bare branches is better white than the colour of a river far away.
+ const REACH: f32 = 1.0 / 4.0;
+ const SUPPORT: f32 = 0.05;
+ /// An unclipped channel above `FLOOR_LO` of its clip level counts more and more as the clip floor it is about to
+ /// become (see [`reconstruct_with`]).
+ const FLOOR_LO: f32 = 0.75;
+ /// A clipped pixel takes its estimated colour (reliable colour around, or the neutral guess) only as far as it is
+ /// brighter (or otherwise unlike) the unclipped pixels nearest its clip: the "rim", unclipped pixels above `RIM_LO`
+ /// of the clip level, weighted towards the clip, the clipped surface's own colour just below it. Up to `NEAR_RIM`
+ /// stops past the rim (the rim is an average, a little below the clip) it is rebuilt with the rim's colour, and
+ /// over the next `DEPTH` stops it goes over to the estimate, so a surface runs into clipping without a step
+ /// whatever its colour and whatever lent the estimate; well past it, or where the rim is another surface (its
+ /// colour differs; a clipped channel differs by at least as much as it lies above the rim's), the estimate applies.
+ const RIM_LO: f32 = 0.85;
+ const NEAR_RIM: f32 = 0.03;
+ const DEPTH: f32 = 0.2;
+ /// Reliable colour counts less and less as both the pixel's own channels (see [`contradiction`]) and the rim's
+ /// colour contradict it, by `AGREE.0` to `AGREE.1` stops: it is another surface's, like bright foliage below a pale
+ /// sky, whose green would turn the sky teal. (Either alone can be wrong: a pixel's own channels are noisy and
+ /// can't speak for the channels that clipped, and the rim can be another surface, like a white window frame
+ /// around a bright view.)
+ const AGREE: (f32, f32) = (0.15, 0.4);
+ /// On the way from the first channel clipping to the last, a partly clipped pixel's brightness goes over to the
+ /// clipped neutral's in the second half, and so does the colour of a strongly coloured one (saturation relative to
+ /// the white above `PALE.1`). A pale one's colour (below `PALE.0`) goes over from the start: the few per cent of
+ /// colour left in a pale sky once a channel clipped is as much the estimate's error as the sky's, and darkening
+ /// shows it.
+ const PALE: (f32, f32) = (0.2, 0.4);
- /// Brightness (as a share of the clip level) from which an unclipped pixel's colour counts fully.
- const FULL_CONFIDENCE: f32 = 0.8;
+ #[inline]
+ fn smoothstep(e0: f32, e1: f32, x: f32) -> f32 {
+ let t = ((x - e0) / (e1 - e0)).clamp(0.0, 1.0);
+ t * t * (3.0 - 2.0 * t)
+ }
- /// How far an unclipped pixel's chromaticity is trusted to stand for a clipped highlight next to it:
- /// `(m / (0.8 · clip))⁴` for its brightest channel `m`, at most 1 (from 80 % of the clip level up;
- /// half of the clip level 0.15, a third of it 0.03, a tenth of it 0.0002).
+ /// One level of a pyramid, `w × h` cells of `N - 1` values and a weight. Pulled: per cell the weighted sums of
+ /// the values and the weight, as densities (per pixel of the cell). Pushed: the estimated values, and still the
+ /// weight. The chromaticity pyramid holds red and blue chromaticity, the rim pyramid white-balanced RGB.
+ struct Level<const N: usize> {
+ w: usize,
+ h: usize,
+ cells: Vec<[f32; N]>,
+ }
+
+ /// Bilinear sample of a level at the centre of pixel `(x, y)` of a grid `scale` times finer.
#[inline]
- fn confidence(p: &[f32; 3], clip: f32) -> f32 {
- let m = (p[0].max(p[1]).max(p[2]) / (FULL_CONFIDENCE * clip)).clamp(0.0, 1.0);
- let m2 = m * m;
- m2 * m2
+ fn sample<const N: usize>(l: &Level<N>, scale: f32, x: usize, y: usize) -> [f32; N] {
+ let (cv, cw, ch) = (&l.cells, l.w, l.h);
+ let fx = ((x as f32 + 0.5) / scale - 0.5).clamp(0.0, cw.saturating_sub(1) as f32);
+ let fy = ((y as f32 + 0.5) / scale - 0.5).clamp(0.0, ch.saturating_sub(1) as f32);
+ let (x0, y0) = (fx.floor() as usize, fy.floor() as usize);
+ let (x1, y1) = ((x0 + 1).min(cw.saturating_sub(1)), (y0 + 1).min(ch.saturating_sub(1)));
+ let (tx, ty) = (fx - x0 as f32, fy - y0 as f32);
+ let g = |xx: usize, yy: usize| cv.get(yy * cw + xx).copied().unwrap_or([0.0; N]);
+ let (a, b, c, d) = (g(x0, y0), g(x1, y0), g(x0, y1), g(x1, y1));
+ std::array::from_fn(|k| {
+ let top = a[k] + (b[k] - a[k]) * tx;
+ let bot = c[k] + (d[k] - c[k]) * tx;
+ top + (bot - top) * ty
+ })
}
- /// Fill `values` coarse to fine (push-pull): each entry keeps its own colour by its confidence `w`
- /// and takes the rest, `1 − w`, from the next coarser level. With confidences of only 0 and 1 this is
- /// a plain fill of the empty entries from the mean of the known ones nearby.
- fn fill(w: usize, h: usize, values: &mut [Weighted]) {
- if w * h <= 1 || values.iter().all(|v| v[3] >= 1.0) || !values.iter().any(|v| v[3] > 0.0) {
- return;
- }
+ /// The half-resolution source level of `img`: every `2 × 2` block's source-weighted chromaticity sums, with
+ /// blocks near clipped pixels or on strong edges left out. Also returns, per cell, whether a clipped pixel lies
+ /// in its row within [`MARGIN`] cells (the vertical half of that test is left to the readers).
-fn sources(img: &Rgb32f, wb: [f32; 3], clip: f32) -> (Level<3>, Vec<bool>) {
++fn sources(img: &Rgb32f, wb: [f32; 3], clip: f32, clipped: &impl Clipped) -> (Level<3>, Vec<bool>) {
+ let (w, h) = (img.width, img.height);
let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
- let mut cv = vec![[0f32; 4]; cw * ch];
- {
- let values = &*values;
- downsample(w, h, &mut cv, |i| values[i]);
- }
- fill(cw, ch, &mut cv);
- values.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
- for (x, v) in row.iter_mut().enumerate() {
- let rest = 1.0 - v[3];
- if rest > 0.0 {
- let u = upsample(&cv, cw, ch, x, y);
- for c in 0..4 {
- v[c] += rest * u[c];
+ // per cell: [red sum, blue sum, weight] densities, luminance, whether it holds a clipped pixel
+ let mut cells = vec![[0f32; 3]; cw * ch];
+ let mut lum = vec![0f32; cw * ch];
+ let mut near = vec![false; cw * ch];
+ cells.par_chunks_mut(cw).zip(lum.par_chunks_mut(cw)).zip(near.par_chunks_mut(cw)).enumerate().for_each(|(y, ((crow, lrow), nrow))| {
+ let mut touched = vec![false; cw];
+ let row = |r: usize| img.data.get(r * w..(r + 1) * w).unwrap_or(&[]);
+ let (r0, r1) = (row(2 * y), row(2 * y + 1));
+ let lo = SOURCE_LO * clip;
+ for x in 0..cw {
+ let (mut a, mut sum, mut n, mut hit) = ([0f32; 3], 0f32, 0usize, false);
+ let span = 2 * x..(2 * x + 2).min(w);
- for p in r0.get(span.clone()).unwrap_or(&[]).iter().chain(r1.get(span).unwrap_or(&[])) {
++ let (i0, i1) = (2 * y * w + 2 * x, (2 * y + 1) * w + 2 * x);
++ let pixels = r0.get(span.clone()).unwrap_or(&[]).iter().zip(i0..).chain(r1.get(span).unwrap_or(&[]).iter().zip(i1..));
++ for (p, i) in pixels {
+ let q = [p[0] * wb[0], p[1] * wb[1], p[2] * wb[2]];
+ let s = q[0].max(0.0) + q[1].max(0.0) + q[2].max(0.0);
+ sum += s;
+ n += 1;
+ let top = p[0].max(p[1]).max(p[2]);
- if top >= clip {
++ if clipped(i, p).iter().any(|&c| c) {
+ hit = true;
+ continue;
+ }
+ if top > lo && s > 1e-4 {
+ let b = smoothstep(SOURCE_LO, SOURCE_HI, top / clip) / s;
+ a[0] += b * q[0].max(0.0);
+ a[1] += b * q[2].max(0.0);
+ a[2] += b * s;
}
}
+ let k = 1.0 / n.max(1) as f32;
+ crow[x] = a.map(|v| v * k);
+ lrow[x] = (sum * k).max(1e-6);
+ touched[x] = hit;
+ }
+ for (x, nr) in nrow.iter_mut().enumerate() {
+ *nr = touched.get(x.saturating_sub(MARGIN)..=(x + MARGIN).min(cw - 1)).is_some_and(|t| t.iter().any(|&t| t));
}
});
+ // leave out cells near clipped pixels and on strong edges (luminance ratios to the neighbours)
+ let (lum, near_ref) = (&lum, &near);
+ let (edge_lo, edge_hi) = (EDGE_LO.exp2(), EDGE_HI.exp2());
+ cells.par_chunks_mut(cw).enumerate().for_each(|(y, row)| {
+ let (ya, yb) = (y.saturating_sub(MARGIN), (y + MARGIN).min(ch - 1));
+ let (ea, eb) = (y.saturating_sub(1), (y + 1).min(ch - 1));
+ for (x, cell) in row.iter_mut().enumerate() {
+ if cell[2] <= 0.0 {
+ continue;
+ }
+ if (ya..=yb).any(|yy| near_ref.get(yy * cw + x).copied().unwrap_or(false)) {
+ *cell = [0.0; 3];
+ continue;
+ }
+ let l = lum.get(y * cw + x).copied().unwrap_or(1.0);
+ let (xa, xb) = (x.saturating_sub(1), (x + 1).min(cw - 1));
+ let (mut lo, mut hi) = (l, l);
+ for yy in ea..=eb {
+ for &v in lum.get(yy * cw + xa..=yy * cw + xb).unwrap_or(&[]) {
+ lo = lo.min(v);
+ hi = hi.max(v);
+ }
+ }
+ let keep = 1.0 - smoothstep(edge_lo, edge_hi, (hi / l).max(l / lo));
+ *cell = cell.map(|v| v * keep);
+ }
+ });
+ (Level { w: cw, h: ch, cells }, near)
}
- /// One pyramid step: each `2 × 2` block of a `w × h` level (`at(i)`: the entry at index `i`) becomes, in
- /// `cv` (`⌈w/2⌉ × ⌈h/2⌉`), the confidence-weighted mean of its colours, with their summed confidence
- /// (at most 1).
- fn downsample(w: usize, h: usize, cv: &mut [Weighted], at: impl Fn(usize) -> Weighted + Sync) {
- let cw = w.div_ceil(2);
- cv.par_chunks_mut(cw).enumerate().for_each(|(y, row)| {
- for (x, out) in row.iter_mut().enumerate() {
- let mut s = [0f32; 4];
- for dy in 0..2 {
- for dx in 0..2 {
- let (sx, sy) = (2 * x + dx, 2 * y + dy);
- if sx < w && sy < h {
- let v = at(sy * w + sx);
- for c in 0..4 {
- s[c] += v[c];
- }
+ /// The quarter-resolution rim level of `img`: every `4 × 4` block's white-balanced RGB of its unclipped pixels
+ /// above [`RIM_LO`] of the clip level, weighted (steeply) towards the clip, as densities. Pixels next to clipped
+ /// ones (`margin`, by half-resolution cell) are left out, like the colour sources: they were demosaiced partly from
+ /// clipped samples, and lens fringes along clipped content would pass for its colour.
-fn rim(img: &Rgb32f, wb: [f32; 3], clip: f32, margin: &(dyn Fn(usize, usize) -> bool + Sync)) -> Level<4> {
++fn rim(img: &Rgb32f, wb: [f32; 3], clip: f32, clipped: &impl Clipped, margin: &(dyn Fn(usize, usize) -> bool + Sync)) -> Level<4> {
+ let (w, h) = (img.width, img.height);
+ let (cw, ch) = (w.div_ceil(4), h.div_ceil(4));
+ let mut cells = vec![[0f32; 4]; cw * ch];
+ cells.par_chunks_mut(cw).enumerate().for_each(|(y, row)| {
+ for (x, cell) in row.iter_mut().enumerate() {
+ let (mut a, mut n) = ([0f32; 4], 0usize);
+ for sy in 4 * y..(4 * y + 4).min(h) {
+ for (i, p) in img.data.get(sy * w + 4 * x..sy * w + (4 * x + 4).min(w)).unwrap_or(&[]).iter().enumerate() {
+ n += 1;
+ let top = p[0].max(p[1]).max(p[2]) / clip;
- if !(RIM_LO..1.0).contains(&top) || margin((4 * x + i) / 2, sy / 2) {
++ if !(RIM_LO..1.0).contains(&top) || margin((4 * x + i) / 2, sy / 2) || clipped(sy * w + 4 * x + i, p).iter().any(|&c| c) {
+ continue;
+ }
+ let k = ((top - RIM_LO) / (1.0 - RIM_LO)).powi(4);
+ for c in 0..3 {
+ a[c] += k * p[c] * wb[c];
}
+ a[3] += k;
}
}
- if s[3] > 0.0 {
- let k = s[3].min(1.0) / s[3];
- *out = s.map(|v| v * k);
+ *cell = a.map(|v| v / n.max(1) as f32);
+ }
+ });
+ Level { w: cw, h: ch, cells }
+ }
+
+ /// The next coarser level: each `2 × 2` block's mean densities.
+ fn pull<const N: usize>(l: &Level<N>) -> Level<N> {
+ let (w, h) = (l.w.div_ceil(2), l.h.div_ceil(2));
+ let mut cells = vec![[0f32; N]; w * h];
+ cells.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
+ for (x, cell) in row.iter_mut().enumerate() {
+ let (mut s, mut n) = ([0f32; N], 0usize);
+ for (sx, sy) in [(2 * x, 2 * y), (2 * x + 1, 2 * y), (2 * x, 2 * y + 1), (2 * x + 1, 2 * y + 1)] {
+ if let Some(c) = (sx < l.w && sy < l.h).then(|| l.cells.get(sy * l.w + sx)).flatten() {
+ for k in 0..N {
+ s[k] += c[k];
+ }
+ n += 1;
+ }
}
+ *cell = s.map(|v| v / n.max(1) as f32);
}
});
+ Level { w, h, cells }
}
- /// Bilinear sample of the `cw × ch` coarse level at fine pixel `(x, y)` (twice the resolution).
- fn upsample(cv: &[Weighted], cw: usize, ch: usize, x: usize, y: usize) -> Weighted {
- let fx = ((x as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (cw - 1) as f32);
- let fy = ((y as f32 + 0.5) / 2.0 - 0.5).clamp(0.0, (ch - 1) as f32);
- let (x0, y0) = (fx.floor() as usize, fy.floor() as usize);
- let (x1, y1) = ((x0 + 1).min(cw - 1), (y0 + 1).min(ch - 1));
- let (tx, ty) = (fx - x0 as f32, fy - y0 as f32);
- let g = |xx: usize, yy: usize| cv[yy * cw + xx];
- let mut v = [0f32; 4];
- for c in 0..4 {
- let top = g(x0, y0)[c] + (g(x1, y0)[c] - g(x0, y0)[c]) * tx;
- let bot = g(x0, y1)[c] + (g(x1, y1)[c] - g(x0, y1)[c]) * tx;
- v[c] = top + (bot - top) * ty;
+ /// The estimated values of the cells of `levels[0]` that `needed` marks (all of them when `None`), and of every
+ /// cell of the coarser levels: each level's own sources blended with the next coarser estimate. The top level is
+ /// its own sources' weighted mean (normalised: however sparse they are in a large empty picture, the empty part
+ /// never pulls the estimate anywhere), and `empty` (at zero weight) when it has none at all.
+ fn push<const N: usize>(levels: &mut [Level<N>], empty: [f32; N], needed: Option<&(dyn Fn(usize, usize) -> bool + Sync)>) {
+ let estimate = |cell: [f32; N], prior: [f32; N]| -> [f32; N] {
+ let weight = cell[N - 1];
+ let d = weight + PRIOR;
+ std::array::from_fn(|k| if k + 1 == N { weight } else { (cell[k] + PRIOR * prior[k]) / d })
+ };
+ let Some(last) = levels.last_mut() else { return };
+ last.cells.iter_mut().for_each(|c| {
+ let weight = c[N - 1];
+ *c = if weight > 0.0 { std::array::from_fn(|k| if k + 1 == N { weight } else { c[k] / weight }) } else { empty };
+ });
+ for i in (0..levels.len().saturating_sub(1)).rev() {
+ let (fine, coarse) = levels.split_at_mut(i + 1);
+ let (Some(fine), Some(coarse)) = (fine.last_mut(), coarse.first()) else { continue };
+ let w = fine.w;
+ let needed = needed.filter(|_| i == 0);
+ fine.cells.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
+ for (x, cell) in row.iter_mut().enumerate() {
+ if needed.is_some_and(|f| !f(x, y)) {
+ continue;
+ }
+ *cell = estimate(*cell, sample(coarse, 2.0, x, y));
+ }
+ });
}
- v
}
- /// White-balanced chromaticity of an unclipped pixel with its confidence (empty: clipped or too dark to tell).
- #[inline]
- fn chroma_of(p: &[f32; 3], clipped: [bool; 3], wb: [f32; 3], clip: f32) -> Weighted {
- if clipped.iter().any(|&b| b) {
- return [0.0; 4];
+ /// How many stops two chromaticities differ by in any channel ratio.
+ fn unlike(a: [f32; 3], b: [f32; 3]) -> f32 {
+ let ratio = |i: usize, j: usize| (a[i].max(1e-6) * b[j].max(1e-6)) / (a[j].max(1e-6) * b[i].max(1e-6));
+ [(0, 1), (0, 2), (1, 2)].iter().map(|&(i, j)| ratio(i, j).log2().abs()).fold(0.0f32, f32::max)
+ }
+
+ /// How far channel `c` of a pixel (sensor values `p`, clipped channels `cl`) only bounds its true value from below:
+ /// fully once clipped, and increasingly from `FLOOR_LO` of the clip on, so that nothing compared with it jumps where
+ /// it clips.
+ fn bound(p: [f32; 3], cl: [bool; 3], clip: f32, c: usize) -> f32 {
+ if cl[c] { 1.0 } else { smoothstep(FLOOR_LO, 1.0, p[c] / clip) }
+ }
+
+ /// How many stops the channels of a partly clipped pixel (sensor values `p`, white-balanced `q`, clipped channels
+ /// `cl`) contradict chromaticity `r`: how far the levels they imply spread, where a clipped channel's only bounds the
+ /// level from below (see [`bound`]). 0 for the pixel's own colour, whatever its level.
+ fn contradiction(p: [f32; 3], q: [f32; 3], cl: [bool; 3], r: [f32; 3], clip: f32) -> f32 {
+ let (mut lo, mut hi) = (f32::INFINITY, 0f32);
+ for c in 0..3 {
+ let level = q[c] / r[c].max(1e-3);
+ hi = hi.max(level);
+ let w = bound(p, cl, clip, c);
+ if r[c] > 1e-3 && w < 1.0 {
+ lo = lo.min(level / (1.0 - w));
+ }
}
- let q = [p[0] * wb[0], p[1] * wb[1], p[2] * wb[2]];
- let s = q[0] + q[1] + q[2];
- let w = confidence(p, clip);
- if s > 1e-4 && w > 0.0 { [q[0].max(0.0) / s * w, q[1].max(0.0) / s * w, q[2].max(0.0) / s * w, w] } else { [0.0; 4] }
+ if hi > 0.0 && lo > 0.0 && lo.is_finite() { (hi / lo).log2().max(0.0) } else { 0.0 }
}
- /// Bits of a [`reconstruct_masked`] mask entry: which channels of the pixel are clipped.
- pub const CLIPPED_R: u8 = 1;
- pub const CLIPPED_G: u8 = 2;
- pub const CLIPPED_B: u8 = 4;
+ /// The white-balanced colour of a partly clipped pixel (sensor values `p`, white-balanced `q`, clipped channels
+ /// `cl`) whose colour is estimated at chromaticity `r`: clipped channels rebuilt, unclipped ones kept. `None`
+ /// when no unclipped channel can tell the level.
+ fn rebuild(p: [f32; 3], q: [f32; 3], cl: [bool; 3], r: [f32; 3], clip: f32, max_level: f32) -> Option<[f32; 3]> {
+ // The white-balanced channel sum the unclipped channels imply: their mean, except that a channel about to clip
+ // counts more and more as the floor it is about to become (continuous with the neighbours where it has).
+ let (mut sum, mut near_clip, mut k) = (0f32, 0f32, 0usize);
+ for c in 0..3 {
+ if !cl[c] && r[c] > 1e-3 {
+ let s = q[c] / r[c];
+ sum += s;
+ near_clip = near_clip.max(s * smoothstep(FLOOR_LO, 1.0, p[c] / clip));
+ k += 1;
+ }
+ }
+ if k == 0 {
+ return None;
+ }
+ // Two clipped channels must not be left at their clip levels: those differ by the white-balance gains (green
+ // clips first), and the pair would show as magenta or red. They keep the estimate's ratio instead, at the level
+ // that lifts each to its clip (no channel above the clipped neutral), and so does a channel about to clip (the
+ // same floor, so nothing jumps where it clips).
+ let floor = (0..3).filter(|&c| cl[c]).map(|c| q[c] / r[c].max(1e-3)).fold(0.0f32, f32::max);
+ let top = r[0].max(r[1]).max(r[2]).max(1e-3);
+ let sum = (sum / k as f32).max(near_clip.max(floor).min(max_level / top));
+ Some(std::array::from_fn(|c| if cl[c] { q[c].max(sum * r[c]) } else { q[c] }))
+ }
- /// Reconstruct partially clipped channels. `clip` is the sensor clip level in normalised units (use slightly
- /// below 1.0, e.g. 0.99), `wb` the white-balance multipliers that will be applied afterwards.
- /// Returns the number of pixels that had at least one clipped channel.
- ///
- /// The chromaticity field is diffused coarse to fine from the unclipped pixels. A clipped pixel is
- /// never known at full resolution, so its chromaticity is always the bilinear sample of the
- /// half-resolution level: we start the pyramid there, straight from the image, and never build
- /// the full-resolution field (same result, a quarter of the memory and far less work).
- ///
- /// Demosaicing spreads clipped samples into the pixels around them (AHD's green filter and colour
- /// differences reach a few pixels), so their colour is not a measurement either: unclipped pixels
- /// within [`DEMOSAIC_REACH`] pixels of a clipped one only give the colour when nothing else does.
+ /// Reconstruct partially clipped channels, with a neutral that is neutral in camera RGB. See
+ /// [`reconstruct_with`].
pub fn reconstruct(img: &mut Rgb32f, wb: [f32; 3], clip: f32) -> usize {
- reconstruct_with(img, wb, clip, DEMOSAIC_REACH, |_, p| [p[0] >= clip, p[1] >= clip, p[2] >= clip])
+ reconstruct_with(img, wb, clip, [1.0; 3])
}
- /// How far (pixels) demosaicing carries a clipped sample into its neighbours' colour. On the issue #548
- /// iPhone frame (AHD), the cloud-edge pixels next to clipped green came out 25 % greener relative to red
- /// than those 6 px out, which match the sky (bilinear: 1–2 px).
- pub const DEMOSAIC_REACH: usize = 6;
-
- /// [`reconstruct`] with the clipped channels given by `mask` (one entry per pixel, bits
- /// [`CLIPPED_R`], [`CLIPPED_G`], [`CLIPPED_B`]) instead of by the values: for a binned image
- /// ([`crate::RawImage::develop_binned_masked`]), whose values are block means (a lower bound for
- /// a channel with clipped samples) and whose mask says which blocks held clipped samples of each
- /// colour. A mask of the wrong length leaves the image as it is (returns 0).
- pub fn reconstruct_masked(img: &mut Rgb32f, wb: [f32; 3], clip: f32, mask: &[u8]) -> usize {
+ /// Reconstruct partially clipped channels. `clip` is the sensor clip level in normalised units (use slightly
+ /// below 1.0, e.g. 0.99), `wb` the white-balance multipliers that will be applied afterwards, `white` the
+ /// white-balanced camera RGB that the colour model applied afterwards renders neutral (`[1, 1, 1]` for a model
+ /// that maps the white-balanced neutral to neutral), scaled to the brightness that `[1, 1, 1]` renders at.
+ /// Fully clipped pixels become `white` times the brightest plausible level; partly clipped ones take the
+ /// chromaticity of reliable unclipped content nearby, and that of `white` where there is none.
+ /// Returns the number of pixels that had at least one clipped channel; the others are left exactly as they were.
+ pub fn reconstruct_with(img: &mut Rgb32f, wb: [f32; 3], clip: f32, white: [f32; 3]) -> usize {
++ rebuild_clipped(img, wb, clip, white, &|_, p| by_value(p, clip))
++}
++
++/// Bits of a [`reconstruct_masked`] mask entry: which channels of the pixel had a clipped sample.
++pub const CLIPPED_R: u8 = 1;
++pub const CLIPPED_G: u8 = 2;
++pub const CLIPPED_B: u8 = 4;
++
++/// [`reconstruct_with`] for a binned image ([`crate::RawImage::develop_binned_masked`]): its values are block
++/// means, and `mask` (one entry per pixel, bits [`CLIPPED_R`], [`CLIPPED_G`], [`CLIPPED_B`]) says which colours
++/// had a clipped sample in the block. Those channels count as clipped with their mean as the lower bound of their
++/// true value, so one clipped specular sample among many unclipped ones neither escapes reconstruction nor
++/// (as the block's maximum would) lifts the channel to the clip level, which reads as a coloured speckle (#548).
++/// A mask of the wrong length is ignored (clipping is then told by the values alone).
++pub fn reconstruct_masked(img: &mut Rgb32f, wb: [f32; 3], clip: f32, white: [f32; 3], mask: &[u8]) -> usize {
+ if mask.len() != img.data.len() {
- return 0;
++ return reconstruct_with(img, wb, clip, white);
+ }
- // (a block mean is no interpolation: its neighbours are measurements)
- reconstruct_with(img, wb, clip, 0, |i, _| {
++ rebuild_clipped(img, wb, clip, white, &|i, p| {
+ let m = mask.get(i).copied().unwrap_or(0);
- [m & CLIPPED_R != 0, m & CLIPPED_G != 0, m & CLIPPED_B != 0]
++ let v = by_value(p, clip);
++ [v[0] || m & CLIPPED_R != 0, v[1] || m & CLIPPED_G != 0, v[2] || m & CLIPPED_B != 0]
+ })
+}
+
- /// Every pixel within `r` pixels (a square) of a `true` one of the `w × h` `marks`.
- fn dilate(w: usize, h: usize, marks: &[bool], r: usize) -> Vec<bool> {
- // a running count along rows, then along columns
- let run = |line: &[bool], out: &mut [bool]| {
- let n = line.len();
- let mut count = line.iter().take(r.min(n)).filter(|&&b| b).count();
- for (i, o) in out.iter_mut().enumerate() {
- if i + r < n && line[i + r] {
- count += 1;
- }
- if i > r && line[i - r - 1] {
- count -= 1;
- }
- *o = count > 0;
- }
- };
- let mut rows = vec![false; w * h];
- rows.par_chunks_mut(w).zip(marks.par_chunks(w)).for_each(|(out, line)| run(line, out));
- // columns: per-column counts of marked rows in the window, swept down the image in strips
- let mut cols = vec![false; w * h];
- let strip = 256usize;
- let rows = &rows;
- let parts: Vec<(usize, Vec<bool>)> = (0..w.div_ceil(strip))
- .into_par_iter()
- .map(|sx| {
- let (x0, x1) = (sx * strip, ((sx + 1) * strip).min(w));
- let n = x1 - x0;
- let mut count = vec![0u32; n];
- for y in 0..r.min(h) {
- for (c, &b) in count.iter_mut().zip(&rows[y * w + x0..y * w + x1]) {
- *c += u32::from(b);
- }
- }
- let mut out = vec![false; n * h];
- for y in 0..h {
- if y + r < h {
- let yy = y + r;
- for (c, &b) in count.iter_mut().zip(&rows[yy * w + x0..yy * w + x1]) {
- *c += u32::from(b);
- }
- }
- if y > r {
- let yy = y - r - 1;
- for (c, &b) in count.iter_mut().zip(&rows[yy * w + x0..yy * w + x1]) {
- *c -= u32::from(b);
- }
- }
- for (o, &c) in out[y * n..(y + 1) * n].iter_mut().zip(&count) {
- *o = c > 0;
- }
- }
- (x0, out)
- })
- .collect();
- for (x0, out) in parts {
- let n = out.len() / h.max(1);
- for y in 0..h {
- cols[y * w + x0..y * w + x0 + n].copy_from_slice(&out[y * n..(y + 1) * n]);
- }
- }
- cols
++/// Which channels of pixel `i` (values `p`) are clipped.
++trait Clipped: Fn(usize, &[f32; 3]) -> [bool; 3] + Sync {}
++impl<F: Fn(usize, &[f32; 3]) -> [bool; 3] + Sync> Clipped for F {}
++
++#[inline]
++fn by_value(p: &[f32; 3], clip: f32) -> [bool; 3] {
++ [p[0] >= clip, p[1] >= clip, p[2] >= clip]
+}
+
- /// [`reconstruct`] with `clipped(index, pixel)` telling the clipped channels; unclipped pixels within
- /// `reach` of a clipped one give its colour only when no other pixel does.
- fn reconstruct_with(img: &mut Rgb32f, wb: [f32; 3], clip: f32, reach: usize, clipped: impl Fn(usize, &[f32; 3]) -> [bool; 3] + Sync) -> usize {
++/// [`reconstruct_with`], with `clipped` telling the clipped channels.
++fn rebuild_clipped(img: &mut Rgb32f, wb: [f32; 3], clip: f32, white: [f32; 3], clipped: &impl Clipped) -> usize {
let (w, h) = (img.width, img.height);
- let marks: Vec<bool> = img.data.par_iter().enumerate().map(|(i, p)| clipped(i, p).iter().any(|&b| b)).collect();
- let count = marks.iter().filter(|&&b| b).count();
- if count == 0 {
- let is_clipped = |p: &[f32; 3]| p[0] >= clip || p[1] >= clip || p[2] >= clip;
- let count = img.data.par_iter().filter(|p| is_clipped(p)).count();
++ let count = img.data.par_iter().enumerate().filter(|(i, p)| clipped(*i, p).iter().any(|&c| c)).count();
+ if count == 0 || w == 0 || h == 0 {
return 0;
}
- // chromaticity (white-balanced ratios to the channel sum) of unclipped pixels, at half resolution
+ // a white far from the camera's neutral (or not a number) is a broken colour model: keep the camera's
+ let plausible = white.iter().all(|v| v.is_finite() && (1.0 / 16.0..=16.0).contains(v));
+ let white = if plausible { white } else { [1.0; 3] };
+ let total = white[0] + white[1] + white[2];
+ let neutral = [white[0] / total, white[1] / total, white[2] / total];
+ // A clipped pixel is never a source, so its chromaticity is always the bilinear sample of the
+ // half-resolution estimate: the pyramid starts there, straight from the image.
- let (first, near) = sources(img, wb, clip);
++ let (first, near) = sources(img, wb, clip, clipped);
+ let mut levels = vec![first];
+ while let Some(l) = levels.last().filter(|l| l.w > 1 || l.h > 1) {
+ let next = pull(l);
+ levels.push(next);
+ }
+ // a clipped pixel samples the cells within one of its own, which are within one of a clipped cell
let (cw, ch) = (w.div_ceil(2), h.div_ceil(2));
- // (a 1 × 1 image has no coarser level: its one pixel is clipped and nothing is known)
- let known = cw * ch < w * h;
- let field = |near: Option<&[bool]>| {
- let mut cv = vec![[0f32; 4]; cw * ch];
- let data = &img.data;
- downsample(w, h, &mut cv, |i| {
- if near.is_some_and(|n| n.get(i).copied().unwrap_or(false)) { [0.0; 4] } else { chroma_of(&data[i], clipped(i, &data[i]), wb, clip) }
+ let needed = |x: usize, y: usize| (y.saturating_sub(1)..=(y + 1).min(ch - 1)).any(|yy| near.get(yy * cw + x).copied().unwrap_or(false));
+ push(&mut levels, [neutral[0], neutral[2], 0.0], Some(&needed));
+ // How much reliable content lies around each needed cell: its density at any scale up to [`REACH`] (level
+ // i's cells span 2^i cells of level 0). Near reliable content some fine level is dense with it; far from it
+ // only the widest one may still hold a little, and the trust fades with that. Kept in the third entry of the
+ // level 0 estimates, which clipped pixels sample with their colour.
+ let reach = REACH * w.max(h) as f32 / 2.0;
+ let widest = (0..levels.len()).find(|&i| (1u64 << i.min(62)) as f32 >= reach).unwrap_or(levels.len().saturating_sub(1));
+ if let Some((first, coarser)) = levels.split_first_mut() {
+ let support: Vec<(&Level<3>, f32)> = coarser.iter().take(widest).enumerate().map(|(i, l)| (l, (1u64 << (i + 1).min(62)) as f32)).collect();
+ let fw = first.w;
+ first.cells.par_chunks_mut(fw).enumerate().for_each(|(y, row)| {
+ for (x, cell) in row.iter_mut().enumerate() {
+ if !needed(x, y) {
+ continue;
+ }
+ let mut t = 0f32;
+ for &(l, scale) in &support {
+ t = t.max(smoothstep(0.0, SUPPORT, sample(l, scale, x, y)[2]));
+ if t >= 1.0 {
+ break;
+ }
+ }
+ cell[2] = t;
+ }
});
- cv
- };
- let near = (reach > 0).then(|| dilate(w, h, &marks, reach));
- let mut cv = field(near.as_deref());
- if near.is_some() && !cv.iter().any(|v| v[3] > 0.0) {
- cv = field(None);
- }
- if known {
- fill(cw, ch, &mut cv);
}
+ let Some(field) = levels.first() else { return count };
+ // the rim (level 0 of its pyramid holds the estimate), if there is one anywhere at all
+ let rims = {
- let mut levels = vec![rim(img, wb, clip, &needed)];
++ let mut levels = vec![rim(img, wb, clip, clipped, &needed)];
+ while let Some(l) = levels.last().filter(|l| l.w > 1 || l.h > 1) {
+ let next = pull(l);
+ levels.push(next);
+ }
+ push(&mut levels, [0.0; 4], None);
+ Some(levels).filter(|levels| levels.last().is_some_and(|top| top.cells.iter().any(|c| c[3] > 0.0)))
+ };
+ let rim_field = rims.as_ref().and_then(|l| l.first());
+ let max_level = wb.iter().cloned().fold(0.0f32, f32::max) * clip;
img.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
for (x, px) in row.iter_mut().enumerate() {
let p = *px;
@@@ -461,133 -780,37 +819,63 @@@ mod tests
assert!(after < before * 0.2, "before {before} after {after}");
// unclipped pixels untouched
assert_eq!(img.get(0, 0), truth.get(0, 0));
+ assert_eq!(img.get(first - 1, 8), truth.get(first - 1, 8));
}
- /// Issue #548: a bright cloud whose green clips, crossed by a dark wire with a cyan fringe. The cloud
- /// takes the colour of the bright sky around it, not the wire's: before, the wire was the nearest
- /// unclipped colour for most of the cloud and turned it cyan.
#[test]
- fn dark_detail_inside_a_highlight_does_not_tint_it() {
- let wb = [2.0f32, 1.0, 1.5];
- // neutral after white balance at luminance `l`
- let grey = |l: f32| [l / wb[0], l / wb[1], l / wb[2]];
- let (w, h) = (128usize, 96usize);
- let wire = |x: usize, y: usize| (y as i32 - 20 - x as i32 / 2).abs() <= 1;
- let disc = |x: usize, y: usize| (x as f32 - 64.0).powi(2) + (y as f32 - 48.0).powi(2) < 40.0f32.powi(2);
- let img = Rgb32f::from_fn(w, h, |x, y| {
- if wire(x, y) {
- [0.05 / wb[0], 0.12 / wb[1], 0.12 / wb[2]]
- } else if disc(x, y) {
- // brighter than green's clip level: only red and blue are left
- grey(1.4).map(|v| v.min(1.0))
- } else {
- grey(0.85)
- }
- });
- let mut out = img.clone();
- assert!(reconstruct(&mut out, wb, 0.99) > 1000);
- let mut worst = 0f32;
- for y in 0..h {
- for x in 0..w {
- let p = out.get(x, y);
- if disc(x, y) && !wire(x, y) {
- let q = [p[0] * wb[0], p[1] * wb[1], p[2] * wb[2]];
- worst = worst.max((q[1] / q[0] - 1.0).abs()).max((q[2] / q[0] - 1.0).abs());
- }
- }
- }
- assert!(worst < 0.03, "the cloud is tinted by up to {:.0} %", worst * 100.0);
- }
-
- /// The more channels clip, the nearer to neutral (at the brightest channel) the rebuilt colour, with no
- /// step where one more channel clips.
- #[test]
- fn desaturates_as_more_channels_clip() {
- let wb = [1.0f32; 3];
- let sky = [0.3 * 2.4, 0.33 * 2.4, 0.37 * 2.4];
- let rebuilt = |centre: [f32; 3]| {
- let mut img = Rgb32f::from_fn(64, 64, |x, y| if (x as i32 - 32).abs() < 16 && (y as i32 - 32).abs() < 16 { centre } else { sky });
- reconstruct(&mut img, wb, 0.99);
- img.get(32, 32)
- };
- // green clipped, red and blue well below their clip level: the plain rebuild (green from the sky's ratios)
- let p = rebuilt([0.5, 1.0, 0.6]);
- assert_eq!((p[0], p[2]), (0.5, 0.6));
- assert!(p[1] == 1.0, "{p:?}");
- let p = rebuilt([0.33, 1.0, 0.4]);
- assert!((p[1] - 0.33 / 0.3 * 0.33).abs() < 0.01 || p[1] == 1.0, "{p:?}");
- // green and blue clipped, red at half: half-way to neutral at the brightest channel (1.0)
- let p = rebuilt([0.5, 1.0, 1.0]);
- assert!((p[0] - 0.75).abs() < 1e-3 && p[1] >= 1.0 && p[2] >= 1.0, "{p:?}");
- // blue just below its clip level, then just above: (nearly) the same colour
- let below = rebuilt([0.5, 1.0, 0.989]);
- let above = rebuilt([0.5, 1.0, 0.99]);
- assert!(below.iter().zip(&above).all(|(a, b)| (a - b).abs() < 0.01), "{below:?} {above:?}");
- // all three clipped: neutral
- let p = rebuilt([1.0, 1.0, 1.0]);
- assert!(p[0] == p[1] && p[1] == p[2], "{p:?}");
- }
-
- /// The dilation matches a brute-force one, across several column strips.
- #[test]
- fn dilate_matches_brute_force() {
- let (w, h) = (600usize, 41usize);
- let marks: Vec<bool> = (0..w * h).map(|i| (i * 2654435761usize) % 997 < 3).collect();
- for r in [0usize, 1, 6, 50] {
- let d = dilate(w, h, &marks, r);
- for y in 0..h {
- for x in 0..w {
- let want =
- (y.saturating_sub(r)..=(y + r).min(h - 1)).any(|yy| (x.saturating_sub(r)..=(x + r).min(w - 1)).any(|xx| marks[yy * w + xx]));
- assert_eq!(d[y * w + x], want, "r {r} at ({x}, {y})");
- }
- }
- }
+ fn fully_clipped_and_no_clipping() {
+ let mut img = Rgb32f::filled(4, 4, [1.0; 3]);
+ assert_eq!(reconstruct(&mut img, [2.0, 1.0, 1.5], 0.99), 16);
+ let p = img.data[0];
+ assert!((p[0] * 2.0 - p[1]).abs() < 1e-5 && (p[2] * 1.5 - p[1]).abs() < 1e-5);
+ let mut img = Rgb32f::filled(4, 4, [0.5; 3]);
+ assert_eq!(reconstruct(&mut img, [2.0, 1.0, 1.5], 0.99), 0);
+ let mut empty = Rgb32f::new(0, 0);
+ assert_eq!(reconstruct(&mut empty, [1.0; 3], 0.99), 0);
}
- /// With nothing bright nearby, dim pixels still give a clipped area their colour.
+ /// `cargo test --release -p lightcraft-raw --lib -- --ignored bench_reconstruct --nocapture`
#[test]
- fn a_highlight_among_dim_pixels_takes_their_colour() {
- let wb = [1.0f32; 3];
- let mut img =
- Rgb32f::from_fn(32, 32, |x, y| if (12..20).contains(&x) && (12..20).contains(&y) { [1.0, 0.5, 0.25] } else { [0.1, 0.05, 0.025] });
- reconstruct(&mut img, wb, 0.99);
- let p = img.get(16, 16);
- assert!((p[0] / p[1] - 2.0).abs() < 0.01, "{p:?}");
+ #[ignore]
+ fn bench_reconstruct() {
+ let img = Rgb32f::from_fn(6000, 4000, |x, y| {
+ let v = 0.4 + 0.8 * ((x as f32 * 0.003).sin() * (y as f32 * 0.002).cos()).abs();
+ [v.min(1.0), (v * 0.7).min(1.0), (v * 0.5).min(1.0)]
+ });
+ let ms = (0..3)
+ .map(|_| {
+ let mut i = img.clone();
+ let t = std::time::Instant::now();
+ reconstruct(&mut i, [2.0, 1.0, 1.5], 0.99);
+ t.elapsed().as_secs_f64() * 1e3
+ })
+ .fold(f64::MAX, f64::min);
+ eprintln!("reconstruct 24 MP: {ms:.0} ms");
}
+
- /// A mask decides which channels are clipped, whatever their values; a mask of another size changes nothing.
++ /// A mask decides which channels are clipped (their values are lower bounds), whatever their values; the
++ /// unmarked pixels are left as they were; a mask of another size leaves clipping to the values.
+ #[test]
+ fn a_mask_marks_the_clipped_channels() {
- let wb = [1.0f32; 3];
- let base = Rgb32f::from_fn(16, 16, |x, _| if x >= 8 { [0.2, 0.5, 0.25] } else { [0.4, 0.4, 0.2] });
- // the right half's red is a lower bound: its blocks held clipped samples
- let mask: Vec<u8> = (0..256).map(|i| if i % 16 >= 8 { CLIPPED_R } else { 0 }).collect();
++ let base = Rgb32f::from_fn(32, 32, |x, _| if x >= 16 { [0.2, 0.5, 0.25] } else { [0.5, 0.5, 0.5] });
++ let mask: Vec<u8> = (0..32 * 32).map(|i| if i % 32 >= 16 { CLIPPED_R } else { 0 }).collect();
+ let mut img = base.clone();
- assert_eq!(reconstruct_masked(&mut img, wb, 0.99, &mask), 128);
- // red rebuilt from the left half's colour (red = green), the other channels kept
- for x in 8..16 {
- let p = img.get(x, 8);
- assert!((p[0] - 0.5).abs() < 1e-4 && p[1] == 0.5 && p[2] == 0.25, "{p:?}");
++ assert_eq!(reconstruct_masked(&mut img, [1.0; 3], 0.99, [1.0; 3], &mask), 512);
++ for (p, b) in img.data.iter().zip(&base.data) {
++ assert!(p.iter().all(|v| v.is_finite()), "{p:?}");
++ if b[0] == 0.5 {
++ assert_eq!(p, b);
++ } else {
++ // red rebuilt from at least its mean, the others kept
++ assert!(p[0] >= 0.2 && p[1] == 0.5 && p[2] == 0.25, "{p:?}");
++ }
+ }
- assert_eq!(img.get(0, 0), base.get(0, 0));
- let mut same = base.clone();
- assert_eq!(reconstruct_masked(&mut same, wb, 0.99, &mask[1..]), 0);
- assert!(same.data == base.data);
- // the thresholds alone see nothing clipped here
++ // red is rebuilt above its mean, towards the neighbours' neutral (as far as the pixel's own blue agrees)
++ assert!(img.get(24, 16)[0] > 0.3, "{:?}", img.get(24, 16));
++ // the values alone see nothing clipped here, nor does a mask of another size
+ let mut plain = base.clone();
- assert_eq!(reconstruct(&mut plain, wb, 0.99), 0);
- }
-
- #[test]
- fn fully_clipped_and_no_clipping() {
- let mut img = Rgb32f::filled(4, 4, [1.0; 3]);
- assert_eq!(reconstruct(&mut img, [2.0, 1.0, 1.5], 0.99), 16);
- let p = img.data[0];
- assert!((p[0] * 2.0 - p[1]).abs() < 1e-5 && (p[2] * 1.5 - p[1]).abs() < 1e-5);
- let mut img = Rgb32f::filled(4, 4, [0.5; 3]);
- assert_eq!(reconstruct(&mut img, [2.0, 1.0, 1.5], 0.99), 0);
- let mut empty = Rgb32f::new(0, 0);
- assert_eq!(reconstruct(&mut empty, [1.0; 3], 0.99), 0);
++ assert_eq!(reconstruct(&mut plain, [1.0; 3], 0.99), 0);
++ assert_eq!(reconstruct_masked(&mut plain, [1.0; 3], 0.99, [1.0; 3], &mask[1..]), 0);
++ assert!(plain.data == base.data);
+ }
}
diff --cc docs/parity.md
index 31a5bf9f2,ad82e3d25..b6468e423
--- a/docs/parity.md
+++ b/docs/parity.md
@@@ -236,12 -237,12 +237,12 @@@ under the UI command that wraps it (a n
| Id | Feature | Tier | Status | Evidence | Missing / notes |
|---|---|---|---|---|---|
- | LR-EDIT-AUTO | Auto settings | P0 | ✅ | `cmd:develop.auto`, `crates/pipeline/src/auto.rs` | |
+ | LR-EDIT-AUTO | Auto settings | P0 | ✅ | `cmd:develop.auto`, `crates/pipeline/src/auto.rs` | a Lightroom-like recipe calibrated black-box on Lightroom's Auto results for 24 Sony raws: Exposure from the scene's scene-linear log-average luminance (Lightroom's observed slope, -0.41 EV per EV), Highlights -66 and Shadows +52 (Lightroom's average Auto values), Contrast +68 and Vibrance +30 fitted on our scales. Held out one photo at a time: on the 16 photos whose start Sony's DRO doesn't brighten, a median dE76 of 6.4 from Lightroom's Auto results against 8.3 unedited; on all 24, 7.7 against 7.9 (issues #318, #209). Calibrated on Sony files only; Whites and Blacks stay 0 |
| LR-EDIT-BW | Black & white | P0 | ✅ | `cmd:develop.treatment` | |
- | LR-EDIT-HDR-MODE | HDR editing | P2 | ⬜ | | |
+ | LR-EDIT-HDR-MODE | HDR editing | P2 | ✅ | `cmd:develop.hdr`, `ctl:hdr.maxEv`, `crates/ui-egui/src/panels/edit.rs` (`hdr_row`) | HDR switch in the Light panel; see Q. HDR |
| LR-EDIT-LIGHT-EXPOSURE | Exposure | P0 | ✅ | `ctl:light.exposure` | |
| LR-EDIT-LIGHT-CONTRAST | Contrast | P0 | ✅ | `ctl:light.contrast` | |
- | LR-EDIT-LIGHT-HIGHLIGHTS | Highlights | P0 | ✅ | `ctl:light.highlights`, `crates/raw/src/highlight.rs` | what Highlights pulls down in a raw's clipped areas is rebuilt by `highlight::reconstruct`: partly clipped pixels take the colour of the unclipped pixels around them, counted by how close to clipping they are (dark wires and branches across a bright cloud no longer tint it) and not from the few pixels next to clipping that demosaicing mixed with clipped samples, and are drawn toward neutral as more of their channels clip (none at one channel well below the others' clip, half-way at two, neutral at three, without steps); binned previews and thumbnails decide clipping per sample before averaging (`develop_binned_masked`), so a block with one clipped specular sample stays its mean instead of jumping to the clip level, which reconstruction turned into green speckles at 500 px (issue #548) |
-| LR-EDIT-LIGHT-HIGHLIGHTS | Highlights | P0 | ✅ | `ctl:light.highlights`, `crates/raw/src/highlight.rs` | sensor-clipped highlights stay neutral when Highlights (or Exposure, Whites) darkens them: fully clipped content is rebuilt to the neutral of the colour model applied after it, partly clipped content takes its colour from reliable unclipped content nearby (not from dark objects, lens fringes or another surface beside it) and white where there is none, and pale partly clipped content (a white sky) turns white on its way to clipping (#523) |
++| LR-EDIT-LIGHT-HIGHLIGHTS | Highlights | P0 | ✅ | `ctl:light.highlights`, `crates/raw/src/highlight.rs` | sensor-clipped highlights stay neutral when Highlights (or Exposure, Whites) darkens them: fully clipped content is rebuilt to the neutral of the colour model applied after it, partly clipped content takes its colour from reliable unclipped content nearby (not from dark objects, lens fringes or another surface beside it) and white where there is none, and pale partly clipped content (a white sky) turns white on its way to clipping (#523); binned previews and thumbnails decide clipping per sample before averaging (`develop_binned_masked`, `highlight::reconstruct_masked`), so a block with one clipped specular sample keeps its mean instead of jumping to the clip level, which reconstruction turned into green speckles at 500 px (#548) |
| LR-EDIT-LIGHT-SHADOWS | Shadows | P0 | ✅ | `ctl:light.shadows` | |
| LR-EDIT-LIGHT-WHITES | Whites | P0 | ✅ | `ctl:light.whites` | |
| LR-EDIT-LIGHT-BLACKS | Blacks | P0 | ✅ | `ctl:light.blacks` | |