PackBits
Encyclopedia
PackBits is a fast, simple lossless compression
Lossless data compression
Lossless data compression is a class of data compression algorithms that allows the exact original data to be reconstructed from the compressed data. The term lossless is in contrast to lossy data compression, which only allows an approximation of the original data to be reconstructed, in exchange...

 scheme for run-length encoding
Run-length encoding
Run-length encoding is a very simple form of data compression in which runs of data are stored as a single data value and count, rather than as the original run...

 of data.

Apple introduced the PackBits format with the release of MacPaint
MacPaint
MacPaint was a bitmap-based graphics painting software program developed by Apple Computer and released with the original Macintosh personal computer on January 22, 1984. It was sold separately for US$195 with its word processor counterpart, MacWrite. MacPaint was notable because it could generate...

 on the Macintosh computer. This compression scheme is one of the types of compression that can be used in TIFF-files. TGA
TGA
TGA may refer to:* The Grand Arkanum, A Nerdcore Hiphop Crew coming out of Worcester, Massachusetts consisting of Danny Fantom, The Prophet, Griffen, Kid Ikarus, and DJ Big Spoon* Thermogravimetric Analysis, materials testing procedure...

-files also use this RLE compression scheme, but treats data stream as pixels instead of bytes.

A PackBits data stream consists of packets with a one-byte header followed by data. The header is a signed byte; the data can be signed, unsigned, or packed (such as MacPaint pixels).

In the following table, n is the value of the header byte as a signed integer.
Header byte Data following the header byte
0 to 127 (1 + n) literal
Literal
Literal may refer to:*Literal and figurative language, taken in a non-figurative sense*Literal translation, the close adherence to the forms of a source language text...

bytes of data
-1 to -127 One byte of data, repeated (1 – n) times in the decompressed output
-128 No operation (skip and treat next byte as a header byte)

Note that interpreting 0 as positive or negative makes no difference in the output. Runs of two bytes adjacent to non-runs are typically written as literal data. It should also be noted that there is no way based on the PackBits data to determine the end of the data stream; that is to say, one must already know the size of the compressed or uncompressed data before reading a PackBits data stream to know where it ends.

Apple Computer (see the external link) provides this short example of packed data:
FE AA 02 80 00 2A FD AA 03 80 00 2A 22 F7 AA

The following code, written in Microsoft Excel VBA, unpacks the data:

Sub UnpackBitsDemo
Dim File As Variant
Dim MyOutput As String
Dim Count As Long
Dim i As Long, j As Long

File = "FE AA 02 80 00 2A FD AA 03 80 00 2A 22 F7 AA"
File = Split(File, " ")

For i = LBound(File) To UBound(File)
Count = Application.WorksheetFunction.Hex2Dec(File(i))
Select Case Count
Case Is >= 128
Count = 256 - Count 'Two's Complement
For j = 0 To Count 'zero-based
MyOutput = MyOutput & File(i + 1) & " "
Next j
i = i + 1 'Adjust the pointer
Case Else
For j = 0 To Count 'zero-based
MyOutput = MyOutput & File(i + j + 1) & " "
Next j
i = i + j 'Adjust the pointer
End Select
Next i

Debug.Print MyOutput
'AA AA AA 80 00 2A AA AA AA AA 80 00 2A 22 AA AA AA AA AA AA AA AA AA AA'
End Sub

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK