Skip to content

Commit

Permalink
perf: speed up blur with rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf committed Aug 25, 2021
1 parent 4c34d47 commit dfd25ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pathfinder_geometry = "0.5.1"
log = "0.4.11"
lazy_static = "1.4.0"
shell-words = { version = "1.0.0", optional = true }
rayon = "1.5.1"

[target.'cfg(target_os = "macos")'.dependencies]
pasteboard = "0.1.3"
Expand Down
31 changes: 26 additions & 5 deletions src/blur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
//! This file is originally from https://github.com/fschutt/fastblur
//! Edited by aloxaf <aloxafx@gmail.com> to process RgbaImage
use image::RgbaImage;
use std::cmp::min;

use image::RgbaImage;
use rayon::prelude::*;

#[derive(Copy, Clone)]
struct SharedMutPtr<>(*mut [[u8; 4]]);

unsafe impl Sync for SharedMutPtr {}

impl SharedMutPtr {
unsafe fn get(&self) -> &mut [[u8; 4]] {
&mut *self.0
}
}

pub fn gaussian_blur(image: RgbaImage, sigma: f32) -> RgbaImage {
let (width, height) = image.dimensions();
let mut raw = image.into_raw();
Expand Down Expand Up @@ -109,7 +122,8 @@ fn box_blur_vert(
) {
let iarr = 1.0 / (blur_radius + blur_radius + 1) as f32;

for i in 0..width {
let frontbuf = SharedMutPtr(frontbuf as *mut [[u8; 4]]);
(0..width).into_par_iter().for_each(|i| {
let col_start = i; //inclusive
let col_end = i + width * (height - 1); //inclusive
let mut ti: usize = i;
Expand Down Expand Up @@ -166,6 +180,7 @@ fn box_blur_vert(
val_b += isize::from(bb[2]) - isize::from(fv[2]);
val_a += isize::from(bb[3]) - isize::from(fv[3]);

let frontbuf = unsafe { frontbuf.get() };
frontbuf[ti] = [
round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
Expand All @@ -188,6 +203,7 @@ fn box_blur_vert(
val_b += isize::from(bb1[2]) - isize::from(bb2[2]);
val_a += isize::from(bb1[3]) - isize::from(bb2[3]);

let frontbuf = unsafe { frontbuf.get() };
frontbuf[ti] = [
round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
Expand All @@ -206,6 +222,7 @@ fn box_blur_vert(
val_b += isize::from(lv[2]) - isize::from(bb[2]);
val_a += isize::from(lv[3]) - isize::from(bb[3]);

let frontbuf = unsafe { frontbuf.get() };
frontbuf[ti] = [
round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
Expand All @@ -215,7 +232,7 @@ fn box_blur_vert(
ti += width;
}
}
}
});
}

#[inline]
Expand All @@ -228,7 +245,8 @@ fn box_blur_horz(
) {
let iarr = 1.0 / (blur_radius + blur_radius + 1) as f32;

for i in 0..height {
let frontbuf = SharedMutPtr(frontbuf as *mut [[u8; 4]]);
(0..height).into_par_iter().for_each(|i| {
let row_start: usize = i * width; // inclusive
let row_end: usize = (i + 1) * width - 1; // inclusive
let mut ti: usize = i * width; // VERTICAL: $i;
Expand Down Expand Up @@ -286,6 +304,7 @@ fn box_blur_horz(
val_b += isize::from(bb[2]) - isize::from(fv[2]);
val_a += isize::from(bb[3]) - isize::from(fv[3]);

let frontbuf = unsafe { frontbuf.get() };
frontbuf[ti] = [
round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
Expand All @@ -310,6 +329,7 @@ fn box_blur_horz(
val_b += isize::from(bb1[2]) - isize::from(bb2[2]);
val_a += isize::from(bb1[3]) - isize::from(bb2[3]);

let frontbuf = unsafe { frontbuf.get() };
frontbuf[ti] = [
round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
Expand All @@ -329,6 +349,7 @@ fn box_blur_horz(
val_b += isize::from(lv[2]) - isize::from(bb[2]);
val_a += isize::from(lv[3]) - isize::from(bb[3]);

let frontbuf = unsafe { frontbuf.get() };
frontbuf[ti] = [
round(val_r as f32 * iarr) as u8,
round(val_g as f32 * iarr) as u8,
Expand All @@ -338,7 +359,7 @@ fn box_blur_horz(
ti += 1;
}
}
}
});
}

#[inline]
Expand Down

0 comments on commit dfd25ae

Please sign in to comment.