From 248bba7bb9ab1492640bd24b5c01f66afe73c0a5 Mon Sep 17 00:00:00 2001 From: bendn Date: Tue, 3 Oct 2023 13:55:26 +0700 Subject: [PATCH] do 270 degree rot out of place --- benches/affine_transformations.rs | 4 ++-- src/affine.rs | 38 ++++++++++++++++++++----------- 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 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, 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 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, 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>(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>(mut img: Image } } +/// Transpose a square image out of place +/// # Safety +/// +/// UB if provided image rectangular +unsafe fn transpose_out( + i: &ImageCloner<'_, CHANNELS>, +) -> Image, 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 ///