arithmetify
A simple implementation of arithmetic coding in Rust.
Features
- Efficient entropy coding using arithmetic coding.
- Customizable distribution models.
- Simple API for encoding and decoding sequences.
Installation
Add arithmetify to your Cargo.toml:
[dependencies]
arithmetify = "0.1.0"
Usage
Here is a simple example demonstrating how to use arithmetify to encode and decode a sequence:
use arithmetify::{ArithmeticEncoder, ArithmeticDecoder};
fn main() {
let weights = [2, 4, 6, 8];
let input = [1, 1, 2, 3, 2, 1, 0];
let mut encoder = ArithmeticEncoder::new();
for &symbol in &input {
encoder.encode_by_weights(weights, symbol);
}
let compressed = encoder.finalize();
let mut decoder = ArithmeticDecoder::new(compressed);
let mut decoded = vec![];
loop {
let symbol = decoder.decode_by_weights(weights);
decoded.push(symbol);
if symbol == 0 { break;
}
}
assert_eq!(input, decoded.as_slice());
}