use js_sys::{Array, Object, Reflect, Uint8Array};
use wasm_bindgen::{JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use super::ImageSizeCap;
use crate::images::ImageData;
pub(super) async fn decode(bytes: &[u8], cap: Option<ImageSizeCap>) -> Option<ImageData> {
let window = web_sys::window()?;
let blob = web_sys::Blob::new_with_u8_array_sequence(&Array::of1(&Uint8Array::from(bytes))).ok()?;
let options = web_sys::ImageBitmapOptions::new();
options.set_premultiply_alpha(web_sys::PremultiplyAlpha::None);
options.set_color_space_conversion(web_sys::ColorSpaceConversion::None);
let bitmap: web_sys::ImageBitmap = JsFuture::from(
window
.create_image_bitmap_with_blob_and_image_bitmap_options(&blob, &options)
.ok()?,
)
.await
.ok()?
.dyn_into()
.ok()?;
let (width, height) = fit(bitmap.width(), bitmap.height(), cap);
let bitmap = if (width, height) == (bitmap.width(), bitmap.height()) {
bitmap
} else {
options.set_resize_width(width);
options.set_resize_height(height);
options.set_resize_quality(web_sys::ResizeQuality::Medium);
let smaller = JsFuture::from(
window
.create_image_bitmap_with_image_bitmap_and_image_bitmap_options(&bitmap, &options)
.ok()?,
)
.await
.ok()?
.dyn_into::<web_sys::ImageBitmap>()
.ok()?;
bitmap.close();
smaller
};
let pixels = read(&bitmap, width, height);
bitmap.close();
pixels
}
fn read(bitmap: &web_sys::ImageBitmap, width: u32, height: u32) -> Option<ImageData> {
if width == 0 || height == 0 {
return None;
}
let canvas = web_sys::OffscreenCanvas::new(width, height).ok()?;
let settings = Object::new();
Reflect::set(&settings, &JsValue::from_str("willReadFrequently"), &JsValue::TRUE).ok()?;
let context: web_sys::OffscreenCanvasRenderingContext2d = canvas
.get_context_with_context_options("2d", &settings)
.ok()??
.dyn_into()
.ok()?;
context.draw_image_with_image_bitmap(bitmap, 0.0, 0.0).ok()?;
let data = context
.get_image_data(0.0, 0.0, f64::from(width), f64::from(height))
.ok()?;
Some(ImageData::from_rgba(data.data().0, width, height))
}
fn fit(width: u32, height: u32, cap: Option<ImageSizeCap>) -> (u32, u32) {
match cap {
Some(cap) if width > cap.width || height > cap.height => {
let scale =
(f64::from(cap.width.max(1)) / f64::from(width)).min(f64::from(cap.height.max(1)) / f64::from(height));
(
((f64::from(width) * scale).round() as u32).max(1),
((f64::from(height) * scale).round() as u32).max(1),
)
}
_ => (width, height),
}
}