#[cfg(feature = "threads")]
use rayon::prelude::*;
#[cfg(feature = "threads")]
fn min_rows_per_task(width: usize) -> usize {
(4096 / width.max(1)).max(1)
}
pub(crate) fn for_each_row<T: Send, S>(
buf: &mut [T],
width: usize,
init: impl Fn() -> S + Sync + Send,
f: impl Fn(&mut S, usize, &mut [T]) + Sync + Send,
) {
#[cfg(feature = "threads")]
buf.par_chunks_mut(width)
.with_min_len(min_rows_per_task(width))
.enumerate()
.for_each_init(init, |state, (j, row)| f(state, j, row));
#[cfg(not(feature = "threads"))]
{
let mut state = init();
for (j, row) in buf.chunks_mut(width).enumerate() {
f(&mut state, j, row);
}
}
}
fn blur_row(row: &[u8], out: &mut [u8], size: usize) {
let width = row.len();
let size_u32 = size as u32;
let div = size_u32 * 2;
let mut sum = u32::from(row[0]) * size_u32;
for &px in &row[..size] {
sum += u32::from(px);
}
for i in 0..size {
sum = sum - u32::from(row[0]) + u32::from(row[i + size]);
out[i] = (sum / div) as u8;
}
for i in size..width - size {
sum = sum - u32::from(row[i - size]) + u32::from(row[i + size]);
out[i] = (sum / div) as u8;
}
for i in width - size..width {
sum = sum - u32::from(row[i - size]) + u32::from(row[width - 1]);
out[i] = (sum / div) as u8;
}
}
fn filter3(src: &[u8], dst: &mut [u8], width: usize, height: usize, pick: impl Fn(u8, u8) -> u8 + Sync + Send) {
debug_assert_eq!(dst.len(), width * height);
for_each_row(
dst,
width,
|| (),
|(), j, out| {
let row = &src[j * width..(j + 1) * width];
let prevrow = &src[j.saturating_sub(1) * width..][..width];
let nextrow = &src[(height - 1).min(j + 1) * width..][..width];
let mut curr = row[0];
let mut next = row[0];
for i in 0..width - 1 {
let prev = curr;
curr = next;
next = row[i + 1];
let t1 = pick(prev, next);
let t2 = pick(nextrow[i], prevrow[i]);
out[i] = pick(curr, pick(t1, t2));
}
let t1 = pick(curr, next);
let t2 = pick(nextrow[width - 1], prevrow[width - 1]);
out[width - 1] = pick(t1, t2);
},
);
}
pub(crate) fn max3(src: &[u8], dst: &mut [u8], width: usize, height: usize) {
filter3(src, dst, width, height, std::cmp::max);
}
pub(crate) fn min3(src: &[u8], dst: &mut [u8], width: usize, height: usize) {
filter3(src, dst, width, height, std::cmp::min);
}
pub(crate) fn blur(buf: &mut [u8], tmp: &mut [u8], width: usize, height: usize, size: usize) {
debug_assert!(size > 0);
if width < 2 * size + 1 || height < 2 * size + 1 {
return;
}
let src: &[u8] = buf;
for_each_row(
tmp,
width,
|| (),
|(), j, out| blur_row(&src[j * width..(j + 1) * width], out, size),
);
let tmp: &[u8] = tmp;
let div = (size * 2) as u32;
for_each_row(
buf,
width,
|| vec![0u32; width],
|acc, j, out| {
acc.fill(0);
for k in (j + 1).saturating_sub(size)..=(j + size).min(height - 1) {
for (a, &v) in acc.iter_mut().zip(&tmp[k * width..(k + 1) * width]) {
*a += u32::from(v);
}
}
let top_clamped = (size - 1).saturating_sub(j) as u32;
let bottom_clamped = (j + size).saturating_sub(height - 1) as u32;
let (first, last) = (&tmp[..width], &tmp[(height - 1) * width..]);
for (((o, &a), &f), &l) in out.iter_mut().zip(acc.iter()).zip(first).zip(last) {
let sum = a + u32::from(f) * top_clamped + u32::from(l) * bottom_clamped;
*o = (sum / div) as u8;
}
},
);
}
#[cfg(test)]
mod tests {
use super::*;
fn transposing_1d_blur(src: &[u8], dst: &mut [u8], width: usize, height: usize, size: usize) {
let size_u32 = size as u32;
for j in 0..height {
let row = &src[j * width..(j + 1) * width];
let mut sum = u32::from(row[0]) * size_u32;
for &px in &row[..size] {
sum = sum.wrapping_add(u32::from(px));
}
for i in 0..size {
sum = sum
.wrapping_sub(u32::from(row[0]))
.wrapping_add(u32::from(row[i + size]));
dst[i * height + j] = (sum / (size_u32 * 2)) as u8;
}
for i in size..width - size {
sum = sum
.wrapping_sub(u32::from(row[i - size]))
.wrapping_add(u32::from(row[i + size]));
dst[i * height + j] = (sum / (size_u32 * 2)) as u8;
}
for i in width - size..width {
sum = sum
.wrapping_sub(u32::from(row[i - size]))
.wrapping_add(u32::from(row[width - 1]));
dst[i * height + j] = (sum / (size_u32 * 2)) as u8;
}
}
}
fn reference_blur(buf: &mut [u8], tmp: &mut [u8], width: usize, height: usize, size: usize) {
if width < 2 * size + 1 || height < 2 * size + 1 {
return;
}
transposing_1d_blur(buf, tmp, width, height, size);
transposing_1d_blur(tmp, buf, height, width, size);
}
fn reference_filter3(src: &[u8], dst: &mut [u8], width: usize, height: usize, pick: fn(u8, u8) -> u8) {
let mut out = dst.iter_mut();
for j in 0..height {
let row = &src[j * width..(j + 1) * width];
let prevrow = &src[j.saturating_sub(1) * width..][..width];
let nextrow = &src[(height - 1).min(j + 1) * width..][..width];
let mut curr = row[0];
let mut next = row[0];
for i in 0..width - 1 {
let prev = curr;
curr = next;
next = row[i + 1];
let t1 = pick(prev, next);
let t2 = pick(nextrow[i], prevrow[i]);
*out.next().unwrap() = pick(curr, pick(t1, t2));
}
let t1 = pick(curr, next);
let t2 = pick(nextrow[width - 1], prevrow[width - 1]);
*out.next().unwrap() = pick(t1, t2);
}
}
fn random_map(width: usize, height: usize, seed: u32) -> Vec<u8> {
let mut x = seed.wrapping_mul(2_654_435_761).wrapping_add(1);
(0..width * height)
.map(|i| {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
match (i / 7 + seed as usize) % 4 {
0 => 255,
1 => (x >> 24) as u8,
2 => 0,
_ => (x >> 28) as u8 * 17,
}
})
.collect()
}
#[test]
fn blur_matches_transposing_blur() {
for (seed, (w, h)) in [
(1, 1),
(2, 3),
(6, 6),
(7, 7),
(7, 9),
(8, 7),
(9, 30),
(31, 8),
(64, 64),
(65, 17),
(100, 7),
(7, 100),
(300, 211),
]
.into_iter()
.enumerate()
{
for size in 1..=4 {
let input = random_map(w, h, seed as u32 * 31 + size as u32);
let (mut a, mut b) = (input.clone(), input);
let (mut ta, mut tb) = (vec![0; w * h], vec![0; w * h]);
reference_blur(&mut a, &mut ta, w, h, size);
blur(&mut b, &mut tb, w, h, size);
assert_eq!(a, b, "{w}x{h} size {size}");
}
}
}
#[test]
fn min_max_match_sequential_filter() {
for (seed, (w, h)) in [(1, 1), (1, 5), (5, 1), (2, 2), (3, 7), (64, 33), (257, 129)]
.into_iter()
.enumerate()
{
let input = random_map(w, h, seed as u32);
let (mut a, mut b) = (vec![0; w * h], vec![0; w * h]);
reference_filter3(&input, &mut a, w, h, std::cmp::max);
max3(&input, &mut b, w, h);
assert_eq!(a, b);
reference_filter3(&input, &mut a, w, h, std::cmp::min);
min3(&input, &mut b, w, h);
assert_eq!(a, b);
}
}
}