xbdev - software development
Sunday May 19, 2024
home | contact | Support

Jpeg file structure layed out simple and easy

by bkenwright@xbdev.net

Complete listing of all the possible markers with a jpeg file.



SOI                              (0xffd8)            Start Of Image

EOI                              (0xffd9)            End Of Image


RST0 to RST7             (0xffd0 – 0xffd7)  Restart Markers (placed within the SOS block).

TEM                            (0xff01)            Temporary for arithmetic coding.



SOF0                           (0xffc0)            Start Of Frame, baseline.

SOF1                           (0xffc1)            Start Of Frame, extended sequential

SOF2                           (0xffc2)            Start Of Frame, progressive

SOF3                           (0xffc3)            Start Of Frame, lossless


DHT                            (0xffc4)            Define Huffman Table

SOF5                           (0xffc5)            Start of frame, differential sequential

SOF6                           (0xffc6)            Start of frame, differential progressive.

SOF7                           (0xffc7)            Start of frame, differential lossless.

JPG                              (0xffc8)            reserved.

SOF9                           (0xffc9)            Start of frame, extended sequential, arithmetic coding.

SOF10                         (0xffca)            Start of frame, progressive, arithmetic coding.

SOF11                         (0xffcb)            Start of frame, lossless, arithmetic coding.

DAC                            (0xffcc)            Define Arithmetic Coding Conditions.

SOF13                         (0xffcd)            Start of frame, differential sequential, arithmetic coding.

SOF14,                        (0xffce)            Start of frame, differential progressive, arithmetic coding.

SOF15,                        (0xffcf)             Start of frame, differential lossless, arithmetic coding.


SOS                             (0xffda)            Start Of Scan

DQT                            (0xffdb)            Define Quantization Table


DNL                            (0xffdc)            Define number of lines

DRI                              (0xffdd)            Define restart inverval.

DHP                            (0xffde)            Define hierarchial progression.

EXP                             (0xffdf)             Expand reference components


APP0 to APP15           (0xffe0–0xffef) Application-specific data


COM                           (0xfffe)             Comment


JPG0 to JPG13            (0xfff0-0xfffd)   Reserved

RES                             (0xff02-0xffbf)  Reserved.




Sequential Files

(90 percent of jpeg files use this format, other 9 percent progressive, and 1 percent other).


0xffd8 (SOI)


0xffe0-0xffef (APP) – Application that saves the file may embed information…e.g. adobe photoshop etc, it can be ignored.

                        (Length – variable).


0xffdb  (DQT) – Define Quantization Table.

-------------DQT Format Details----------------------


Parameter                     Size (bits)         Values              Description

DQT                            16                    0xffdb              Define quantisation table marker.

Lq                                16                    65n + 2            Quantization Table Length

Pq                                4                      0                      Quantization Table Precision

Tq                                4                      0-3                   Quantization Table Identifier

Qk                               8                      1-255               Quantization Elements


e.g. Q0, Q1, Q2… Q62, Q63.

(each quantisation table has its own marker, so you could have 2 quantization markers which one is for the Y component, and the other for the Cb and Cr components).


0xffc0 (SOF) – Start Of Frame

------------SOF Format Details----------------------

Parameter                     Size (bits)         Values              Description

SOF                             16                    0xffc0               Start Of Frame

Lf                                 16                    3Nf+8              Frame header length

P                                  8                      8                      Sample precision

Y                                 16                    0-65535           Number of lines

X                                 16                    1-65535           Samples per line

Nf                                8                      1-255               Number of image components (e.g. 3 for Y, U and V).

--------------Repeats for the number of components (e.g. Nf)-----------------

Ci                                 8                      0-255               Component identifier

Hi                                 4                      1-4                   Horizontal Sampling Factor

Vi                                 4                      1-4                   Vertical Sampling Factor

Tqi                               8                      0-3                   Quantization Table Selector.




0xffc4 (DHT) – Define Huffman Table

-------------DHT Format Details---------------------

Parameter                     Size (bits)         Values              Description

DHT                            16                    0xffc4               Define Huffman Table Marker

Lh                                16                    *                      Huffman Table Definition Length

Tc                                4                      0,1                   Table Class; 0=DC 1=AC

Th                                4                      0,1                   Huffman Table Identifier

Li                                 8                      0-255               Huffman Code Length

(repeats 16 times, for each of how many 1bit codes, how many 2 bit cods… how many 16 bit codes).

Vij                                8                      0-255               Huffman Code Value

(repeats x times… where x is how many codes was determined for Li, e.g. if there was only 4, two-bit codes and 7 three-bit codes, we would have a sequence of 11 bytes here, each byte representing the equivalent bit-code.




0xffd8 (SOS) – Start Of Scan


Parameter                     Size (bits)         Values              Description

SOS                             16                    0xffd8              Start Of Scan

Ls                                16                    2Ns + 6           Scan header length

Ns                                8                      1-4                   Number of image components

Csj                               8                      0-255               Scan Component Selector

Tdj                               4                      0-1                   DC Coding Table Selector

Taj                               4                      0-1                   AC Coding Table Selector

Ss                                8                      0                      Start of spectral selection

Se                                8                      63                    End of spectral selection

Ah                                4                      0                      Successive Approximation Bit High

Ai                                 4                      0                      Successive Approximation Bit Low


~~~~~ After the SOS Header Information follows the encoded data which is in the following format.~~~~~~~


[dc huf][value]  [ac huf][value]  [ac huf] [value]  [ac huf] [value]…..

repeats for each component.



Decoding is as follows.

Get DC Huf Value from the bits… read each one, one at a time until we have a match.  This decodes to how many bits we read next to get our actual value that we want.

After we have obtained this value we start again.

Get AC Huf value this time, so we read in the bits one at a time until we get a match in our huf codes.  This decodes to “two” parts, e.g. 00101 decodes to 0xAE, the A stands for how many zeros in our 64 element array, and the E stands for the number of bytes we should read in next to get our wanted value which will follow these zeros. (its based on run-length encoding).

We keep repeating the AC huf coding reading, until we reach a ac-huf value that decodes to 0x00 which mean the rest are zero, or we reach 64, which indicated that we have reached the end of this block.


Data is stored in blocks of 8x8 e.g. a sequence of 64 elements.  We must extract the elements from the coded data an put them into the 64 element array, and we’ll de-zigzag it and inverse cosine transform it and shift is by 128, then convert the YUV to RGB….and wow we will have an image!

I didn’t’ say it was going to be easy to decode a jpeg file.



0xffd9 (EOI)



~Note to the wise~

The Jpeg file could contain APP markers or COM markers which could effectively be ignored, just read in the header size that follows the marker id, e.g. 0xfffe, and read to its end.



Advert (Support Website)

Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.