• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// This worker listens for a message that is the blob of data that is an encoded image.
2// In principle, this worker could also load the image, but I didn't want to introduce
3// network lag in this comparison. When it has decoded the image and converted it into
4// unpremul bytes, it returns the width, height, and the pixels in an array buffer via
5// a worker message.
6self.addEventListener('message', (e) => {
7    const blob = e.data;
8    createImageBitmap(blob).then((bitmap) => {
9        const oCanvas = new OffscreenCanvas(bitmap.width, bitmap.height);
10        const ctx2d = oCanvas.getContext('2d');
11        ctx2d.drawImage(bitmap, 0, 0);
12
13        const imageData = ctx2d.getImageData(0, 0, bitmap.width, bitmap.height);
14        const arrayBuffer = imageData.data.buffer;
15        self.postMessage({
16            width: bitmap.width,
17            height: bitmap.height,
18            decodedArrayBuffer: arrayBuffer
19        }, [
20          arrayBuffer // give up ownership of this object
21        ]);
22    });
23});