# README
ExifReader
JPEG (Joint Photographic Experts Group) is a widely used image format that uses lossy compression to reduce the size of images. The file format is constructed with a well-defined structure of segments that contain image data, metadata, and other information. Understanding the structure of a JPEG file helps in working with image data, metadata (like EXIF), and how compression is applied.
Here’s a breakdown of how a JPEG file is constructed:
- Overall Structure of a JPEG File
JPEG files consist of a sequence of segments, each identified by a marker. These segments contain different types of data, such as image metadata, color information, quantization tables, and the actual compressed image data.
The basic structure of a JPEG file looks like this:
[ SOI ] -> [ APPn ] -> [ DQT ] -> [ SOFn ] -> [ DHT ] -> [ SOS ] -> [ Image Data ] -> [ EOI ]
• SOI: Start of Image (always the first marker, 0xFFD8)
• APPn: Application-specific segments (APP0, APP1, etc.), often contain metadata like EXIF
• DQT: Define Quantization Table (defines compression quality)
• SOFn: Start of Frame (defines image dimensions, color space, etc.)
• DHT: Define Huffman Table (used in entropy coding)
• SOS: Start of Scan (marks the beginning of the actual image data)
• Image Data: The compressed image data
• EOI: End of Image (always the last marker, 0xFFD9)
Now, let’s look at each segment in detail.
- JPEG File Segments
Each segment begins with a marker, which is a two-byte value that tells the decoder what type of data follows. The general format of each segment is:
Marker (2 bytes) | Length (2 bytes) | Data (variable length)
• Marker: Identifies the type of segment (e.g., SOI, APPn, etc.). The first byte is always 0xFF, and the second byte identifies the segment type.
• Length: A 2-byte value that specifies the length of the data following the marker (excluding the marker itself).
• Data: Contains the actual information relevant to that segment (e.g., image metadata, compressed image data, etc.).
3. JPEG Markers
Here are some important JPEG markers and their roles:
Marker Hex Code Description SOI 0xFFD8 Start of Image (always the first marker) APPn 0xFFE0 to 0xFFEF Application segments (e.g., EXIF is stored in APP1, 0xFFE1) DQT 0xFFDB Define Quantization Table SOF0 0xFFC0 Start of Frame (Baseline DCT, non-progressive) SOF2 0xFFC2 Start of Frame (Progressive DCT) DHT 0xFFC4 Define Huffman Table SOS 0xFFDA Start of Scan (beginning of compressed image data) EOI 0xFFD9 End of Image (always the last marker)
- Key Segments in Detail
a. SOI (Start of Image) – 0xFFD8
• Purpose: Marks the beginning of the JPEG file. No length or data is associated with this marker.
• Structure:
FFD8
b. APPn (Application Segments)
• Purpose: Stores metadata, such as JFIF, EXIF, or other application-specific data.
• Examples:
• APP0 (0xFFE0): JFIF (JPEG File Interchange Format) segment, which describes the version of JPEG used.
• APP1 (0xFFE1): EXIF (Exchangeable Image File Format) metadata. This contains camera settings, date and time, and possibly GPS information.
Structure:
FFE1 [Length] "Exif\0\0" [EXIF Data]
c. DQT (Define Quantization Table) – 0xFFDB
• Purpose: Contains the quantization tables that control the level of compression for different parts of the image. Quantization tables specify how much precision is preserved for each color component (e.g., luminance, chrominance) in the image.
Structure:
FFDB [Length] [Quantization Table Data]
d. SOF (Start of Frame) – 0xFFC0 to 0xFFC2
• Purpose: Contains important information about the image, such as:
• Image width and height
• Number of color components (e.g., 3 for RGB)
• The number of bits per color component
Example (SOF0 for baseline DCT):
FFC0 [Length] [Image Data] [Width] [Height] [Bits per Sample] ...
e. DHT (Define Huffman Table) – 0xFFC4
• Purpose: Contains the Huffman tables used for entropy coding. Huffman coding is a lossless compression technique used in JPEG to further compress the image data after quantization.
Structure:
FFC4 [Length] [Huffman Table Data]
f. SOS (Start of Scan) – 0xFFDA
• Purpose: Marks the start of the actual compressed image data. It indicates which components are included in the scan (e.g., luminance and chrominance), as well as the Huffman tables to use.
Structure:
FFDA [Length] [Compressed Image Data]
g. EOI (End of Image) – 0xFFD9
• Purpose: Marks the end of the JPEG file.
Structure:
FFD9
- How Compression Works in JPEG
JPEG uses Discrete Cosine Transform (DCT), quantization, and entropy coding (Huffman coding) to compress image data.
• DCT (Discrete Cosine Transform):
• The image is divided into 8x8 pixel blocks.
• Each block is transformed using DCT into frequency domain coefficients.
• The result is that most of the image data is concentrated in a small number of coefficients (lower frequencies).
• Quantization:
• After the DCT, the frequency coefficients are quantized (reduced in precision) to reduce the amount of data.
• The level of quantization determines the amount of compression and the quality of the image.
• A quantization table (stored in the DQT segment) defines how much precision to retain for each frequency.
• Entropy Coding (Huffman Coding):
• After quantization, the remaining data is compressed using Huffman coding, a lossless compression technique.
• Huffman tables (stored in the DHT segment) define the mapping from symbols (pixel values) to variable-length codes.
Example of a Simple JPEG Structure:
FFD8 // SOI (Start of Image) FFE0 0010 4A 46 49 46 // APP0 (JFIF header) FFD9 // EOI (End of Image)
In this case:
• The file starts with FFD8, marking it as a JPEG file.
• An APP0 segment follows, containing the JFIF header (which includes the length and version info).
• Finally, the file ends with FFD9, indicating the end of the image data.
6. Putting It All Together
A typical JPEG file might look like this:
[ SOI ] -> [ APP0 (JFIF) ] -> [ APP1 (EXIF) ] -> [ DQT ] -> [ SOF ] -> [ DHT ] -> [ SOS ] -> [ Compressed Image Data ] -> [ EOI ]
Each marker tells the decoder what kind of data is coming next and how to interpret it. The image data itself is highly compressed using DCT and Huffman encoding techniques.
Conclusion
JPEG files are composed of various segments, each with a specific purpose, such as storing metadata (e.g., EXIF), compression tables (DQT, DHT), and the actual image data (SOS). By following these markers and understanding the segments, we can parse, modify, or compress/decompress JPEG images.