lzw

Imports

Imports #

"bufio"
"errors"
"fmt"
"io"
"bufio"
"errors"
"fmt"
"io"

Constants & Variables

LSB const #

LSB means Least Significant Bits first, as used in the GIF file format.

const LSB Order = iota

MSB const #

MSB means Most Significant Bits first, as used in the TIFF and PDF file formats.

const MSB

decoderInvalidCode const #

const decoderInvalidCode = 0xffff

errClosed var #

var errClosed = *ast.CallExpr

errOutOfCodes var #

errOutOfCodes is an internal error that means that the writer has run out of unused codes and a clear code needs to be sent next.

var errOutOfCodes = *ast.CallExpr

flushBuffer const #

const flushBuffer = *ast.BinaryExpr

invalidCode const #

const invalidCode = *ast.BinaryExpr

invalidEntry const #

A hash table entry is a uint32. Zero is an invalid entry since the lower 12 bits of a valid entry must be a non-literal code.

const invalidEntry = 0

maxCode const #

A code is a 12 bit value, stored as a uint32 when encoding to avoid type conversions when shifting bits.

const maxCode = *ast.BinaryExpr

maxWidth const #

const maxWidth = 12

tableMask const #

const tableMask = *ast.BinaryExpr

tableSize const #

There are 1<<12 possible codes, which is an upper bound on the number of valid hash table entries at any given point in time. tableSize is 4x that.

const tableSize = *ast.BinaryExpr

Type Aliases

Order type #

Order specifies the bit ordering in an LZW data stream.

type Order int

Interfaces

writer interface #

A writer is a buffered, flushable writer.

type writer interface {
io.ByteWriter
Flush() error
}

Structs

Reader struct #

Reader is an io.Reader which can be used to read compressed data in the LZW format.

type Reader struct {
r io.ByteReader
bits uint32
nBits uint
width uint
read func(*Reader) (uint16, error)
litWidth int
err error
clear uint16
eof uint16
hi uint16
overflow uint16
last uint16
suffix [*ast.BinaryExpr]uint8
prefix [*ast.BinaryExpr]uint16
output [*ast.BinaryExpr]byte
o int
toRead []byte
}

Writer struct #

Writer is an LZW compressor. It writes the compressed form of the data to an underlying writer (see [NewWriter]).

type Writer struct {
w writer
litWidth uint
order Order
write func(*Writer, uint32) error
nBits uint
width uint
bits uint32
hi uint32
overflow uint32
savedCode uint32
err error
table [tableSize]uint32
}

Functions

Close method #

Close closes the [Writer], flushing any pending output. It does not close w's underlying writer.

func (w *Writer) Close() error

Close method #

Close closes the [Reader] and returns an error for any future read operation. It does not close the underlying [io.Reader].

func (r *Reader) Close() error

NewReader function #

NewReader creates a new [io.ReadCloser]. Reads from the returned [io.ReadCloser] read and decompress data from r. If r does not also implement [io.ByteReader], the decompressor may read more data than necessary from r. It is the caller's responsibility to call Close on the ReadCloser when finished reading. The number of bits to use for literal codes, litWidth, must be in the range [2,8] and is typically 8. It must equal the litWidth used during compression. It is guaranteed that the underlying type of the returned [io.ReadCloser] is a *[Reader].

func NewReader(r io.Reader, order Order, litWidth int) io.ReadCloser

NewWriter function #

NewWriter creates a new [io.WriteCloser]. Writes to the returned [io.WriteCloser] are compressed and written to w. It is the caller's responsibility to call Close on the WriteCloser when finished writing. The number of bits to use for literal codes, litWidth, must be in the range [2,8] and is typically 8. Input bytes must be less than 1<

func NewWriter(w io.Writer, order Order, litWidth int) io.WriteCloser

Read method #

Read implements io.Reader, reading uncompressed bytes from its underlying [Reader].

func (r *Reader) Read(b []byte) (int, error)

Reset method #

Reset clears the [Writer]'s state and allows it to be reused again as a new [Writer].

func (w *Writer) Reset(dst io.Writer, order Order, litWidth int)

Reset method #

Reset clears the [Reader]'s state and allows it to be reused again as a new [Reader].

func (r *Reader) Reset(src io.Reader, order Order, litWidth int)

Write method #

Write writes a compressed representation of p to w's underlying writer.

func (w *Writer) Write(p []byte) (n int, err error)

decode method #

decode decompresses bytes from r and leaves them in d.toRead. read specifies how to decode bytes into codes. litWidth is the width in bits of literal codes.

func (r *Reader) decode()

incHi method #

incHi increments e.hi and checks for both overflow and running out of unused codes. In the latter case, incHi sends a clear code, resets the writer state and returns errOutOfCodes.

func (w *Writer) incHi() error

init method #

func (r *Reader) init(src io.Reader, order Order, litWidth int)

init method #

func (w *Writer) init(dst io.Writer, order Order, litWidth int)

newReader function #

func newReader(src io.Reader, order Order, litWidth int) *Reader

newWriter function #

func newWriter(dst io.Writer, order Order, litWidth int) *Writer

readLSB method #

readLSB returns the next code for "Least Significant Bits first" data.

func (r *Reader) readLSB() (uint16, error)

readMSB method #

readMSB returns the next code for "Most Significant Bits first" data.

func (r *Reader) readMSB() (uint16, error)

writeLSB method #

writeLSB writes the code c for "Least Significant Bits first" data.

func (w *Writer) writeLSB(c uint32) error

writeMSB method #

writeMSB writes the code c for "Most Significant Bits first" data.

func (w *Writer) writeMSB(c uint32) error

Generated with Arrow