Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,36 @@ fn main() {
// )
// .unwrap();

median_blur(
fast_bilateral_filter(
&cvt.to_immutable_ref(),
&mut dst_image,
35,
7.,
5.,
ThreadingPolicy::Single,
)
.unwrap();

// bilateral_filter(
// &cvt.to_immutable_ref(),
// &mut dst_image,
// BilateralBlurParams {
// kernel_size: 35,
// spatial_sigma: 7.,
// range_sigma: 5.,
// },
// EdgeMode2D::new(EdgeMode::Clamp),
// Scalar::default(),
// ThreadingPolicy::Adaptive,
// )
// .unwrap();
// median_blur(
// &cvt.to_immutable_ref(),
// &mut dst_image,
// 35,
// ThreadingPolicy::Single,
// )
// .unwrap();

// tent_blur(
// &cvt.to_immutable_ref(),
// &mut dst_image,
Expand Down Expand Up @@ -340,7 +362,7 @@ fn main() {

if components == 3 {
image::save_buffer(
"blurred_stack_next35_2.jpg",
"blurred_stack_next_fbil.jpg",
bytes.as_bytes(),
dimensions.0,
dimensions.1,
Expand All @@ -349,7 +371,7 @@ fn main() {
.unwrap();
} else {
image::save_buffer(
"blurred_stack_next_f1.png",
"blurred_stack_next_bil.png",
bytes.as_bytes(),
dimensions.0,
dimensions.1,
Expand Down
33 changes: 19 additions & 14 deletions src/bilateral/bp8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ impl BilateralUnit<u8> for ExecutionUnit4<'_> {
let dst_row = &mut dst_row[..useful_width];
let src_row = &src_row[..useful_width];
for (x, (dst, center)) in dst_row
.chunks_exact_mut(N)
.zip(src_row.chunks_exact(N))
.as_chunks_mut::<N>()
.0
.iter_mut()
.zip(src_row.as_chunks::<N>().0.iter())
.enumerate()
{
let mut sum0 = 0f32;
Expand All @@ -139,7 +141,7 @@ impl BilateralUnit<u8> for ExecutionUnit4<'_> {
{
let c_slice = (y + ky) * a_stride + x * N;
let c_px_slice = &a_src[c_slice..(c_slice + N * self.params.kernel_size)];
for (c_px, &rwz) in c_px_slice.chunks_exact(N).zip(ky_row.iter()) {
for (c_px, &rwz) in c_px_slice.as_chunks::<N>().0.iter().zip(ky_row.iter()) {
let z0 = rwz * ss[(center[0] as u16 * 256 + c_px[0] as u16) as usize];
let z1 = rwz * ss[(center[1] as u16 * 256 + c_px[1] as u16) as usize];
let z2 = rwz * ss[(center[2] as u16 * 256 + c_px[2] as u16) as usize];
Expand Down Expand Up @@ -178,8 +180,10 @@ impl BilateralUnit<u8> for ExecutionUnit3<'_> {
let dst_row = &mut dst_row[..useful_width];
let src_row = &src_row[..useful_width];
for (x, (dst, center)) in dst_row
.chunks_exact_mut(N)
.zip(src_row.chunks_exact(N))
.as_chunks_mut::<N>()
.0
.iter_mut()
.zip(src_row.as_chunks::<N>().0.iter())
.enumerate()
{
let mut sum0 = 0f32;
Expand All @@ -196,7 +200,7 @@ impl BilateralUnit<u8> for ExecutionUnit3<'_> {
{
let c_slice = (y + ky) * a_stride + x * N;
let c_px_slice = &a_src[c_slice..(c_slice + N * self.params.kernel_size)];
for (c_px, &rwz) in c_px_slice.chunks_exact(N).zip(ky_row.iter()) {
for (c_px, &rwz) in c_px_slice.as_chunks::<N>().0.iter().zip(ky_row.iter()) {
let z0 = rwz * ss[(center[0] as u16 * 256 + c_px[0] as u16) as usize];
let z1 = rwz * ss[(center[1] as u16 * 256 + c_px[1] as u16) as usize];
let z2 = rwz * ss[(center[2] as u16 * 256 + c_px[2] as u16) as usize];
Expand Down Expand Up @@ -230,8 +234,10 @@ impl BilateralUnit<u8> for ExecutionUnitPlane<'_> {
let dst_row = &mut dst_row[..useful_width];
let src_row = &src_row[..useful_width];
for (x, (dst, center)) in dst_row
.chunks_exact_mut(N)
.zip(src_row.chunks_exact(N))
.as_chunks_mut::<N>()
.0
.iter_mut()
.zip(src_row.as_chunks::<N>().0.iter())
.enumerate()
{
let mut sum0 = 0f32;
Expand All @@ -243,7 +249,7 @@ impl BilateralUnit<u8> for ExecutionUnitPlane<'_> {
{
let c_slice = (y + ky) * a_stride + x * N;
let c_px_slice = &a_src[c_slice..(c_slice + N * self.params.kernel_size)];
for (c_px, &rwz) in c_px_slice.chunks_exact(N).zip(ky_row.iter()) {
for (c_px, &rwz) in c_px_slice.as_chunks::<N>().0.iter().zip(ky_row.iter()) {
let z0 = rwz * ss[(center[0] as u16 * 256 + c_px[0] as u16) as usize];
sum0 += z0 * c_px[0] as f32;
iw0 += z0;
Expand Down Expand Up @@ -271,7 +277,7 @@ fn bilateral_filter_impl<const N: usize>(
src.size_matches_mut(dst)?;

let arena = make_arena::<u8, N>(
src.data.as_ref(),
src.projected(),
src.row_stride() as usize,
src.size(),
ArenaPads::constant(params.kernel_size / 2),
Expand Down Expand Up @@ -320,12 +326,11 @@ fn bilateral_filter_impl<const N: usize>(

let thread_count = threading_policy.thread_count(src.width, src.height) as u32;
let thread_pool = novtb::ThreadPool::new(thread_count as usize);
dst.data
.borrow_mut()
dst.projected()
.tb_par_chunks_exact_mut(dst_row_stride)
.for_each_enumerated(&thread_pool, |y, dst| {
let src_row = &src.data.as_ref()
[y * src.row_stride() as usize..(y + 1) * src.row_stride() as usize];
let src_row = &src.data.as_ref()[y * src.row_stride() as usize
..y * src.row_stride() as usize + src.width as usize * src.channels.channels()];
_unit.execute(arena_src, y, dst, src_row)
});

Expand Down
8 changes: 7 additions & 1 deletion src/bilateral/neon/bp8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ impl<const N: usize> BilateralUnit<u8> for BilateralExecutionUnitNeon<'_, N> {
let a_stride = self.arena.width * self.arena.components;
let mut dst_row = &mut dst_row[..useful_width];
let mut src_row = &src_row[..useful_width];
let mut offset = 0usize;

unsafe {
for (wx, (dst, center)) in dst_row
Expand Down Expand Up @@ -164,6 +165,7 @@ impl<const N: usize> BilateralUnit<u8> for BilateralExecutionUnitNeon<'_, N> {
let vss0 = vqmovn_u16(vcombine_u16(ss0, ss1));
let vss1 = vqmovn_u16(vcombine_u16(ss2, ss3));
vst1q_u8(dst.as_mut_ptr().cast(), vcombine_u8(vss0, vss1));
offset += 16;
}

dst_row = dst_row.chunks_exact_mut(16).into_remainder();
Expand All @@ -184,7 +186,7 @@ impl<const N: usize> BilateralUnit<u8> for BilateralExecutionUnitNeon<'_, N> {

let cx = vshll_n_u8::<8>(vld1_u8(center.as_ptr().cast()));

let x = wx * 8;
let x = offset + wx * 8;

for (ky, ky_row) in sliced_range
.chunks_exact(self.params.kernel_size)
Expand Down Expand Up @@ -230,6 +232,7 @@ impl<const N: usize> BilateralUnit<u8> for BilateralExecutionUnitNeon<'_, N> {

let vss0 = vqmovn_u16(vcombine_u16(ss0, ss1));
vst1_u8(dst.as_mut_ptr().cast(), vss0);
offset += 8;
}
}

Expand All @@ -240,6 +243,8 @@ impl<const N: usize> BilateralUnit<u8> for BilateralExecutionUnitNeon<'_, N> {
let mut sum0 = 0f32;
let mut iw0 = 0f32;

let x = x + offset;

let sx = x % N;
for (ky, ky_row) in sliced_range
.chunks_exact(self.params.kernel_size)
Expand All @@ -258,6 +263,7 @@ impl<const N: usize> BilateralUnit<u8> for BilateralExecutionUnitNeon<'_, N> {
iw0 = if iw0 == 0. { 1. } else { iw0 };

*dst = (sum0 / iw0).round().min(255.).max(0.) as u8;
offset += 1;
}
}
}
Loading