Every JPEG image is a sequence of byte pairs called markers, each beginning with FF. The structure never varies: a start marker, optional metadata segments, table definitions, the actual image data, and an end marker. Lose certain parts and the file becomes undecodable. Lose others and you only strip away extra information.
The Frame Markers SOI and EOI
Every JPEG file begins with the Start of Image marker, written as FF D8. This pair of bytes tells any decoder that a JPEG image follows. At the very end, the End of Image marker FF D9 signals that the image data is complete. These two markers form the outer shell of the file. If either is missing or corrupted, the file may not open at all. The soi eoi marker pair is the most basic requirement for a valid JPEG.
Metadata Segments JFIF and Exif
Application Markers After SOI
Right after the SOI, you often find application markers that hold metadata. The jfif vs exif distinction matters here. APP0 (FF E0) contains the JFIF header, which includes basic information like version and pixel aspect ratio. APP1 (FF E1) holds Exif metadata, such as camera model, date taken, and orientation. These segments are optional. A JPEG without them still displays normally, but you lose the embedded details. The jpeg header bytes that follow SOI are mostly optional, except for the table definitions that come later.
Table Definitions Quantization and Huffman
Two marker types define how the image data is compressed and decompressed. Losing either one makes the entire image undecodable.
- DQT (FF DB) Define Quantization Tables. These tables control how much detail is kept for each 8x8 pixel block. Without them, the decoder cannot reverse the compression.
- DHT (FF C4) Define Huffman Tables. These tables map compressed bit patterns back to actual values. If they are missing, the scan data is meaningless.
Because these tables are essential, damage to them is fatal. A corrupted DQT or DHT segment usually means the image cannot be recovered at all without reconstructing the tables from a known source.
Start of Frame (SOF) Markers
The Start of Frame marker tells the decoder the type of JPEG compression used. There are two common variants:
- SOF0 (FF C0) marks a baseline JPEG. This is the standard, widely compatible format.
- SOF2 (FF C2) marks a progressive JPEG. Progressive images load in multiple passes, first a rough version then finer detail.
The SOF marker also includes image dimensions and number of color components. If this marker is lost, the decoder cannot know the image size or color setup, making the file unreadable.
Start of Scan (SOS) and the Compressed Data
Inside the Scan Data
The SOS marker (FF DA) begins the actual compressed image data. This segment makes up the bulk of the file. After SOS, the data is a stream of Huffman-encoded bits that represent the 8x8 pixel blocks of the image. The decoder uses the quantization and Huffman tables defined earlier to reconstruct the pixels.
Inside the scan data, you may encounter Restart markers (FF D0 through FF D7). These markers allow the decoder to resynchronise if an error occurs in the data stream. If the compressed data is damaged but restart markers are present, part of the image may still decode correctly, while corrupted sections might show artifacts or missing slices. Without restart markers, a single bit error can ruin everything after it.
Critical Vs. Optional Parts What Can You Lose?
What Is Fatal and What Is Cosmetic
Understanding which parts are essential helps you assess damage:
- Critical (fatal if lost): SOI (FF D8), EOI (FF D9), DQT, DHT, SOF0/SOF2, and the SOS marker itself. Without these, the decoder cannot start, find the end, decompress, or know the image dimensions.
- Optional (cosmetic loss): APP0 (JFIF) and APP1 (Exif). Losing them removes metadata like camera settings or orientation but the image still displays. Restart markers are optional but helpful for error recovery.
If you have a corrupted JPEG, check whether the critical markers are intact. If only the Exif data is damaged, you can often view the image normally. If DQT or DHT are missing, recovery is much harder and may require specialized tools like jpeg.repair.
How to Inspect a JPEG File Yourself
Check the Markers in a Hex Editor
Open the JPEG file in a hex editor that displays bytes as hexadecimal pairs. Then work through this procedure:
- Look for the first two bytes: FF D8. That is the SOI marker.
- Scan for FF E0 (APP0) or FF E1 (APP1) to find metadata segments.
- Find FF DB for quantization tables and FF C4 for Huffman tables. Their presence is a good sign.
- Locate FF C0 (baseline) or FF C2 (progressive) to confirm the frame type.
- Look for FF DA to see where the compressed data starts.
- At the end, check for FF D9. If it is missing, the file is truncated.
If you see FF D0 through FF D7 inside the scan data, those are restart markers. Their presence can help limit damage from errors.