Crates.io | arithmetify |
lib.rs | arithmetify |
version | 0.1.3 |
source | src |
created_at | 2024-09-04 19:17:23.690014 |
updated_at | 2024-09-05 20:56:18.696725 |
description | A simple implementation of arithmetic coding |
homepage | |
repository | https://github.com/khoda81/arithmetify |
max_upload_size | |
id | 1363673 |
size | 34,403 |
A simple implementation of arithmetic coding in Rust.
Add arithmetify
to your Cargo.toml
:
[dependencies]
arithmetify = "0.1.0"
Here is a simple example demonstrating how to use arithmetify
to encode and decode a sequence:
use arithmetify::{ArithmeticEncoder, ArithmeticDecoder};
fn main() {
// Define symbol weights representing their probabilities.
let weights = [2, 4, 6, 8];
// The input sequence to be encoded.
let input = [1, 1, 2, 3, 2, 1, 0];
// Initialize the arithmetic encoder.
let mut encoder = ArithmeticEncoder::new();
// Encode each symbol in the input sequence using the defined weights.
for &symbol in &input {
encoder.encode_by_weights(weights, symbol);
}
// Finalize the encoding process and get the compressed data.
let compressed = encoder.finalize();
// Initialize the arithmetic decoder with the compressed data.
let mut decoder = ArithmeticDecoder::new(compressed);
// Decode the compressed data back into a sequence.
let mut decoded = vec![];
loop {
let symbol = decoder.decode_by_weights(weights);
decoded.push(symbol);
if symbol == 0 { // Assuming 0 as a termination symbol for this example.
break;
}
}
// Verify that the decoded sequence matches the original input.
assert_eq!(input, decoded.as_slice());
}