An open API service indexing awesome lists of open source software.

https://github.com/recp/huff

canonical huffman coding
https://github.com/recp/huff

canonical-huffman compression deflate huffman huffman-coding png

Last synced: 4 months ago
JSON representation

canonical huffman coding

Awesome Lists containing this project

README

        

# 🗜️ Huffman Coding (In Progress)

This library is designed to make **Huffman coding** easy to use, while providing
optimized and reusable utilities. It aims to simplify the integration of Huffman
coding into projects, improve maintainability, and ensure robust testing.

Huffman coding is a cornerstone of many compression algorithms and formats, widely used in:
- 🖼️ Images: JPEG, PNG, WebP, GIF.
- 🎥 Video: MPEG, H.264, HEVC (H.265).
- 🎵 Audio: MP3, AAC, Opus.
- 📦 Compression: ZIP, DEFLATE, Brotli.
- 🔗 Networking: HTTP/2, HTTP/3 (QUIC) header compression.

This library seeks to serve as a shared foundation for these and other
applications, offering standardized, high-performance tools for encoding and
decoding Huffman streams.

🚨 Don't use this in production until tests are ready

## 🔧 Usage

### Initializing a Huffman Table
The library provides two initialization functions:
- `huff_init_lsb()` for LSB-first bitstreams.
- `huff_init_msb()` for MSB-first bitstreams.

```c
// Example: Initializing a Huffman table for LSB-first bitstreams (e.g., DEFLATE)
huff_table_t table;
uint8_t lengths[] = {3, 3, 3, 3}; // Bit lengths for each symbol
uint16_t symbols[] = {0, 1, 2, 3}; // Corresponding symbols
huff_init_lsb(&table, lengths, symbols, 4);
```

## Decoding a Symbol

```c
// LSB-first decoding
bitstream_t bitstream = 0b10110010; // Example LSB-first bitstream
uint8_t bit_length = 8; // Number of valid bits
uint8_t used_bits;
uint_fast16_t symbol = huff_decode_lsb(&table, bitstream, bit_length, &used_bits);
```

## TODO

- [x] lsb
- [ ] sub tables?
- [ ] msb
- [ ] tests
- [ ] build
- [ ] documentation