Skip to content

Commit

Permalink
Merge pull request #54 from jamsinclair/upgrade-to-oxipng-9
Browse files Browse the repository at this point in the history
Upgrade to Oxipng 9
  • Loading branch information
jamsinclair authored Apr 3, 2024
2 parents 1584520 + 14c3df2 commit 09496df
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 160 deletions.
7 changes: 7 additions & 0 deletions packages/oxipng/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/oxipng/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayBuffer>

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<OptimiseOptions>`
Expand Down
158 changes: 14 additions & 144 deletions packages/oxipng/codec/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/oxipng/codec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion packages/oxipng/codec/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
11 changes: 11 additions & 0 deletions packages/oxipng/codec/pkg-parallel/squoosh_oxipng.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>}
*/
Expand Down Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions packages/oxipng/codec/pkg-parallel/squoosh_oxipng.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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: '' };
}
}
Binary file modified packages/oxipng/codec/pkg-parallel/squoosh_oxipng_bg.wasm
Binary file not shown.
Loading

0 comments on commit 09496df

Please sign in to comment.