Merge pull request #35 from GoogleChromeLabs/load-codec

Load mozjpeg codec and encode image
This commit is contained in:
Surma
2018-05-22 14:23:10 +02:00
committed by GitHub
9 changed files with 274 additions and 4 deletions

View File

@@ -1,8 +1,10 @@
import { h, Component } from 'preact';
import { bind } from '../../lib/util';
import { bind, bitmapToImageData } from '../../lib/util';
import * as style from './style.scss';
import Output from '../output';
import {MozJpegEncoder} from '../../lib/codec-wrappers/mozjpeg-enc';
type Props = {};
type State = {
@@ -28,8 +30,13 @@ export default class App extends Component<Props, State> {
const fileInput = event.target as HTMLInputElement;
if (!fileInput.files || !fileInput.files[0]) return;
// TODO: handle decode error
const img = await createImageBitmap(fileInput.files[0]);
this.setState({ img });
const bitmap = await createImageBitmap(fileInput.files[0]);
const data = await bitmapToImageData(bitmap);
const encoder = new MozJpegEncoder();
const compressedData = await encoder.encode(data);
const blob = new Blob([compressedData], {type: 'image/jpeg'});
const compressedImage = await createImageBitmap(blob);
this.setState({ img: compressedImage });
}
render({ }: Props, { img }: State) {
@@ -47,3 +54,4 @@ export default class App extends Component<Props, State> {
);
}
}