Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(retouch): row-parallel Blur/Sharpen dabs, 2.6-3.3x faster strokes (#5426)

Juan Antonio Redondo78d06781a03d
diff --git a/crates/algo/src/retouch.rs b/crates/algo/src/retouch.rs
index f7c1c1855..e7d33453d 100644
--- a/crates/algo/src/retouch.rs
+++ b/crates/algo/src/retouch.rs
@@ -7,6 +7,7 @@
//! "Protect Tones" works on luminance (scaling RGB so hue and saturation are kept, with gamut
//! compression instead of per-channel clipping).
+use rayon::prelude::*;
use serde::{Deserialize, Serialize};
/// Tonal range targeted by Dodge/Burn.
@@ -124,56 +125,56 @@ pub fn local_blur(data: &[f32], w: usize, h: usize, ch: usize, alpha: Option<usi
let k: Vec<f32> = (-r..=r).map(|i| (-((i * i) as f32) / (2.0 * sigma * sigma).max(1e-6)).exp()).collect();
let ks: f32 = k.iter().sum();
let k: Vec<f32> = k.into_iter().map(|v| v / ks).collect();
- // Horizontal pass over the window rows extended vertically by r.
+ // Horizontal pass over the window rows extended vertically by r. Rows are independent, so
+ // both passes run them in parallel: a 2500 px Blur dab is ~10⁹ multiply-adds (#5426).
let th = oh + 2 * r as usize;
let stride = ow * ch;
let mut tmp = vec![0.0f32; stride * th];
- let mut row = vec![0.0f32; (ow + 2 * r as usize) * ch];
- for ty in 0..th {
- let y = (y0 as i32 + ty as i32 - r).clamp(0, h as i32 - 1) as usize;
- // Clamp and premultiply each source pixel once, rather than once for every kernel tap.
- for (rx, px) in row.chunks_exact_mut(ch).enumerate() {
- let x = (x0 as i32 + rx as i32 - r).clamp(0, w as i32 - 1) as usize;
- let i = (y * w + x) * ch;
- px.copy_from_slice(&data[i..i + ch]);
- if let Some(a) = alpha {
- let av = px[a];
- for (c, v) in px.iter_mut().enumerate() {
- if c != a {
- *v *= av;
+ tmp.par_chunks_mut(stride).enumerate().for_each_init(
+ || vec![0.0f32; (ow + 2 * r as usize) * ch],
+ |row, (ty, dst)| {
+ let y = (y0 as i32 + ty as i32 - r).clamp(0, h as i32 - 1) as usize;
+ // Clamp and premultiply each source pixel once, rather than once for every kernel tap.
+ for (rx, px) in row.chunks_exact_mut(ch).enumerate() {
+ let x = (x0 as i32 + rx as i32 - r).clamp(0, w as i32 - 1) as usize;
+ let i = (y * w + x) * ch;
+ px.copy_from_slice(&data[i..i + ch]);
+ if let Some(a) = alpha {
+ let av = px[a];
+ for (c, v) in px.iter_mut().enumerate() {
+ if c != a {
+ *v *= av;
+ }
}
}
}
- }
- let dst = &mut tmp[ty * stride..(ty + 1) * stride];
- // Contiguous accumulation can be vectorized for any channel layout. Each output still
- // sums the same products in the same kernel order as the scalar convolution.
- for (i, kv) in k.iter().enumerate() {
- for (d, s) in dst.iter_mut().zip(&row[i * ch..i * ch + stride]) {
- *d += s * kv;
+ // Contiguous accumulation can be vectorized for any channel layout. Each output still
+ // sums the same products in the same kernel order as the scalar convolution.
+ for (i, kv) in k.iter().enumerate() {
+ for (d, s) in dst.iter_mut().zip(&row[i * ch..i * ch + stride]) {
+ *d += s * kv;
+ }
}
- }
- }
+ },
+ );
let mut out = vec![0.0f32; ow * oh * ch];
- for oy in 0..oh {
- let dst = &mut out[oy * stride..(oy + 1) * stride];
+ out.par_chunks_mut(stride).enumerate().for_each(|(oy, dst)| {
for (i, kv) in k.iter().enumerate() {
for (d, s) in dst.iter_mut().zip(&tmp[(oy + i) * stride..(oy + i + 1) * stride]) {
*d += s * kv;
}
}
- for ox in 0..ow {
- let o = (oy * ow + ox) * ch;
- if let Some(a) = alpha {
- let av = out[o + a];
- for c in 0..ch {
+ if let Some(a) = alpha {
+ for (ox, px) in dst.chunks_exact_mut(ch).enumerate() {
+ let av = px[a];
+ for (c, v) in px.iter_mut().enumerate() {
if c != a {
- out[o + c] = if av > 1e-6 { out[o + c] / av } else { data[((y0 + oy) * w + x0 + ox) * ch + c] };
+ *v = if av > 1e-6 { *v / av } else { data[((y0 + oy) * w + x0 + ox) * ch + c] };
}
}
}
}
- }
+ });
out
}
diff --git a/crates/engine/src/retouch_cmds.rs b/crates/engine/src/retouch_cmds.rs
index 9ea7fd597..f41dff7e8 100644
--- a/crates/engine/src/retouch_cmds.rs
+++ b/crates/engine/src/retouch_cmds.rs
@@ -18,6 +18,7 @@ use photocraft_geom::Rect;
use photocraft_paint::retouch::{Footprint, Region, Smudge, alpha_index, apply_coverage, apply_dab_stroke, stroke_coverage};
use photocraft_paint::{BrushSettings, Stroke, StrokePoint};
use photocraft_raster::{Surface, from_rgba_into, to_rgba};
+use rayon::prelude::*;
use serde_json::{Value, json};
use crate::commands::{CommandSpec, blend_from_str};
@@ -977,19 +978,26 @@ impl DabTool {
let win = ((r.x0 - work.rect.x0) as usize, (r.y0 - work.rect.y0) as usize, (r.x1 - work.rect.x0) as usize, (r.y1 - work.rect.y0) as usize);
let out = if sharpen { local_sharpen(&work.data, w, h, n, a, win, 1.0, protect) } else { local_blur(&work.data, w, h, n, a, win, sigma) };
let rw = r.width() as usize;
- for y in r.y0..r.y1 {
+ // Row-parallel, like the blur itself (#5426).
+ let (wx0, wy0) = (work.rect.x0, work.rect.y0);
+ work.data.par_chunks_mut(w * n).enumerate().for_each(|(row, line)| {
+ let y = wy0 + row as i32;
+ if y < r.y0 || y >= r.y1 {
+ return;
+ }
for x in r.x0..r.x1 {
let k = per_dab(fp.at(x, y) * strength, sp);
if k <= 0.0 {
continue;
}
let o = ((y - r.y0) as usize * rw + (x - r.x0) as usize) * n;
- let px = work.px_mut(x, y);
+ let i = (x - wx0) as usize * n;
+ let px = &mut line[i..i + n];
for c in 0..n {
px[c] += (out[o + c] - px[c]) * k;
}
}
- }
+ });
})
}
Self::Smudge { strength, finger, max_size } => {