forked from external-repos/squoosh
* Port resize to wasm * Expose resize algorithms * Lanczos3 working! * lol copy paste * Adding support for other resizers * Don’t track generated README * Cache wasm instance
22 lines
840 B
TypeScript
22 lines
840 B
TypeScript
import imagequant, { QuantizerModule } from '../../../codecs/imagequant/imagequant';
|
|
import wasmUrl from '../../../codecs/imagequant/imagequant.wasm';
|
|
import { QuantizeOptions } from './processor-meta';
|
|
import { initEmscriptenModule } from '../util';
|
|
|
|
let emscriptenModule: Promise<QuantizerModule>;
|
|
|
|
export async function process(data: ImageData, opts: QuantizeOptions): Promise<ImageData> {
|
|
if (!emscriptenModule) emscriptenModule = initEmscriptenModule(imagequant, wasmUrl);
|
|
|
|
const module = await emscriptenModule;
|
|
|
|
const result = opts.zx ?
|
|
module.zx_quantize(data.data, data.width, data.height, opts.dither)
|
|
:
|
|
module.quantize(data.data, data.width, data.height, opts.maxNumColors, opts.dither);
|
|
|
|
module.free_result();
|
|
|
|
return new ImageData(new Uint8ClampedArray(result.buffer), result.width, result.height);
|
|
}
|