Graphics Interchange Format - Compression Example

Compression Example

The trivial example of a large image of solid color demonstrates the variable-length LZW compression used in GIF files.

------------------------+-------------------------+--------------------------------------------- CODE | PIXELS | NOTES ------------------------+-------------------------+--------------------------------------------- no. | value | length | this code | accumulated | Relations using N¡ apply only to same- N¡ | N¡+256| (bits) | N¡ | N¡(N¡+1)/2 |<-- color pixels until coding table is full. ________________________________________________________________________________________________ 0: | 100h | 9 | | | Clear code table -------+-------+ +-----------+-------------+-------------------------- 1: | FFh | | 1 | 1 | Top left pixel color chosen as the | | | | | highest index of a 256-color palette -------+-------+ +-----------+-------------+-------------------------- 2: | 102h | | 2 | 3 | -------+-------+ +-----------+-------------+-------------------------- 3: | 103h | | 3 | 6 | : : | | : : | 255: | 1FFh | | 255 | 32 640 | Last 9-bit code -------+-------+--------+-----------+-------------+-------------------------- 256: | 200h | 10 | 256 | 32 896 | : : | | : : | 767: | 3FFh | | 767 | 294 528 | Last 10-bit code -------+-------+--------+-----------+-------------+-------------------------- 768: | 400h | 11 | 768 | 295 296 | : : | | : | : | 1791: | 7FFh | | 1791 | 1 604 736 | Last 11-bit code -------+-------+--------+-----------+-------------+-------------------------- 1792: | 800h | 12 | 1792 | 1 606 528 | : : | | : | : | 3839: | FFFh | | 3839 | 7 370 880 | Code table full -------+-------+ +-----------+-------------+-------------------------- : | FFFh | | 3839 | The maximum code may repeat for more same-color pixels. : | | | | Overall data compression asymptotically approaches : | | | | 3839 x 8 / 12 = 2559-1/3 -------+-------+ +-----------+-------------+-------------------------- | 101h | | | | End of image data _____________________________________________________________________________

The code values shown are packed into bytes which are then packed into blocks of up to 255 bytes. A block of image data begins with a byte that declares the number of bytes to follow. The last block of data for an image is marked by a zero block-length byte.

Read more about this topic:  Graphics Interchange Format

Famous quotes containing the word compression:

    Do they [the publishers of Murphy] not understand that if the book is slightly obscure it is because it is a compression and that to compress it further can only make it more obscure?
    Samuel Beckett (1906–1989)

    The triumphs of peace have been in some proximity to war. Whilst the hand was still familiar with the sword-hilt, whilst the habits of the camp were still visible in the port and complexion of the gentleman, his intellectual power culminated; the compression and tension of these stern conditions is a training for the finest and softest arts, and can rarely be compensated in tranquil times, except by some analogous vigor drawn from occupations as hardy as war.
    Ralph Waldo Emerson (1803–1882)