mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-18 03:29:17 +00:00
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:
@@ -1,46 +0,0 @@
|
||||
import webp_dec, { WebPModule } from '../../../codecs/webp_dec/webp_dec';
|
||||
// Using require() so TypeScript doesn’t complain about this not being a module.
|
||||
const wasmBinaryUrl = require('../../../codecs/webp_dec/webp_dec.wasm');
|
||||
|
||||
// API exposed by wasm module. Details in the codec’s README.
|
||||
|
||||
export default class WebpDecoder {
|
||||
private emscriptenModule: Promise<WebPModule>;
|
||||
|
||||
constructor() {
|
||||
this.emscriptenModule = new Promise((resolve) => {
|
||||
const m = webp_dec({
|
||||
// Just to be safe, don’t automatically invoke any wasm functions
|
||||
noInitialRun: false,
|
||||
locateFile(url: string): string {
|
||||
// Redirect the request for the wasm binary to whatever webpack gave us.
|
||||
if (url.endsWith('.wasm')) {
|
||||
return wasmBinaryUrl;
|
||||
}
|
||||
return url;
|
||||
},
|
||||
onRuntimeInitialized() {
|
||||
// An Emscripten is a then-able that, for some reason, `then()`s itself,
|
||||
// causing an infite loop when you wrap it in a real promise. Deleting the `then`
|
||||
// prop solves this for now.
|
||||
// See: https://github.com/kripken/emscripten/blob/incoming/src/postamble.js#L129
|
||||
// TODO(surma@): File a bug with Emscripten on this.
|
||||
delete (m as any).then;
|
||||
resolve(m);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async decode(data: ArrayBuffer): Promise<ImageData> {
|
||||
const m = await this.emscriptenModule;
|
||||
const rawImage = m.decode(data);
|
||||
m.free_result();
|
||||
|
||||
return new ImageData(
|
||||
new Uint8ClampedArray(rawImage.buffer),
|
||||
rawImage.width,
|
||||
rawImage.height,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import webp_enc, { WebPModule } from '../../../codecs/webp_enc/webp_enc';
|
||||
// Using require() so TypeScript doesn’t complain about this not being a module.
|
||||
import { EncodeOptions } from './encoder';
|
||||
const wasmBinaryUrl = require('../../../codecs/webp_enc/webp_enc.wasm');
|
||||
|
||||
export default class WebPEncoder {
|
||||
private emscriptenModule: Promise<WebPModule>;
|
||||
|
||||
constructor() {
|
||||
this.emscriptenModule = new Promise((resolve) => {
|
||||
const m = webp_enc({
|
||||
// Just to be safe, don’t automatically invoke any wasm functions
|
||||
noInitialRun: false,
|
||||
locateFile(url: string): string {
|
||||
// Redirect the request for the wasm binary to whatever webpack gave us.
|
||||
if (url.endsWith('.wasm')) {
|
||||
return wasmBinaryUrl;
|
||||
}
|
||||
return url;
|
||||
},
|
||||
onRuntimeInitialized() {
|
||||
// An Emscripten is a then-able that, for some reason, `then()`s itself,
|
||||
// causing an infite loop when you wrap it in a real promise. Deleten the `then`
|
||||
// prop solves this for now.
|
||||
// See: https://github.com/kripken/emscripten/blob/incoming/src/postamble.js#L129
|
||||
// TODO(surma@): File a bug with Emscripten on this.
|
||||
delete (m as any).then;
|
||||
resolve(m);
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async encode(data: ImageData, options: EncodeOptions): Promise<ArrayBuffer> {
|
||||
const module = await this.emscriptenModule;
|
||||
|
||||
const resultView = module.encode(data.data, data.width, data.height, options);
|
||||
const result = new Uint8Array(resultView);
|
||||
module.free_result();
|
||||
|
||||
return result.buffer as ArrayBuffer;
|
||||
}
|
||||
}
|
||||
7
src/codecs/webp/decoder-meta.ts
Normal file
7
src/codecs/webp/decoder-meta.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export const name = 'WASM WebP Decoder';
|
||||
|
||||
const supportedMimeTypes = ['image/webp'];
|
||||
|
||||
export function canHandleMimeType(mimeType: string): boolean {
|
||||
return supportedMimeTypes.includes(mimeType);
|
||||
}
|
||||
@@ -1,17 +1,20 @@
|
||||
import { blobToArrayBuffer } from '../../lib/util';
|
||||
import DecoderWorker from './Decoder.worker';
|
||||
import webp_dec, { WebPModule } from '../../../codecs/webp_dec/webp_dec';
|
||||
import wasmUrl from '../../../codecs/webp_dec/webp_dec.wasm';
|
||||
import { initWasmModule } from '../util';
|
||||
|
||||
export const name = 'WASM WebP Decoder';
|
||||
export async function decode(blob: Blob): Promise<ImageData> {
|
||||
const decoder = await new DecoderWorker();
|
||||
return decoder.decode(await blobToArrayBuffer(blob));
|
||||
}
|
||||
let emscriptenModule: Promise<WebPModule>;
|
||||
|
||||
export async function isSupported(): Promise<boolean> {
|
||||
return true;
|
||||
}
|
||||
export async function decode(data: ArrayBuffer): Promise<ImageData> {
|
||||
if (!emscriptenModule) emscriptenModule = initWasmModule(webp_dec, wasmUrl);
|
||||
|
||||
const supportedMimeTypes = ['image/webp'];
|
||||
export function canHandleMimeType(mimeType: string): boolean {
|
||||
return supportedMimeTypes.includes(mimeType);
|
||||
const module = await emscriptenModule;
|
||||
const rawImage = module.decode(data);
|
||||
const result = new ImageData(
|
||||
new Uint8ClampedArray(rawImage.buffer),
|
||||
rawImage.width,
|
||||
rawImage.height,
|
||||
);
|
||||
|
||||
module.free_result();
|
||||
return result;
|
||||
}
|
||||
|
||||
72
src/codecs/webp/encoder-meta.ts
Normal file
72
src/codecs/webp/encoder-meta.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
export enum WebPImageHint {
|
||||
WEBP_HINT_DEFAULT, // default preset.
|
||||
WEBP_HINT_PICTURE, // digital picture, like portrait, inner shot
|
||||
WEBP_HINT_PHOTO, // outdoor photograph, with natural lighting
|
||||
WEBP_HINT_GRAPH, // Discrete tone image (graph, map-tile etc).
|
||||
}
|
||||
|
||||
export interface EncodeOptions {
|
||||
quality: number;
|
||||
target_size: number;
|
||||
target_PSNR: number;
|
||||
method: number;
|
||||
sns_strength: number;
|
||||
filter_strength: number;
|
||||
filter_sharpness: number;
|
||||
filter_type: number;
|
||||
partitions: number;
|
||||
segments: number;
|
||||
pass: number;
|
||||
show_compressed: number;
|
||||
preprocessing: number;
|
||||
autofilter: number;
|
||||
partition_limit: number;
|
||||
alpha_compression: number;
|
||||
alpha_filtering: number;
|
||||
alpha_quality: number;
|
||||
lossless: number;
|
||||
exact: number;
|
||||
image_hint: number;
|
||||
emulate_jpeg_size: number;
|
||||
thread_level: number;
|
||||
low_memory: number;
|
||||
near_lossless: number;
|
||||
use_delta_palette: number;
|
||||
use_sharp_yuv: number;
|
||||
}
|
||||
export interface EncoderState { type: typeof type; options: EncodeOptions; }
|
||||
|
||||
export const type = 'webp';
|
||||
export const label = 'WebP';
|
||||
export const mimeType = 'image/webp';
|
||||
export const extension = 'webp';
|
||||
// These come from struct WebPConfig in encode.h.
|
||||
export const defaultOptions: EncodeOptions = {
|
||||
quality: 75,
|
||||
target_size: 0,
|
||||
target_PSNR: 0,
|
||||
method: 4,
|
||||
sns_strength: 50,
|
||||
filter_strength: 60,
|
||||
filter_sharpness: 0,
|
||||
filter_type: 1,
|
||||
partitions: 0,
|
||||
segments: 4,
|
||||
pass: 1,
|
||||
show_compressed: 0,
|
||||
preprocessing: 0,
|
||||
autofilter: 0,
|
||||
partition_limit: 0,
|
||||
alpha_compression: 1,
|
||||
alpha_filtering: 1,
|
||||
alpha_quality: 100,
|
||||
lossless: 0,
|
||||
exact: 0,
|
||||
image_hint: 0,
|
||||
emulate_jpeg_size: 0,
|
||||
thread_level: 0,
|
||||
low_memory: 0,
|
||||
near_lossless: 100,
|
||||
use_delta_palette: 0,
|
||||
use_sharp_yuv: 0,
|
||||
};
|
||||
@@ -1,80 +1,18 @@
|
||||
import EncoderWorker from './Encoder.worker';
|
||||
import webp_enc, { WebPModule } from '../../../codecs/webp_enc/webp_enc';
|
||||
import wasmUrl from '../../../codecs/webp_enc/webp_enc.wasm';
|
||||
import { EncodeOptions } from './encoder-meta';
|
||||
import { initWasmModule } from '../util';
|
||||
|
||||
export enum WebPImageHint {
|
||||
WEBP_HINT_DEFAULT, // default preset.
|
||||
WEBP_HINT_PICTURE, // digital picture, like portrait, inner shot
|
||||
WEBP_HINT_PHOTO, // outdoor photograph, with natural lighting
|
||||
WEBP_HINT_GRAPH, // Discrete tone image (graph, map-tile etc).
|
||||
}
|
||||
|
||||
export interface EncodeOptions {
|
||||
quality: number;
|
||||
target_size: number;
|
||||
target_PSNR: number;
|
||||
method: number;
|
||||
sns_strength: number;
|
||||
filter_strength: number;
|
||||
filter_sharpness: number;
|
||||
filter_type: number;
|
||||
partitions: number;
|
||||
segments: number;
|
||||
pass: number;
|
||||
show_compressed: number;
|
||||
preprocessing: number;
|
||||
autofilter: number;
|
||||
partition_limit: number;
|
||||
alpha_compression: number;
|
||||
alpha_filtering: number;
|
||||
alpha_quality: number;
|
||||
lossless: number;
|
||||
exact: number;
|
||||
image_hint: number;
|
||||
emulate_jpeg_size: number;
|
||||
thread_level: number;
|
||||
low_memory: number;
|
||||
near_lossless: number;
|
||||
use_delta_palette: number;
|
||||
use_sharp_yuv: number;
|
||||
}
|
||||
export interface EncoderState { type: typeof type; options: EncodeOptions; }
|
||||
|
||||
export const type = 'webp';
|
||||
export const label = 'WebP';
|
||||
export const mimeType = 'image/webp';
|
||||
export const extension = 'webp';
|
||||
// These come from struct WebPConfig in encode.h.
|
||||
export const defaultOptions: EncodeOptions = {
|
||||
quality: 75,
|
||||
target_size: 0,
|
||||
target_PSNR: 0,
|
||||
method: 4,
|
||||
sns_strength: 50,
|
||||
filter_strength: 60,
|
||||
filter_sharpness: 0,
|
||||
filter_type: 1,
|
||||
partitions: 0,
|
||||
segments: 4,
|
||||
pass: 1,
|
||||
show_compressed: 0,
|
||||
preprocessing: 0,
|
||||
autofilter: 0,
|
||||
partition_limit: 0,
|
||||
alpha_compression: 1,
|
||||
alpha_filtering: 1,
|
||||
alpha_quality: 100,
|
||||
lossless: 0,
|
||||
exact: 0,
|
||||
image_hint: 0,
|
||||
emulate_jpeg_size: 0,
|
||||
thread_level: 0,
|
||||
low_memory: 0,
|
||||
near_lossless: 100,
|
||||
use_delta_palette: 0,
|
||||
use_sharp_yuv: 0,
|
||||
};
|
||||
|
||||
export async function encode(data: ImageData, options: EncodeOptions) {
|
||||
// We need to await this because it's been comlinked.
|
||||
const encoder = await new EncoderWorker();
|
||||
return encoder.encode(data, options);
|
||||
let emscriptenModule: Promise<WebPModule>;
|
||||
|
||||
export async function encode(data: ImageData, options: EncodeOptions): Promise<ArrayBuffer> {
|
||||
if (!emscriptenModule) emscriptenModule = initWasmModule(webp_enc, wasmUrl);
|
||||
|
||||
const module = await emscriptenModule;
|
||||
const resultView = module.encode(data.data, data.width, data.height, options);
|
||||
const result = new Uint8Array(resultView);
|
||||
module.free_result();
|
||||
|
||||
// wasm can’t run on SharedArrayBuffers, so we hard-cast to ArrayBuffer.
|
||||
return result.buffer as ArrayBuffer;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { h, Component } from 'preact';
|
||||
import { bind } from '../../lib/initial-util';
|
||||
import { inputFieldCheckedAsNumber, inputFieldValueAsNumber } from '../../lib/util';
|
||||
import { EncodeOptions, WebPImageHint } from './encoder';
|
||||
import { EncodeOptions, WebPImageHint } from './encoder-meta';
|
||||
import * as styles from './styles.scss';
|
||||
import '../../custom-els/RangeInput';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user