Introduction
JPEG is the most common format for photographs. It is named after the Joint Photographic Experts Group, whose 1992 standard (ITU-T T.81, ISO/IEC 10918-1) defines the compression. The compression is lossy: it divides the image into 8 by 8 blocks, keeps the detail the eye notices most and discards the rest, so files are small and the quality setting trades size against artefacts.
The standard defines the compressed data but not a complete file. The file around it
comes from JFIF, the JPEG File Interchange Format, or from Exif, which cameras use to add
the date, exposure, orientation and location. Both use the same extensions:
.jpg, .jpeg, .jpe and .jfif.
Structure
A JPEG file is a sequence of segments, each starting with a marker: the byte
FF and a code. Most markers are followed by a 2-byte big-endian length that
counts itself but not the marker. The file starts with SOI (FF D8) and ends
with EOI (FF D9).
Between them, application segments carry metadata: APP0 for JFIF and
APP1 for Exif and XMP. Quantisation tables (DQT), the frame
header (SOF0 for baseline, SOF2 for progressive) and Huffman
tables (DHT) follow. The start of scan (SOS) is followed by the
compressed image data, in which any FF byte is written as
FF 00 so it cannot be mistaken for a marker.
0SOI start of imageFF D82 bytes2APP0 JFIF or APP1 Exif metadatavaries…DQT quantisation tablesvaries…SOF0 or SOF2 size, precision and componentsvaries…DHT Huffman tablesvaries…SOS start of scan, then the compressed datavaries…EOI end of imageFF D92 bytes
Links
Software
- Adobe Photoshop (Windows, macOS)
- Adobe Lightroom (Windows, macOS, mobile)
- GIMP (Windows, macOS, Linux)
- Paint.NET (Windows)
- IrfanView (Windows)
- XnView MP (Windows, macOS, Linux)
- darktable (Windows, macOS, Linux)
- ImageMagick (Windows, macOS, Linux)
- Squoosh (web browser)
Developers
- libjpeg-turbo, the common C library for reading and writing JPEG
- MozJPEG, C encoder built on libjpeg-turbo that makes smaller files
- Pillow, Python imaging library
- sharp, Node.js image processing
Reference
- ITU-T T.81, the JPEG compression standard, hosted by the W3C
- JPEG File Interchange Format 1.02, hosted by the W3C