Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update oxipng to v9 [updated] #1396

Merged
merged 7 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
391 changes: 80 additions & 311 deletions codecs/oxipng/Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions codecs/oxipng/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ wasm-opt = ["-O", "--no-validation"]
crate-type = ["cdylib"]

[dependencies]
oxipng = { version = "4.0.3", default-features = false, features = ["libdeflater"] }
libdeflater = { version = "0.7.1", features = ["freestanding"] }
oxipng = { version = "9.0", default-features = false, features = ["freestanding"] }
wasm-bindgen = "0.2.73"
log = { version = "0.4.11", features = ["release_max_level_off"] }
wasm-bindgen-rayon = { version = "1.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion codecs/oxipng/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OxiPNG

- Source: <https://github.com/shssoichiro/oxipng>
- Version: v3.0.0
- Version: v9.0.0
- License: MIT
2 changes: 1 addition & 1 deletion codecs/oxipng/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -e
rm -rf pkg,{-parallel}
export CFLAGS="${CFLAGS} -DUNALIGNED_ACCESS_IS_FAST=1"
wasm-pack build -t web
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel -- -Z build-std=panic_abort,std --features=parallel
RUSTFLAGS='-C target-feature=+atomics,+bulk-memory' wasm-pack build -t web -d pkg-parallel . -- -Z build-std=panic_abort,std --features=parallel
rm pkg{,-parallel}/.gitignore
2 changes: 1 addition & 1 deletion codecs/oxipng/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oxipng",
"scripts": {
"build": "RUST_IMG=rustlang/rust@sha256:744aeea5a38f95aa7a96ec37269a65f0c6197a1cdd87d6534e12bb869141d807 ../build-rust.sh ./build.sh"
"build": "RUST_IMG=rustlang/rust@sha256:5fd16a5576c22c8fdd5d659247755999e426c04de8dcf18a41ea446c5f253309 ../build-rust.sh ./build.sh"
}
}
2 changes: 1 addition & 1 deletion codecs/oxipng/pkg-parallel/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OxiPNG

- Source: <https://github.com/shssoichiro/oxipng>
- Version: v3.0.0
- Version: v9.0.0
- License: MIT
4 changes: 3 additions & 1 deletion codecs/oxipng/pkg-parallel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
],
"module": "squoosh_oxipng.js",
"types": "squoosh_oxipng.d.ts",
"sideEffects": false
"sideEffects": [
"./snippets/*"
]
}
9 changes: 5 additions & 4 deletions codecs/oxipng/pkg-parallel/squoosh_oxipng.d.ts

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

8 changes: 5 additions & 3 deletions codecs/oxipng/pkg-parallel/squoosh_oxipng.js

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

Binary file modified codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm
Binary file not shown.
3 changes: 1 addition & 2 deletions codecs/oxipng/pkg-parallel/squoosh_oxipng_bg.wasm.d.ts

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

2 changes: 1 addition & 1 deletion codecs/oxipng/pkg/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OxiPNG

- Source: <https://github.com/shssoichiro/oxipng>
- Version: v3.0.0
- Version: v9.0.0
- License: MIT
4 changes: 3 additions & 1 deletion codecs/oxipng/pkg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@
],
"module": "squoosh_oxipng.js",
"types": "squoosh_oxipng.d.ts",
"sideEffects": false
"sideEffects": [
"./snippets/*"
]
}
8 changes: 5 additions & 3 deletions codecs/oxipng/pkg/squoosh_oxipng.d.ts

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

8 changes: 5 additions & 3 deletions codecs/oxipng/pkg/squoosh_oxipng.js

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

Binary file modified codecs/oxipng/pkg/squoosh_oxipng_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion codecs/oxipng/pkg/squoosh_oxipng_bg.wasm.d.ts

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

29 changes: 18 additions & 11 deletions codecs/oxipng/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#[cfg(feature = "parallel")]
pub use wasm_bindgen_rayon::init_thread_pool;

