Compare commits

...

3 Commits

Author SHA1 Message Date
61f0408bad blur: boxes_for_gaussian changes 2025-03-31 15:41:57 -04:00
9199791f51 blur: usage of width_sub_radius 2025-03-31 15:32:24 -04:00
b6fbc99dac update 2025-03-30 15:44:40 -04:00
2 changed files with 11 additions and 12 deletions

4
Cargo.lock generated
View File

@@ -739,9 +739,9 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.21.2" version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2806eaa3524762875e21c3dcd057bc4b7bfa01ce4da8d46be1cd43649e1cc6b" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]] [[package]]
name = "oorandom" name = "oorandom"

View File

@@ -30,18 +30,17 @@ impl Blur {
/// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each element in the output array contains the radius of the box filter for the corresponding pass. /// Approximate 1D Gaussian filter of standard deviation sigma with N box filter passes. Each element in the output array contains the radius of the box filter for the corresponding pass.
fn boxes_for_gaussian<const N: usize>(sigma: f32) -> [usize; N] { fn boxes_for_gaussian<const N: usize>(sigma: f32) -> [usize; N] {
let w_ideal = (12.0 * sigma * sigma / N as f32 + 1.0).sqrt(); let sigma_sq = sigma.powi(2);
let w_ideal = (12.0 * sigma_sq / N as f32 + 1.0).sqrt();
let mut w = w_ideal as usize; let mut w = w_ideal as usize;
w -= 1 - (w & 1); w -= 1 - (w & 1);
let mut m = 0.25 * (N * (w + 3)) as f32; let m = (0.25 * (N * (w + 3)) as f32 - 3.0 * sigma_sq / (w + 1) as f32).round() as usize;
m -= 3.0 * sigma * sigma / (w + 1) as f32;
let m = m.round() as usize;
let mut result = [0; N]; (0..N)
for (i, value) in result.iter_mut().enumerate() { .map(|i| (w + 1 - 2 * (i < m) as usize) / 2)
*value = (if i < m { w - 1 } else { w + 1 }) / 2; .collect::<Vec<_>>()
} .try_into()
result .unwrap()
} }
/// Perform one pass of the 2D box filter of the given radius. The result will be written to the src slice, while the buf slice is used as a scratch space. /// Perform one pass of the 2D box filter of the given radius. The result will be written to the src slice, while the buf slice is used as a scratch space.
@@ -67,7 +66,7 @@ impl Blur {
.for_each(|(src_row, dst_row)| { .for_each(|(src_row, dst_row)| {
// First we build a value for the beginning of each row. We assume periodic boundary conditions, so we need to push the left index to the opposite side of the row. // First we build a value for the beginning of each row. We assume periodic boundary conditions, so we need to push the left index to the opposite side of the row.
let width_sub_radius = width - radius; let width_sub_radius = width - radius;
let mut value = src_row[width - radius - 1]; let mut value = src_row[width_sub_radius - 1];
for j in 0..radius { for j in 0..radius {
value += src_row[width_sub_radius + j] + src_row[j]; value += src_row[width_sub_radius + j] + src_row[j];
} }