I 'd better take a look at my blog <Simple Analysis of JPEG file formats> Article
JPEG files are divided into two parts: tag and compressed data.
The code stream formats are: soi, app0info, dqtinfo, driinfo, sofinfo, dhtinfo, sosinfo, and EOI. The specific structure is as follows:
# Define byte unsigned char
# Define word unsigned short int
# Define SOI 0xffd8 // start of Image
# Define EOI 0xffd9 // end of Image
Static struct app0info {
Word marker; // 0xffe0
Word Length; // 16 for usual JPEG
Byte j1_signature [5]; // = "jfif", '/0' = 0x4a, 0x49,0x00
Word version; // 0x0101,0x0102
Byte xyunits; // 0 for default
Word xdensity;
Word ydensity;
Byte num_of_x;
Byte num_of_y;
// Rgb0 --- rgbn ??
} App0info;
Static struct dqtinfo {// define quantization table
Word marker; // 0 xffdb
Word Length; // 132 for usual case
Byte qtyinfo; // PQ () =: 8bit or 16bit precision; TQ () =,: ID of QT table
Byte ytable [64]; // may be word size according to PQ, zigzag arrange
Byte qtcbcrinfo;
Byte cbcrtable [64];
} Dqtinfo;
Static struct driinfo {// define start Interval
Word marker; // 0 xffdd
Word length;
Word Ri;
} Driinfo;
Static struct sofinfo {// start of Frame
Word marker; // 0xffc0
Word Length; // = 17 for a true color YCbCr
Byte precision; // For basemode: 0x08, 8bit/sample
Word height;
Word width;
Byte num_of_component; // 0x01 for Grayscale, 0x03 for ycrcb
Byte idy;
Byte y_sfactor;
Byte qty;
Byte idcb;
Byte cb_sfactor;
Byte qtcb;
Byte idcr;
Byte cr_sfactor;
Byte qctl;
} Sofinfo;
Static struct dhtinfo {// define Huffman table
Word marker; // 0xffc4
Word Length; // = 0x01a2
Byte ht_ydc_info; // Tc (): 0 for DC and 1 for AC, th (): For base mode, it can be
Byte ydc_index [16];
Byte ydc_values [12];
Byte ht_yac_info; // 0x10
Byte yac_index [16];
Byte yac_value [162];
Byte ht_cbcrdc_info;
Byte cbcrdc_index [16];
Byte cbcrdc_value [12];
Byte ht_cbcrac_info; // 0x11
Byte cbcrac_index [16];
Byte cbcrac_values [162];
} Dhtinfo;
Static struct sosinfo {
Word marker; // 0 xffda
Word Length; // = 12
Byte num_scan_component; // for base Mode 1, 3
Byte idy;
Byte hty;
Byte idcb;
Byte htcb;
Byte idcr;
Byte hTCR;
Byte SS, Se, BF; // 0x00, 0x3f, 0x00
} Sosinfo;