diff --git a/codecs/resize/pkg/squoosh_resize.d.ts b/codecs/resize/pkg/squoosh_resize.d.ts index d0f4ca97..56114c7f 100644 --- a/codecs/resize/pkg/squoosh_resize.d.ts +++ b/codecs/resize/pkg/squoosh_resize.d.ts @@ -9,9 +9,9 @@ * @param {number} typ_idx * @param {boolean} premultiply * @param {boolean} color_space_conversion -* @returns {Uint8Array} +* @returns {Uint8ClampedArray} */ -export function resize(input_image: Uint8Array, input_width: number, input_height: number, output_width: number, output_height: number, typ_idx: number, premultiply: boolean, color_space_conversion: boolean): Uint8Array; +export function resize(input_image: Uint8Array, input_width: number, input_height: number, output_width: number, output_height: number, typ_idx: number, premultiply: boolean, color_space_conversion: boolean): Uint8ClampedArray; export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; diff --git a/codecs/resize/pkg/squoosh_resize.js b/codecs/resize/pkg/squoosh_resize.js index ec3bfdeb..2e6a0dd4 100644 --- a/codecs/resize/pkg/squoosh_resize.js +++ b/codecs/resize/pkg/squoosh_resize.js @@ -26,8 +26,16 @@ function getInt32Memory0() { return cachegetInt32Memory0; } -function getArrayU8FromWasm0(ptr, len) { - return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len); +let cachegetUint8ClampedMemory0 = null; +function getUint8ClampedMemory0() { + if (cachegetUint8ClampedMemory0 === null || cachegetUint8ClampedMemory0.buffer !== wasm.memory.buffer) { + cachegetUint8ClampedMemory0 = new Uint8ClampedArray(wasm.memory.buffer); + } + return cachegetUint8ClampedMemory0; +} + +function getClampedArrayU8FromWasm0(ptr, len) { + return getUint8ClampedMemory0().subarray(ptr / 1, ptr / 1 + len); } /** * @param {Uint8Array} input_image @@ -38,7 +46,7 @@ function getArrayU8FromWasm0(ptr, len) { * @param {number} typ_idx * @param {boolean} premultiply * @param {boolean} color_space_conversion -* @returns {Uint8Array} +* @returns {Uint8ClampedArray} */ export function resize(input_image, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion) { try { @@ -48,7 +56,7 @@ export function resize(input_image, input_width, input_height, output_width, out wasm.resize(retptr, ptr0, len0, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion); var r0 = getInt32Memory0()[retptr / 4 + 0]; var r1 = getInt32Memory0()[retptr / 4 + 1]; - var v1 = getArrayU8FromWasm0(r0, r1).slice(); + var v1 = getClampedArrayU8FromWasm0(r0, r1).slice(); wasm.__wbindgen_free(r0, r1 * 1); return v1; } finally { diff --git a/codecs/resize/pkg/squoosh_resize_bg.wasm b/codecs/resize/pkg/squoosh_resize_bg.wasm index f9ceede2..b910c97b 100644 Binary files a/codecs/resize/pkg/squoosh_resize_bg.wasm and b/codecs/resize/pkg/squoosh_resize_bg.wasm differ diff --git a/codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts b/codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts new file mode 100644 index 00000000..05cc6841 --- /dev/null +++ b/codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts @@ -0,0 +1,7 @@ +/* tslint:disable */ +/* eslint-disable */ +export const memory: WebAssembly.Memory; +export function resize(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number): void; +export function __wbindgen_add_to_stack_pointer(a: number): number; +export function __wbindgen_malloc(a: number): number; +export function __wbindgen_free(a: number, b: number): void; diff --git a/codecs/resize/src/lib.rs b/codecs/resize/src/lib.rs index fce921a4..6e550b10 100644 --- a/codecs/resize/src/lib.rs +++ b/codecs/resize/src/lib.rs @@ -8,6 +8,7 @@ use cfg_if::cfg_if; use resize::Pixel; use resize::Type; use wasm_bindgen::prelude::*; +use wasm_bindgen::Clamped; mod srgb; use srgb::{linear_to_srgb, Clamp}; @@ -66,7 +67,7 @@ pub fn resize( typ_idx: usize, premultiply: bool, color_space_conversion: bool, -) -> Vec { +) -> Clamped> { let typ = match typ_idx { 0 => Type::Triangle, 1 => Type::Catrom, @@ -91,7 +92,7 @@ pub fn resize( typ, ); resizer.resize(input_image.as_slice(), output_image.as_mut_slice()); - return output_image; + return Clamped(output_image); } // Otherwise, we convert to f32 images to keep the @@ -138,5 +139,5 @@ pub fn resize( .clamp(0.0, 255.0) as u8; } - return output_image; + return Clamped(output_image); } diff --git a/libsquoosh/src/codecs.ts b/libsquoosh/src/codecs.ts index e95cd227..57c6665d 100644 --- a/libsquoosh/src/codecs.ts +++ b/libsquoosh/src/codecs.ts @@ -163,19 +163,17 @@ export const preprocessors = { target_width: width, target_height: height, })); - const resizeResult = resize.resize( - buffer, - input_width, - input_height, - width, - height, - resizeNameToIndex(method), - premultiply, - linearRGB, - ); return new ImageData( - // ImageData does not accept Uint8Array so we convert it to a clamped array - new Uint8ClampedArray(resizeResult), + resize.resize( + buffer, + input_width, + input_height, + width, + height, + resizeNameToIndex(method), + premultiply, + linearRGB, + ), width, height, ); diff --git a/libsquoosh/src/missing-types.d.ts b/libsquoosh/src/missing-types.d.ts index da972340..4319a188 100644 --- a/libsquoosh/src/missing-types.d.ts +++ b/libsquoosh/src/missing-types.d.ts @@ -18,6 +18,11 @@ declare module 'asset-url:../../codecs/oxipng/pkg/squoosh_oxipng_bg.wasm' { export default value; } +declare module 'asset-url:../../codecs/resize/pkg/squoosh_resize_bg.wasm' { + const value: string; + export default value; +} + // These don't exist in NodeJS types so we're not able to use them but they are referenced in some emscripten and codec types // Thus, we need to explicitly assign them to be `never` // We're also not able to use the APIs that use these types