Add convolution with edge saturating

This commit is contained in:
cdo committed 2026-10-28 19:40:11 +00:00
1 parent e267f7ef7e
commit cdcab41c73
1 file changed
+44
+44
View File
@@ -7,6 +7,32 @@ pub trait Kernel {
fn convolve<P>(&self, img: &image::Image<P>) -> image::Image<P>
where
P: image::Pixel;
fn convolve_sat<P>(&self, img: &image::Image<P>) -> image::Image<P>
where
P: image::Pixel;
}
fn at_sat<P>(x: isize, y: isize, img: &image::Image<P>) -> P
where
P: Copy,
{
img.at(
if x < 0 {
0
} else if x as usize >= img.extent.x {
img.extent.x - 1
} else {
x as usize
},
if y < 0 {
0
} else if y as usize >= img.extent.y {
img.extent.y - 1
} else {
y as usize
},
)
}
impl<const N: usize> Kernel for [[f32; N]; N] {
@@ -29,6 +55,24 @@ impl<const N: usize> Kernel for [[f32; N]; N] {
});
result
}
fn convolve_sat<P>(&self, img: &image::Image<P>) -> image::Image<P>
where
P: image::Pixel,
{
let l = N as isize / 2;
let mut result = image::Image::with_extent(img.extent);
result.for_each(|x, y, p| {
for i in 0..N {
for j in 0..N {
let xi = (x + j) as isize - l;
let yi = (y + i) as isize - l;
*p += at_sat(xi, yi, img) * self[i][j];
}
}
});
result
}
}
#[cfg(test)]