semmixcolor and perception

Elements of image description: introduction

An image file is more than a list of pixel colors. To reproduce the picture, a program has to know how many pixels there are, what shape they are, how colors are stored, which are transparent, in what order the rows come and how it was all compressed. This part of the section covers these elements one by one.

Raster images

A raster image is a rectangular grid of points, pixels. Each pixel has one color. Image size is given in pixels, e.g. 1920 × 1080. A vector image (SVG, a PDF with graphics) describes shapes instead and can be enlarged without losing quality. This section is about raster images.

What a file describes

ElementQuestionMore
dimensionshow many pixels across and down
pixel aspectwhether a pixel is squarepixel aspect ratio
palettewhether colors are stored directly or as indices into a color tablecolor palette
color depthhow many bits per pixel or per componentcolor resolution
gamma and profilehow values relate to light and to colorsgamma correction
transparencywhich pixels let the background through, and how muchtransparency
row orderwhether rows come in sequence or interlacedinterlacing
compressionhow the data was reducedLZW algorithm

How much data an image takes

Uncompressed, an image takes width × height × bits per pixel:

1920 × 1080 pixels, 24 bits (full RGB color): 1920 · 1080 · 3 bytes = 6,220,800 bytes ≈ 5.9 MB the same image with a 256-color palette (8 bits per pixel): 1920 · 1080 · 1 byte + 256 · 3 bytes of palette ≈ 2.0 MB

Compression reduces that further. Lossless compression (LZW in GIF, Deflate in PNG) restores the data bit for bit. Lossy compression (JPEG, WebP, AVIF) removes detail the eye will not notice and can shrink a photo by a factor of tens.

Formats and their choices

FormatColorsTransparencyCompression
GIFpalette of up to 256 colorsone transparent colorLZW, lossless
PNGpalette or full color up to 16 bits per componentalpha channelDeflate, lossless
JPEGfull color, 8 bits per componentnoneDCT, lossy
WebP, AVIFfull color, AVIF also 10-12 bitsalpha channellossy or lossless

The GIF format is described here in the most detail, byte by byte, in The GIF format step by step. It is simple, well documented and contains nearly every element in the table above.