diff --git a/src/lib.rs b/src/lib.rs index 22a8eed..4bb702d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,6 @@ use wasm_bindgen::prelude::*; #[global_allocator] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; -// TODO: Add adjustable 'punch' // TODO: Avoid panicing infrasturcture (checked division, .get, no unwrap) #[derive(Clone, Copy, Debug, PartialEq, Eq, Error)] @@ -35,11 +34,11 @@ pub enum EncodingError { /// Decode for WASM target. If an error occurs, the function will throw a `JsError`. #[wasm_bindgen(js_name = "decode")] -pub fn wasm_decode(blur_hash: &str, width: usize, height: usize) -> Result, JsValue> { - decode(blur_hash, width, height).map_err(|err| js_sys::Error::new(&err.to_string()).into()) +pub fn wasm_decode(blur_hash: &str, width: usize, height: usize, punch: Option) -> Result, JsValue> { + decode(blur_hash, width, height, punch).map_err(|err| js_sys::Error::new(&err.to_string()).into()) } -pub fn decode(blur_hash: &str, width: usize, height: usize) -> Result, Error> { +pub fn decode(blur_hash: &str, width: usize, height: usize, punch: Option) -> Result, Error> { if blur_hash.len() < 6 { return Err(Error::LengthInvalid); } @@ -65,6 +64,7 @@ pub fn decode(blur_hash: &str, width: usize, height: usize) -> Result, E // It represents a floating-point value of (max + 1) / 166. let quantised_maximum_value = decode_base83_string(blur_hash.get(1..2).unwrap()); let maximum_value = ((quantised_maximum_value + 1) as f64) / 166f64; + let punch_value = punch.unwrap_or(1.0); let mut colours: Vec<[f64; 3]> = Vec::with_capacity(num_x * num_y); @@ -76,7 +76,7 @@ pub fn decode(blur_hash: &str, width: usize, height: usize) -> Result, E } else { // 4. AC components, 2 digits each, nx * ny - 1 components in total. let value = decode_base83_string(blur_hash.get((4 + i * 2)..(4 + i * 2 + 2)).unwrap()); - colours.push(decode_ac(value, maximum_value * 1.0)); + colours.push(decode_ac(value, maximum_value * punch_value)); } } @@ -203,9 +203,9 @@ pub fn encode( bytes_per_pixel, 0, |a, b| { - (normalisation + normalisation * f64::cos((PI * x as f64 * a) / width as f64) - * f64::cos((PI * y as f64 * b) / height as f64)) + * f64::cos((PI * y as f64 * b) / height as f64) }, ); diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 866307b..003f9de 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -6,14 +6,14 @@ use std::convert::TryFrom; fn err_if_hash_length_less_than_6() { assert_eq!( Err(blurhash_wasm::Error::LengthInvalid), - decode("L", 40, 30) + decode("L", 40, 30, None) ); } #[test] fn decodes_ok() { // From the online demo - let res = decode("LUDT3yayV?ay%jWBa#a}9Xj[j@fP", 40, 30); + let res = decode("LUDT3yayV?ay%jWBa#a}9Xj[j@fP", 40, 30, None); // From a known encode/decode let expected = image::open("tests/data/decode-test-expected.png") @@ -122,7 +122,7 @@ fn round_trips_ok() { .unwrap() .to_rgba(); - let decode_res = decode(&actual_encode, 32, 32); + let decode_res = decode(&actual_encode, 32, 32, None); match decode_res { Ok(actual_decode) => {