https://github.com/recp/huff
canonical huffman coding
https://github.com/recp/huff
c canonical-huffman compression deflate huffman huffman-coding inflate jpeg png zip zlib
Last synced: about 2 months ago
JSON representation
canonical huffman coding
- Host: GitHub
- URL: https://github.com/recp/huff
- Owner: recp
- Created: 2024-12-29T20:35:14.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-07-09T22:59:54.000Z (7 months ago)
- Last Synced: 2025-10-14T05:48:45.504Z (4 months ago)
- Topics: c, canonical-huffman, compression, deflate, huffman, huffman-coding, inflate, jpeg, png, zip, zlib
- Language: C
- Homepage:
- Size: 57.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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