semmixcolor and perception

The GIF format: introduction

GIF is one of the simplest image formats and one of the oldest still in use. This part of the section takes it apart byte by byte, using a real 65-byte file you can download and inspect in any hex editor.

History

CompuServe developed the format in 1987 as GIF87a, to send images over modems. In 1989 came GIF89a with extension blocks: transparency, delay between frames, and comments. Animated GIFs are built on them.

FeatureGIF
colorspalette of up to 256 colors, each 24 bits
compressionLZW, lossless
transparencyone palette color (binary)
interlacingoptional, 4 passes
animationmultiple images with delays (GIF89a)
byte orderlittle-endian: least significant byte first

The example file

A 4 × 2 pixel image in four colors: #c85028, white, black, #1b98e0.

pixel indices: appearance: 0 0 1 1 0 3 3 1

File: przyklad.gif (65 bytes). Enlarged it looks like this: example 4 by 2 pixel GIF image

The whole file byte by byte

OffsetBytesMeaningMore
00-0547 49 46 38 39 61"GIF89a"header
06-0704 00width 4logical screen descriptor
08-0902 00height 2
0AF1global color table, 4 colors
0B00background color: index 0
0C00pixel aspect: not given
0D-18C8 50 28 FF FF FF 00 00 00 1B 98 E0palette: 4 × 3 bytesglobal color table
19-2521 FE 09 73 65 6D 6D 69 78 2E 70 6C 00comment "semmix.pl"comments
26-2D21 F9 04 00 00 00 00 00graphic controlgraphic control
2E-372C 00 00 00 00 04 00 02 00 00image descriptorimage block
3802LZW minimum code size
39-3F05 04 12 30 13 05 00sub-block of 5 LZW bytes, terminator
403Bend of file (trailer)block order

The most interesting bytes are 04 12 30 13 05: the image's eight pixels compressed by LZW into 36 bits. Decoding them step by step is on the LZW compression in GIF and LZW decompression in GIF pages.

The file was generated by a program written for this section and checked with an independent system decoder: every pixel reads back as intended.