Search code examples
javascriptjpegweb-workeremscripten

Decode JPEG in web worker


How can I decode a JPEG in a web worker to get access to the pixel data, without including code (written by myself or 3rd party) to decode the JPEG, but using a built-in browser API.


I understand that I can render a JPEG to a canvas in the main thread, read off the pixel data, and pass it to the worker if that's where I want it, but I am specifically investigating the possibilities of decoding the JPEG in the worker.

I also understand that I can use a third party library, but I am wondering if there is anything built into browsers that can do this, since it seems strange to have to send code that duplicates browser ability.


Solution

  • tl;dr: No.

    After your edit, the question whether you can use built-in API (which is only canvas at this moment) the answer is no. You cannot create HTMLElements, including canvas in web workers because they are GUI components and most GUI frameworks refuse to allow GUI component instantiation outside GUI thread.

    This is of course quite unfortunate, it means there's no way to use native API (CanvasRenderingContext2D) to do any image operations at all.

    Your question as I answer it now was already asked and answered: Web Workers and Canvas

    I would suggest favoriting that question for updates - I hope as well that in future image processing will be possible in web workers.