Make images smaller using best-in-class codecs, right in the browser.
JavaScript
Adressing smaller review comments
Surmab15545402a14
diff --git a/src/codecs/browser-jpeg/decoder.ts b/src/codecs/browser-jpeg/decoder.tsindex 9eb56c1a..d6107bdd 100644--- a/src/codecs/browser-jpeg/decoder.ts+++ b/src/codecs/browser-jpeg/decoder.ts@@ -1,4 +1,4 @@-import { canDecode, fileToBitmap } from '../../lib/util';+import { canDecodeImage, fileToBitmap } from '../../lib/util';export const name = 'Browser JPEG Decoder';export const supportedExtensions = ['jpg', 'jpeg'];@@ -11,5 +11,5 @@ export async function decode(file: File): Promise<ImageBitmap> {const jpegFile = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAn/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AJVAA//Z';export function isSupported(): Promise<boolean> {- return canDecode(jpegFile);+ return canDecodeImage(jpegFile);}diff --git a/src/codecs/browser-png/decoder.ts b/src/codecs/browser-png/decoder.tsindex e3ecea45..357b0ab9 100644--- a/src/codecs/browser-png/decoder.ts+++ b/src/codecs/browser-png/decoder.ts@@ -1,4 +1,4 @@-import { canDecode, fileToBitmap } from '../../lib/util';+import { canDecodeImage, fileToBitmap } from '../../lib/util';export const name = 'Browser PNG Decoder';export const supportedExtensions = ['png'];@@ -11,5 +11,5 @@ export async function decode(file: File): Promise<ImageBitmap> {const pngFile = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=';export function isSupported(): Promise<boolean> {- return canDecode(pngFile);+ return canDecodeImage(pngFile);}diff --git a/src/codecs/browser-webp/decoder.ts b/src/codecs/browser-webp/decoder.tsindex 450f0d37..baa962fd 100644--- a/src/codecs/browser-webp/decoder.ts+++ b/src/codecs/browser-webp/decoder.ts@@ -1,4 +1,4 @@-import { canDecode, fileToBitmap } from '../../lib/util';+import { canDecodeImage, fileToBitmap } from '../../lib/util';export const name = 'Browser WebP Decoder';export const supportedExtensions = ['webp'];@@ -11,5 +11,5 @@ export async function decode(file: File): Promise<ImageBitmap> {const webpFile = 'data:image/webp;base64,UklGRh4AAABXRUJQVlA4TBEAAAAvAAAAAAfQ//73v/+BiOh/AAA=';export function isSupported(): Promise<boolean> {- return canDecode(webpFile);+ return canDecodeImage(webpFile);}diff --git a/src/codecs/webp/Decoder.worker.ts b/src/codecs/webp/Decoder.worker.tsindex 9975a4e7..d09fae11 100644--- a/src/codecs/webp/Decoder.worker.ts+++ b/src/codecs/webp/Decoder.worker.ts@@ -32,7 +32,7 @@ export default class WebpDecoder {},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`+ // 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.diff --git a/src/codecs/webp/decoder.ts b/src/codecs/webp/decoder.tsindex d5f89a08..685a2d7e 100644--- a/src/codecs/webp/decoder.ts+++ b/src/codecs/webp/decoder.ts@@ -11,6 +11,5 @@ export async function decode(file: File): Promise<ImageBitmap> {}export async function isSupported(): Promise<boolean> {- // TODO(@surma): Should we do wasm detection here or something?return true;}diff --git a/src/components/App/index.tsx b/src/components/App/index.tsxindex ed10c255..9cfc88a2 100644--- a/src/components/App/index.tsx+++ b/src/components/App/index.tsx@@ -184,7 +184,6 @@ export default class App extends Component<Props, State> {if (!decoder) {throw new Error('Can’t find a decoder for the given file');}- console.log(`Decoding using ${decoder.name}`);const bmp = await decoder.decode(file);// compute the corresponding ImageData once since it only changes when the file changes:const data = await bitmapToImageData(bmp);@@ -251,7 +250,7 @@ export default class App extends Component<Props, State> {loadedCounter: loadingCounter,};- this.setState({ images });+ this.setState({ images, error: '' });}render({ }: Props, { loading, error, images }: State) {diff --git a/src/lib/util.ts b/src/lib/util.tsindex d4f7cc2b..1c731b09 100644--- a/src/lib/util.ts+++ b/src/lib/util.ts@@ -96,7 +96,7 @@ export async function canvasEncode(data: ImageData, type: string, quality?: numbreturn blob;}-export function canDecode(data: string): Promise<boolean> {+export function canDecodeImage(data: string): Promise<boolean> {return new Promise((resolve) => {const img = document.createElement('img');img.src = data;