Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alienhoboken/node-huffman-coding
Node.js module implementing an Adaptive Huffman Coding algorithm
https://github.com/alienhoboken/node-huffman-coding
Last synced: about 1 month ago
JSON representation
Node.js module implementing an Adaptive Huffman Coding algorithm
- Host: GitHub
- URL: https://github.com/alienhoboken/node-huffman-coding
- Owner: AlienHoboken
- License: gpl-2.0
- Created: 2015-10-26T12:37:58.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-17T19:05:40.000Z (about 9 years ago)
- Last Synced: 2024-04-23T17:12:19.439Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 654 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# About
This is an Node.js module implementing the Adaptive Huffman Coding algorithm described in Sayood's Data Compression book. The code was originally written by Id Software and Tim Angus and was compiled to Javascript using Emscripten. It was then turned into a Node.js module. The base code is taken from the ioQuake3 engine and thus this project is published under the same GNU license.#Prerequisites
To use, the only prerequisite is to have node.js installed. If you wish to build then you need to have the appropriate build tools installed and setup as well as Emscripten if you wish to compile to JS.#Using
Find the module `huffman.js` in the module directory. Use huffman compress as follows:
```JavaScript
//Parameters: String to compress, length of string, offset within string to start compression at.
var huff_compress = require('./huffman.js').cwrap('compress', 'string', ['string', 'number', 'number']);
var msg = "Hello, world!";
var compressed_msg = huff_compress(msg, msg.length, 3);
console.log(msg.length);
```
Would return: "Hel[compressed text]"To avoid passing raw bytes from the module to the calling program, the module will return a string of hex codes representing the bytes of the encoded string. This string most be decoded into a buffer object. You can use the following code snippet to do this for you:
```JavaScript
/*
Convenience method which turns a space delimited string of octets into a buffer
*/
function hexStrToBuffer(hexStr) {
var hexArr = new Array();
var tokens = hexStr.split(" ");//find all the hex octets until the second null byte
var firstNullHit = false;
for(var i=0; i < tokens.length && (tokens[i] != 0 || !firstNullHit); i++) {
hexArr.push("0x" + tokens[i]);
if(tokens[i] == 0)
firstNullHit = true;
}var buffer = new Buffer(hexArr);
return buffer;
}
```#Building
To compile the C code to JS using Emscripten, please run: `make`To compile the C code to a system executbale run: `make -f makefile.c`
# Good to Know
Currently only compresison is supported, decompression is a WIP. Uses the same prebuilt frequency table as the ioQuake3 engine. This frequency table can be modified in the common.h file.