Ranged Asymmetric Numeral Systems (rANS) encoder and decoder. Under the hood, this is a high-level wrapper over ryg-rans-sys.
ANS is a family of modern entropy coding methods introduced by Jarek Duda from Jagiellonian University. It serves as an alternative to arithmetic and Huffman coding, combining the performance and compression ratio of both. Many recent compression algorithms, such as Facebook’s Zstandard, Apple’s LZFSE, or JPEG XL, use ANS under the hood.
See the ryg_rans repository for more details about the underlying implementation.
Add the following to your Cargo.toml
:
toml
[dependencies]
rans = "0.2.0"
```rust use rans::bytedecoder::{ByteRansDecSymbol, ByteRansDecoder}; use rans::byteencoder::{ByteRansEncSymbol, ByteRansEncoder}; use rans::{RansDecSymbol, RansDecoder, RansEncSymbol, RansEncoder, RansEncoderMulti};
const SCALE_BITS: u32 = 2;
// Encode two symbols let mut encoder = ByteRansEncoder::new(1024); let symbol1 = ByteRansEncSymbol::new(0, 2, SCALEBITS); let symbol2 = ByteRansEncSymbol::new(2, 2, SCALEBITS);
encoder.put(&symbol1); encoder.put(&symbol2); encoder.flush();
let mut data = encoder.data().to_owned();
// Decode the encoded data let mut decoder = ByteRansDecoder::new(data); let symbol1 = ByteRansDecSymbol::new(0, 2); let symbol2 = ByteRansDecSymbol::new(2, 2);
// Please note that the data is being decoded in reverse asserteq!(decoder.get(SCALEBITS), 2); // Decoder returns cumulative frequency decoder.advance(&symbol2, SCALEBITS); asserteq!(decoder.get(SCALEBITS), 0); decoder.advance(&symbol1, SCALEBITS); ```
The project is licensed under the MIT license.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the project by you shall be licensed as MIT, without any additional terms or conditions.