Processors and encoding

This commit is contained in:
Jake Archibald
2020-11-08 13:25:14 +00:00
parent 6dbb182f7b
commit b1a639c182
2 changed files with 17 additions and 36 deletions

1
.gitignore vendored
View File

@@ -10,3 +10,4 @@ src/features-worker/index.ts
src/features-worker/tsconfig.json src/features-worker/tsconfig.json
src/client/lazy-app/worker-bridge/meta.ts src/client/lazy-app/worker-bridge/meta.ts
src/client/lazy-app/feature-meta/index.ts src/client/lazy-app/feature-meta/index.ts
src/client/tsconfig.json

View File

@@ -16,6 +16,7 @@ import {
PreprocessorState, PreprocessorState,
ProcessorState, ProcessorState,
EncoderState, EncoderState,
encoderMap,
} from '../feature-meta'; } from '../feature-meta';
import Output from '../Output'; import Output from '../Output';
import Options from '../Options'; import Options from '../Options';
@@ -129,59 +130,38 @@ async function processImage(
if (processorState.resize.enabled) { if (processorState.resize.enabled) {
result = await resize(signal, source, processorState.resize, workerBridge); result = await resize(signal, source, processorState.resize, workerBridge);
} }
if (processorState.quantizer.enabled) { if (processorState.quantize.enabled) {
result = await workerBridge.imageQuant( result = await workerBridge.quantize(
signal, signal,
result, result,
processorState.quantizer, processorState.quantize,
); );
} }
return result; return result;
} }
async function compressImage( async function compressImage(
signal: AbortSignal,
image: ImageData, image: ImageData,
encodeData: EncoderState, encodeData: EncoderState,
sourceFilename: string, sourceFilename: string,
processor: Processor, workerBridge: WorkerBridge,
): Promise<File> { ): Promise<File> {
const compressedData = await (() => { assertSignal(signal);
switch (encodeData.type) {
case oxiPNG.type:
return processor.oxiPngEncode(image, encodeData.options);
case mozJPEG.type:
return processor.mozjpegEncode(image, encodeData.options);
case webP.type:
return processor.webpEncode(image, encodeData.options);
case avif.type:
return processor.avifEncode(image, encodeData.options);
case browserPNG.type:
return processor.browserPngEncode(image);
case browserJPEG.type:
return processor.browserJpegEncode(image, encodeData.options);
case browserWebP.type:
return processor.browserWebpEncode(image, encodeData.options);
case browserGIF.type:
return processor.browserGifEncode(image);
case browserTIFF.type:
return processor.browserTiffEncode(image);
case browserJP2.type:
return processor.browserJp2Encode(image);
case browserBMP.type:
return processor.browserBmpEncode(image);
case browserPDF.type:
return processor.browserPdfEncode(image);
default:
throw Error(`Unexpected encoder ${JSON.stringify(encodeData)}`);
}
})();
const encoder = encoderMap[encodeData.type]; const encoder = encoderMap[encodeData.type];
// The type of encodeData.options is enforced via the previous line
const compressedData = await encoder.encode(
signal,
workerBridge,
image,
encodeData.options as any,
);
return new File( return new File(
[compressedData], [compressedData],
sourceFilename.replace(/.[^.]*$/, `.${encoder.extension}`), sourceFilename.replace(/.[^.]*$/, `.${encoder.meta.extension}`),
{ type: encoder.mimeType }, { type: encoder.meta.mimeType },
); );
} }