atomicobject / heatshrink

data compression library for embedded/real-time systems
ISC License
1.31k stars 176 forks source link

Decoder for JavaScript? #67

Closed slaff closed 3 years ago

slaff commented 3 years ago

I would like to encode with heatsink a bitmap image data on a micro-controller and display it in the browser.
Therefore my question is: is there a JavaScript implementation of the decoder?

BenBE commented 3 years ago

Currently not, but you should be able to import the decoder source with little changes using Emscripten.

silentbicycle commented 3 years ago

Most of the complexity is to account for suspending/resuming on IO. Otherwise, it's a pretty straightforward implementation of LZSS (https://en.wikipedia.org/wiki/LZSS). I have been meaning to add a one-pass, blocking reference implementation in C that could be used for porting, so I will prioritize that for the 0.5.0 release.

I'm not aware of a Javascript implementation, and I'm not proficient with JS myself, but if somebody writes one with appropriate licensing we could include it in a contrib/ directory.

slaff commented 3 years ago

Thank you all for your answers. I have found the following project: https://github.com/iotile/heatshrink-ts.git which is a TypeScript implementation of the decoding. Using tsc, an app that converts TypeScript to JavaScript, I was able to get a JavaScript version of the decoder that worked like charm. The unpolished but working JS file is attached to this post. To use it from JavaScript the following minimum code sample can be used:

<script src="heatshrink.js"></script>
<script>
    var decoder = new HeatshrinkDecoder(10, 4, encodedInput.byteLength);
    decoder.process(encodedInput);
    var data = decoder.getOutput();
</script>

In the constructor 10 and 4 should be replaced with the windowBits and lookaheadBits values used in the encoder to encode the content.

heatshrink.zip