From 0d1e5ef1192acd75d476ecfebd92193e6b6d2880 Mon Sep 17 00:00:00 2001 From: Surma Date: Fri, 15 Feb 2019 14:20:06 +0000 Subject: [PATCH] Simplify rotation code to 90deg only --- codecs/rotate/rotate.rs | 93 +++++++++----------------------------- codecs/rotate/rotate.wasm | Bin 533 -> 353 bytes 2 files changed, 21 insertions(+), 72 deletions(-) diff --git a/codecs/rotate/rotate.rs b/codecs/rotate/rotate.rs index 4a766c1e..ccac2ef2 100644 --- a/codecs/rotate/rotate.rs +++ b/codecs/rotate/rotate.rs @@ -4,87 +4,36 @@ use std::slice::from_raw_parts_mut; // https://rustwasm.github.io/book/reference/code-size.html #[cfg(not(debug_assertions))] #[inline] -pub fn unwrap_abort(o: Option) -> T { - use std::process; +fn unwrap_abort(o: Option) -> T { match o { Some(t) => t, - None => process::abort(), + None => std::process::abort(), } } -// Normal panic-y behavior for debug builds #[cfg(debug_assertions)] -unsafe fn unchecked_unwrap(o: Option) -> T { +fn unwrap_abort(o: Option) -> T { o.unwrap() } #[no_mangle] -fn rotate(input_width: isize, input_height: isize, rotate: isize) { - let mut i = 0isize; - - // In the straight-copy case, d1 is x, d2 is y. - // x starts at 0 and increases. - // y starts at 0 and increases. - let mut d1_start: isize = 0; - let mut d1_limit: isize = input_width; - let mut d1_advance: isize = 1; - let mut d1_multiplier: isize = 1; - let mut d2_start: isize = 0; - let mut d2_limit: isize = input_height; - let mut d2_advance: isize = 1; - let mut d2_multiplier: isize = input_width; - - if rotate == 90 { - // d1 is y, d2 is x. - // y starts at its max value and decreases. - // x starts at 0 and increases. - d1_start = input_height - 1; - d1_limit = input_height; - d1_advance = -1; - d1_multiplier = input_width; - d2_start = 0; - d2_limit = input_width; - d2_advance = 1; - d2_multiplier = 1; - } else if rotate == 180 { - // d1 is x, d2 is y. - // x starts at its max and decreases. - // y starts at its max and decreases. - d1_start = input_width - 1; - d1_limit = input_width; - d1_advance = -1; - d1_multiplier = 1; - d2_start = input_height - 1; - d2_limit = input_height; - d2_advance = -1; - d2_multiplier = input_width; - } else if rotate == 270 { - // d1 is y, d2 is x. - // y starts at 0 and increases. - // x starts at its max and decreases. - d1_start = 0; - d1_limit = input_height; - d1_advance = 1; - d1_multiplier = input_width; - d2_start = input_width - 1; - d2_limit = input_width; - d2_advance = -1; - d2_multiplier = 1; - } - - let num_pixels = (input_width * input_height) as usize; - let in_b: &mut [u32]; - let out_b: &mut [u32]; - unsafe { - in_b = from_raw_parts_mut::(4 as *mut u32, num_pixels); - out_b = from_raw_parts_mut::((input_width * input_height * 4 + 4) as *mut u32, num_pixels); - } - - for d2 in 0..d2_limit { - for d1 in 0..d1_limit { - let in_idx = (d1_start + d1 * d1_advance) * d1_multiplier + (d2_start + d2 * d2_advance) * d2_multiplier; - *unwrap_abort(out_b.get_mut(i as usize)) = *unwrap_abort(in_b.get(in_idx as usize)); - i += 1; +fn rotate(width: usize, height: usize, _rotate: usize) { + let num_pixels = width * height; + let in_b: &mut [u32]; + let out_b: &mut [u32]; + unsafe { + in_b = from_raw_parts_mut::(4 as *mut u32, num_pixels); + out_b = from_raw_parts_mut::((num_pixels * 4 + 4) as *mut u32, num_pixels); + } + + let new_width = height; + let _new_height = width; + for y in 0..height { + for x in 0..width { + let new_x = (new_width - 1) - y; + let new_y = x; + *unwrap_abort(out_b.get_mut(new_y * new_width + new_x)) = + *unwrap_abort(in_b.get(y * width + x)); + } } - } } diff --git a/codecs/rotate/rotate.wasm b/codecs/rotate/rotate.wasm index a3ba42655643fae2907b391a42b85864db526183..4ff666499b95b845874e0c3ea1816d7764f95e62 100755 GIT binary patch delta 247 zcmW+wIc~#15Su-$YqYC<0!Ni!;96U_aFLG;QUrm^021;7H-2E!=MR~6GaM2m!DsyO zkI((j1yB6zT;BcWH@Ld%8m@^z)!!wWU~n?Ft8{Rn_q8{vMny1G87j$Gqf7|G_Rpd9 zbXH*`VUi4%1%s5Zp~r}%Zz&>Q?erg58Yztw2yJS1WO literal 533 zcmY*Vy-ve05We#{NgKzsE895}6C-myP*`||G^mk~mC~y1!jy;yU}WbVcmSS*r{S(e zLddf3=lkySXG43~AOK)#Rsbs+h5^VDAlM9dh?o(=Jinv?<2ZhS8{mH|u5G^QHrxG+ z0oOE7UAt=@+e1fKHqE*{woUiECX?^C$M)FCJs(8Ed?)lngg|ETx7frtv-A)#Sjj(e z>5as1xbqZ;kTJ#~1rpfE&KW`>5)+BGLMIGqDPy7xMHP`ebBtkH(V21*k)e~8FpPkO z%8Q1UCuwbo^m!P<)Tv7@BHnvoWa#TMnq#ynI5s@#*)sL1u6~a4FbDaD`Ryss}b zpNS~R&PnB_XF10tSPCEjawTu~!O((BwhvxD;#Q85i7{yjS^}FZ12+cro}|CUlcaA} rM`AEys*$H+R>Wm6kmHIkQmEMTECtWGQgSYo;%09CI*ucNvmnP0S>;dc