use oxipng::AlphaOptim;
use oxipng::{BitDepth, ColorType, Interlacing};
use wasm_bindgen::prelude::*;
use wasm_bindgen::Clamped;

#[wasm_bindgen]
pub fn optimise(data: &[u8], level: u8, interlace: bool) -> Vec<u8> {
pub fn optimise(
data: Clamped<Vec<u8>>,
width: u32,
height: u32,
level: u8,
interlace: bool,
) -> Vec<u8> {
let mut options = oxipng::Options::from_preset(level);
options.alphas.insert(AlphaOptim::Black);
options.alphas.insert(AlphaOptim::White);
options.alphas.insert(AlphaOptim::Up);
options.alphas.insert(AlphaOptim::Down);
options.alphas.insert(AlphaOptim::Left);
options.alphas.insert(AlphaOptim::Right);
options.interlace = Some(if interlace { 1 } else { 0 });
options.optimize_alpha = true;
options.interlace = Some(if interlace {
Interlacing::Adam7
} else {
Interlacing::None
});

options.deflate = oxipng::Deflaters::Libdeflater;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I just saw that Oxipng now also offers Zopfli. Coo. We should expose that at some point.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We absolutely should

oxipng::optimize_from_memory(data, &options).unwrap_throw()
let raw = oxipng::RawImage::new(width, height, ColorType::RGBA, BitDepth::Eight, data.0)
.unwrap_throw();
raw.create_optimized_png(&options).unwrap_throw()
}
2 changes: 1 addition & 1 deletion codecs/rust.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ARG RUST_IMG=rust:1.47

FROM emscripten/emsdk:2.0.8 AS wasm-tools
WORKDIR /opt/wasm-tools
RUN wget -qO- https://github.com/rustwasm/wasm-pack/releases/download/v0.9.1/wasm-pack-v0.9.1-x86_64-unknown-linux-musl.tar.gz | tar -xzf - --strip 1
RUN wget -qO- https://github.com/rustwasm/wasm-pack/releases/download/v0.12.1/wasm-pack-v0.12.1-x86_64-unknown-linux-musl.tar.gz | tar -xzf - --strip 1

FROM $RUST_IMG AS rust
ARG RUST_IMG
Expand Down
13 changes: 3 additions & 10 deletions src/features/encoders/oxiPNG/client/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { canvasEncode } from 'client/lazy-app/util/canvas';
import {
abortable,
blobToArrayBuffer,
inputFieldChecked,
} from 'client/lazy-app/util';
import { inputFieldChecked } from 'client/lazy-app/util';
import { EncodeOptions } from '../shared/meta';
import type WorkerBridge from 'client/lazy-app/worker-bridge';
import { h, Component } from 'preact';
Expand All @@ -18,9 +13,7 @@ export async function encode(
imageData: ImageData,
options: EncodeOptions,
) {
const pngBlob = await abortable(signal, canvasEncode(imageData, 'image/png'));
const pngBuffer = await abortable(signal, blobToArrayBuffer(pngBlob));
return workerBridge.oxipngEncode(signal, pngBuffer, options);
return workerBridge.oxipngEncode(signal, imageData, options);
}

type Props = {
Expand Down Expand Up @@ -56,7 +49,7 @@ export class Options extends Component<Props, {}> {
<Range
name="level"
min="0"
max="3"
max="6"
step="1"
value={options.level}
onInput={this.onChange}
Expand Down
11 changes: 8 additions & 3 deletions src/features/encoders/oxiPNG/worker/oxipngEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function initST() {
let wasmReady: ReturnType<typeof initMT | typeof initST>;

export default async function encode(
data: ArrayBuffer,
data: ImageData,
options: EncodeOptions,
): Promise<ArrayBuffer> {
if (!wasmReady) {
Expand All @@ -45,6 +45,11 @@ export default async function encode(
}

const optimise = await wasmReady;
return optimise(new Uint8Array(data), options.level, options.interlace)
.buffer;
return optimise(
data.data,
data.width,
data.height,
options.level,
options.interlace,
).buffer;
}
Loading