mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
Return Uint8ClampedArray in resize operation
This commit is contained in:
4
codecs/resize/pkg/squoosh_resize.d.ts
generated
vendored
4
codecs/resize/pkg/squoosh_resize.d.ts
generated
vendored
@@ -9,9 +9,9 @@
|
|||||||
* @param {number} typ_idx
|
* @param {number} typ_idx
|
||||||
* @param {boolean} premultiply
|
* @param {boolean} premultiply
|
||||||
* @param {boolean} color_space_conversion
|
* @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;
|
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
||||||
|
|
||||||
|
|||||||
16
codecs/resize/pkg/squoosh_resize.js
generated
16
codecs/resize/pkg/squoosh_resize.js
generated
@@ -26,8 +26,16 @@ function getInt32Memory0() {
|
|||||||
return cachegetInt32Memory0;
|
return cachegetInt32Memory0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getArrayU8FromWasm0(ptr, len) {
|
let cachegetUint8ClampedMemory0 = null;
|
||||||
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
|
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
|
* @param {Uint8Array} input_image
|
||||||
@@ -38,7 +46,7 @@ function getArrayU8FromWasm0(ptr, len) {
|
|||||||
* @param {number} typ_idx
|
* @param {number} typ_idx
|
||||||
* @param {boolean} premultiply
|
* @param {boolean} premultiply
|
||||||
* @param {boolean} color_space_conversion
|
* @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) {
|
export function resize(input_image, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion) {
|
||||||
try {
|
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);
|
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 r0 = getInt32Memory0()[retptr / 4 + 0];
|
||||||
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
||||||
var v1 = getArrayU8FromWasm0(r0, r1).slice();
|
var v1 = getClampedArrayU8FromWasm0(r0, r1).slice();
|
||||||
wasm.__wbindgen_free(r0, r1 * 1);
|
wasm.__wbindgen_free(r0, r1 * 1);
|
||||||
return v1;
|
return v1;
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Binary file not shown.
7
codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts
generated
vendored
Normal file
7
codecs/resize/pkg/squoosh_resize_bg.wasm.d.ts
generated
vendored
Normal 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;
|
||||||
@@ -8,6 +8,7 @@ use cfg_if::cfg_if;
|
|||||||
use resize::Pixel;
|
use resize::Pixel;
|
||||||
use resize::Type;
|
use resize::Type;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::Clamped;
|
||||||
|
|
||||||
mod srgb;
|
mod srgb;
|
||||||
use srgb::{linear_to_srgb, Clamp};
|
use srgb::{linear_to_srgb, Clamp};
|
||||||
@@ -66,7 +67,7 @@ pub fn resize(
|
|||||||
typ_idx: usize,
|
typ_idx: usize,
|
||||||
premultiply: bool,
|
premultiply: bool,
|
||||||
color_space_conversion: bool,
|
color_space_conversion: bool,
|
||||||
) -> Vec<u8> {
|
) -> Clamped<Vec<u8>> {
|
||||||
let typ = match typ_idx {
|
let typ = match typ_idx {
|
||||||
0 => Type::Triangle,
|
0 => Type::Triangle,
|
||||||
1 => Type::Catrom,
|
1 => Type::Catrom,
|
||||||
@@ -91,7 +92,7 @@ pub fn resize(
|
|||||||
typ,
|
typ,
|
||||||
);
|
);
|
||||||
resizer.resize(input_image.as_slice(), output_image.as_mut_slice());
|
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
|
// Otherwise, we convert to f32 images to keep the
|
||||||
@@ -138,5 +139,5 @@ pub fn resize(
|
|||||||
.clamp(0.0, 255.0) as u8;
|
.clamp(0.0, 255.0) as u8;
|
||||||
}
|
}
|
||||||
|
|
||||||
return output_image;
|
return Clamped(output_image);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,19 +163,17 @@ export const preprocessors = {
|
|||||||
target_width: width,
|
target_width: width,
|
||||||
target_height: height,
|
target_height: height,
|
||||||
}));
|
}));
|
||||||
const resizeResult = resize.resize(
|
|
||||||
buffer,
|
|
||||||
input_width,
|
|
||||||
input_height,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
resizeNameToIndex(method),
|
|
||||||
premultiply,
|
|
||||||
linearRGB,
|
|
||||||
);
|
|
||||||
return new ImageData(
|
return new ImageData(
|
||||||
// ImageData does not accept Uint8Array so we convert it to a clamped array
|
resize.resize(
|
||||||
new Uint8ClampedArray(resizeResult),
|
buffer,
|
||||||
|
input_width,
|
||||||
|
input_height,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
resizeNameToIndex(method),
|
||||||
|
premultiply,
|
||||||
|
linearRGB,
|
||||||
|
),
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
);
|
);
|
||||||
|
|||||||
5
libsquoosh/src/missing-types.d.ts
vendored
5
libsquoosh/src/missing-types.d.ts
vendored
@@ -18,6 +18,11 @@ declare module 'asset-url:../../codecs/oxipng/pkg/squoosh_oxipng_bg.wasm' {
|
|||||||
export default value;
|
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
|
// 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`
|
// Thus, we need to explicitly assign them to be `never`
|
||||||
// We're also not able to use the APIs that use these types
|
// We're also not able to use the APIs that use these types
|
||||||
|
|||||||
Reference in New Issue
Block a user