https://en.wikipedia.org/wiki/GIF

The format supports up to 8 bits per pixel for each image, allowing a single image to reference its own palette of up to 256 different colors chosen from the 24-bit RGB color space. It also supports animations and allows a separate palette of up to 256 colors for each frame. GIF images are compressed using the Lempel–Ziv–Welch (LZW) lossless data compression technique to reduce the file size without degrading the visual quality.

History

The original version of GIF was called 87a. In 1989, CompuServe released an enhanced version, called 89a, which added support for animation delays (multiple images in a stream were already supported in 87a), transparent background colors, and storage of application-specific metadata. The two versions can be distinguished by looking at the first six bytes of the file (the “magic number” or signature), which, when interpreted as ASCII, read “GIF87a” and “GIF89a”, respectively. GIF was one of the first two image formats commonly used on Web sites, the other being the black-and-white XBM.

File format

GIF files start with a fixed-length header (“GIF87a” or “GIF89a”) giving the version, followed by a fixed-length Logical Screen Descriptor giving the pixel dimensions and other characteristics of the logical screen.

The screen descriptor may also specify the presence and size of a Global Color Table, which follows next if present.

Thereafter, the file is divided into segments, each introduced by a 1-byte sentinel: - An image (introduced by 0x2C, an ASCII comma ‘,’) - An extension block (introduced by 0x21, an ASCII exclamation point ‘!’) - The trailer (a single byte of value 0x3B, an ASCII semicolon ‘;’), which should be the last byte of the file.

An image starts with a fixed-length Image Descriptor, which may specify the presence and size of a Local Color Table (which follows next if present).

The image data follows: one byte giving the bit width of the unencoded symbols (which must be at least 2 bits wide, even for bi-color images), followed by a linked list of sub-blocks containing the LZW-encoded data.

Format

Example

Example