qoi
qoi copied to clipboard
Implementing in WebAssembly
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?
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.
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.
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.
following this totorial https://emscripten.org/docs/getting_started/Tutorial.html
I got:
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
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.
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
i think this is suitable as an exercise for the readers to handwrite wasm.