GIF: Lossless and Paletted

CompuServe created the GIF format in the 1980s to provide a way of exchanging images at this early Internet service. The original version was 87a (GIF87a), and an improved version is 89a (GIF89a). If JPEG is optimized for continuous color images, GIF is optimized for what is known as "flat color" graphics, graphics that have sharp rather than continuous edges between changing colors.

GIF is an uncomplicated 8-bit format, supporting 256 colors. Additionally, to further save storage space, GIFs are based on an optimized palette, which maps each color in the palette to a specific RGB value.

Tip

There actually is a way to support 24-bit truecolor GIFs, but such a practice is purely a hack, and with truecolor supported in JPEG and PNG, unnecessary for web graphics.

The 256 colors supported with GIFs is an optimal number of colors for using a color management technique known as palettes. With palettes, rather than each pixel having its own embedded RGB color, which takes 24 bits, it contains an 8-bit lookup value that is then used with a color lookup table to find the actual color. A version of PNG—8-bit PNG—also supports a palette.

The GIF format utilizes a lossless compression technique known as LZW lossless data compression. This type of compression works by finding a sequence of characters that form a pattern and then entering such in a table, where it's mapped to a given, shorter length code. Then, whenever the pattern repeats, the shorter code is used. As such, ...

Get Painting the Web now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.