diff --git a/codecs/oxipng/src/lib.rs b/codecs/oxipng/src/lib.rs index 2f5442fa..1047a2ab 100644 --- a/codecs/oxipng/src/lib.rs +++ b/codecs/oxipng/src/lib.rs @@ -1,11 +1,18 @@ #[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) -> Vec { +pub fn optimise( + data: Clamped>, + width: u32, + height: u32, + level: u8, + interlace: bool, +) -> Vec { let mut options = oxipng::Options::from_preset(level); options.optimize_alpha = true; options.interlace = Some(if interlace { @@ -14,5 +21,7 @@ pub fn optimise(data: &[u8], level: u8, interlace: bool) -> Vec { Interlacing::None }); - 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() } diff --git a/src/features/encoders/oxiPNG/client/index.tsx b/src/features/encoders/oxiPNG/client/index.tsx index 20d73475..225a903c 100644 --- a/src/features/encoders/oxiPNG/client/index.tsx +++ b/src/features/encoders/oxiPNG/client/index.tsx @@ -18,9 +18,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 = { diff --git a/src/features/encoders/oxiPNG/worker/oxipngEncode.ts b/src/features/encoders/oxiPNG/worker/oxipngEncode.ts index 110ba7d8..f964f91d 100644 --- a/src/features/encoders/oxiPNG/worker/oxipngEncode.ts +++ b/src/features/encoders/oxiPNG/worker/oxipngEncode.ts @@ -35,7 +35,7 @@ async function initST() { let wasmReady: ReturnType; export default async function encode( - data: ArrayBuffer, + data: ImageData, options: EncodeOptions, ): Promise { if (!wasmReady) { @@ -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; }