fast image operations
do 270 degree rot out of place
bendn 2023-10-03
parent f863d29 · commit 248bba7
-rw-r--r--benches/affine_transformations.rs4
-rw-r--r--src/affine.rs38
2 files changed, 27 insertions, 15 deletions
diff --git a/benches/affine_transformations.rs b/benches/affine_transformations.rs
index 0899900..912eff0 100644
--- a/benches/affine_transformations.rs
+++ b/benches/affine_transformations.rs
@@ -28,8 +28,8 @@ bench!(fn flip_h() { run flip_h() } flip_hc);
bench!(fn flip_v() { run flip_v() } flip_vc);
bench!(fn rotate_90() { run rot_90() } rot_90c);
bench!(fn rotate_180() { run rot_180() } rot_180c);
-bench!(fn rotate_270() { run rot_270() } rot_280c);
+bench!(fn rotate_270() { run rot_270() } rot_270c);
iai::main!(
flip_h, flip_v, rotate_90, rotate_180, rotate_270, flip_hc, flip_vc, rot_90c, rot_180c,
- rot_280c
+ rot_270c
);
diff --git a/src/affine.rs b/src/affine.rs
index b03dbba..2568693 100644
--- a/src/affine.rs
+++ b/src/affine.rs
@@ -107,16 +107,8 @@ impl<const CHANNELS: usize> ImageCloner<'_, CHANNELS> {
/// UB if the image is not square
#[must_use = "function does not modify the original image"]
pub unsafe fn rot_90(&self) -> Image<Vec<u8>, CHANNELS> {
- let mut out = self.alloc();
// SAFETY: yep
- unsafe {
- mattr::transpose(
- self.flatten(),
- out.flatten_mut(),
- self.height() as usize,
- self.width() as usize,
- )
- };
+ let mut out = unsafe { transpose_out(self) };
// SAFETY: sqar
unsafe { crev(out.as_mut()) };
out
@@ -128,9 +120,9 @@ impl<const CHANNELS: usize> ImageCloner<'_, CHANNELS> {
/// UB if the image is not square
#[must_use = "function does not modify the original image"]
pub unsafe fn rot_270(&self) -> Image<Vec<u8>, CHANNELS> {
- let mut out = self.flip_h();
- // SAFETY: sqar
- unsafe { transpose(&mut out.as_mut()) };
+ // SAFETY: yep
+ let mut out = unsafe { transpose_out(self) };
+ out.flip_v();
out
}
}
@@ -178,7 +170,7 @@ unsafe fn crev<const CHANNELS: usize, T: DerefMut<Target = [u8]>>(mut img: Image
let mut start = 0;
let mut end = size - 1;
while start < end {
- // SAFETY: hmm
+ // SAFETY: hmm
unsafe { b.swap_unchecked(i * size + start, i * size + end) };
start += 1;
end -= 1;
@@ -186,6 +178,26 @@ unsafe fn crev<const CHANNELS: usize, T: DerefMut<Target = [u8]>>(mut img: Image
}
}
+/// Transpose a square image out of place
+/// # Safety
+///
+/// UB if provided image rectangular
+unsafe fn transpose_out<const CHANNELS: usize>(
+ i: &ImageCloner<'_, CHANNELS>,
+) -> Image<Vec<u8>, CHANNELS> {
+ let mut out = i.alloc();
+ // SAFETY: yep
+ unsafe {
+ mattr::transpose(
+ i.flatten(),
+ out.flatten_mut(),
+ i.height() as usize,
+ i.width() as usize,
+ )
+ };
+ out
+}
+
/// Transpose a square image
/// # Safety
///