mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-17 03:00:13 +00:00
Add support for PNG decoding (encoding still buggy)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { promises as fsp } from "fs";
|
||||
import { instantiateEmscriptenWasm } from "./emscripten-utils.js";
|
||||
import { instantiateEmscriptenWasm, pathify } from "./emscripten-utils.js";
|
||||
|
||||
// MozJPEG
|
||||
import mozEnc from "../../codecs/mozjpeg/enc/mozjpeg_enc.js";
|
||||
@@ -19,6 +19,14 @@ import avifEncWasm from "asset-url:../../codecs/avif/enc/avif_enc.wasm";
|
||||
import avifDec from "../../codecs/avif/dec/avif_dec.js";
|
||||
import avifDecWasm from "asset-url:../../codecs/avif/dec/avif_dec.wasm";
|
||||
|
||||
// PNG
|
||||
import pngEncDecInit, {
|
||||
encode as pngEncode,
|
||||
decode as pngDecode
|
||||
} from "../../codecs/png/pkg/squoosh_png.js";
|
||||
import pngEncDecWasm from "asset-url:../../codecs/png/pkg/squoosh_png_bg.wasm";
|
||||
const pngEncDecPromise = pngEncDecInit(fsp.readFile(pathify(pngEncDecWasm)));
|
||||
|
||||
// Our decoders currently rely on a `ImageData` global.
|
||||
import ImageData from "./image_data.js";
|
||||
globalThis.ImageData = ImageData;
|
||||
@@ -114,5 +122,19 @@ export default {
|
||||
min: 0,
|
||||
max: 62
|
||||
}
|
||||
},
|
||||
png: {
|
||||
name: "PNG",
|
||||
extension: "png",
|
||||
detectors: [/^\x89PNG\x0D\x0A\x1A\x0A/],
|
||||
dec: async () => {
|
||||
await pngEncDecPromise;
|
||||
return { decode: (...args) => pngDecode(...args) };
|
||||
},
|
||||
enc: async () => {
|
||||
await pngEncDecPromise;
|
||||
return { encode: (...args) => new Uint8Array(pngEncode(...args)) };
|
||||
},
|
||||
defaultEncoderOptions: {}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
export function instantiateEmscriptenWasm(factory, path) {
|
||||
export function pathify(path) {
|
||||
if (path.startsWith("file://")) {
|
||||
path = path.slice("file://".length);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
export function instantiateEmscriptenWasm(factory, path) {
|
||||
return factory({
|
||||
locateFile() {
|
||||
return path;
|
||||
return pathify(path);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
export default class ImageData {
|
||||
constructor(data, width, height) {
|
||||
this.data = data;
|
||||
// Need to manually copy the memory as wasm-bindgen does not by default
|
||||
// and by the time we get control in JS land, the memory has already
|
||||
// been corrupted.
|
||||
// FIXME: This is bad because it’s overhead that we should only need
|
||||
// to pay for Rust, not for C++.
|
||||
this.data = new Uint8ClampedArray(data);
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user