LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
M5.3: faster per-pixel stage and row-parallel guided filter
Brandon Thomas0892148bab71
diff --git a/Cargo.lock b/Cargo.lockindex b52843184..cdf92bed5 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -2158,6 +2158,7 @@ dependencies = [name = "lightcraft-raster"version = "0.1.0"dependencies = [+ "libc","lightcraft-color","lightcraft-geom","proptest",diff --git a/crates/engine/src/media.rs b/crates/engine/src/media.rsindex 0840895af..cac58cc29 100644--- a/crates/engine/src/media.rs+++ b/crates/engine/src/media.rs@@ -20,7 +20,7 @@ use lightcraft_raster::{Histogram, Rgb32f};use serde::{Deserialize, Serialize};/// Bump when the pipeline's output changes, to invalidate cached thumbnails.-pub const RENDER_CACHE_VERSION: u64 = 2;+pub const RENDER_CACHE_VERSION: u64 = 3;/// 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 --git a/crates/pipeline/src/finish.rs b/crates/pipeline/src/finish.rsindex 601df578a..196a75b2c 100644--- a/crates/pipeline/src/finish.rs+++ b/crates/pipeline/src/finish.rs@@ -128,6 +128,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInflet out_to_norm = frame.out_to_norm(w, h);let aspect = w as f32 / h as f32;+ let srgb = srgb_lut();let mut out = Rgba8::new(w, h);for_rows(&mut out.data, w, |y, row| {for (x, px) in row.iter_mut().enumerate() {@@ -184,7 +185,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf// --- local exposure / temp / tintif l_exp != 0.0 {- let g = 2f32.powf(l_exp);+ let g = l_exp.exp2();c = c.map(|v| v * g);}if l_temp != 0.0 || l_tint != 0.0 {@@ -236,7 +237,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf}}if delta != 0.0 {- let g = 2f32.powf(delta);+ let g = delta.exp2();c = c.map(|v| v * g);}@@ -305,7 +306,7 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInf}// --- encode, curves, grain- let mut e = r.map(|v| linear_to_srgb(v.clamp(0.0, 1.0)));+ let mut e = r.map(|v| encode_srgb(srgb, v));if let Some(l) = &curves {e = [l[0].eval(e[0]), l[1].eval(e[1]), l[2].eval(e[2])];}@@ -325,8 +326,50 @@ pub fn finish(p: &Prepared, s: &DevelopSettings, frame: &Frame, info: &SourceInfout}+const SRGB_LUT_N: usize = 4096;++fn srgb_lut() -> &'static [f32; SRGB_LUT_N + 1] {+ static LUT: std::sync::OnceLock<Box<[f32; SRGB_LUT_N + 1]>> = std::sync::OnceLock::new();+ LUT.get_or_init(|| {+ let mut t = Box::new([0.0f32; SRGB_LUT_N + 1]);+ for (i, v) in t.iter_mut().enumerate() {+ *v = linear_to_srgb(i as f32 / SRGB_LUT_N as f32);+ }+ t+ })+}++/// Linear → sRGB-encoded (clamped to 0..1) by an interpolated table: within 2e-5 of the exact+/// curve (≪ one 8-bit or 10-bit step), several times faster than `powf`.+#[inline]+fn encode_srgb(lut: &[f32; SRGB_LUT_N + 1], v: f32) -> f32 {+ let f = v.clamp(0.0, 1.0) * SRGB_LUT_N as f32;+ let i = (f as usize).min(SRGB_LUT_N - 1);+ let t = f - i as f32;+ lut[i] + (lut[i + 1] - lut[i]) * t+}+#[inline]fn enc(v: f32) -> u8 {// `v` is already sRGB-encoded; round to 8 bits.(v.clamp(0.0, 1.0) * 255.0 + 0.5) as u8}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn srgb_table_matches_the_exact_curve() {+ let lut = srgb_lut();+ let mut worst = 0.0f32;+ for i in 0..=200_000 {+ // dense near black, where the curve bends most+ let v = (i as f32 / 200_000.0).powi(3);+ worst = worst.max((encode_srgb(lut, v) - linear_to_srgb(v)).abs());+ }+ assert!(worst < 2e-5, "{worst}");+ assert_eq!(encode_srgb(lut, -1.0), 0.0);+ assert!((encode_srgb(lut, 2.0) - 1.0).abs() < 1e-6);+ }+}diff --git a/crates/pipeline/src/local.rs b/crates/pipeline/src/local.rsindex f1b612104..faaacf766 100644--- a/crates/pipeline/src/local.rs+++ b/crates/pipeline/src/local.rs@@ -63,23 +63,31 @@ pub fn effective_wb(info: &SourceInfo, s: &DevelopSettings) -> (f64, f64) {/// 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.+fn guided_coeffs(p: &Plane, sigma: f32, eps: f32) -> (Plane, Plane) {let mean = gaussian(p, sigma);- let sq = p.map(|v| v * v);- let corr = gaussian(&sq, sigma);- let mut a = Plane::new(p.width, p.height);- let mut b = Plane::new(p.width, p.height);- for i in 0..p.len() {- let var = (corr.data[i] - mean.data[i] * mean.data[i]).max(0.0);- let ai = var / (var + eps);- a.data[i] = ai;- b.data[i] = mean.data[i] - ai * mean.data[i];- }- let ma = gaussian(&a, sigma);- let mb = gaussian(&b, sigma);+ let corr = 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);+ (gaussian(&a, sigma), gaussian(&b, sigma))+}++/// `q = a·p + b` per pixel.+fn guided_apply(p: &Plane, a: &Plane, b: &Plane) -> Plane {let mut q = Plane::new(p.width, p.height);- for i in 0..p.len() {- q.data[i] = ma.data[i] * p.data[i] + mb.data[i];- }+ let w = p.width;+ 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}@@ -94,28 +102,9 @@ pub fn guided_fast(p: &Plane, sigma: f32, eps: f32) -> Plane {}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 ls = sigma / s as f32;- let mean = gaussian(&lo, ls);- let corr = gaussian(&lo.map(|v| v * v), ls);- let mut a = Plane::new(lw, lh);- let mut b = Plane::new(lw, lh);- for i in 0..lo.len() {- let var = (corr.data[i] - mean.data[i] * mean.data[i]).max(0.0);- let ai = var / (var + eps);- a.data[i] = ai;- b.data[i] = mean.data[i] - ai * mean.data[i];- }- let ma = resize(&gaussian(&a, ls), p.width, p.height, Filter::Bilinear);- let mb = resize(&gaussian(&b, ls), p.width, p.height, Filter::Bilinear);- let mut q = Plane::new(p.width, p.height);- let w = p.width;- for_rows(&mut q.data, w, |y, row| {- for (x, v) in row.iter_mut().enumerate() {- let i = y * w + x;- *v = ma.data[i] * p.data[i] + mb.data[i];- }- });- q+ let (ma, mb) = guided_coeffs(&lo, sigma / s as f32, eps);+ let up = |c: &Plane| resize(c, p.width, p.height, Filter::Bilinear);+ guided_apply(p, &up(&ma), &up(&mb))}/// Noise reduction at output resolution: luminance via an edge-aware self-guided filter on@@ -140,7 +129,7 @@ pub fn denoise(img: &mut Rgb32f, s: &DevelopSettings, src_long: usize, out_long:for (x, p) in row.iter_mut().enumerate() {let i = y * w + x;let d = (f.data[i] - l.data[i]) * k;- let g = 2f32.powf(d);+ let g = d.exp2();*p = p.map(|v| v * g);}});diff --git a/crates/raster/Cargo.toml b/crates/raster/Cargo.tomlindex bb59b3486..57dbd9390 100644--- a/crates/raster/Cargo.toml+++ b/crates/raster/Cargo.toml@@ -20,3 +20,4 @@ parallel = ["dep:rayon"][dev-dependencies]proptest = { workspace = true }+libc = { workspace = true }diff --git a/crates/raster/examples/blur_bench.rs b/crates/raster/examples/blur_bench.rsindex 0a222a336..0bad9f6ef 100644--- a/crates/raster/examples/blur_bench.rs+++ b/crates/raster/examples/blur_bench.rs@@ -1,16 +1,34 @@-//! Micro-benchmark for the blur/map primitives (min of N runs, robust to a loaded machine):+//! Micro-benchmark for the blur/map primitives (min of N runs, wall-clock and process CPU time, so+//! it stays meaningful on a loaded machine)://! `RAYON_NUM_THREADS=1 cargo run --release -p lightcraft-raster --example blur_bench`.use lightcraft_raster::{Plane, Rgb32f, blur::gaussian};use std::time::Instant;-fn best(n: usize, mut f: impl FnMut()) -> f64 {- (0..n)- .map(|_| {- let t = Instant::now();- f();- t.elapsed().as_secs_f64() * 1e3- })- .fold(f64::MAX, f64::min)+/// Process CPU time in ms (all threads). The only `unsafe` is this libc clock read, in a dev-only example.+#[allow(unsafe_code)]+fn cpu_ms() -> f64 {+ #[cfg(unix)]+ {+ let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };+ // SAFETY: `clock_gettime` only writes into the timespec we pass.+ unsafe { libc::clock_gettime(libc::CLOCK_PROCESS_CPUTIME_ID, &mut ts) };+ ts.tv_sec as f64 * 1e3 + ts.tv_nsec as f64 / 1e6+ }+ #[cfg(not(unix))]+ {+ 0.0+ }+}++fn best(n: usize, mut f: impl FnMut()) -> String {+ let (mut wall, mut cpu) = (f64::MAX, f64::MAX);+ for _ in 0..n {+ let (t, c) = (Instant::now(), cpu_ms());+ f();+ cpu = cpu.min(cpu_ms() - c);+ wall = wall.min(t.elapsed().as_secs_f64() * 1e3);+ }+ format!("{wall:.1} ms wall, {cpu:.1} ms cpu")}fn main() {@@ -18,7 +36,7 @@ fn main() {let p = Plane::from_fn(w, h, |x, y| ((x * 7 + y * 13) % 255) as f32 / 255.0);let c = Rgb32f::from_fn(w, h, |x, y| [x as f32 / w as f32, y as f32 / h as f32, 0.5]);for sigma in [1.0f32, 4.0, 24.0] {- println!("plane gaussian σ={sigma}: {:.1} ms", best(9, || drop(gaussian(&p, sigma))));+ println!("plane gaussian σ={sigma}: {}", best(9, || drop(gaussian(&p, sigma))));}- println!("rgb gaussian σ=4: {:.1} ms", best(9, || drop(gaussian(&c, 4.0))));+ println!("rgb gaussian σ=4: {}", best(9, || drop(gaussian(&c, 4.0))));}diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rsindex 3cc311ed4..4e358ee10 100644--- a/crates/raster/src/lib.rs+++ b/crates/raster/src/lib.rs@@ -98,6 +98,22 @@ impl<T: Copy + Default> Image<T> {});out}+ /// Per-pixel combination of two images of the same size (row-parallel).+ pub fn zip_map<U: Copy + Default + Sync, V: Copy + Default + Send>(&self, other: &Image<U>, f: impl Fn(T, U) -> V + Sync + Send) -> Image<V>+ where+ T: Sync,+ {+ assert_eq!((self.width, self.height), (other.width, other.height), "zip_map: size mismatch");+ let mut out = Image::<V>::new(self.width, self.height);+ let w = self.width;+ par_rows(&mut out.data, w, |y, row| {+ let (a, b) = (&self.data[y * w..(y + 1) * w], &other.data[y * w..(y + 1) * w]);+ for ((o, s), t) in row.iter_mut().zip(a).zip(b) {+ *o = f(*s, *t);+ }+ });+ out+ }/// Crop to `[x0, x0+w) × [y0, y0+h)` (clamped to the image).pub fn crop(&self, x0: usize, y0: usize, w: usize, h: usize) -> Self {let x0 = x0.min(self.width);