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

#20 in Compression

Download history 220055/week @ 2023-12-07 235022/week @ 2023-12-14 138753/week @ 2023-12-21 165702/week @ 2023-12-28 235227/week @ 2024-01-04 212709/week @ 2024-01-11 216418/week @ 2024-01-18 209858/week @ 2024-01-25 237004/week @ 2024-02-01 234353/week @ 2024-02-08 227378/week @ 2024-02-15 247548/week @ 2024-02-22 246943/week @ 2024-02-29 249535/week @ 2024-03-07 237033/week @ 2024-03-14 208264/week @ 2024-03-21

986,118 downloads per month
Used in 2,048 crates (12 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

~185KB