Skip to content

Adds adjustable punch #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<Vec<u8>, 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<f64>) -> Result<Vec<u8>, 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<Vec<u8>, Error> {
pub fn decode(blur_hash: &str, width: usize, height: usize, punch: Option<f64>) -> Result<Vec<u8>, Error> {
if blur_hash.len() < 6 {
return Err(Error::LengthInvalid);
}
Expand All @@ -65,6 +64,7 @@ pub fn decode(blur_hash: &str, width: usize, height: usize) -> Result<Vec<u8>, 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);

Expand All @@ -76,7 +76,7 @@ pub fn decode(blur_hash: &str, width: usize, height: usize) -> Result<Vec<u8>, 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));
}
}

Expand Down Expand Up @@ -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)
},
);

Expand Down
6 changes: 3 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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) => {
Expand Down