Reorganize rotate.rs, separate rotations into their own functions

This commit is contained in:
Joachim Viide
2019-02-21 01:25:50 +02:00
committed by Surma
parent 5d32126565
commit 14baa6ebf8

View File

@@ -1,4 +1,4 @@
use std::slice::from_raw_parts_mut; use std::slice::{from_raw_parts, from_raw_parts_mut};
// This function is taken from Zachary Dremann // This function is taken from Zachary Dremann
// https://github.com/GoogleChromeLabs/squoosh/pull/462 // https://github.com/GoogleChromeLabs/squoosh/pull/462
@@ -24,23 +24,28 @@ impl<T> HardUnwrap<T> for Option<T> {
const TILE_SIZE: usize = 16; const TILE_SIZE: usize = 16;
#[no_mangle] fn get_buffers<'a>(width: usize, height: usize) -> (&'a [u32], &'a mut [u32]) {
fn rotate(width: usize, height: usize, rotate: usize) {
let num_pixels = width * height; let num_pixels = width * height;
let in_b: &mut [u32]; let in_b: &[u32];
let out_b: &mut [u32]; let out_b: &mut [u32];
unsafe { unsafe {
in_b = from_raw_parts_mut::<u32>(8 as *mut u32, num_pixels); in_b = from_raw_parts::<u32>(8 as *const u32, num_pixels);
out_b = from_raw_parts_mut::<u32>((num_pixels * 4 + 8) as *mut u32, num_pixels); out_b = from_raw_parts_mut::<u32>((num_pixels * 4 + 8) as *mut u32, num_pixels);
} }
return (in_b, out_b);
}
match rotate { #[inline(never)]
0 => { fn rotate_0(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
for (in_p, out_p) in in_b.iter().zip(out_b.iter_mut()) { for (in_p, out_p) in in_b.iter().zip(out_b.iter_mut()) {
*out_p = *in_p; *out_p = *in_p;
} }
} }
90 => {
#[inline(never)]
fn rotate_90(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
let new_width = height; let new_width = height;
let _new_height = width; let _new_height = width;
for y_start in (0..height).step_by(TILE_SIZE) { for y_start in (0..height).step_by(TILE_SIZE) {
@@ -59,12 +64,18 @@ fn rotate(width: usize, height: usize, rotate: usize) {
} }
} }
} }
180 => {
#[inline(never)]
fn rotate_180(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
for (in_p, out_p) in in_b.iter().zip(out_b.iter_mut().rev()) { for (in_p, out_p) in in_b.iter().zip(out_b.iter_mut().rev()) {
*out_p = *in_p; *out_p = *in_p;
} }
} }
270 => {
#[inline(never)]
fn rotate_270(width: usize, height: usize) {
let (in_b, out_b) = get_buffers(width, height);
let new_width = height; let new_width = height;
let new_height = width; let new_height = width;
for y_start in (0..height).step_by(TILE_SIZE) { for y_start in (0..height).step_by(TILE_SIZE) {
@@ -82,6 +93,14 @@ fn rotate(width: usize, height: usize, rotate: usize) {
} }
} }
} }
#[no_mangle]
fn rotate(width: usize, height: usize, rotate: usize) {
match rotate {
0 => rotate_0(width, height),
90 => rotate_90(width, height),
180 => rotate_180(width, height),
270 => rotate_270(width, height),
_ => std::process::abort(), _ => std::process::abort(),
} }
} }