mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Blurs run as box passes now filter the whole area at once instead of tile by tile: the horizontal passes run over the source rows only and the vertical passes over column strips, with the canvas edge repeated virtually instead of materialising a 3-sigma margin around every tile (at radius 500 that margin was ~25 MP of floats per 4 MP tile, copied and transposed several times). The results are written to the layer's tile columns in parallel. Output matches the previous running sums (8-bit: identical except 114 of 2.1 M samples one level apart; 16-bit: within 2/65535) and a direct-convolution oracle at every depth. Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Brandon Thomas <[email protected]>
This commit is contained in:
6 files changed
+423
-141
No files matched your search
@@ -1,6 +1,7 @@
|
||||
//! Timing: Gaussian Blur radius 10 on a 6016×6016 RGBA8 layer.
|
||||
//! `cargo run --release -p photocraft-algo --example bench_gaussian`
|
||||
use photocraft_algo::{FilterParams, apply, output_area};
|
||||
//! Timing: Gaussian Blur radius 10 on a 6016×6016 RGBA8 layer, as a document layer filling the
|
||||
//! canvas is filtered (edges repeated at the canvas edge).
|
||||
//! `cargo run --release -p photocraft-algo --example bench_gaussian [-- <size> <radius>]`
|
||||
use photocraft_algo::{FilterParams, apply_in, output_area};
|
||||
use photocraft_color::PixelFormat;
|
||||
use photocraft_geom::Rect;
|
||||
use photocraft_raster::Surface;
|
||||
@@ -15,8 +16,13 @@ fn main() {
|
||||
s.write_interleaved(r, &bytes);
|
||||
let p = FilterParams::GaussianBlur { radius };
|
||||
let area = output_area(&p, s.content_bounds(), r, None).intersect(&r);
|
||||
let t = std::time::Instant::now();
|
||||
let out = apply(&s, &p, area, r, None);
|
||||
let dt = t.elapsed();
|
||||
// Best of a few runs (the first also pays for page faults).
|
||||
let mut dt = std::time::Duration::MAX;
|
||||
let mut out = s.clone();
|
||||
for _ in 0..5 {
|
||||
let t = std::time::Instant::now();
|
||||
out = apply_in(&s, &p, area, r, None, r);
|
||||
dt = dt.min(t.elapsed());
|
||||
}
|
||||
println!("gaussian radius {radius} on {n}x{n} RGBA8: {:.2?} ({} tiles)", dt, out.tile_count());
|
||||
}
|
||||
+303
-121
@@ -1,9 +1,11 @@
|
||||
//! Blurs: Gaussian, box, motion, radial, surface.
|
||||
|
||||
use photocraft_geom::Rect;
|
||||
use photocraft_raster::Interrupt;
|
||||
|
||||
use crate::image::{Edge, Image, premultiply, unpremultiply};
|
||||
use crate::{Ctx, RadialMethod};
|
||||
use crate::photo_util::{par_map, par_rows};
|
||||
use crate::{Ctx, FilterParams, RadialMethod};
|
||||
|
||||
/// Normalized Gaussian kernel with standard deviation `sigma` (radius 3σ).
|
||||
pub(crate) fn gaussian_kernel(sigma: f32) -> Vec<f32> {
|
||||
@@ -69,11 +71,13 @@ pub(crate) fn conv_sep(src: &Image, out: Rect, kx: &[f32], ky: &[f32], alpha: bo
|
||||
pub(crate) fn gaussian(src: &Image, out: Rect, ctx: &Ctx, radius: f32) -> Vec<f32> {
|
||||
// Exact kernel for small radii; beyond that three box passes approximate the Gaussian with
|
||||
// cost independent of the radius (Kovesi, "Fast almost-Gaussian filtering", 2010).
|
||||
if radius <= 4.0 {
|
||||
let k = gaussian_kernel(radius);
|
||||
return conv_sep(src, out, &k, &k, ctx.alpha);
|
||||
match box_widths(&FilterParams::GaussianBlur { radius }) {
|
||||
Some(boxes) => box_passes(src, out, ctx.alpha, &boxes),
|
||||
None => {
|
||||
let k = gaussian_kernel(radius);
|
||||
conv_sep(src, out, &k, &k, ctx.alpha)
|
||||
}
|
||||
}
|
||||
gaussian_boxes(src, out, ctx.alpha, radius)
|
||||
}
|
||||
|
||||
/// Box widths (odd) whose `n`-fold convolution has standard deviation `sigma`.
|
||||
@@ -91,143 +95,219 @@ fn boxes_for_gauss(sigma: f32, n: usize) -> Vec<usize> {
|
||||
(0..n).map(|i| if i < m { wl as usize } else { wu as usize }).collect()
|
||||
}
|
||||
|
||||
/// Running-sum box blur of every row of an interleaved `w × h × n` buffer (edges clamped).
|
||||
fn box_rows(buf: &mut [f32], w: usize, n: usize, r: usize) {
|
||||
if r == 0 || w == 0 {
|
||||
return;
|
||||
/// The box widths (odd) a blur runs as running-sum passes along each axis, or `None` when it uses a
|
||||
/// direct kernel (small radii, where the kernel is quicker).
|
||||
pub(crate) fn box_widths(params: &FilterParams) -> Option<Vec<usize>> {
|
||||
match *params {
|
||||
FilterParams::GaussianBlur { radius } if radius > 4.0 => Some(boxes_for_gauss(radius, 3)),
|
||||
FilterParams::BoxBlur { radius } => {
|
||||
let r = radius.max(0.0).round() as usize;
|
||||
(r > 4).then(|| vec![2 * r + 1])
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Scratch buffers of one worker for [`box_blur`].
|
||||
#[derive(Default)]
|
||||
struct Scratch {
|
||||
line: Vec<f32>,
|
||||
next: Vec<f32>,
|
||||
acc: Vec<f32>,
|
||||
}
|
||||
|
||||
/// One running-sum box pass of radius `r` along a line of elements of `w` floats each (a pixel's
|
||||
/// channels, or a strip of a row): element `i` of `dst` is the mean of elements `i ..= i + 2r` of
|
||||
/// `src`, which is `2r` elements longer.
|
||||
fn box_pass(src: &[f32], dst: &mut [f32], w: usize, r: usize, acc: &mut Vec<f32>) {
|
||||
// Pixels are short elements: fixed sizes let the compiler keep the sums in registers.
|
||||
match w {
|
||||
1 => box_pass_px::<1>(src, dst, r),
|
||||
2 => box_pass_px::<2>(src, dst, r),
|
||||
3 => box_pass_px::<3>(src, dst, r),
|
||||
4 => box_pass_px::<4>(src, dst, r),
|
||||
5 => box_pass_px::<5>(src, dst, r),
|
||||
0 => {}
|
||||
_ => {
|
||||
let norm = 1.0 / (2 * r + 1) as f32;
|
||||
acc.clear();
|
||||
acc.resize(w, 0.0);
|
||||
for e in src.chunks_exact(w).take(2 * r + 1) {
|
||||
for (a, s) in acc.iter_mut().zip(e) {
|
||||
*a += s;
|
||||
}
|
||||
}
|
||||
for (i, d) in dst.chunks_exact_mut(w).enumerate() {
|
||||
for (d, a) in d.iter_mut().zip(acc.iter()) {
|
||||
*d = a * norm;
|
||||
}
|
||||
let (add, sub) = ((i + 2 * r + 1) * w, i * w);
|
||||
if let (Some(add), Some(sub)) = (src.get(add..add + w), src.get(sub..sub + w)) {
|
||||
for ((a, add), sub) in acc.iter_mut().zip(add).zip(sub) {
|
||||
*a += add - sub;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// [`box_pass`] for elements of `N` floats.
|
||||
fn box_pass_px<const N: usize>(src: &[f32], dst: &mut [f32], r: usize) {
|
||||
let (src, dst) = (src.as_chunks::<N>().0, dst.as_chunks_mut::<N>().0);
|
||||
let norm = 1.0 / (2 * r + 1) as f32;
|
||||
let blur_row = |row: &mut [f32]| {
|
||||
// The row with clamped edges materialised (r + 1 pixels each side), so the running sum
|
||||
// reads without bounds clamping, all channels per step.
|
||||
let pad = r + 1;
|
||||
let mut src = Vec::with_capacity((w + 2 * pad) * n);
|
||||
for _ in 0..pad {
|
||||
src.extend_from_slice(&row[..n]);
|
||||
let mut acc = [0.0f32; N];
|
||||
for e in src.iter().take(2 * r + 1) {
|
||||
for c in 0..N {
|
||||
acc[c] += e[c];
|
||||
}
|
||||
}
|
||||
for (i, d) in dst.iter_mut().enumerate() {
|
||||
for c in 0..N {
|
||||
d[c] = acc[c] * norm;
|
||||
}
|
||||
if let (Some(add), Some(sub)) = (src.get(i + 2 * r + 1), src.get(i)) {
|
||||
for c in 0..N {
|
||||
acc[c] += add[c] - sub[c];
|
||||
}
|
||||
}
|
||||
src.extend_from_slice(row);
|
||||
for _ in 0..pad {
|
||||
src.extend_from_slice(&row[(w - 1) * n..w * n]);
|
||||
}
|
||||
let mut acc = [0.0f32; 8];
|
||||
let acc = &mut acc[..n.min(8)];
|
||||
if n > 8 {
|
||||
// Unusual channel counts: per channel.
|
||||
for c in 0..n {
|
||||
let mut a: f32 = (0..=2 * r).map(|i| src[(pad - r + i) * n + c]).sum();
|
||||
for x in 0..w {
|
||||
row[x * n + c] = a * norm;
|
||||
a += src[(x + pad + r + 1) * n + c] - src[(x + pad - r) * n + c];
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
for i in 0..=2 * r {
|
||||
let o = (pad - r + i) * n;
|
||||
for c in 0..n {
|
||||
acc[c] += src[o + c];
|
||||
}
|
||||
}
|
||||
for x in 0..w {
|
||||
let (add, sub) = ((x + pad + r + 1) * n, (x + pad - r) * n);
|
||||
for c in 0..n {
|
||||
row[x * n + c] = acc[c] * norm;
|
||||
acc[c] += src[add + c] - src[sub + c];
|
||||
}
|
||||
}
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
buf.par_chunks_mut(w * n).for_each(blur_row);
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
buf.chunks_mut(w * n).for_each(blur_row);
|
||||
}
|
||||
|
||||
fn transpose(buf: &[f32], w: usize, h: usize, n: usize) -> Vec<f32> {
|
||||
let mut out = vec![0.0f32; buf.len()];
|
||||
if w == 0 || h == 0 {
|
||||
return out;
|
||||
}
|
||||
const B: usize = 32; // cache-friendly blocks
|
||||
// Bands of B output rows (= input columns) are independent: transpose them in parallel.
|
||||
let band = |(i, chunk): (usize, &mut [f32])| {
|
||||
let bx = i * B;
|
||||
for by in (0..h).step_by(B) {
|
||||
for y in by..(by + B).min(h) {
|
||||
for x in bx..(bx + B).min(w) {
|
||||
let (s, d) = ((y * w + x) * n, ((x - bx) * h + y) * n);
|
||||
chunk[d..d + n].copy_from_slice(&buf[s..s + n]);
|
||||
}
|
||||
}
|
||||
/// Box passes of the given radii in turn along the line in `s.line` (`w`-float elements, as many
|
||||
/// as `dst` holds plus twice the radii's sum; consumed), ending in `dst`.
|
||||
fn line_passes(dst: &mut [f32], w: usize, radii: &[usize], s: &mut Scratch) {
|
||||
let Scratch { line, next, acc } = s;
|
||||
let Some(len) = dst.len().checked_div(w) else { return };
|
||||
let mut tail: usize = radii.iter().sum();
|
||||
for (k, &r) in radii.iter().enumerate() {
|
||||
tail -= r;
|
||||
if k + 1 == radii.len() {
|
||||
box_pass(line, dst, w, r, acc);
|
||||
} else {
|
||||
next.resize((len + 2 * tail) * w, 0.0);
|
||||
box_pass(line, next, w, r, acc);
|
||||
std::mem::swap(line, next);
|
||||
}
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
out.par_chunks_mut(B * h * n).enumerate().for_each(band);
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
out.chunks_mut(B * h * n).enumerate().for_each(band);
|
||||
out
|
||||
}
|
||||
|
||||
/// Gaussian blur by three box passes per axis over a premultiplied window.
|
||||
fn gaussian_boxes(src: &Image, out: Rect, alpha: bool, sigma: f32) -> Vec<f32> {
|
||||
box_passes(src, out, alpha, &boxes_for_gauss(sigma, 3))
|
||||
/// Rows of the horizontal passes handed to one worker (they share its scratch buffers).
|
||||
const ROWS_PER_TASK: usize = 8;
|
||||
/// Width in pixels of the column strips the vertical passes work on (a divisor of the tile size).
|
||||
pub(crate) const STRIP: i32 = 64;
|
||||
|
||||
/// Samples of a column strip of an output area.
|
||||
pub(crate) type Strip = (Rect, Vec<f32>);
|
||||
|
||||
/// Box blurs of the given (odd) widths, one after another along each axis, of the `n`-channel
|
||||
/// pixels of `src` extended beyond it by repeating its edge pixels (alpha-weighted when `alpha`).
|
||||
/// `read(row, buf)` fills `buf` with the samples of a one-pixel-high rectangle inside `src`.
|
||||
///
|
||||
/// Running sums make the cost independent of the widths, and only `src`'s own rows and the output
|
||||
/// columns are blurred: the repeated edges are never read or blurred as 2-D margins, so a huge
|
||||
/// radius costs about what a small one does. Returns the straight results for `out` as column
|
||||
/// strips (`None` when `ctl` was cancelled).
|
||||
pub(crate) fn box_blur(
|
||||
src: Rect,
|
||||
read: &(dyn Fn(Rect, &mut Vec<f32>) + Sync),
|
||||
out: Rect,
|
||||
n: usize,
|
||||
alpha: bool,
|
||||
boxes: &[usize],
|
||||
ctl: &Interrupt,
|
||||
) -> Option<Vec<Strip>> {
|
||||
let radii: Vec<usize> = boxes.iter().map(|w| w / 2).collect();
|
||||
let reach = i32::try_from(radii.iter().sum::<usize>()).unwrap_or(i32::MAX);
|
||||
let (ow, oh) = (out.width() as usize, out.height() as usize);
|
||||
let used = src.intersect(&out.inflate(reach));
|
||||
if used.is_empty() || n == 0 {
|
||||
// Nothing to read: transparent, as `Image::get` reads outside an image.
|
||||
return Some(strips(out).map(|t| (t, vec![0.0; t.width() as usize * oh * n])).collect());
|
||||
}
|
||||
// Horizontal passes over `used`'s rows only (the repeated rows above and below it would just
|
||||
// repeat their results), for the output columns. Each row is extended to the passes' reach by
|
||||
// repeating its end pixels.
|
||||
let mut h = vec![0.0f32; used.height() as usize * ow * n];
|
||||
let (left, right) = ((used.x0 - out.x0.saturating_sub(reach)) as usize, (out.x1.saturating_add(reach) - used.x1) as usize);
|
||||
par_rows(&mut h, ow * ROWS_PER_TASK, n, |task, chunk| {
|
||||
let (mut row, mut s) = (Vec::new(), Scratch::default());
|
||||
for (k, dst) in chunk.chunks_exact_mut(ow * n).enumerate() {
|
||||
if ctl.cancelled() {
|
||||
return;
|
||||
}
|
||||
let y = used.y0 + (task * ROWS_PER_TASK + k) as i32;
|
||||
read(Rect::new(used.x0, y, used.x1, y + 1), &mut row);
|
||||
premultiply(&mut row, n, alpha);
|
||||
let (Some(first), Some(last)) = (row.get(..n), row.get(row.len().saturating_sub(n)..)) else { return };
|
||||
s.line.clear();
|
||||
(0..left).for_each(|_| s.line.extend_from_slice(first));
|
||||
s.line.extend_from_slice(&row);
|
||||
(0..right).for_each(|_| s.line.extend_from_slice(last));
|
||||
line_passes(dst, n, &radii, &mut s);
|
||||
}
|
||||
});
|
||||
// Vertical passes, strip by strip, the same way down the columns.
|
||||
let (top, bottom) = ((used.y0 - out.y0.saturating_sub(reach)) as usize, (out.y1.saturating_add(reach) - used.y1) as usize);
|
||||
let (rows, stride) = (used.height() as usize, ow * n);
|
||||
let strips: Vec<Rect> = strips(out).collect();
|
||||
let done = par_map(strips.len(), |k| {
|
||||
let t = strips[k];
|
||||
if ctl.cancelled() {
|
||||
return (t, Vec::new());
|
||||
}
|
||||
let (sx, w) = ((t.x0 - out.x0) as usize * n, t.width() as usize * n);
|
||||
let row = |y: usize| &h[y * stride + sx..y * stride + sx + w];
|
||||
let mut s = Scratch::default();
|
||||
(0..top).for_each(|_| s.line.extend_from_slice(row(0)));
|
||||
(0..rows).for_each(|y| s.line.extend_from_slice(row(y)));
|
||||
(0..bottom).for_each(|_| s.line.extend_from_slice(row(rows - 1)));
|
||||
let mut res = vec![0.0f32; oh * w];
|
||||
line_passes(&mut res, w, &radii, &mut s);
|
||||
unpremultiply(&mut res, n, alpha);
|
||||
(t, res)
|
||||
});
|
||||
(!ctl.cancelled()).then_some(done)
|
||||
}
|
||||
|
||||
/// Box blurs of the given (odd) widths, one after another along each axis, over a premultiplied
|
||||
/// window; running sums make the cost independent of the widths.
|
||||
/// Column strips of `out` at multiples of [`STRIP`], so each lies inside one tile column.
|
||||
fn strips(out: Rect) -> impl Iterator<Item = Rect> {
|
||||
(out.x0.div_euclid(STRIP) * STRIP..out.x1)
|
||||
.step_by(STRIP as usize)
|
||||
.map(move |x| Rect::new(x.max(out.x0), out.y0, x.saturating_add(STRIP).min(out.x1), out.y1))
|
||||
}
|
||||
const _: () = assert!(photocraft_geom::TILE_SIZE % STRIP == 0);
|
||||
|
||||
/// [`box_blur`] of an image (edges repeated) into one interleaved buffer for `out`.
|
||||
fn box_passes(src: &Image, out: Rect, alpha: bool, boxes: &[usize]) -> Vec<f32> {
|
||||
let n = src.ch;
|
||||
let margin: i32 = boxes.iter().map(|w| (*w as i32 - 1) / 2).sum::<i32>() + 1;
|
||||
let win = Rect::new(out.x0 - margin, out.y0 - margin, out.x1 + margin, out.y1 + margin);
|
||||
let (ww, wh) = (win.width() as usize, win.height() as usize);
|
||||
let mut p = vec![0.0f32; ww * wh * n];
|
||||
let fill = |yy: usize, row: &mut [f32]| {
|
||||
let y = win.y0 + yy as i32;
|
||||
for (xx, px) in row.chunks_exact_mut(n).enumerate() {
|
||||
for (c, v) in px.iter_mut().enumerate() {
|
||||
*v = src.get(win.x0 + xx as i32, y, c);
|
||||
}
|
||||
}
|
||||
let read = |r: Rect, buf: &mut Vec<f32>| {
|
||||
buf.clear();
|
||||
buf.extend_from_slice(src.row(r));
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
p.par_chunks_mut(ww * n).enumerate().for_each(|(y, row)| fill(y, row));
|
||||
let ow = out.width() as usize * n;
|
||||
let mut res = vec![0.0f32; ow * out.height() as usize];
|
||||
if res.is_empty() {
|
||||
return res;
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
p.chunks_mut(ww * n).enumerate().for_each(|(y, row)| fill(y, row));
|
||||
premultiply(&mut p, n, alpha);
|
||||
for w in boxes {
|
||||
box_rows(&mut p, ww, n, (w - 1) / 2);
|
||||
for (t, data) in box_blur(src.rect, &read, out, n, alpha, boxes, &Interrupt::NONE).unwrap_or_default() {
|
||||
let (x0, w) = ((t.x0 - out.x0) as usize * n, t.width() as usize * n);
|
||||
for (dst, s) in res.chunks_exact_mut(ow).zip(data.chunks_exact(w)) {
|
||||
dst[x0..x0 + w].copy_from_slice(s);
|
||||
}
|
||||
}
|
||||
let mut t = transpose(&p, ww, wh, n);
|
||||
for w in boxes {
|
||||
box_rows(&mut t, wh, n, (w - 1) / 2);
|
||||
}
|
||||
let p = transpose(&t, wh, ww, n);
|
||||
let (ow, oh) = (out.width() as usize, out.height() as usize);
|
||||
let mut res = vec![0.0f32; ow * oh * n];
|
||||
for y in 0..oh {
|
||||
let s = ((y + margin as usize) * ww + margin as usize) * n;
|
||||
res[y * ow * n..(y + 1) * ow * n].copy_from_slice(&p[s..s + ow * n]);
|
||||
}
|
||||
unpremultiply(&mut res, n, alpha);
|
||||
res
|
||||
}
|
||||
|
||||
pub(crate) fn boxed(src: &Image, out: Rect, ctx: &Ctx, radius: f32) -> Vec<f32> {
|
||||
let r = radius.max(0.0).round() as usize;
|
||||
// Running sums cost the same at any radius; the direct kernel is quicker for the smallest.
|
||||
if r <= 4 {
|
||||
let k = vec![1.0 / (2 * r + 1) as f32; 2 * r + 1];
|
||||
return conv_sep(src, out, &k, &k, ctx.alpha);
|
||||
match box_widths(&FilterParams::BoxBlur { radius }) {
|
||||
Some(boxes) => box_passes(src, out, ctx.alpha, &boxes),
|
||||
None => {
|
||||
let r = radius.max(0.0).round() as usize;
|
||||
let k = vec![1.0 / (2 * r + 1) as f32; 2 * r + 1];
|
||||
conv_sep(src, out, &k, &k, ctx.alpha)
|
||||
}
|
||||
}
|
||||
box_passes(src, out, ctx.alpha, &[2 * r + 1])
|
||||
}
|
||||
|
||||
fn average_samples(src: &Image, out: Rect, ctx: &Ctx, mut offsets: impl FnMut(f32, f32, &mut Vec<(f32, f32)>)) -> Vec<f32> {
|
||||
@@ -440,6 +520,8 @@ fn surface_direct(src: &Image, out: Rect, ctx: &Ctx, r: i32, t: f32) -> Vec<f32>
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use photocraft_color::{ColorMode, PixelFormat, SampleType};
|
||||
use photocraft_raster::Surface;
|
||||
|
||||
#[test]
|
||||
fn box_gaussian_matches_exact_kernel() {
|
||||
@@ -456,7 +538,7 @@ mod tests {
|
||||
let out = Rect::new(0, 0, 120, 8);
|
||||
let k = gaussian_kernel(12.0);
|
||||
let exact = conv_sep(&img, out, &k, &k, true);
|
||||
let fast = gaussian_boxes(&img, out, true, 12.0);
|
||||
let fast = box_passes(&img, out, true, &boxes_for_gauss(12.0, 3));
|
||||
let err = exact.iter().zip(&fast).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
|
||||
assert!(err < 0.02, "max error {err}");
|
||||
assert_eq!(boxes_for_gauss(10.0, 3).len(), 3);
|
||||
@@ -486,6 +568,106 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The definition the running sums must match: each box applied in turn as a direct
|
||||
/// convolution (cost per pixel proportional to the radius) to `img`, which covers `out` grown
|
||||
/// by every box's reach (its own edge handling included).
|
||||
fn direct_boxes(img: &Image, out: Rect, boxes: &[usize], alpha: bool) -> Vec<f32> {
|
||||
let mut cur = img.clone();
|
||||
let mut left: i32 = boxes.iter().map(|w| (w / 2) as i32).sum();
|
||||
for &w in boxes {
|
||||
left -= (w / 2) as i32;
|
||||
let k = vec![1.0 / w as f32; w];
|
||||
let rect = out.inflate(left);
|
||||
cur = Image { rect, ch: img.ch, data: conv_sep(&cur, rect, &k, &k, alpha) };
|
||||
}
|
||||
cur.data
|
||||
}
|
||||
|
||||
/// Noisy pixels over `r` (alpha varying, some fully transparent).
|
||||
fn noisy(fmt: PixelFormat, r: Rect, seed: u32) -> Surface {
|
||||
let mut s = Surface::new(fmt);
|
||||
let mut st = seed;
|
||||
let mut v = Vec::new();
|
||||
for _ in 0..r.width() * r.height() {
|
||||
for c in 0..fmt.channels() {
|
||||
st ^= st << 13;
|
||||
st ^= st >> 17;
|
||||
st ^= st << 5;
|
||||
let x = (st % 256) as f32 / 255.0;
|
||||
v.push(if fmt.alpha && c + 1 == fmt.channels() && st.is_multiple_of(5) { 0.0 } else { x });
|
||||
}
|
||||
}
|
||||
s.write_region(r, &v);
|
||||
s
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layer_box_blurs_match_direct_convolution_at_any_radius() {
|
||||
// Gaussian and Box Blur at radii up to several times the layer (37 × 23), filtered as a
|
||||
// document layer (canvas edge repeated, as Photoshop does) and on their own (transparent
|
||||
// beyond), at every depth, with and without alpha, 1 to 5 channels.
|
||||
let canvas = Rect::new(0, 0, 37, 23);
|
||||
let formats = [
|
||||
PixelFormat::RGBA8,
|
||||
PixelFormat::RGBA16,
|
||||
PixelFormat::RGBA32F,
|
||||
PixelFormat::GRAY8,
|
||||
PixelFormat::CMYKA8,
|
||||
PixelFormat::new(ColorMode::Lab, SampleType::F32, false),
|
||||
];
|
||||
let params = [
|
||||
FilterParams::GaussianBlur { radius: 4.5 },
|
||||
FilterParams::GaussianBlur { radius: 9.3 },
|
||||
FilterParams::GaussianBlur { radius: 40.0 },
|
||||
FilterParams::BoxBlur { radius: 5.0 },
|
||||
FilterParams::BoxBlur { radius: 70.0 },
|
||||
];
|
||||
for (i, fmt) in formats.into_iter().enumerate() {
|
||||
let s = noisy(fmt, canvas, 0x9e37_79b9 + i as u32);
|
||||
// A level of the depth (colour and alpha are each rounded on write) plus float noise.
|
||||
let tol = match fmt.sample {
|
||||
SampleType::U8 => 1.0 / 255.0,
|
||||
SampleType::U16 => 1.0 / 65535.0,
|
||||
_ => 0.0,
|
||||
} + 2e-5;
|
||||
for p in ¶ms {
|
||||
let boxes = box_widths(p).expect("a box-pass blur");
|
||||
let reach: i32 = boxes.iter().map(|w| (w / 2) as i32).sum();
|
||||
let area = crate::output_area(p, s.content_bounds(), canvas, None);
|
||||
for (got, img, out) in [
|
||||
(crate::apply_in(&s, p, area, canvas, None, canvas), Image::read_clamped(&s, canvas.inflate(reach), canvas), canvas),
|
||||
(crate::apply(&s, p, area, canvas, None), Image::read(&s, area.inflate(reach)), area),
|
||||
] {
|
||||
// Compared alpha-weighted: colour is meaningless where (nearly) transparent.
|
||||
let (mut got, mut want) = (got.read_region(out), direct_boxes(&img, out, &boxes, fmt.alpha));
|
||||
premultiply(&mut got, img.ch, fmt.alpha);
|
||||
premultiply(&mut want, img.ch, fmt.alpha);
|
||||
let err = got.iter().zip(&want).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max);
|
||||
assert!(err <= tol, "{} on {fmt:?} over {out:?}: max error {err}", p.label());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layer_box_blurs_mix_by_the_selection_and_stop_when_cancelled() {
|
||||
let canvas = Rect::new(0, 0, 37, 23);
|
||||
let s = noisy(PixelFormat::RGBA32F, canvas, 7);
|
||||
let p = FilterParams::GaussianBlur { radius: 12.0 };
|
||||
let full = crate::apply_in(&s, &p, canvas, canvas, None, canvas);
|
||||
let half = Surface::with_default(PixelFormat::new(ColorMode::Grayscale, SampleType::F32, false), &[0.5]);
|
||||
let mixed = crate::apply_in(&s, &p, canvas, canvas, Some(&half), canvas);
|
||||
for (x, y) in [(0, 0), (18, 11), (36, 22)] {
|
||||
let (o, f, m) = (s.pixel(x, y), full.pixel(x, y), mixed.pixel(x, y));
|
||||
for c in 0..4 {
|
||||
assert!((m[c] - (o[c] + f[c]) / 2.0).abs() < 1e-5, "({x},{y}) channel {c}");
|
||||
}
|
||||
}
|
||||
let cancel = || true;
|
||||
let ctl = Interrupt::cancel_only(&cancel);
|
||||
assert!(crate::apply_in_with(&s, &p, canvas, canvas, None, canvas, &ctl).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn surface_blur_8bit_histogram_matches_the_direct_sum() {
|
||||
// 8-bit levels with some fully transparent pixels, which the sum skips.
|
||||
|
||||
@@ -74,6 +74,13 @@ impl Image {
|
||||
if self.rect.contains(x, y) { self.data[self.index(x, y) + c] } else { 0.0 }
|
||||
}
|
||||
|
||||
/// Samples of the pixels of a one-pixel-high rectangle (must be inside).
|
||||
#[inline]
|
||||
pub fn row(&self, r: Rect) -> &[f32] {
|
||||
let i = self.index(r.x0, r.y0);
|
||||
&self.data[i..i + r.width() as usize * self.ch]
|
||||
}
|
||||
|
||||
/// Pixel slice at `(x, y)` (must be inside).
|
||||
#[inline]
|
||||
pub fn px(&self, x: i32, y: i32) -> &[f32] {
|
||||
|
||||
+85
-13
@@ -938,6 +938,9 @@ pub fn apply_tiled_with(
|
||||
if area.is_empty() {
|
||||
return Some(out);
|
||||
}
|
||||
if let Some(boxes) = blur::box_widths(params) {
|
||||
return apply_box_blur(out, surface, area, extent, selection, &boxes, ctl);
|
||||
}
|
||||
let fmt = surface.format();
|
||||
let ctx = Ctx { bounds, mode: fmt.mode, alpha: fmt.alpha };
|
||||
let halo = params.halo();
|
||||
@@ -983,19 +986,7 @@ pub fn apply_tiled_with(
|
||||
};
|
||||
let mut data = kernel(params, src, *t, &ctx);
|
||||
if let Some(sel) = selection {
|
||||
let n = src.ch;
|
||||
let w = t.width() as usize;
|
||||
for (i, px) in data.chunks_exact_mut(n).enumerate() {
|
||||
let (xx, yy) = (t.x0 + (i % w) as i32, t.y0 + (i / w) as i32);
|
||||
let k = sel.pixel(xx, yy)[0].clamp(0.0, 1.0);
|
||||
if k >= 1.0 {
|
||||
continue;
|
||||
}
|
||||
for (c, v) in px.iter_mut().enumerate() {
|
||||
let o = src.get(xx, yy, c);
|
||||
*v = o + (*v - o) * k;
|
||||
}
|
||||
}
|
||||
mix_selection(&mut data, *t, sel, src);
|
||||
}
|
||||
tick();
|
||||
(*t, data)
|
||||
@@ -1032,6 +1023,87 @@ pub fn apply_tiled_with(
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// [`apply_tiled_with`] for the blurs run as box passes ([`blur::box_widths`]): the whole area at
|
||||
/// once (in column bands of about [`RESULT_BUDGET`] bytes of floats), so the wide halo of a large
|
||||
/// radius is read once rather than around every tile. Pixels beyond `extent` repeat its edge, as
|
||||
/// [`Image::read_clamped`] does for tiles; without one the surface is read (transparent where empty).
|
||||
fn apply_box_blur(
|
||||
mut out: Surface,
|
||||
surface: &Surface,
|
||||
area: Rect,
|
||||
extent: Option<Rect>,
|
||||
selection: Option<&Surface>,
|
||||
boxes: &[usize],
|
||||
ctl: &photocraft_raster::Interrupt,
|
||||
) -> Option<Surface> {
|
||||
let fmt = surface.format();
|
||||
let n = fmt.channels();
|
||||
let reach = i32::try_from(boxes.iter().map(|w| w / 2).sum::<usize>()).unwrap_or(i32::MAX);
|
||||
let grown = area.inflate(reach);
|
||||
let src = extent.map(|e| grown.intersect(&e)).filter(|r| !r.is_empty()).unwrap_or(grown);
|
||||
let read = |r: Rect, buf: &mut Vec<f32>| surface.read_region_into(r, buf);
|
||||
// Per output column: the horizontal results for every source row, plus the final samples.
|
||||
let column_bytes = (src.height() as usize + area.height() as usize) * n * std::mem::size_of::<f32>();
|
||||
let band = i32::try_from(RESULT_BUDGET / column_bytes.max(1)).unwrap_or(i32::MAX).max(blur::STRIP) / blur::STRIP * blur::STRIP;
|
||||
let bands: Vec<Rect> = (area.x0..area.x1).step_by(band as usize).map(|x| Rect::new(x, area.y0, x.saturating_add(band).min(area.x1), area.y1)).collect();
|
||||
for (i, b) in bands.iter().enumerate() {
|
||||
let strips = blur::box_blur(src, &read, *b, n, fmt.alpha, boxes, ctl)?;
|
||||
// Each strip lies in one tile column: the columns' tiles are written in parallel.
|
||||
let mut columns: Vec<(Surface, Vec<blur::Strip>)> = Vec::new();
|
||||
for (t, data) in strips {
|
||||
match columns.last_mut() {
|
||||
Some((_, v)) if v.first().is_some_and(|(r, _)| r.x0.div_euclid(photocraft_geom::TILE_SIZE) == t.x0.div_euclid(photocraft_geom::TILE_SIZE)) => {
|
||||
v.push((t, data))
|
||||
}
|
||||
_ => columns.push((Surface::with_default(fmt, &surface.default_pixel()), vec![(t, data)])),
|
||||
}
|
||||
}
|
||||
for (s, v) in &mut columns {
|
||||
let r = v.iter().fold(Rect::EMPTY, |acc, (t, _)| if acc.is_empty() { *t } else { acc.union(t) });
|
||||
s.put_tiles(out.take_tiles(r));
|
||||
}
|
||||
let write = |(s, v): &mut (Surface, Vec<blur::Strip>)| {
|
||||
for (t, data) in v.iter_mut() {
|
||||
if let Some(sel) = selection {
|
||||
mix_selection(data, *t, sel, &Image::read(surface, *t));
|
||||
}
|
||||
s.write_region(*t, data);
|
||||
}
|
||||
};
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
columns.par_iter_mut().for_each(write);
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
columns.iter_mut().for_each(write);
|
||||
for (s, _) in columns {
|
||||
out.put_tiles(s.tiles().map(|(c, t)| (*c, t.clone())));
|
||||
}
|
||||
ctl.progress(0.98 * (i + 1) as f32 / bands.len() as f32);
|
||||
}
|
||||
out.prune();
|
||||
ctl.progress(1.0);
|
||||
Some(out)
|
||||
}
|
||||
|
||||
/// Mixes filtered `data` for `t` back toward the original pixels in `orig` by the selection's
|
||||
/// coverage (channel 0 of `sel`).
|
||||
fn mix_selection(data: &mut [f32], t: Rect, sel: &Surface, orig: &Image) {
|
||||
let w = t.width() as usize;
|
||||
for (i, px) in data.chunks_exact_mut(orig.ch.max(1)).enumerate() {
|
||||
let (x, y) = (t.x0 + (i % w) as i32, t.y0 + (i / w) as i32);
|
||||
let k = sel.pixel(x, y)[0].clamp(0.0, 1.0);
|
||||
if k >= 1.0 {
|
||||
continue;
|
||||
}
|
||||
for (c, v) in px.iter_mut().enumerate() {
|
||||
let o = orig.get(x, y, c);
|
||||
*v = o + (*v - o) * k;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Bytes of float filter results held at once by [`apply_tiled`].
|
||||
const RESULT_BUDGET: usize = 256 << 20;
|
||||
|
||||
|
||||
@@ -34,7 +34,9 @@ fn run(s: &Surface, p: &FilterParams) -> Surface {
|
||||
fn all_filters() -> Vec<FilterParams> {
|
||||
vec![
|
||||
FilterParams::GaussianBlur { radius: 2.5 },
|
||||
FilterParams::GaussianBlur { radius: 9.0 },
|
||||
FilterParams::BoxBlur { radius: 2.0 },
|
||||
FilterParams::BoxBlur { radius: 6.0 },
|
||||
FilterParams::MotionBlur { angle: 30.0, distance: 7.0 },
|
||||
FilterParams::RadialBlur { amount: 20.0, method: RadialMethod::Spin, center_x: 0.5, center_y: 0.5 },
|
||||
FilterParams::RadialBlur { amount: 30.0, method: RadialMethod::Zoom, center_x: 0.3, center_y: 0.6 },
|
||||
|
||||
@@ -236,7 +236,7 @@ fn main() {
|
||||
|
||||
// ---- filters / adjustments / layers ------------------------------------------------------
|
||||
select(&mut s, 0);
|
||||
for radius in [4.0, 40.0] {
|
||||
for radius in [4.0, 40.0, 250.0] {
|
||||
b.time(&format!("Gaussian Blur r {radius} (photo layer) + refresh"), |b| {
|
||||
let t = Instant::now();
|
||||
exec(&mut s, "filter.blur.gaussianBlur", json!({"radius": radius}));
|
||||
@@ -246,6 +246,19 @@ fn main() {
|
||||
v
|
||||
});
|
||||
}
|
||||
// Filter dialog live preview, per radius change (#464): the command on the proxy plus its
|
||||
// flatten for upload.
|
||||
for radius in [40.0, 250.0] {
|
||||
b.time(&format!("Gaussian Blur dialog preview r {radius}"), |_| {
|
||||
let st = s.active().expect("doc");
|
||||
let k = photocraft_ui_egui::proxy::factor(&st.doc);
|
||||
let t = Instant::now();
|
||||
let p = photocraft_ui_egui::filter_dialog::preview_document(&st.doc, st.active_layer, "filter.blur.gaussianBlur", &json!({"radius": radius}), k)
|
||||
.expect("preview");
|
||||
std::hint::black_box(photocraft_compose::flatten(&p));
|
||||
ms(t)
|
||||
});
|
||||
}
|
||||
// Image › Adjustments dialog preview, per settings change (#77). Before: the command re-run
|
||||
// on a downsampled proxy (plus its flatten for upload). After: the document with a temporary
|
||||
// clipped adjustment layer, recomposited over the target's area by the canvas compositor.
|
||||
|
||||
Reference in new issue
Block a user