mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-15 18:19:47 +00:00
This makes building simpler and allows us to potentially use multithreading version in the future. For now points to a custom fork of OxiPNG that enables WebAssembly support, as PR is still pending review.
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
import * as wasm from './oxipng_bg.wasm';
|
|
|
|
let cachegetUint8Memory = null;
|
|
function getUint8Memory() {
|
|
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasm.memory.buffer) {
|
|
cachegetUint8Memory = new Uint8Array(wasm.memory.buffer);
|
|
}
|
|
return cachegetUint8Memory;
|
|
}
|
|
|
|
let WASM_VECTOR_LEN = 0;
|
|
|
|
function passArray8ToWasm(arg) {
|
|
const ptr = wasm.__wbindgen_malloc(arg.length * 1);
|
|
getUint8Memory().set(arg, ptr / 1);
|
|
WASM_VECTOR_LEN = arg.length;
|
|
return ptr;
|
|
}
|
|
|
|
let cachegetInt32Memory = null;
|
|
function getInt32Memory() {
|
|
if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) {
|
|
cachegetInt32Memory = new Int32Array(wasm.memory.buffer);
|
|
}
|
|
return cachegetInt32Memory;
|
|
}
|
|
|
|
function getArrayU8FromWasm(ptr, len) {
|
|
return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
|
|
}
|
|
/**
|
|
* @param {Uint8Array} data
|
|
* @param {number} level
|
|
* @returns {Uint8Array}
|
|
*/
|
|
export function optimise(data, level) {
|
|
const retptr = 8;
|
|
const ret = wasm.optimise(retptr, passArray8ToWasm(data), WASM_VECTOR_LEN, level);
|
|
const memi32 = getInt32Memory();
|
|
const v0 = getArrayU8FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
|
|
wasm.__wbindgen_free(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
|
|
return v0;
|
|
}
|
|
|
|
let cachedTextDecoder = new TextDecoder('utf-8');
|
|
|
|
function getStringFromWasm(ptr, len) {
|
|
return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
|
|
}
|
|
|
|
export const __wbindgen_throw = function(arg0, arg1) {
|
|
throw new Error(getStringFromWasm(arg0, arg1));
|
|
};
|
|
|