do 270 degree rot out of place

This commit is contained in:
bendn 2023-10-03 13:55:26 +07:00
parent f863d295d6
commit 248bba7bb9
No known key found for this signature in database
GPG key ID: 0D9D3A2A3B2A93D6
2 changed files with 27 additions and 15 deletions

View file

@ -28,8 +28,8 @@ bench!(fn flip_h() { run flip_h() } flip_hc);
bench!(fn flip_v() { run flip_v() } flip_vc); bench!(fn flip_v() { run flip_v() } flip_vc);
bench!(fn rotate_90() { run rot_90() } rot_90c); bench!(fn rotate_90() { run rot_90() } rot_90c);
bench!(fn rotate_180() { run rot_180() } rot_180c); 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!( iai::main!(
flip_h, flip_v, rotate_90, rotate_180, rotate_270, flip_hc, flip_vc, rot_90c, rot_180c, flip_h, flip_v, rotate_90, rotate_180, rotate_270, flip_hc, flip_vc, rot_90c, rot_180c,
rot_280c rot_270c
); );

View file

@ -107,16 +107,8 @@ impl<const CHANNELS: usize> ImageCloner<'_, CHANNELS> {
/// UB if the image is not square /// UB if the image is not square
#[must_use = "function does not modify the original image"] #[must_use = "function does not modify the original image"]
pub unsafe fn rot_90(&self) -> Image<Vec<u8>, CHANNELS> { pub unsafe fn rot_90(&self) -> Image<Vec<u8>, CHANNELS> {
let mut out = self.alloc();
// SAFETY: yep // SAFETY: yep
unsafe { let mut out = unsafe { transpose_out(self) };
mattr::transpose(
self.flatten(),
out.flatten_mut(),
self.height() as usize,
self.width() as usize,
)
};
// SAFETY: sqar // SAFETY: sqar
unsafe { crev(out.as_mut()) }; unsafe { crev(out.as_mut()) };
out out
@ -128,9 +120,9 @@ impl<const CHANNELS: usize> ImageCloner<'_, CHANNELS> {
/// UB if the image is not square /// UB if the image is not square
#[must_use = "function does not modify the original image"] #[must_use = "function does not modify the original image"]
pub unsafe fn rot_270(&self) -> Image<Vec<u8>, CHANNELS> { pub unsafe fn rot_270(&self) -> Image<Vec<u8>, CHANNELS> {
let mut out = self.flip_h(); // SAFETY: yep
// SAFETY: sqar let mut out = unsafe { transpose_out(self) };
unsafe { transpose(&mut out.as_mut()) }; out.flip_v();
out out
} }
} }
@ -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 /// Transpose a square image
/// # Safety /// # Safety
/// ///