LZW as GIF, TIFF, PDF and Unix compress (.Z) use it: a streaming decoder and encoder.
moon add gaato/lzw///|
test "compress and decompress" {
let text = b"TOBEORNOTTOBEORTOBEORNOT"
let gif = @lzw.Format::gif(8)
let stream = @lzw.compress(text, gif)
assert_eq(@lzw.decompress(stream, gif, max_output=16 * 1024 * 1024), text)
let z = @lzw.Format::unix_compress()
assert_eq(@lzw.decompress(@lzw.compress(text, z), z), text)
}
///|
test "decode what compress wrote" {
// `printf 'hello hello hello' | compress -c -f`
let z = b"\x1f\x9d\x90\x68\xca\xb0\x61\xf3\x06\x44\xc0\x81\x05\x0f\x12\x04"
assert_eq(
@lzw.decompress(z, @lzw.Format::unix_compress()),
b"hello hello hello",
)
}///|
/// Decodes what `read` hands over, `None` once it has no more, passing the
/// output to `write` as it is decoded.
fn decode_all(
format : @lzw.Format,
read : () -> Bytes?,
write : (Bytes) -> Unit,
) -> Unit raise {
let decoder = @lzw.Decoder(format)
let out = FixedArray::make(64 * 1024, b'\x00')
for ;; {
let piece = read()
let end = piece is None
let mut input = piece.unwrap_or(b"")[:]
for ;; {
let status = decoder.step(input, out.mut_view(), end~)
input = input[decoder.last_consumed():]
write(Bytes::from_array(out[:decoder.last_produced()]))
match status {
Done => return
NeedMoreInput => break
NeedMoreOutput => ()
}
}
}
}
///|
/// A source that hands over `pieces` one by one, then `None`.
fn pieces_of(pieces : Array[Bytes]) -> () -> Bytes? {
let mut next = 0
() => {
if next < pieces.length() {
next += 1
Some(pieces[next - 1])
} else {
None
}
}
}
///|
test "decode a stream chunk by chunk" {
let tiff = @lzw.Format::tiff()
let stream = @lzw.compress(b"a strip of a TIFF image", tiff)
let decoded = Buffer()
let pieces = [stream[:5].to_owned(), stream[5:].to_owned()]
decode_all(tiff, pieces_of(pieces), bytes => decoded.write_bytes(bytes))
assert_eq(decoded.to_bytes(), b"a strip of a TIFF image")
}| Format | Bits packed from | Codes | Widens | Ends |
|---|---|---|---|---|
| gif(min_code_size) | least significant | clear 1 << n, end after it; n + 1 to 12 bits | when the next code passes a power of two | at the end code |
| tiff(), pdf() | most significant | clear 256, end 257; 9 to 12 bits | one code early | at the end code |
| pdf(early_change=false) | most significant | as TIFF's | as GIF's | at the end code |
| unix_compress(max_bits~, block_mode~) | least significant | clear 256 in block mode, no end; 9 to max_bits (10 to 16) | as GIF's, with padding at each width change and clear | with the input |
| Kind | When |
|---|---|
| Truncated | The input ends before the end code, or inside .Z's header. |
| Corrupt | A first code that is not a literal, a code past the table, or a .Z header that is not one. |
| TrailingData | Bytes after the end code. |
| LimitExceeded | A .Z stream wider than max_bits (resource: Memory), or more than max_output (resource: Output). |
| Unsupported | A .Z header over 16 bits or of 9 bits; .Z under EndOfMember; Flush; a GIF minimum code size outside 1 to 8 or a byte it cannot code; a .Z max_bits outside 10 to 16. |
pub struct Decoder {
// private fields
}fn step(self : Decoder, input : BytesView, output : MutArrayView[Byte], end~ : Bool) -> Status raise CodecErrorfn Decoder::step(self : Decoder, input : BytesView, output : MutArrayView[Byte], end~ : Bool) -> Status raise CodecErrorpub struct Encoder {
// private fields
}fn step(self : Encoder, input : BytesView, output : MutArrayView[Byte], action~ : EncodeAction) -> Status raise CodecErrorfn Encoder::step(self : Encoder, input : BytesView, output : MutArrayView[Byte], action~ : EncodeAction) -> Status raise CodecErrorfn decompress(input : BytesView, format : Format, max_output? : Int, allow_missing_end? : Bool) -> Bytes raise CodecErrorInstall
Download zipLZW as GIF, TIFF, PDF and Unix compress (.Z) use it: a streaming decoder and encoder.