PackBits Compression

Description

In choosing a simple byte-oriented run-length compression scheme, we arbitrarily
chose the Apple Macintosh PackBits scheme. It has a good worst case behavior
(at most 1 extra byte for every 128 input bytes). For Macintosh users, the toolbox
utilities PackBits and UnPackBits will do the work for you, but it is easy to implement
your own routines.

A pseudo code fragment to unpack might look like this:

Loop until you get the number of unpacked bytes you are expecting:
Read the next source byte into n.
If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
Else if n is between -127 and -1 inclusive, copy the next byte -n+1
times.
Else if n is -128, noop.
Endloop

In the inverse routine, it is best to encode a 2-byte repeat run as a replicate run
except when preceded and followed by a literal run. In that case, it is best to merge
the three runs into one literal run. Always encode 3-byte repeats as replicate runs.

That is the essence of the algorithm. Here are some additional rules:

• Pack each row separately. Do not compress across row boundaries.

• The number of uncompressed bytes per row is defined to be (ImageWidth + 7)
/ 8. If the uncompressed bitmap is required to have an even number of bytes per
row, decompress into word-aligned buffers.

• If a run is larger than 128 bytes, encode the remainder of the run as one or more
additional replicate runs.

When PackBits data is decompressed, the result should be interpreted as per compression
type 1 (no compression).

This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a comment