Expand description

LZW encoder and decoder.

This crate provides a Encoder and Decoder to compress and decompress LZW data. This particular implementation provides the GIF and TIFF variation, as well as the original fixed 12 bit LZW variation.

It’s fast, and use limited memory to do so: the decoder only uses the stack.

It works with any std::io::Read and std::io::Write.

Examples

Encoding GIF data

use salzweg::{
    decoder::{DecodingError, GifStyleDecoder},
    encoder::{EncodingError, GifStyleEncoder},
    Endianness,
};

let data = [0, 0, 1, 3];
let mut compressed = vec![];
let mut decompressed = vec![];

GifStyleEncoder::encode(&data[..], &mut compressed, 2).expect("Compression failed");

assert_eq!(compressed, [0x04, 0x32, 0x05]);

GifStyleDecoder::decode(&compressed[..], &mut decompressed, 2).expect("Decompression failed");

assert_eq!(decompressed, data);

Compressing a file using the TIFF variation

use salzweg::encoder::TiffStyleEncoder;
use std::{fs::File, io::BufReader};

let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
    .parent()
    .expect("Couldn't find parent folder")
    .join("test-assets/lorem_ipsum.txt");

let output_file = std::io::sink(); // Let's pretend this is a file.

let data = BufReader::new(File::open(path).expect("Couldn't open the file"));

TiffStyleEncoder::encode(data, output_file).expect("Compression failed");

Modules

Contains the implementation of fixed and variable code length decoders.
Contains the implementation of fixed and variable code length encoders.

Enums

Code size increase strategy.
The bit ordering when encoding or decoding LZW.