Format:JPEG
| Versions | JFIF 1.00-1.02 (libjpeg writes 1.01 by default); Exif 2.x; Adobe APP14 version 100 |
|---|---|
| Magic | FF D8 FF (SOI followed by the first marker, usually E0 JFIF, E1 Exif, EE Adobe or DB) |
| Extensions | .jpg, .jpeg, .jpe, .jfif, .jif |
| Endianness | big endian (segment lengths, sizes); Exif inside APP1 is TIFF (II or MM) |
| Website | https://www.w3.org/Graphics/JPEG/itu-t81.pdf |
| On this wiki | Facts · Code · Notes · Topics |
The ITU-T T.81 / ISO/IEC 10918-1 still-image format (.jpg, .jpeg, .jfif) as stored in files: a sequence of big-endian marker segments (FF xx + 16-bit length) starting with SOI FF D8, with the image wrapped in a JFIF (APP0), Exif (APP1) or Adobe (APP14) container, quantisation (DQT) and Huffman (DHT) tables, a frame header (SOFn), and one or more scans (SOS + entropy-coded data) ending with EOI FF D9.
Overview
A JPEG file is a JPEG interchange format datastream (ITU-T T.81 Annex B) inside a thin container convention: JFIF (APP0), Exif (APP1) or Adobe's APP14 marker. Everything is a marker: an FF byte, a marker code and, for all markers except SOI, EOI, RST0-7 and TEM, a big-endian 16-bit length that counts itself but not the marker. Any number of FF fill bytes may precede a marker. Almost every encoder and decoder you meet in binaries is Library:Libjpeg-turbo or the original IJG Library:Libjpeg; the details below follow the libjpeg-turbo source.
Marker codes
| Code | Name | Length? | Meaning |
|---|---|---|---|
D8 |
SOI | no | start of image, always first |
E0-EF |
APP0-APP15 | yes | application data (JFIF, Exif, ICC, Adobe, ...) |
FE |
COM | yes | comment text |
DB |
DQT | yes | quantisation table(s) |
C4 |
DHT | yes | Huffman table(s) |
CC |
DAC | yes | arithmetic-coding conditioning |
DD |
DRI | yes | restart interval in MCUs (u16) |
C0 / C1 / C2 / C3 |
SOF0-3 | yes | frame header: baseline / extended sequential / progressive / lossless, Huffman |
C5-C7, C9-CB, CD-CF |
SOF5-15 | yes | differential (hierarchical) and arithmetic-coded variants |
DA |
SOS | yes | scan header, followed by entropy-coded data |
D0-D7 |
RST0-7 | no | restart markers inside entropy-coded data, cycling 0..7 |
DC |
DNL | yes | number of lines (libjpeg rejects it: "Empty JPEG image (DNL not supported)") |
D9 |
EOI | no | end of image |
C8, F0-FD, 01 |
JPG, JPGn, TEM | reserved / JPEG extensions |
Segments
- APP0 JFIF (segment length 16 as libjpeg writes it):
"JFIF\0", version major and minor (libjpeg default 1.01), density unit (0 aspect ratio only, 1 dots per inch, 2 dots per cm), X and Y density (u16 each; default 1 and 1), thumbnail width and height (bytes), then widthheight3 RGB bytes. A second APP0"JFXX\0"holds an extension thumbnail (type 0x10 JPEG, 0x11 palette, 0x13 RGB). - APP1 Exif:
"Exif\0\0"then a complete TIFF structure (II*\0orMM\0*, IFD offsets relative to the TIFF header). XMP is a separate APP1 starting"http://ns.adobe.com/xap/1.0/\0". - APP2 ICC profile:
"ICC_PROFILE\0", a 1-based sequence number, the chunk count, then up to 65519 profile bytes per marker (65533 - 14). - APP13
"Photoshop 3.0\0"holds 8BIM image resources (IPTC). - APP14 Adobe (length 14):
"Adobe", u16 version (100), u16 flags0, u16 flags1, one byte colour transform: 0 = none (RGB or CMYK), 1 = YCbCr, 2 = YCCK. - DQT: one or more tables, each a byte
Pq<<4 | Tq(precision 0 = 8-bit, 1 = 16-bit; table id 0-3) and 64 values in zigzag order. - DHT: one or more tables, each
Tc<<4 | Th(class 0 DC, 1 AC; id 0-3), 16 counts of codes of length 1..16, then the symbols. The Annex K.3 example tables (12 DC symbols, 162 AC symbols) are what non-optimising encoders write. - SOFn: sample precision (8 or 12; 2-16 for lossless), height, width (u16 each), component count, then per component: id,
H<<4 | Vsampling factors, quantisation table id. Component ids are usually 1, 2, 3 (YCbCr); libjpeg writesR,G,B(82, 71, 66) for RGB andC,M,Y,Kfor CMYK. - SOS: component count, per component its id and
Td<<4 | Tatable ids, then Ss, Se (spectral selection) andAh<<4 | Al(successive approximation). A sequential scan is Ss 0, Se 63, Ah 0, Al 0.
Entropy-coded data
After SOS comes entropy-coded data up to the next marker. A data byte FF is stuffed as FF 00; FF D0-FF D7 are restart markers (DC predictors and the bit buffer reset there); any other FF xx ends the scan. To find the end of a scan without decoding, look for FF followed by anything but 00, FF or D0-D7.
Colour space
The format does not say what the components mean; decoders guess. libjpeg's rule (default_decompress_parms) is the de-facto standard: 1 component = grayscale; 3 components = YCbCr if there is a JFIF marker, else per the Adobe transform (0 RGB, 1 YCbCr), else YCbCr if the ids are 1,2,3, RGB if they are 'R','G','B', otherwise YCbCr; 4 components = YCCK if the Adobe transform is 2, else CMYK. See Fact:Libjpeg/behavior/Colour space guess.
Encoder fingerprints
What a file says about the program that wrote it:
- libjpeg layout: SOI, APP0 JFIF 1.01 (density 1x1, unit 0 unless the application sets it), one DQT marker per table, SOF, one DHT marker per table in the order DC0, AC0, DC1, AC1, optional DRI, SOS, EOI. Other encoders (cameras, Photoshop) often pack all tables into one DQT and one DHT.
- IJG quality: libjpeg's
jpeg_set_quality(q)scales the Annex K tables bys = q < 50 ? 5000/q : 200 - 2qasmax(1, min(255, (base*s + 50)/100)). If both tables match exactly for some q, the file was almost certainly written by libjpeg (or something copying its formula) at that quality. Quality 75 (libjpeg's default) gives a luminance table starting 8 6 5 8 12 20 26 31 (natural order) and a chrominance table starting 9 9 12 24 50 50 50 50. - Huffman tables: the Annex K.3 tables mean the encoder did not optimise them (libjpeg's default for sequential files); libjpeg's progressive files always carry optimised tables, a DHT before each scan.
- Progressive script: libjpeg's
jpeg_simple_progressionfor YCbCr writes 10 scans: DC of all components (Al 1), Y AC 1-5 (Al 2), Cr AC 1-63 (Al 1), Cb AC 1-63 (Al 1), Y AC 6-63 (Al 2), Y AC 1-63 refinement (Ah 2, Al 1), DC refinement (Ah 1), then Cr, Cb and Y AC refinements (Ah 1, Al 0).
The wiki's jpegdump prints all of this for a file.
Pitfalls
- DQT values are stored in zigzag order; compare them with natural-order tables (like libjpeg's source) only after reordering.
FF D8inside a file is not proof of a JPEG start: check for a followingFFand a valid marker. Exif thumbnails, MPF (APP2"MPF\0") secondary images and depth maps are complete JPEGs inside or after the main one, and data after EOI is common (motion photos, appended archives).- The JFIF thumbnail size fields are almost always 0; trust the APP0 length, not the fields.
- 12-bit JPEG (SOF1 with precision 12) and lossless JPEG (SOF3) need a decoder built for them; libjpeg-turbo 3.0+ has 8-, 12- and 16-bit (lossless) paths in one library.
Code & repositories
- https://github.com/libjpeg-turbo/libjpeg-turbo @
2fa570c514a4b0e70342ef8b32ce1cda21b85710· license: IJG AND BSD-3-Clause AND Zlib (declared: the detector found none; check the repository)
The official libjpeg-turbo source (main branch, 3.2.0 + 47 commits). Its history also contains the IJG releases as tags jpeg-1 to jpeg-9f, so one clone answers questions about both libraries. testimages/ holds free test JPEGs (testorig.jpg, testimgari.jpg arithmetic, testimgint.jpg, 12-bit and lossless samples). License: IJG License + BSD-3-Clause (LICENSE.md, README.ijg), SIMD code Zlib.
Tested on: Read at commit 2fa570c5 and at every release tag for the version markers; not built (no C compiler on the test machine). Behaviour checked on files written by Pillow 12.3.0, which links libjpeg-turbo v3.1.4.1. · checked 2026-10-17; last activity 2026-10-13; Note: the latest release ships binaries; the wiki links only the source, never the binaries
Evidence & references
- ITU-T T.81 (1992) | ISO/IEC 10918-1:1994, Annex B (syntax), Annex K (example tables); JFIF 1.02 (ECMA TR/98); Adobe Technical Note 5116 (APP14).
- libjpeg-turbo source at commit 2fa570c514a4b0e70342ef8b32ce1cda21b85710 (3.2.0 + 47 commits): src/jcmarker.c (marker enum lines 23-86,
emit_jfif_app0355,emit_adobe_app14390,write_frame_header500,write_scan_header563), src/jcparam.c (tables 75-94,jpeg_quality_scaling132, defaults 208-273,jpeg_simple_progression477), src/jdapimin.cdefault_decompress_parms139, src/jcicc.c 35-38. - Tested by writing 11 files with Pillow 12.3.0 (libjpeg-turbo v3.1.4.1 inside) and walking them with jpegdump: defaults, quality 30 and 90, 4:4:4, progressive, optimised, restart interval, grayscale, CMYK, RGB (
keep_rgb), DPI + comment, Exif.