forked from external-repos/squoosh
Two workers & worker termination (#198)
* Refactoring codecs * Plugging in new processor * Fixing decorator * MozJPEG free issue * Better worker aborting, and terminate workers that aren't used for 10 seconds * Better comment * Ooops, half-typed comment * Uncommenting problematic line * Surma fixed it! * Abstracting WASM initialisation * Better comment * Don't need this. * Adding ticket * noInitalRun * Reverting MozJPEG issue demo * Making a const for worker timeout * Inline docs * Bail early rather than nesting * Addressing nits
This commit is contained in:
21
src/codecs/imagequant/processor.ts
Normal file
21
src/codecs/imagequant/processor.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import imagequant, { QuantizerModule } from '../../../codecs/imagequant/imagequant';
|
||||
import wasmUrl from '../../../codecs/imagequant/imagequant.wasm';
|
||||
import { QuantizeOptions } from './processor-meta';
|
||||
import { initWasmModule } from '../util';
|
||||
|
||||
let emscriptenModule: Promise<QuantizerModule>;
|
||||
|
||||
export async function process(data: ImageData, opts: QuantizeOptions): Promise<ImageData> {
|
||||
if (!emscriptenModule) emscriptenModule = initWasmModule(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);
|
||||
}
|
||||
Reference in New Issue
Block a user