Return Uint8ClampedArray in resize operation

This commit is contained in:
ergunsh
2021-06-08 20:28:22 +02:00
parent a18ed360eb
commit 5707eeff41
7 changed files with 40 additions and 21 deletions

View File

@@ -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;

View File

@@ -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 {

7
codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts generated vendored Normal file
View File

@@ -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;

View File

@@ -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<u8> {
) -> Clamped<Vec<u8>> {
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);
}

View File

@@ -163,7 +163,8 @@ export const preprocessors = {
target_width: width,
target_height: height,
}));
const resizeResult = resize.resize(
return new ImageData(
resize.resize(
buffer,
input_width,
input_height,
@@ -172,10 +173,7 @@ export const preprocessors = {
resizeNameToIndex(method),
premultiply,
linearRGB,
);
return new ImageData(
// ImageData does not accept Uint8Array so we convert it to a clamped array
new Uint8ClampedArray(resizeResult),
),
width,
height,
);

View File

@@ -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