From 5707eeff411f1976492968f319692219e9d76fe3 Mon Sep 17 00:00:00 2001 From: ergunsh Date: Tue, 8 Jun 2021 20:28:22 +0200 Subject: [PATCH] Return Uint8ClampedArray in resize operation --- codecs/resize/pkg/squoosh_resize.d.ts | 4 ++-- codecs/resize/pkg/squoosh_resize.js | 16 +++++++++---- codecs/resize/pkg/squoosh_resize_bg.wasm | Bin 37053 -> 37052 bytes codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts | 7 ++++++ codecs/resize/src/lib.rs | 7 +++--- libsquoosh/src/codecs.ts | 22 ++++++++---------- libsquoosh/src/missing-types.d.ts | 5 ++++ 7 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts 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 f9ceede2e2333bc34d42a55a24372a79930e29df..b910c97b050df20c1024224d8b3dfab092e6d9e8 100644 GIT binary patch delta 372 zcmdnHkZI3CrVZw7d<#|`U=Yw_;8tK%U~^nB*_Ew;v2*h_wkURF8E&AA93M!gpHEPX zv2*f5m1#hZg=!O!vw64m86D0Es}C?ZN;LW9v@vRg$rqrv7&73wUBJ`4;Dd_a7JL4bh^Nap~tG!j1-$QDB4 zX9L;1Nc;jIn-huO3S)k^aR?hzGW@C6EsGeeXc0QrGHz9%CCgDjAS@x6e2 zB_zHrkgpBo!}Qm)Ffed~5N@tpj%R~yI&a;Eksa56Z8!hw4R>tu;u8OA-6O?zV| NEA(k^_US8R0sxRSQG);g delta 397 zcmdn9kZJEirVZw7e1BFQU=Yw_;8tK%U~~L4*_Ew;v19W#wkURF8E&AA93M!gpHEPX zv19T=m1#hZg=!O!vw64m86D0BpgE3`j&oLRo{%BV#B_J{=9O7poIqKVlDo`2YYmtf z92*)AFmQj;oGe?R!Pqm|t)iQ;XY-{Bb(TbL1_lN`AU?t%z`zBhvw>I|iJt>x3nB5d zfNWkQem;=RiNtRKvIUX&t(&82rTGJOLtk9>;$gtVFuaF4 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