Fix PNG decoder returning Rust-owned ImageData

This commit is contained in:
Ingvar Stepanyan
2020-12-08 20:19:48 +00:00
parent 4e4778397f
commit 5758e5db9a
6 changed files with 103 additions and 242 deletions

View File

@@ -1,7 +1,22 @@
use std::io::Cursor;
use wasm_bindgen::prelude::*;
use web_sys::ImageData;
use wasm_bindgen::Clamped;
// Custom ImageData bindings to allow construction with
// a JS-owned copy of the data.
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = ImageData)]
pub type ImageData;
#[wasm_bindgen(constructor)]
fn new_with_owned_u8_clamped_array_and_sh(
data: Clamped<Vec<u8>>,
sw: u32,
sh: u32,
) -> ImageData;
}
#[wasm_bindgen(catch)]
pub fn encode(data: &[u8], width: u32, height: u32) -> Vec<u8> {
@@ -38,10 +53,9 @@ pub fn decode(data: &[u8]) -> ImageData {
}
}
ImageData::new_with_u8_clamped_array_and_sh(
wasm_bindgen::Clamped(&mut buf),
ImageData::new_with_owned_u8_clamped_array_and_sh(
wasm_bindgen::Clamped(buf),
info.width,
info.height,
)
.unwrap()
}