Make images smaller using best-in-class codecs, right in the browser.
JavaScript
Simplify rotation code to 90deg only
Surma0d1e5ef1192a
diff --git a/codecs/rotate/rotate.rs b/codecs/rotate/rotate.rsindex 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<T>(o: Option<T>) -> T {- use std::process;+fn unwrap_abort<T>(o: Option<T>) -> 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<T>(o: Option<T>) -> T {+fn unwrap_abort<T>(o: Option<T>) -> 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::<u32>(4 as *mut u32, num_pixels);- out_b = from_raw_parts_mut::<u32>((input_width * input_height * 4 + 4) as *mut u32, num_pixels);- }+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::<u32>(4 as *mut u32, num_pixels);+ out_b = from_raw_parts_mut::<u32>((num_pixels * 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;+ 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.wasmindex a3ba4265..4ff66649 100755Binary files a/codecs/rotate/rotate.wasm and b/codecs/rotate/rotate.wasm differ