PhotoCraft: An open-source, photo editor written by scratch in pure Rust
perf: parallelize Filter Gallery cores (Sobel edges, Kuwahara)
Brandon Thomascb97c8502785
diff --git a/crates/algo/src/artistic_fx.rs b/crates/algo/src/artistic_fx.rsindex 383c247ed..6cb1ef9ec 100644--- a/crates/algo/src/artistic_fx.rs+++ b/crates/algo/src/artistic_fx.rs@@ -8,6 +8,7 @@// Effects walk several per-pixel planes in lockstep; index loops read clearest.#![allow(clippy::needless_range_loop)]+use rayon::prelude::*;use std::f32::consts::PI;use photocraft_geom::Rect;@@ -119,13 +120,14 @@ fn sobel(p: &[f32], w: usize, h: usize) -> (Vec<f32>, Vec<f32>) {let at = |x: isize, y: isize| p[(y.clamp(0, h as isize - 1) as usize) * w + x.clamp(0, w as isize - 1) as usize];let mut gx = vec![0.0; w * h];let mut gy = vec![0.0; w * h];- for y in 0..h as isize {+ gx.par_chunks_mut(w).zip(gy.par_chunks_mut(w)).enumerate().for_each(|(y, (gxr, gyr))| {+ let y = y as isize;for x in 0..w as isize {- let i = y as usize * w + x as usize;- gx[i] = ((at(x + 1, y - 1) + 2.0 * at(x + 1, y) + at(x + 1, y + 1)) - (at(x - 1, y - 1) + 2.0 * at(x - 1, y) + at(x - 1, y + 1))) / 8.0;- gy[i] = ((at(x - 1, y + 1) + 2.0 * at(x, y + 1) + at(x + 1, y + 1)) - (at(x - 1, y - 1) + 2.0 * at(x, y - 1) + at(x + 1, y - 1))) / 8.0;+ let xi = x as usize;+ gxr[xi] = ((at(x + 1, y - 1) + 2.0 * at(x + 1, y) + at(x + 1, y + 1)) - (at(x - 1, y - 1) + 2.0 * at(x - 1, y) + at(x - 1, y + 1))) / 8.0;+ gyr[xi] = ((at(x - 1, y + 1) + 2.0 * at(x, y + 1) + at(x + 1, y + 1)) - (at(x - 1, y - 1) + 2.0 * at(x, y - 1) + at(x + 1, y - 1))) / 8.0;}- }+ });(gx, gy)}@@ -201,7 +203,8 @@ fn kuwahara(win: &mut Win, r: usize) {let i = ((y.clamp(0, h as isize - 1) as usize) * w + x.clamp(0, w as isize - 1) as usize) * 5;&m[i..i + 5]};- for y in 0..h as isize {+ win.c.par_chunks_mut(w).enumerate().for_each(|(y, row)| {+ let y = y as isize;for x in 0..w as isize {let mut best = f32::MAX;let mut col = [0.0; 3];@@ -213,9 +216,9 @@ fn kuwahara(win: &mut Win, r: usize) {col = [q[0], q[1], q[2]];}}- win.c[y as usize * w + x as usize] = col;+ row[x as usize] = col;}- }+ });}fn kuwahara_reach(r: usize) -> i32 {