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

#27 in Compression

Download history 340231/week @ 2024-08-13 366390/week @ 2024-08-20 292532/week @ 2024-08-27 317787/week @ 2024-09-03 294738/week @ 2024-09-10 293463/week @ 2024-09-17 317292/week @ 2024-09-24 333614/week @ 2024-10-01 302594/week @ 2024-10-08 323925/week @ 2024-10-15 300293/week @ 2024-10-22 310522/week @ 2024-10-29 306113/week @ 2024-11-05 326937/week @ 2024-11-12 331800/week @ 2024-11-19 228882/week @ 2024-11-26

1,248,422 downloads per month
Used in 2,152 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