Skip to content

Commit

Permalink
Added SIMD gradient for u8
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Sep 26, 2024
1 parent cb95e1d commit d3b55b2
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ workspace = { members = ["app"] }

[package]
name = "fast_morphology"
version = "0.1.2"
version = "0.1.3"
edition = "2021"
description = "Fast morphological operations for images"
readme = "README.md"
keywords = ["morph", "morphology", "dilate", "erode"]
license = "Apache-2.0 OR BSD-3-Clause"
license = "BSD-3-Clause OR Apache-2.0"
authors = ["Radzivon Bartoshyk"]
documentation = "https://github.com/awxkee/fast_morphology.git"
categories = ["multimedia::images"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Fast morphology in pure Rust

This crate provides fast 2D arbitrary shaped structuring element for planar, RGB and RGBA images.
Library provides high performance erosion, dilation, closing and opening.
Library provides high performance erosion, dilation, closing, opening, tophat, gradient and blackhat.

In most cases performance when implemented fully in hardware faster than OpenCV.

Expand Down Expand Up @@ -43,7 +43,7 @@ new_image.save("dilated.jpg").unwrap();

## Results

Here is some examply bokeh effect
Here is some example bokeh effect

<p float="left">
<img src="https://github.com/awxkee/fast_morphology/blob/master/assets/fruits.jpg?raw=true" width="273" height="409">
Expand Down
13 changes: 7 additions & 6 deletions app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fast_morphology::{
dilate, dilate_rgb, dilate_rgba, erode, erode_rgba, morphology_image, BorderMode, ImageSize,
KernelShape, MorphExOp, MorphologyThreadingPolicy,
dilate, dilate_rgb, dilate_rgba, erode, erode_rgba, morphology_image, morphology_rgba,
BorderMode, ImageSize, KernelShape, MorphExOp, MorphologyThreadingPolicy,
};
use image::{DynamicImage, EncodableLayout, GenericImageView, ImageReader};
use opencv::core::{
Expand Down Expand Up @@ -67,7 +67,7 @@ fn gaussian_kernel(size: usize, sigma: f32) -> Vec<Vec<f32>> {
}

fn main() {
let radius_size = 67;
let radius_size = 5;
let mut structuring_element = circle_se(radius_size);

opencv::core::set_use_opencl(false).expect("Failed to disable OpenCL");
Expand Down Expand Up @@ -99,7 +99,7 @@ fn main() {
let mut channel_2_dst = vec![0u8; dimensions.0 as usize * dimensions.1 as usize];
let mut channel_3_dst = vec![0u8; dimensions.0 as usize * dimensions.1 as usize];

for ((((a, dst_1), dst_2), dst_3)) in bytes
for (((a, dst_1), dst_2), dst_3) in bytes
.chunks_exact(3)
.zip(&mut channel_1_src)
.zip(&mut channel_2_src)
Expand Down Expand Up @@ -168,9 +168,10 @@ fn main() {
let mut dst = vec![0u8; rgba_image.len()];

let exec_time = Instant::now();
erode_rgba(
morphology_rgba(
&rgba_image,
&mut dst,
MorphExOp::TopHat,
image_size,
&structuring_element,
KernelShape::new(se_size, se_size),
Expand Down Expand Up @@ -229,7 +230,7 @@ fn main() {

let new_image = morphology_image(
img,
MorphExOp::TopHat,
MorphExOp::Gradient,
&structuring_element,
KernelShape::new(se_size, se_size),
BorderMode::default(),
Expand Down
17 changes: 16 additions & 1 deletion src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
use crate::neon::morph_gradient_neon;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use crate::sse::morph_gradient_sse;
use num_traits::SaturatingSub;
use std::ops::Sub;

Expand Down Expand Up @@ -53,7 +57,18 @@ where

impl MorphGradient<u8> for u8 {
fn morph_gradient(dilation: &[u8], erosion: &[u8], dst: &mut [u8]) {
make_morph_gradient_sat(dilation, erosion, dst)
let mut _dispatcher: fn(&[u8], &[u8], &mut [u8]) = make_morph_gradient_sat;
#[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
{
_dispatcher = morph_gradient_neon;
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
if std::arch::is_x86_feature_detected!("sse4.1") {
_dispatcher = morph_gradient_sse;
}
}
_dispatcher(dilation, erosion, dst)
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern crate core;
mod arena;
mod arena_roi;
mod border_mode;
mod difference;
#[cfg(feature = "image")]
mod dynamic_image;
mod filter;
Expand All @@ -42,6 +43,7 @@ mod morph_base;
mod morph_gray_alpha;
mod morph_rgb;
mod morph_rgba;
mod neon;
mod op;
mod op_f32;
mod op_impl;
Expand All @@ -50,10 +52,11 @@ mod op_u16;
mod ops;
mod packing;
mod se_scan;
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
mod sse;
mod structuring_element;
mod thread_policy;
mod unsafe_slice;
mod difference;

pub use border_mode::BorderMode;
#[cfg(feature = "image")]
Expand Down
89 changes: 89 additions & 0 deletions src/neon/gradient_unsigned_8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (c) Radzivon Bartoshyk. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
use std::arch::aarch64::*;

Check failure on line 29 in src/neon/gradient_unsigned_8.rs

View workflow job for this annotation

GitHub Actions / Build

unresolved import `std::arch::aarch64`

pub fn morph_gradient_neon(dilation: &[u8], erosion: &[u8], dst: &mut [u8]) {
if dilation.len() != erosion.len() || erosion.len() != dst.len() {
panic!(
"All array must match in size for gradient but received v0: {}, v1: {}, v2: {}",
dilation.len(),
erosion.len(),
dst.len()
);
}
let length = dilation.len();
let mut _cx = 0usize;

unsafe {
while _cx + 64 < length {
let v0_set = vld1q_u8_x4(dilation.get_unchecked(_cx..).as_ptr());
let v1_set = vld1q_u8_x4(erosion.get_unchecked(_cx..).as_ptr());
let result_set = uint8x16x4_t(
vqsubq_u8(v0_set.0, v1_set.0),
vqsubq_u8(v0_set.1, v1_set.1),
vqsubq_u8(v0_set.2, v1_set.2),
vqsubq_u8(v0_set.3, v1_set.3),
);
vst1q_u8_x4(dst.get_unchecked_mut(_cx..).as_mut_ptr(), result_set);
_cx += 64;
}

while _cx + 32 < length {
let v0_set = vld1q_u8_x2(dilation.get_unchecked(_cx..).as_ptr());
let v1_set = vld1q_u8_x2(erosion.get_unchecked(_cx..).as_ptr());
let result_set =
uint8x16x2_t(vqsubq_u8(v0_set.0, v1_set.0), vqsubq_u8(v0_set.1, v1_set.1));
vst1q_u8_x2(dst.get_unchecked_mut(_cx..).as_mut_ptr(), result_set);
_cx += 32;
}

while _cx + 16 < length {
let v0_set = vld1q_u8(dilation.get_unchecked(_cx..).as_ptr());
let v1_set = vld1q_u8(erosion.get_unchecked(_cx..).as_ptr());
let result_set = vqsubq_u8(v0_set, v1_set);
vst1q_u8(dst.get_unchecked_mut(_cx..).as_mut_ptr(), result_set);
_cx += 16;
}

while _cx + 8 < length {
let v0_set = vld1_u8(dilation.get_unchecked(_cx..).as_ptr());
let v1_set = vld1_u8(erosion.get_unchecked(_cx..).as_ptr());
let result_set = vqsub_u8(v0_set, v1_set);
vst1_u8(dst.get_unchecked_mut(_cx..).as_mut_ptr(), result_set);
_cx += 8;
}

while _cx < length {
*dst.get_unchecked_mut(_cx) = dilation
.get_unchecked(_cx)
.saturating_sub(*erosion.get_unchecked(_cx));
_cx += 1;
}
}
}
31 changes: 31 additions & 0 deletions src/neon/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) Radzivon Bartoshyk. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
mod gradient_unsigned_8;

pub use gradient_unsigned_8::*;

Check warning on line 31 in src/neon/mod.rs

View workflow job for this annotation

GitHub Actions / Build

unused import: `gradient_unsigned_8::*`
2 changes: 1 addition & 1 deletion src/op_u16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
use crate::border_mode::BorderMode;
use crate::difference::MorphGradient;
use crate::morph_gray_alpha::make_morphology_gray_alpha;
use crate::morph_rgb::make_morphology_rgb;
use crate::morph_rgba::make_morphology_rgba;
use crate::op_impl::make_morphology;
use crate::op_type::MorphOp;
use crate::structuring_element::KernelShape;
use crate::{ImageSize, MorphExOp, MorphologyThreadingPolicy};
use crate::difference::MorphGradient;

/// Dilate a gray (planar) stored in u16 image
///
Expand Down
89 changes: 89 additions & 0 deletions src/sse/gradient_unsigned_8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* * Copyright (c) Radzivon Bartoshyk. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#[cfg(target_arch = "x86")]
use std::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
pub fn morph_gradient_sse(dilation: &[u8], erosion: &[u8], dst: &mut [u8]) {
unsafe {
morph_gradient_sse_impl(dilation, erosion, dst);
}
}
#[inline]
#[target_feature(enable = "sse4.1")]
unsafe fn morph_gradient_sse_impl(dilation: &[u8], erosion: &[u8], dst: &mut [u8]) {
if dilation.len() != erosion.len() || erosion.len() != dst.len() {
panic!(
"All array must match in size for gradient but received v0: {}, v1: {}, v2: {}",
dilation.len(),
erosion.len(),
dst.len()
);
}
let length = dilation.len();
let mut _cx = 0usize;
unsafe {
while _cx + 64 < length {
let v0_ptr = dilation.get_unchecked(_cx..).as_ptr();
let v0_set = (
_mm_loadu_si128(v0_ptr as *const __m128i),
_mm_loadu_si128(v0_ptr.add(16) as *const __m128i),
_mm_loadu_si128(v0_ptr.add(32) as *const __m128i),
_mm_loadu_si128(v0_ptr.add(48) as *const __m128i),
);
let v1_ptr = erosion.get_unchecked(_cx..).as_ptr();
let v1_set = (
_mm_loadu_si128(v1_ptr as *const __m128i),
_mm_loadu_si128(v1_ptr.add(16) as *const __m128i),
_mm_loadu_si128(v1_ptr.add(32) as *const __m128i),
_mm_loadu_si128(v1_ptr.add(48) as *const __m128i),
);
let result_set = (
_mm_subs_epu8(v0_set.0, v1_set.0),
_mm_subs_epu8(v0_set.1, v1_set.1),
_mm_subs_epu8(v0_set.2, v1_set.2),
_mm_subs_epu8(v0_set.3, v1_set.3),
);
let v_dst_ptr = dst.get_unchecked_mut(_cx..).as_mut_ptr();
_mm_storeu_si128(v_dst_ptr as *mut __m128i, result_set.0);
_mm_storeu_si128(v_dst_ptr.add(16) as *mut __m128i, result_set.1);
_mm_storeu_si128(v_dst_ptr.add(32) as *mut __m128i, result_set.2);
_mm_storeu_si128(v_dst_ptr.add(48) as *mut __m128i, result_set.3);
_cx += 64;
}
while _cx + 32 < length {
let v0_ptr = dilation.get_unchecked(_cx..).as_ptr();
let v0_set = (
_mm_loadu_si128(v0_ptr as *const __m128i),
_mm_loadu_si128(v0_ptr.add(16) as *const __m128i),
);
let v1_ptr = erosion.get_unchecked(_cx..).as_ptr();
let v1_set = (
_mm_loadu_si128(v1_ptr as *const __m128i),
_mm_loadu_si128(v1_ptr.add(16) as *const __m128i),
);
let result_set = (
_mm_subs_epu8(v0_set.0, v1_set.0),
_mm_subs_epu8(v0_set.1, v1_set.1),
);
let v_dst_ptr = dst.get_unchecked_mut(_cx..).as_mut_ptr();
_mm_storeu_si128(v_dst_ptr as *mut __m128i, result_set.0);
_mm_storeu_si128(v_dst_ptr.add(16) as *mut __m128i, result_set.1);
_cx += 32;
}
while _cx + 16 < length {
let v0_ptr = dilation.get_unchecked(_cx..).as_ptr();
let v0_set = _mm_loadu_si128(v0_ptr as *const __m128i);
let v1_ptr = erosion.get_unchecked(_cx..).as_ptr();
let v1_set = _mm_loadu_si128(v1_ptr as *const __m128i);
let result_set = _mm_subs_epu8(v0_set, v1_set);
let v_dst_ptr = dst.get_unchecked_mut(_cx..).as_mut_ptr();
_mm_storeu_si128(v_dst_ptr as *mut __m128i, result_set);
_cx += 16;
}
while _cx < length {
*dst.get_unchecked_mut(_cx) = dilation
.get_unchecked(_cx)
.saturating_sub(*erosion.get_unchecked(_cx));
_cx += 1;
}
}
}
Expand Down
Loading

0 comments on commit d3b55b2

Please sign in to comment.