UNPKG

compress-jpeg

Version:

A simple JPEG compression library for WebAssembly

239 lines (203 loc) 8.04 kB
let wasm; function addToExternrefTable0(obj) { const idx = wasm.__externref_table_alloc(); wasm.__wbindgen_externrefs.set(idx, obj); return idx; } function getClampedArrayU8FromWasm0(ptr, len) { ptr = ptr >>> 0; return getUint8ClampedArrayMemory0().subarray(ptr / 1, ptr / 1 + len); } let cachedDataViewMemory0 = null; function getDataViewMemory0() { if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { cachedDataViewMemory0 = new DataView(wasm.memory.buffer); } return cachedDataViewMemory0; } function getStringFromWasm0(ptr, len) { ptr = ptr >>> 0; return decodeText(ptr, len); } let cachedUint8ArrayMemory0 = null; function getUint8ArrayMemory0() { if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); } return cachedUint8ArrayMemory0; } let cachedUint8ClampedArrayMemory0 = null; function getUint8ClampedArrayMemory0() { if (cachedUint8ClampedArrayMemory0 === null || cachedUint8ClampedArrayMemory0.byteLength === 0) { cachedUint8ClampedArrayMemory0 = new Uint8ClampedArray(wasm.memory.buffer); } return cachedUint8ClampedArrayMemory0; } function handleError(f, args) { try { return f.apply(this, args); } catch (e) { const idx = addToExternrefTable0(e); wasm.__wbindgen_exn_store(idx); } } function passArray8ToWasm0(arg, malloc) { const ptr = malloc(arg.length * 1, 1) >>> 0; getUint8ArrayMemory0().set(arg, ptr / 1); WASM_VECTOR_LEN = arg.length; return ptr; } function takeFromExternrefTable0(idx) { const value = wasm.__wbindgen_externrefs.get(idx); wasm.__externref_table_dealloc(idx); return value; } let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); const MAX_SAFARI_DECODE_BYTES = 2146435072; let numBytesDecoded = 0; function decodeText(ptr, len) { numBytesDecoded += len; if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) { cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); cachedTextDecoder.decode(); numBytesDecoded = len; } return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); } let WASM_VECTOR_LEN = 0; /** * Compress an ImageData using a simplified JPEG-style pipeline. * * **Parameters:** * - `image_data`: The RGBA ImageData to compress. * - `compression`: A value from 0.0–1.0: * - 0.0 = no compression (highest quality) * - 1.0 = strongest compression (lowest quality) * * **Returns:** * A new `ImageData` object containing the visually compressed pixels. * @param {ImageData} image_data * @param {number} compression * @returns {ImageData} */ export function compress_jpeg(image_data, compression) { const ret = wasm.compress_jpeg(image_data, compression); if (ret[2]) { throw takeFromExternrefTable0(ret[1]); } return takeFromExternrefTable0(ret[0]); } const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']); async function __wbg_load(module, imports) { if (typeof Response === 'function' && module instanceof Response) { if (typeof WebAssembly.instantiateStreaming === 'function') { try { return await WebAssembly.instantiateStreaming(module, imports); } catch (e) { const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type); if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') { console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); } else { throw e; } } } const bytes = await module.arrayBuffer(); return await WebAssembly.instantiate(bytes, imports); } else { const instance = await WebAssembly.instantiate(module, imports); if (instance instanceof WebAssembly.Instance) { return { instance, module }; } else { return instance; } } } function __wbg_get_imports() { const imports = {}; imports.wbg = {}; imports.wbg.__wbg___wbindgen_throw_dd24417ed36fc46e = function(arg0, arg1) { throw new Error(getStringFromWasm0(arg0, arg1)); }; imports.wbg.__wbg_data_83b2a9a09dd4ab39 = function(arg0, arg1) { const ret = arg1.data; const ptr1 = passArray8ToWasm0(ret, wasm.__wbindgen_malloc); const len1 = WASM_VECTOR_LEN; getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true); getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true); }; imports.wbg.__wbg_height_93d6779af8295699 = function(arg0) { const ret = arg0.height; return ret; }; imports.wbg.__wbg_new_with_u8_clamped_array_and_sh_5ac77cce3f6497ff = function() { return handleError(function (arg0, arg1, arg2, arg3) { const ret = new ImageData(getClampedArrayU8FromWasm0(arg0, arg1), arg2 >>> 0, arg3 >>> 0); return ret; }, arguments) }; imports.wbg.__wbg_width_73a6511a2370c184 = function(arg0) { const ret = arg0.width; return ret; }; imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) { // Cast intrinsic for `Ref(String) -> Externref`. const ret = getStringFromWasm0(arg0, arg1); return ret; }; imports.wbg.__wbindgen_init_externref_table = function() { const table = wasm.__wbindgen_externrefs; const offset = table.grow(4); table.set(0, undefined); table.set(offset + 0, undefined); table.set(offset + 1, null); table.set(offset + 2, true); table.set(offset + 3, false); }; return imports; } function __wbg_finalize_init(instance, module) { wasm = instance.exports; __wbg_init.__wbindgen_wasm_module = module; cachedDataViewMemory0 = null; cachedUint8ArrayMemory0 = null; cachedUint8ClampedArrayMemory0 = null; wasm.__wbindgen_start(); return wasm; } function initSync(module) { if (wasm !== undefined) return wasm; if (typeof module !== 'undefined') { if (Object.getPrototypeOf(module) === Object.prototype) { ({module} = module) } else { console.warn('using deprecated parameters for `initSync()`; pass a single object instead') } } const imports = __wbg_get_imports(); if (!(module instanceof WebAssembly.Module)) { module = new WebAssembly.Module(module); } const instance = new WebAssembly.Instance(module, imports); return __wbg_finalize_init(instance, module); } async function __wbg_init(module_or_path) { if (wasm !== undefined) return wasm; if (typeof module_or_path !== 'undefined') { if (Object.getPrototypeOf(module_or_path) === Object.prototype) { ({module_or_path} = module_or_path) } else { console.warn('using deprecated parameters for the initialization function; pass a single object instead') } } if (typeof module_or_path === 'undefined') { module_or_path = new URL('compress_jpeg_bg.wasm', import.meta.url); } const imports = __wbg_get_imports(); if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { module_or_path = fetch(module_or_path); } const { instance, module } = await __wbg_load(await module_or_path, imports); return __wbg_finalize_init(instance, module); } export { initSync }; export default __wbg_init;