JPEG文件根据不同的标记将文件分成不同的标记段,解析方式与dicom文件类似。
每个tag固定为2个byte
每个标记之前可以填充数目不限的0xFF
主要的标记(marker)如下表
marker | marker名称 | 含义 |
---|
0xFFD8 | SOI | 图像开始(Start of Image) |
0xFFD9 | EOI | 图像结束(End of Image) |
0xFFC0 | SOF0 | 帧开始(Start of Frame 0) |
0xFFC4 | DHT | 霍夫曼(Huffman)表 |
0xFFDA | SOS | 扫描线开始 |
0xFFDB | DQT | 量化表 |
0xFFE0 | APP0 | JFIF(JPEG File Interchange Format)应用数据块 |
0xFFE1~0xFFEF | APPn | 其它的应用数据块(n, 1~15) |
比较详细的标记列表
0xFFC0 ~ 0xFFCF
标记代码(Code Assignment) | 符号(Symbol) | 描述(Description) | |
---|
Start of Frame markers, none-hierarchical Huffman coding |
0xFFC0 | SOF0 | BaseLine DCT(离散余弦变换,Discrete Cosine Transform) |
0xFFC1 | SOF1 | Extended sequential DCT |
0xFFC2 | SOF2 | Progressive DCT |
0xFFC3 | SOF3 | Spatial (sequential) lossless |
Huffman table specification |
0xFFC4 | DHT | Define Huffman tables(s) |
Start of Frame markers, hierarchical Huffman coding |
0xFFC5 | SOF5 | Differential sequential DCT |
0xFFC6 | SOF6 | Differential progressive DCT |
0xFFC7 | SOF7 | Differential spatial lossless |
arithmetic coding conditioning specification |
0xFFCC | DAC | Define arithmetic conditioning table |
Start of Frame markers, hierarchical arithmetic |
0xFFCD | SOF13 | Differential sequential DCT |
0xFFCE | SOF14 | Differential progressive DCT |
0xFFCF | SOF15 | Differential spatial Lossless |
0xFFD0 ~ 0xFFDF
标记代码(Code Assignment) | 符号(Symbol) | 描述(Description) |
---|
Restart of interval termination |
0xFFD0~0xFFD7 | RSTm | Restart with modulo 8 counter m |
Other marker |
0xFFD8 | SOI | Start of image |
0xFFD9 | EOI | End of image |
0xFFDA | SOS | Start of scan |
0xFFDB | DQT | Define quantization table(s) |
0xFFDC | DNL | Define number of lines |
0xFFDD | DRI | Define restart interval |
0xFFDE | DHP | Define hierarchical progression |
0xFFDF | EXP | Expand reference image(s) |
0xFFE0 ~ 0xFFEF
标记代码(Code Assignment) | 符号(Symbol) | 描述(Description) |
---|
0xFFE0~0xFFEF | APPn | Reserved for application use |
0xFFF0 ~ 0xFFFE
标记代码(Code Assignment) | 符号(Symbol) | 描述(Description) |
---|
0xFFF0~0xFFFD | JPGn | Reserved for JPEG extension |
0xFFFE | COM | Comment |
Reserved markers
标记代码(Code Assignment) | 符号(Symbol) | 描述(Description) |
---|
0xFF01 | TEM | For remporary use in arithmetic coding |
0xFF02~0XFFBF | RES | Reserved |
主要标记域的结构
每个标记域的结构轮廓一致如下
marker | size | data |
---|
2bytes | 2bytes,表示当前标记域 size和data两项共占用的字节数 | 当前标记域的数据,具体结构划分根据marker的不同进行不同的划分,长度为(size-2)bytes |
######
APP0域(标记为0xFFE0)的详细结构
长度 | 块的名称 | 说明 |
---|
2bytes | APP0(JFIF application segment) | JFIF应用数据块 |
2bytes | length of APP0 block | APP0块的长度 |
5bytes | "JFIF"+"0" | 识别APP0标记 |
1byte | Major version | 主要版本号(如版本1.02中的1) |
1byte | Minor version | 次要版本号(如版本1.02中的02) |
1byte | Units for the X and Y densities | X和Y的密度单位 units=0:无单位 units=1:点数/英寸 units=2:点数/厘米 |
2bytes | X density | 水平方向像素密度 |
2bytes | Y density | 竖直方向像素密度 |
1byte | X thumbnail | 缩略图水平像素数目 |
1byte | Y thumbnail | 缩略图竖直像素数目 |
3n | Thumbnail RGB bitmap | 缩略RGB位图(n为缩略图的像素数) |
| Optional JFIF extension APP0 marker segment(s) | 任选的JFIF扩展APP0标记段 |
... | | ... |
APP1域(标记为0xFFE1)的详细结构
长度 | 块的名称 | 说明 |
---|
2bytes | APP1 | APP1标记 |
2bytes | length of APP1 block | APP1块的长度 |
6bytes | "EXIF"+"00" | 识别APP1标记 |
8bytes | TIFF Header | TIFF头信息 |
| IFD0 Image File Directory | 1. IFD0(main image):Directory / Link to IFD1 2. Data area of IFD0 |
| Exif SubIFD | 1. Exif SubIFD: Directory / End of link 2. Data area of IFD0 |
| IFD1 Image File Directory | 1. IFD1(thumbnail image): directory / End of link 2. Data area of IFD1 |
| Thumbnail image | 缩略图,可能为JPEG格式存储,也可能为TIFF格式存储 |
TIFF Header的详细结构
长度 | 说明 |
---|
2bytes | Byte align "II"或"MM" 字节顺序:II(Intel字节序)表示小端存储,MM(Motorola字节序)表示大端存储 |
2bytes | Tag Mark 0x2a00或0x002a 验证字节顺序:如果为小端(II),则为0x2a00,如果为大端 (MM),则为0x002a |
4bytes | Offset to first IFD 到IFD0的偏移,偏移起始位置为TIFFHeader的位置 APP1段中IFD0,Exif SubIFD, IFD1的偏移都以TIFFHeader的位置作为起始位置 |
Image File Directory的结构
每个IFD中包含多个Directory Entry,每个Entry记录图片的一条属性信息。
长度 | 含义 |
---|
2bytes | Directory Entry数量 |
12bytes | Directory Entry0 |
12bytes | Directory Entry1 |
...... |
12bytes | Directory EntryN |
4bytes | Offset to next IFD |
| Directory Entry Data Area(存储Directory EntEntry对应的值) |
Directory Entry的结构
2bytes | 2bytes | 4bytes | 4bytes |
---|
Exif Tag(表示当前Entry记录的信息含义) | Component Type(存储元素的数据类型) | Component Count(存储元素的数量) | 存储值小于等于4bytes,则为对应值,大于4bytes,则为基于TIFF Header的偏移量 |
Component Type
Component Type Value | Format | bytes |
---|
1 | Unsigned Byte | 1 |
2 | Ascii String | 1 |
3 | Unsigned Short | 2 |
4 | Unsigned Long | 4 |
5 | Unsigned rational | 8 |
6 | Signed Byte | 1 |
7 | Undefined | 1 |
8 | Signed Short | 2 |
9 | Signed Long | 4 |
10 | Signed Rational | 8 |
11 | Single Float | 4 |
12 | Double Float | 8 |
Exif Tag含义举例
Tag ID | Tag Name |
---|
0x0100 | ImageWidth |
0x0101 | ImageHeight |
...... |
更多Exif Tag含义可查阅
https://exiftool.org/TagNames/EXIF.html
参考文章:
- Exif Tags
- JPEG文件格式解析(一) Exif 与 JFIF
- JPEG图像编码标准-共86页文档