Package lzw :: Class BitPacker
[frames] | no frames]

Class BitPacker

source code

object --+
         |
        BitPacker

Translates a stream of lzw codepoints into a variable width packed stream of bytes, for use by BitUnpacker. One of a (potential) set of encoders for a stream of LZW codepoints, intended to behave as closely to the TIFF variable-width encoding scheme as closely as possible.

The inbound stream of integer lzw codepoints are packed into variable width bit fields, starting at the smallest number of bits it can and then increasing the bit width as it anticipates the LZW code size growing to overflow.

This class knows all kinds of intimate things about how it's upstream codepoint processors work; it knows the control codes CLEAR_CODE and END_OF_INFO_CODE, and (more intimately still), it makes assumptions about the rate of growth of it's consumer's codebook. This is ok, as long as the underlying encoder/decoders don't know any intimate details about their BitPackers/Unpackers

Instance Methods
 
__init__(self, initial_code_size)
Takes an initial code book size (that is, the count of known codes at the beginning of encoding, or after a clear)
source code
 
pack(self, codepoints)
Given an iterator of integer codepoints, returns an iterator over bytes containing the codepoints packed into varying lengths, with bit width growing to accomodate an input code that it assumes will grow by one entry per codepoint seen.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, initial_code_size)
(Constructor)

source code 

Takes an initial code book size (that is, the count of known codes at the beginning of encoding, or after a clear)

Overrides: object.__init__

pack(self, codepoints)

source code 

Given an iterator of integer codepoints, returns an iterator over bytes containing the codepoints packed into varying lengths, with bit width growing to accomodate an input code that it assumes will grow by one entry per codepoint seen.

Widths will be reset to the given initial_code_size when the LZW CLEAR_CODE or END_OF_INFO_CODE code appears in the input, and bytes following END_OF_INFO_CODE will be aligned to the next byte boundary.

>>> import lzw
>>> pkr = lzw.BitPacker(258)
>>> [ b for b in pkr.pack([ 1, 257]) ] == [ chr(0), chr(0xC0), chr(0x40) ]
True