qoi icon indicating copy to clipboard operation
qoi copied to clipboard

Implementing in WebAssembly

Open ThatGuySam opened this issue 3 years ago • 7 comments

This may be a dumb and obvious question but how would you implement this in WebAssembly so you can use it with things like Node Serverless Functions and Edge Workers?

ThatGuySam avatar Nov 24 '21 19:11 ThatGuySam

The simplest way would be to use Emscripten for that: https://emscripten.org/ It can compile C and C++ into WebAssembly. Given that qoi is a simple header-only library, this should be almost as easy as running a hello world for Emscripten.

It helps that no machine assembly / SIMD instructions are used in qoi, which is usually the primary pain when compiling into WebAssembly.

gkrasin avatar Nov 24 '21 20:11 gkrasin

I would assume I would have to pass in the whole image file into the "wasmed" qoi script as opposed to streaming the file.

Again, forgive my ignorance if these are simple questions.

ThatGuySam avatar Nov 24 '21 20:11 ThatGuySam

That sounds right. I recommend following this guide: https://developer.mozilla.org/en-US/docs/WebAssembly/existing_C_to_wasm

It shows how to compile libwebp into WebAssembly and get it working in the browser, so it should be very similar in the qoi case, just simpler.

gkrasin avatar Nov 24 '21 20:11 gkrasin

following this totorial https://emscripten.org/docs/getting_started/Tutorial.html

I got: image

error (looks like i neeed to read readme carefully)

Anyway, as mentioned fs related logic shold be different for wasm, it could be array of pixels as input + dimensions

lifeart avatar Nov 25 '21 17:11 lifeart

Rust supports compiling directly to WebAssembly. It should be fairly straight forward to take the Rust clone and create a JavaScript wrapper as an alternative solution.

Robaire avatar Dec 03 '21 22:12 Robaire

This is also on my bucket list. If i find the time today or tomorrow, i can try creating a "wasm polyfill" for QOI support based on the Zig impl

ikskuh avatar Dec 04 '21 07:12 ikskuh

i think this is suitable as an exercise for the readers to handwrite wasm.

farteryhr avatar Jul 05 '23 08:07 farteryhr