Make images smaller using best-in-class codecs, right in the browser.
JavaScript
Rename `resize` package
Ingvar Stepanyan68177b7590b0
diff --git a/codecs/resize/Cargo.toml b/codecs/resize/Cargo.tomlindex cd47881c..b1f14fe6 100644--- a/codecs/resize/Cargo.toml+++ b/codecs/resize/Cargo.toml@@ -1,5 +1,5 @@[package]-name = "squooshresize"+name = "resize"version = "0.1.0"authors = ["Surma <[email protected]>"]diff --git a/codecs/resize/pkg/resize.d.ts b/codecs/resize/pkg/resize.d.tsindex b0cc0a7b..25a0e4fe 100644--- a/codecs/resize/pkg/resize.d.ts+++ b/codecs/resize/pkg/resize.d.ts@@ -1,13 +1,24 @@/* tslint:disable *//**-* @param {Uint8Array} arg0-* @param {number} arg1-* @param {number} arg2-* @param {number} arg3-* @param {number} arg4-* @param {number} arg5-* @param {boolean} arg6-* @param {boolean} arg7+* @param {Uint8Array} input_image+* @param {number} input_width+* @param {number} input_height+* @param {number} output_width+* @param {number} output_height+* @param {number} typ_idx+* @param {boolean} premultiply+* @param {boolean} color_space_conversion* @returns {Uint8Array}*/-export function resize(arg0: Uint8Array, arg1: number, arg2: number, arg3: number, arg4: number, arg5: number, arg6: boolean, arg7: 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): Uint8Array;++/**+* If `module_or_path` is {RequestInfo}, makes a request and+* for everything else, calls `WebAssembly.instantiate` directly.+*+* @param {RequestInfo | BufferSource | WebAssembly.Module} module_or_path+*+* @returns {Promise<any>}+*/+export default function init (module_or_path: RequestInfo | BufferSource | WebAssembly.Module): Promise<any>;+\ No newline at end of filediff --git a/codecs/resize/pkg/resize.js b/codecs/resize/pkg/resize.jsindex 656cee33..f7d41ff0 100644--- a/codecs/resize/pkg/resize.js+++ b/codecs/resize/pkg/resize.js@@ -1,7 +1,6 @@(function() {- var wasm;const __exports = {};-+ let wasm;let cachegetUint8Memory = null;function getUint8Memory() {@@ -20,95 +19,77 @@return ptr;}- function getArrayU8FromWasm(ptr, len) {- return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);- }-- let cachedGlobalArgumentPtr = null;- function globalArgumentPtr() {- if (cachedGlobalArgumentPtr === null) {- cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();+ let cachegetInt32Memory = null;+ function getInt32Memory() {+ if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasm.memory.buffer) {+ cachegetInt32Memory = new Int32Array(wasm.memory.buffer);}- return cachedGlobalArgumentPtr;+ return cachegetInt32Memory;}- let cachegetUint32Memory = null;- function getUint32Memory() {- if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {- cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);- }- return cachegetUint32Memory;+ function getArrayU8FromWasm(ptr, len) {+ return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);}/**- * @param {Uint8Array} arg0- * @param {number} arg1- * @param {number} arg2- * @param {number} arg3- * @param {number} arg4- * @param {number} arg5- * @param {boolean} arg6- * @param {boolean} arg7+ * @param {Uint8Array} input_image+ * @param {number} input_width+ * @param {number} input_height+ * @param {number} output_width+ * @param {number} output_height+ * @param {number} typ_idx+ * @param {boolean} premultiply+ * @param {boolean} color_space_conversion* @returns {Uint8Array}*/- __exports.resize = function(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7) {- const ptr0 = passArray8ToWasm(arg0);- const len0 = WASM_VECTOR_LEN;- const retptr = globalArgumentPtr();- wasm.resize(retptr, ptr0, len0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);- const mem = getUint32Memory();- const rustptr = mem[retptr / 4];- const rustlen = mem[retptr / 4 + 1];-- const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();- wasm.__wbindgen_free(rustptr, rustlen * 1);- return realRet;-+ __exports.resize = function(input_image, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion) {+ const retptr = 8;+ const ret = wasm.resize(retptr, passArray8ToWasm(input_image), WASM_VECTOR_LEN, input_width, input_height, output_width, output_height, typ_idx, premultiply, color_space_conversion);+ 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;};- const heap = new Array(32);-- heap.fill(undefined);+ function init(module) {- heap.push(undefined, null, true, false);+ let result;+ const imports = {};- let heap_next = heap.length;+ if (module instanceof URL || typeof module === 'string' || module instanceof Request) {- function dropObject(idx) {- if (idx < 36) return;- heap[idx] = heap_next;- heap_next = idx;- }-- __exports.__wbindgen_object_drop_ref = function(i) { dropObject(i); };-- function init(path_or_module) {- let instantiation;- const imports = { './resize': __exports };- if (path_or_module instanceof WebAssembly.Module) {- instantiation = WebAssembly.instantiate(path_or_module, imports)- .then(instance => {- return { instance, module: path_or_module }- });- } else {- const data = fetch(path_or_module);- if (typeof WebAssembly.instantiateStreaming === 'function') {- instantiation = WebAssembly.instantiateStreaming(data, imports)- .catch(e => {- console.warn("`WebAssembly.instantiateStreaming` failed. Assuming this is because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);- return data+ const response = fetch(module);+ if (typeof WebAssembly.instantiateStreaming === 'function') {+ result = WebAssembly.instantiateStreaming(response, imports)+ .catch(e => {+ console.warn("`WebAssembly.instantiateStreaming` failed. Assuming this is because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);+ return response+ .then(r => r.arrayBuffer())+ .then(bytes => WebAssembly.instantiate(bytes, imports));+ });+ } else {+ result = response.then(r => r.arrayBuffer()).then(bytes => WebAssembly.instantiate(bytes, imports));- });+ }} else {- instantiation = data- .then(response => response.arrayBuffer())- .then(buffer => WebAssembly.instantiate(buffer, imports));++ result = WebAssembly.instantiate(module, imports)+ .then(result => {+ if (result instanceof WebAssembly.Instance) {+ return { instance: result, module };+ } else {+ return result;+ }+ });}+ return result.then(({instance, module}) => {+ wasm = instance.exports;+ init.__wbindgen_wasm_module = module;++ return wasm;+ });}- return instantiation.then(({instance}) => {- wasm = init.wasm = instance.exports;- });-};-self.wasm_bindgen = Object.assign(init, __exports);+ self.wasm_bindgen = Object.assign(init, __exports);+})();diff --git a/codecs/resize/pkg/resize_bg.d.ts b/codecs/resize/pkg/resize_bg.d.tsindex d8049a99..51e147c3 100644--- a/codecs/resize/pkg/resize_bg.d.ts+++ b/codecs/resize/pkg/resize_bg.d.ts@@ -1,6 +1,5 @@/* tslint:disable */export const memory: WebAssembly.Memory;-export function __wbindgen_global_argument_ptr(): number;export function __wbindgen_malloc(a: number): number;export function __wbindgen_free(a: number, b: number): void;export function resize(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number): void;diff --git a/codecs/resize/pkg/resize_bg.wasm b/codecs/resize/pkg/resize_bg.wasmindex dadb009e..c9620d5e 100644Binary files a/codecs/resize/pkg/resize_bg.wasm and b/codecs/resize/pkg/resize_bg.wasm differ