forked from external-repos/squoosh
Add a call-out to Web Codecs API
This commit is contained in:
26
src/client/lazy-app/util/web-codecs/index.ts
Normal file
26
src/client/lazy-app/util/web-codecs/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { drawableToImageData } from 'client/lazy-app/util';
|
||||
|
||||
const hasImageDecoder = typeof ImageDecoder !== 'undefined';
|
||||
export async function isTypeSupported(mimeType: string): Promise<boolean> {
|
||||
if (!hasImageDecoder) {
|
||||
return false;
|
||||
}
|
||||
return ImageDecoder.isTypeSupported(mimeType);
|
||||
}
|
||||
export async function decode(
|
||||
blob: Blob | File,
|
||||
mimeType: string,
|
||||
): Promise<ImageData> {
|
||||
if (!hasImageDecoder) {
|
||||
throw Error(
|
||||
`This browser does not support ImageDecoder. This function should not have been called.`,
|
||||
);
|
||||
}
|
||||
const decoder = new ImageDecoder({
|
||||
type: mimeType,
|
||||
// Non-obvious way to turn an Blob into a ReadableStream
|
||||
data: new Response(blob).body!,
|
||||
});
|
||||
const result = await decoder.decode();
|
||||
return drawableToImageData(result.image);
|
||||
}
|
||||
Reference in New Issue
Block a user