From d49ebf4a41fe3b754295ea0898522a69461d47df Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 11 Nov 2026 16:25:33 -0500 Subject: [PATCH] Camera Raw: read each source row once when building the preview proxy (#11064) * Camera Raw: read each source row once when building the preview proxy The proxy box-averages k x k blocks, with k chosen from the longer side of the domain. Blocks past the last row re-read that row once per missing row, so a one-row domain (a sparse document a pixel high and a million wide) read the same row k times: quadratic in its width, on the UI thread. Read every source row once and weight the last one by the block rows it stands for; the averages are unchanged. * address review: accumulate camera raw preview rows in f64 --- .../src/dialogs/camera_raw_ui/convert.rs | 109 ++++++++++++++---- 1 file changed, 89 insertions(+), 20 deletions(-) diff --git a/crates/ui-egui/src/dialogs/camera_raw_ui/convert.rs b/crates/ui-egui/src/dialogs/camera_raw_ui/convert.rs index 4fd1852aae..f61c57e27d 100644 --- a/crates/ui-egui/src/dialogs/camera_raw_ui/convert.rs +++ b/crates/ui-egui/src/dialogs/camera_raw_ui/convert.rs @@ -48,6 +48,37 @@ pub(super) fn apply_raw_conversions(app: &mut PhotocraftApp, index: usize, d: &p Ok(()) } +/// `area` of `surf` averaged over `k`×`k` blocks, row-major (`ceil(w/k)` × `ceil(h/k)`). A block +/// past the last row repeats that row, as a clamped edge; each source row is still read once, +/// weighted by the block rows it stands for (a one-row domain read the same row `k` times, which +/// is quadratic in its width). +fn box_average(surf: &photocraft_raster::Surface, area: Rect, k: usize) -> Vec<[f32; 4]> { + let (w, h) = (area.width() as usize, area.height() as usize); + let (pw, ph) = (w.div_ceil(k), h.div_ceil(k)); + let mut proxy = vec![[0.0f32; 4]; pw * ph]; + let mut row = vec![[0.0f32; 4]; w]; + for py in 0..ph { + // f64: a weighted f32 sample near f32::MAX would overflow to infinity. + let mut acc = vec![[0.0f64; 5]; pw]; + for sy in py * k..((py + 1) * k).min(h) { + let weight = if sy == h - 1 { ((py + 1) * k - sy) as f64 } else { 1.0 }; + let y = area.y0 + sy as i32; + surf.read_rgba_into(Rect::new(area.x0, y, area.x1, y + 1), &mut row); + for (x, q) in row.iter().enumerate() { + let a = &mut acc[x / k]; + for c in 0..4 { + a[c] += f64::from(q[c]) * weight; + } + a[4] += weight; + } + } + for (x, a) in acc.iter().enumerate() { + proxy[py * pw + x] = [0, 1, 2, 3].map(|c| (a[c] / a[4]) as f32); + } + } + proxy +} + /// What the preview develops, and its size: the engine filter domain, including pixels outside the /// canvas, since spatial Camera Raw stages (vignette, grain, local contrast) must agree with the /// committed result. @@ -70,26 +101,7 @@ pub(super) fn preview_area(canvas: Rect, surf: &photocraft_raster::Surface) -> R pub(super) fn box_proxy(surf: &photocraft_raster::Surface, area: Rect, w: usize, h: usize) -> (Vec<[f32; 4]>, usize, usize, usize) { let k = w.max(h).div_ceil(PROXY_SIDE).max(1); let (pw, ph) = (w.div_ceil(k), h.div_ceil(k)); - let mut proxy = vec![[0.0f32; 4]; pw * ph]; - let mut row = vec![[0.0f32; 4]; w]; - for py in 0..ph { - // Box-average k×k blocks. - let mut acc = vec![[0.0f32; 5]; pw]; - for dy in 0..k { - let y = area.y0 + (py * k + dy).min(h - 1) as i32; - surf.read_rgba_into(Rect::new(area.x0, y, area.x1, y + 1), &mut row); - for (x, q) in row.iter().enumerate() { - let a = &mut acc[x / k]; - for c in 0..4 { - a[c] += q[c]; - } - a[4] += 1.0; - } - } - for (x, a) in acc.iter().enumerate() { - proxy[py * pw + x] = [a[0] / a[4], a[1] / a[4], a[2] / a[4], a[3] / a[4]]; - } - } + let proxy = box_average(surf, area, k); (proxy, pw, ph, k) } @@ -204,3 +216,60 @@ pub(super) fn open_pixels( app.camera_raw = Some(d); Ok(()) } + +#[cfg(test)] +mod box_average_tests { + use super::{Rect, box_average}; + use photocraft_color::PixelFormat; + use photocraft_raster::Surface; + + /// The clamped reference: every block row read, past the last row the last row again. + fn reference(surf: &Surface, area: Rect, k: usize) -> Vec<[f32; 4]> { + let (w, h) = (area.width() as usize, area.height() as usize); + let (pw, ph) = (w.div_ceil(k), h.div_ceil(k)); + let mut out = Vec::new(); + let mut row = vec![[0.0f32; 4]; w]; + for py in 0..ph { + let mut acc = vec![[0.0f32; 5]; pw]; + for dy in 0..k { + let y = area.y0 + (py * k + dy).min(h - 1) as i32; + surf.read_rgba_into(Rect::new(area.x0, y, area.x1, y + 1), &mut row); + for (x, q) in row.iter().enumerate() { + for c in 0..4 { + acc[x / k][c] += q[c]; + } + acc[x / k][4] += 1.0; + } + } + out.extend(acc.iter().map(|a| [a[0] / a[4], a[1] / a[4], a[2] / a[4], a[3] / a[4]])); + } + out + } + + #[test] + fn blocks_past_the_last_row_match_the_clamped_average() { + let mut surf = Surface::new(PixelFormat::RGBA32F); + for x in 0..37 { + for y in 0..5 { + surf.fill_rect(Rect::new(x, y, x + 1, y + 1), &[x as f32 / 37.0, y as f32 / 5.0, 0.5, 1.0]); + } + } + for (area, k) in [(Rect::new(0, 0, 37, 5), 2), (Rect::new(0, 0, 37, 5), 4), (Rect::new(0, 2, 37, 3), 16), (Rect::new(3, 0, 37, 5), 7)] { + let (got, want) = (box_average(&surf, area, k), reference(&surf, area, k)); + assert_eq!(got.len(), want.len()); + for (g, w) in got.iter().zip(&want) { + assert!(g.iter().zip(w).all(|(a, b)| (a - b).abs() < 1e-5), "{area:?} k={k}: {g:?} vs {w:?}"); + } + } + } + + /// Samples near f32::MAX stay finite: the weighted sums are held in f64. + #[test] + fn huge_samples_do_not_overflow() { + let mut surf = Surface::new(PixelFormat::RGBA32F); + let big = f32::MAX / 2.0; + surf.fill_rect(Rect::new(0, 0, 2, 1), &[big, -big, big, 1.0]); + let got = box_average(&surf, Rect::new(0, 0, 2, 1), 4); + assert!(got.iter().flatten().all(|v| v.is_finite()), "{got:?}"); + } +}