14 releases

0.1.8 Jan 22, 2024
0.1.7 Jul 10, 2022
0.1.6 Apr 21, 2022
0.1.5 Apr 17, 2021
0.1.0-alpha Jul 27, 2020

#24 in Compression

Download history 337316/week @ 2024-07-21 345135/week @ 2024-07-28 315121/week @ 2024-08-04 344011/week @ 2024-08-11 377050/week @ 2024-08-18 310627/week @ 2024-08-25 301993/week @ 2024-09-01 299664/week @ 2024-09-08 293265/week @ 2024-09-15 313992/week @ 2024-09-22 328144/week @ 2024-09-29 316536/week @ 2024-10-06 314571/week @ 2024-10-13 302094/week @ 2024-10-20 306468/week @ 2024-10-27 313797/week @ 2024-11-03

1,258,054 downloads per month
Used in 2,125 crates (13 directly)

MIT/Apache

115KB
2K SLoC

weezl

LZW en- and decoding that goes weeeee!

Overview

This library, written in purely safe and dependency-less Rust, provides encoding and decoding for lzw compression in the style as it occurs in gif and tiff image formats. It has a standalone binary that may be used to handle those data streams but it is not compatible with Spencer's compress and uncompress binaries (though a drop-in may be developed at a later point).

Using in a no_std environment is also possible though an allocator is required. This, too, may be relaxed in a later release. A feature flag already exists but currently turns off almost all interfaces.

License

All code is dual licensed MIT OR Apache-2.0.


lib.rs:

LZW decoder and encoder

This crates provides an Encoder and a Decoder in their respective modules. The code words are written from and to bit byte slices (or streams) where it is possible to write either the most or least significant bits first. The maximum possible code size is 12 bits, the smallest available code size is 2 bits.

Example

These two code blocks show the compression and corresponding decompression. Note that you must use the same arguments to Encoder and Decoder, otherwise the decoding might fail or produce bad results.

use weezl::{BitOrder, encode::Encoder};

let data = b"Hello, world"; let compressed = Encoder::new(BitOrder::Msb, 9) .encode(data) .unwrap();


use weezl::{BitOrder, decode::Decoder};

let decompressed = Decoder::new(BitOrder::Msb, 9)
    .decode(&compressed)
    .unwrap();
assert_eq!(decompressed, data);

LZW Details

The de- and encoder expect the LZW stream to start with a clear code and end with an end code which are defined as follows:

  • CLEAR_CODE == 1 << min_code_size
  • END_CODE == CLEAR_CODE + 1

For optimal performance, all buffers and input and output slices should be as large as possible and at least 2048 bytes long. This extends to input streams which should have similarly sized buffers. This library uses Rust's standard allocation interfaces (Box and Vec to be precise). Since there are no ways to handle allocation errors it is not recommended to operate it on 16-bit targets.

Allocations and standard library

The main algorithm can be used in no_std as well, although it requires an allocator. This restriction might be lifted at a later stage. For this you should deactivate the std feature. The main interfaces stay intact but the into_stream combinator is no available.

Dependencies

~180KB