Code Monkey home page Code Monkey logo

rans-rs's Introduction

rans-rs

Rust Build Status crates.io Documentation MIT licensed codecov

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.

Usage

Add the following to your Cargo.toml:

[dependencies]
rans = "0.4.0"

Examples

use rans::byte_decoder::{ByteRansDecSymbol, ByteRansDecoder};
use rans::byte_encoder::{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, SCALE_BITS);
let symbol2 = ByteRansEncSymbol::new(2, 2, SCALE_BITS);

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
assert_eq!(decoder.get(SCALE_BITS), 2); // Decoder returns cumulative frequency
decoder.advance(&symbol2, SCALE_BITS);
assert_eq!(decoder.get(SCALE_BITS), 0);
decoder.advance(&symbol1, SCALE_BITS);

License

The project is licensed under the MIT license.

Contribution

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.

Developing

pre-commit

We encourage contributors to use predefined pre-commit hooks --- to install them in your local repo, make sure you have pre-commit installed and run

pre-commit install

rans-rs's People

Contributors

dependabot[bot] avatar m4tx avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.