diff --git a/packages/oxipng/CHANGELOG.md b/packages/oxipng/CHANGELOG.md index b9ef6f9..d34b97a 100644 --- a/packages/oxipng/CHANGELOG.md +++ b/packages/oxipng/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## @jsquash/oxipng@2.2.0 + +### Adds + +- Updates oxipng to v9.0 +- Adds support to optimise raw image data directly and output as an optimised PNG. This is useful for when you have raw image data and want to optimise it without having to encode to a PNG first. + ## @jsquash/oxipng@2.1.0 ### Adds diff --git a/packages/oxipng/README.md b/packages/oxipng/README.md index 635f4b0..88f965d 100644 --- a/packages/oxipng/README.md +++ b/packages/oxipng/README.md @@ -21,10 +21,10 @@ Note: You will need to either manually include the wasm files from the codec dir ### optimise(data: ArrayBuffer, options?: OptimiseOptions): Promise -Optimises a PNG image buffer and resolves to the optimised buffer output +Optimises a PNG image buffer or raw image data and resolves to the optimised PNG image buffer output #### data -Type: `ArrayBuffer` +Type: `ArrayBuffer | ImageData` #### options Type: `Partial` diff --git a/packages/oxipng/codec/Cargo.lock b/packages/oxipng/codec/Cargo.lock index d55e256..53479d4 100644 --- a/packages/oxipng/codec/Cargo.lock +++ b/packages/oxipng/codec/Cargo.lock @@ -2,24 +2,12 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "adler" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" - [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - [[package]] name = "bitvec" version = "1.0.1" @@ -44,12 +32,6 @@ version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17febce684fd15d89027105661fec94afb475cb995fbc59d2865198446ba2eea" -[[package]] -name = "byteorder" -version = "1.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" - [[package]] name = "cc" version = "1.0.82" @@ -65,21 +47,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "color_quant" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" - -[[package]] -name = "crc32fast" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" -dependencies = [ - "cfg-if", -] - [[package]] name = "crossbeam-channel" version = "0.5.8" @@ -130,23 +97,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] -name = "fdeflate" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d329bdeac514ee06249dabc27877490f17f5d371ec693360768b838e19f3ae10" -dependencies = [ - "simd-adler32", -] - -[[package]] -name = "flate2" -version = "1.0.27" +name = "equivalent" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6c98ee8095e9d1dcbf2fcc6d95acccb90d1c81db1e44725c6a984b1dbdfb010" -dependencies = [ - "crc32fast", - "miniz_oxide", -] +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "funty" @@ -156,9 +110,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "hashbrown" -version = "0.12.3" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" [[package]] name = "hermit-abi" @@ -166,40 +120,17 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "443144c8cdadd93ebf52ddb4056d257f5b52c04d3c804e657d19eb73fc33668b" -[[package]] -name = "image" -version = "0.24.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f3dfdbdd72063086ff443e297b61695500514b1e41095b6fb9a5ab48a70a711" -dependencies = [ - "bytemuck", - "byteorder", - "color_quant", - "num-rational", - "num-traits", - "png", -] - [[package]] name = "indexmap" -version = "1.9.3" +version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ - "autocfg", + "equivalent", "hashbrown", "rayon", ] -[[package]] -name = "itertools" -version = "0.10.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" -dependencies = [ - "either", -] - [[package]] name = "js-sys" version = "0.3.64" @@ -217,18 +148,18 @@ checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "libdeflate-sys" -version = "0.11.0" +version = "1.19.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6784b6b84b67d71b4307963d456a9c7c29f9b47c658f533e598de369e34277" +checksum = "cc9caa76c8cc6ee8c4efcf8f4514a812ebcad3aa7d3b548efe4d26da1203f177" dependencies = [ "cc", ] [[package]] name = "libdeflater" -version = "0.11.0" +version = "1.19.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8e285aa6a046fd338b2592c16bee148b2b00789138ed6b7bb56bb13d585050d" +checksum = "265a985bd31e5f22e2b2ac107cbed44c6ccf40ae236e46963cd00dd213e4bd03" dependencies = [ "libdeflate-sys", ] @@ -248,46 +179,6 @@ dependencies = [ "autocfg", ] -[[package]] -name = "miniz_oxide" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" -dependencies = [ - "adler", - "simd-adler32", -] - -[[package]] -name = "num-integer" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" -dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-rational" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.16" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" -dependencies = [ - "autocfg", -] - [[package]] name = "num_cpus" version = "1.16.0" @@ -306,15 +197,13 @@ checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "oxipng" -version = "8.0.0" +version = "9.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "630638e107fb436644c300e781d3f17e1b04656138ba0d40564be4be3b06db32" +checksum = "28e5c341ef78a228e47a551bfd15ff885d8c501af49f953358763a538c01f14d" dependencies = [ "bitvec", "crossbeam-channel", - "image", "indexmap", - "itertools", "libdeflater", "log", "rayon", @@ -323,19 +212,6 @@ dependencies = [ "rustc_version", ] -[[package]] -name = "png" -version = "0.17.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd75bf2d8dd3702b9707cdbc56a5b9ef42cec752eb8b3bafc01234558442aa64" -dependencies = [ - "bitflags", - "crc32fast", - "fdeflate", - "flate2", - "miniz_oxide", -] - [[package]] name = "proc-macro2" version = "1.0.66" @@ -418,12 +294,6 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" -[[package]] -name = "simd-adler32" -version = "0.3.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" - [[package]] name = "spmc" version = "0.3.0" diff --git a/packages/oxipng/codec/Cargo.toml b/packages/oxipng/codec/Cargo.toml index ad06ee6..cc813f8 100644 --- a/packages/oxipng/codec/Cargo.toml +++ b/packages/oxipng/codec/Cargo.toml @@ -12,7 +12,7 @@ wasm-opt = ["-O", "--no-validation"] crate-type = ["cdylib"] [dependencies] -oxipng = { version = "8.0.0", default-features = false, features = ["freestanding"] } +oxipng = { version = "9.0", default-features = false, features = ["freestanding"] } wasm-bindgen = "0.2.87" log = { version = "0.4.11", features = ["release_max_level_off"] } wasm-bindgen-rayon = { version = "1.0", optional = true } diff --git a/packages/oxipng/codec/package.json b/packages/oxipng/codec/package.json index ddaaf44..9eeb0e2 100644 --- a/packages/oxipng/codec/package.json +++ b/packages/oxipng/codec/package.json @@ -1,7 +1,8 @@ { "name": "oxipng", "scripts": { - "build": "./build.sh" + "build": "./build.sh && npm run patch-pre-script", + "patch-pre-script": "cat pre.js >> pkg/squoosh_oxipng.js && cat pre.js >> pkg-parallel/squoosh_oxipng.js" }, "type": "module" } diff --git a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.d.ts b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.d.ts index b56a89e..2659cfe 100644 --- a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.d.ts +++ b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.d.ts @@ -9,6 +9,16 @@ */ export function optimise(data: Uint8Array, level: number, interlace: boolean, optimize_alpha: boolean): Uint8Array; /** +* @param {Uint8ClampedArray} data +* @param {number} width +* @param {number} height +* @param {number} level +* @param {boolean} interlace +* @param {boolean} optimize_alpha +* @returns {Uint8Array} +*/ +export function optimise_raw(data: Uint8ClampedArray, width: number, height: number, level: number, interlace: boolean, optimize_alpha: boolean): Uint8Array; +/** * @param {number} num_threads * @returns {Promise} */ @@ -38,6 +48,7 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl export interface InitOutput { readonly optimise: (a: number, b: number, c: number, d: number, e: number, f: number) => void; + readonly optimise_raw: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void; readonly __wbg_wbg_rayon_poolbuilder_free: (a: number) => void; readonly wbg_rayon_poolbuilder_numThreads: (a: number) => number; readonly wbg_rayon_poolbuilder_receiver: (a: number) => number; diff --git a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.js b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.js index 335435b..52bc1fd 100644 --- a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.js +++ b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng.js @@ -80,6 +80,31 @@ export function optimise(data, level, interlace, optimize_alpha) { } } +/** +* @param {Uint8ClampedArray} data +* @param {number} width +* @param {number} height +* @param {number} level +* @param {boolean} interlace +* @param {boolean} optimize_alpha +* @returns {Uint8Array} +*/ +export function optimise_raw(data, width, height, level, interlace, optimize_alpha) { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + wasm.optimise_raw(retptr, ptr0, len0, width, height, level, interlace, optimize_alpha); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v2 = getArrayU8FromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 1); + return v2; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } +} + function getObject(idx) { return heap[idx]; } function dropObject(idx) { @@ -257,3 +282,27 @@ async function __wbg_init(input, maybe_memory) { export { initSync } export default __wbg_init; +const isServiceWorker = globalThis.ServiceWorkerGlobalScope !== undefined; +const isRunningInCloudFlareWorkers = isServiceWorker && typeof self !== 'undefined' && globalThis.caches && globalThis.caches.default !== undefined; +const isRunningInNode = typeof process === 'object' && process.release && process.release.name === 'node'; + +if (isRunningInCloudFlareWorkers || isRunningInNode) { + if (!globalThis.ImageData) { + // Simple Polyfill for ImageData Object + globalThis.ImageData = class ImageData { + constructor(data, width, height) { + this.data = data; + this.width = width; + this.height = height; + } + }; + } + + if (import.meta.url === undefined) { + import.meta.url = 'https://localhost'; + } + + if (typeof self !== 'undefined' && self.location === undefined) { + self.location = { href: '' }; + } +} diff --git a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm index 616868f..72df593 100644 Binary files a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm and b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm differ diff --git a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm.d.ts b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm.d.ts index b28ce1f..1740588 100644 --- a/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm.d.ts +++ b/packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm.d.ts @@ -1,6 +1,7 @@ /* tslint:disable */ /* eslint-disable */ export function optimise(a: number, b: number, c: number, d: number, e: number, f: number): void; +export function optimise_raw(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number): void; export function __wbg_wbg_rayon_poolbuilder_free(a: number): void; export function wbg_rayon_poolbuilder_numThreads(a: number): number; export function wbg_rayon_poolbuilder_receiver(a: number): number; diff --git a/packages/oxipng/codec/pkg/squoosh_oxipng.d.ts b/packages/oxipng/codec/pkg/squoosh_oxipng.d.ts index ae6e7ee..49daa1e 100644 --- a/packages/oxipng/codec/pkg/squoosh_oxipng.d.ts +++ b/packages/oxipng/codec/pkg/squoosh_oxipng.d.ts @@ -8,12 +8,23 @@ * @returns {Uint8Array} */ export function optimise(data: Uint8Array, level: number, interlace: boolean, optimize_alpha: boolean): Uint8Array; +/** +* @param {Uint8ClampedArray} data +* @param {number} width +* @param {number} height +* @param {number} level +* @param {boolean} interlace +* @param {boolean} optimize_alpha +* @returns {Uint8Array} +*/ +export function optimise_raw(data: Uint8ClampedArray, width: number, height: number, level: number, interlace: boolean, optimize_alpha: boolean): Uint8Array; export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; export interface InitOutput { readonly memory: WebAssembly.Memory; readonly optimise: (a: number, b: number, c: number, d: number, e: number, f: number) => void; + readonly optimise_raw: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void; readonly __wbindgen_add_to_stack_pointer: (a: number) => number; readonly __wbindgen_malloc: (a: number, b: number) => number; readonly __wbindgen_free: (a: number, b: number, c: number) => void; diff --git a/packages/oxipng/codec/pkg/squoosh_oxipng.js b/packages/oxipng/codec/pkg/squoosh_oxipng.js index e45a2e2..534093d 100644 --- a/packages/oxipng/codec/pkg/squoosh_oxipng.js +++ b/packages/oxipng/codec/pkg/squoosh_oxipng.js @@ -63,6 +63,31 @@ export function optimise(data, level, interlace, optimize_alpha) { } } +/** +* @param {Uint8ClampedArray} data +* @param {number} width +* @param {number} height +* @param {number} level +* @param {boolean} interlace +* @param {boolean} optimize_alpha +* @returns {Uint8Array} +*/ +export function optimise_raw(data, width, height, level, interlace, optimize_alpha) { + try { + const retptr = wasm.__wbindgen_add_to_stack_pointer(-16); + const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + wasm.optimise_raw(retptr, ptr0, len0, width, height, level, interlace, optimize_alpha); + var r0 = getInt32Memory0()[retptr / 4 + 0]; + var r1 = getInt32Memory0()[retptr / 4 + 1]; + var v2 = getArrayU8FromWasm0(r0, r1).slice(); + wasm.__wbindgen_free(r0, r1 * 1); + return v2; + } finally { + wasm.__wbindgen_add_to_stack_pointer(16); + } +} + async function __wbg_load(module, imports) { if (typeof Response === 'function' && module instanceof Response) { if (typeof WebAssembly.instantiateStreaming === 'function') { @@ -155,7 +180,6 @@ async function __wbg_init(input) { export { initSync } export default __wbg_init; - const isServiceWorker = globalThis.ServiceWorkerGlobalScope !== undefined; const isRunningInCloudFlareWorkers = isServiceWorker && typeof self !== 'undefined' && globalThis.caches && globalThis.caches.default !== undefined; const isRunningInNode = typeof process === 'object' && process.release && process.release.name === 'node'; diff --git a/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm b/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm index 843c0c1..1b51e5a 100644 Binary files a/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm and b/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm differ diff --git a/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm.d.ts b/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm.d.ts index 7ee560a..794bc53 100644 --- a/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm.d.ts +++ b/packages/oxipng/codec/pkg/squoosh_oxipng_bg.wasm.d.ts @@ -2,6 +2,7 @@ /* eslint-disable */ export const memory: WebAssembly.Memory; export function optimise(a: number, b: number, c: number, d: number, e: number, f: number): void; +export function optimise_raw(a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number): void; export function __wbindgen_add_to_stack_pointer(a: number): number; export function __wbindgen_malloc(a: number, b: number): number; export function __wbindgen_free(a: number, b: number, c: number): void; diff --git a/packages/oxipng/codec/src/lib.rs b/packages/oxipng/codec/src/lib.rs index 2c53845..f7f1fd1 100644 --- a/packages/oxipng/codec/src/lib.rs +++ b/packages/oxipng/codec/src/lib.rs @@ -1,14 +1,41 @@ #[cfg(feature = "parallel")] pub use wasm_bindgen_rayon::init_thread_pool; -use oxipng::Interlacing; +use oxipng::{BitDepth, ColorType, Interlacing}; use wasm_bindgen::prelude::*; +use wasm_bindgen::Clamped; #[wasm_bindgen] pub fn optimise(data: &[u8], level: u8, interlace: bool, optimize_alpha: bool) -> Vec { let mut options = oxipng::Options::from_preset(level); - options.interlace = Some(if interlace { Interlacing::Adam7 } else { Interlacing::None }); + options.interlace = Some(if interlace { + Interlacing::Adam7 + } else { + Interlacing::None + }); options.optimize_alpha = optimize_alpha; oxipng::optimize_from_memory(data, &options).unwrap_throw() } + +#[wasm_bindgen] +pub fn optimise_raw( + data: Clamped>, + width: u32, + height: u32, + level: u8, + interlace: bool, + optimize_alpha: bool, +) -> Vec { + let mut options = oxipng::Options::from_preset(level); + options.interlace = Some(if interlace { + Interlacing::Adam7 + } else { + Interlacing::None + }); + options.optimize_alpha = optimize_alpha; + + let raw = oxipng::RawImage::new(width, height, ColorType::RGBA, BitDepth::Eight, data.0) + .unwrap_throw(); + raw.create_optimized_png(&options).unwrap_throw() +} diff --git a/packages/oxipng/optimise.ts b/packages/oxipng/optimise.ts index 897351f..fb98137 100644 --- a/packages/oxipng/optimise.ts +++ b/packages/oxipng/optimise.ts @@ -25,18 +25,21 @@ async function initMT(moduleOrPath?: InitInput) { default: init, initThreadPool, optimise, + optimise_raw, } = await import('./codec/pkg-parallel/squoosh_oxipng.js'); await init(moduleOrPath); await initThreadPool(globalThis.navigator.hardwareConcurrency); - return optimise; + return { optimise, optimise_raw }; } async function initST(moduleOrPath?: InitInput) { - const { default: init, optimise } = await import( - './codec/pkg/squoosh_oxipng.js' - ); + const { + default: init, + optimise, + optimise_raw, + } = await import('./codec/pkg/squoosh_oxipng.js'); await init(moduleOrPath); - return optimise; + return { optimise, optimise_raw }; } let wasmReady: ReturnType; @@ -65,11 +68,23 @@ export async function init( } export default async function optimise( - data: ArrayBuffer, + data: ArrayBuffer | ImageData, options: Partial = {}, ): Promise { const _options = { ...defaultOptions, ...options }; - const optimise = await init(); + const { optimise, optimise_raw } = await init(); + + if (data instanceof ImageData) { + return optimise_raw( + data.data, + data.width, + data.height, + _options.level, + _options.interlace, + _options.optimiseAlpha, + ).buffer; + } + return optimise( new Uint8Array(data), _options.level, diff --git a/packages/oxipng/package.json b/packages/oxipng/package.json index 61a90ab..2d14659 100644 --- a/packages/oxipng/package.json +++ b/packages/oxipng/package.json @@ -1,6 +1,6 @@ { "name": "@jsquash/oxipng", - "version": "2.1.0", + "version": "2.2.0", "main": "index.js", "description": "Wasm png optimiser supporting the browser. Repackaged from Squoosh App.", "repository": "jamsinclair/jSquash/packages/oxipng", @@ -21,7 +21,7 @@ "license": "Apache-2.0", "scripts": { "clean": "rm -rf dist", - "build": "npm run clean && tsc && cp -r codec package.json README.md CHANGELOG.md .npmignore ../../LICENSE dist", + "build": "npm run clean && tsc && cp -r codec package.json README.md CHANGELOG.md .npmignore ../../LICENSE dist && rm -rf dist/codec/target", "prepublishOnly": "[[ \"$PWD\" == *'/dist' ]] && exit 0 || (echo 'Please run npm publish from the dist directory' && exit 1)" }, "dependencies": { diff --git a/packages/png/README.md b/packages/png/README.md index 92cee76..ffd7162 100644 --- a/packages/png/README.md +++ b/packages/png/README.md @@ -37,6 +37,8 @@ const imageData = await decode(await formData.get('image').arrayBuffer()); ### encode(data: ImageData): Promise +> ℹ️ You may want to use the [@jsquash/oxipng](/packages/oxipng) package instead. It can both optimise and encode to PNG directly from raw image data. + Encodes raw RGB image data to PNG format and resolves to an ArrayBuffer of binary data. #### data diff --git a/test/node/oxipng.test.js b/test/node/oxipng.test.js index d6bbb78..ad8b9f7 100644 --- a/test/node/oxipng.test.js +++ b/test/node/oxipng.test.js @@ -15,3 +15,18 @@ test('can successfully optimise png image', async (t) => { t.assert(optimisedImage instanceof ArrayBuffer); t.assert(optimisedImage.byteLength < testImage.byteLength); }); + +test('can successfully optimise from raw image data and output png image', async (t) => { + const optimiseWasmModule = await importWasmModule( + 'node_modules/@jsquash/oxipng/codec/pkg/squoosh_oxipng_bg.wasm', + ); + await init(optimiseWasmModule); + + const testRawImageData = new ImageData( + new Uint8ClampedArray(4 * 50 * 50), + 50, + 50, + ); + const optimisedImage = await optimise(testRawImageData); + t.assert(optimisedImage instanceof ArrayBuffer); +});