mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 08:47:31 +00:00
19 lines
700 B
TypeScript
19 lines
700 B
TypeScript
import { builtinDecode, sniffMimeType, canDecodeImageType } from '../lib/util';
|
|
import Processor from './processor';
|
|
|
|
export async function decodeImage(blob: Blob, processor: Processor): Promise<ImageData> {
|
|
const mimeType = await sniffMimeType(blob);
|
|
const canDecode = await canDecodeImageType(mimeType);
|
|
|
|
try {
|
|
if (!canDecode) {
|
|
if (mimeType === 'image/avif') return await processor.avifDecode(blob);
|
|
if (mimeType === 'image/webp') return await processor.webpDecode(blob);
|
|
// If it's not one of those types, fall through and try built-in decoding for a laugh.
|
|
}
|
|
return await builtinDecode(blob);
|
|
} catch (err) {
|
|
throw Error("Couldn't decode image");
|
|
}
|
|
}
|