標簽圖像文件格式(Tag Image File Format,TIFF)是一種靈活的位圖格式,主要用來存儲包括照片和藝術圖在內的圖像,最初由 Aldus 公司與微軟公司一起為 PostScript 打印開發。TIFF 與 JPEG 和 PNG 一起成為流行的高位彩色圖像格式。
TIFF文件以 .tif 或 .tiff 為擴展名。其數據格式是一種3級體系結構,TIFF 文件內部結構可以分成三個部分,分別是:文件頭信息區(IFH)、圖像文件目錄(IFD)和圖像數據區。其中所有的標簽都是以升序排列,這些標簽信息是用來處理文件中的圖像信息的。
由於 TIFF 文件中使用的偏移量為 4 個字節,所以,tif 文件最大為 4GB,而新的 BigTIFF 文件格式是為打破此限制而設計的格式,此處不表。
圖像文件頭 IFH
圖像頭文件(Image File Header 簡稱 IFH),IFH 數據結構包含 3 個成員共計8個字節,如下所示:
Offset | DataType | Value |
---|---|---|
0 | Word | Byte order indication |
2 | Word | Version number (always 42) |
4 | Unsigned Long | Offset to first IFD |
詳細解釋:
-
Byte order indication:
2 字節,位於文件最開始,其取值為 "MM"(0x4d4d) 或 "II"(0x4949),"MM" 表示大字節在先的字節順序,"II" 表示小字節在先的字節順序。 -
Version number:
2 字節,其值永遠為 "42"(0x002a),是“為了其深刻的哲學意義”而選擇的。 -
Offset to first IFD:
4 字節,第一個 IFD 相對於文件起始位置的偏移量(對於多頁 TIFF 可以存在多個 IFD)
注意:
- TIFF 文件中所使用的偏移量,都是相對於文件頭位置的偏移量;
- 偏移量必須以 Word 邊界開始,也就是說,所有偏移量必須為偶數。
圖像文件目錄(IFD)
圖像文件目錄(Image File Directory)是 TIFF 圖中非常重要的數據結構,一個 TIFF 文件可以包含多個 IFD,這時表示此文件包含多個圖像,一個 IFD 標識一個圖像的屬性。
IFD 包含 3 類成員,如下所示:
Offset | DataType | Value |
---|---|---|
0 | Word | Directory Entry Count |
2 | Word | Directory Entry 0 |
14 | Word | Directory Entry 1 |
![]() |
Word | ![]() |
2+(Count-1)*12 | Word | Directory Entry (count-1) |
2+(Count)*12 | Unsigned Long | Offset to next IFD |
詳細解釋:
-
Directory Entry Count:
2字節,接下來的 DE(Directory Entry)的個數 -
Directory Entry:
12字節,表示圖像的某一個屬性,共有 Directory Entry Count 個 -
Offset to next IFD:
4字節,下一個 IFD 的偏移量,如果已經是最后一個IFD,則此值為 NULL(0x00000000)
Directory Entry
Directory Entry 簡稱 DE, 簡單的說,一個 DE 記錄一個圖像的屬性,例如圖像的 長、寬、分辨率等。其存儲結構如下所示:
Offset | DataType | Value |
---|---|---|
0 | Word | Tag |
2 | Word | Type |
4 | Unsigned Long | Length |
8 | Unsigned Long | value Offset |
詳細解釋:
-
Tag:
2 字節,該屬性的標簽編號,在 IFD 中,多個 DE 的 Tag 值是升序排列的,此編號可以在 TIFF 的規范中找到,此處列出幾個常用的編號(用戶也可以定義自己的私有標簽):TagName Decimal Hex Type Value ImageWidth 256 0x100 SHORT or LONG ImageLength 257 0x101 SHORT or LONG BitsPerSample 258 0x102 SHORT 4 or 8 Compression 259 0x103 SHORT PhotometricInterpretation 262 0x106 SHORT StripOffsets 273 0x111 SHORT or LONG RowsPerStrip 278 0x116 SHORT or LONG StripByteCounts 279 0x117 LONG or SHORT XResolution 282 0x11A RATIONAL YResolution 283 0x11B RATIONAL ResolutionUnit 296 0x128 SHORT -
Type:
2 字節,該屬性的數據的數據類型,該值可以在 TIFF 規范中找到,下面列出:Value Type Description 1 BYTE 8-bit unsigned integer. 2 ASCII 8-bit byte that contains a 7-bit ASCII code; the last byte must be NUL (binary zero). 3 SHORT 16-bit (2-byte) unsigned integer. 4 LONG 32-bit (4-byte) unsigned integer. 5 RATIONAL Two LONGs: the first represents the numerator of a fraction; the second, the denominator. 6 SBYTE An 8-bit signed (twos-complement) integer. 7 UNDEFINED An 8-bit byte that may contain anything, depending on the definition of the field. 8 SSHORT A 16-bit (2-byte) signed (twos-complement) integer. 9 SLONG A 32-bit (4-byte) signed (twos-complement) integer. 10 SRATIONAL Two SLONG’s: the first represents the numerator of a fraction, the second the denominator. 11 FLOAT Single precision (4-byte) IEEE format. 12 DOUBLE Double precision (8-byte) IEEE format. -
Length:
4 字節,數據的數量,注意,此處為數據的數量而不是數據的字節數 -
valueOffset:
4 字節,如果數據的數量乘以數據類型的字節長度(即實際數據的字節數)小於等於 4,則此處存儲數據,否則,此處存儲數據位置的偏移量(相對於文件頭的偏移量)
其文件結構總覽如下圖所示:

圖像數據例子
TIFF 文件的 IFD 並不一定緊跟在 IFH 后面,相反,它常常位於圖像數據的后面,即 TIFF 圖像文件的一般組織形式是:
IFH -- 圖像數據 -- IFD
下面我們制作一個 TIFF 格式的文件進行簡單分析
用 畫圖 軟件繪制一個 7*9 像素的圖像,保存為 TIFF 格式
TIFF例子文件
放大截圖顯示為:

其文件的實際內容為(16進制):
Offset | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | a | b | c | d | e | f |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0000 0000 | 49 | 49 | 2a | 00 | ca | 00 | 00 | 00 | 80 | 3e | 14 | 2a | 40 | 39 | 78 | b6 |
0000 0010 | 00 | 84 | 26 | 61 | 41 | f8 | 61 | 22 | 1d | 08 | 00 | 3e | 56 | 2b | 37 | f3 |
0000 0020 | b9 | de | 04 | 16 | 0a | cb | 48 | a4 | 40 | 92 | 3c | 86 | 90 | 44 | 0c | e4 |
0000 0030 | c7 | 1b | d1 | ee | 03 | 7d | 82 | 0c | c1 | 71 | 10 | 84 | aa | 4e | 65 | 0f |
0000 0040 | 47 | c5 | 00 | 00 | 04 | 18 | 91 | 48 | bf | dd | 4e | a0 | 02 | a1 | 50 | a2 |
0000 0050 | 14 | 0a | 00 | cb | 45 | 73 | 6a | 6a | 1d | 84 | 3f | 58 | ec | 77 | fa | d9 |
0000 0060 | 6c | 20 | 71 | 80 | 9b | 02 | 40 | c8 | 3c | f6 | 63 | 6a | 42 | 1e | d0 | 87 |
0000 0070 | e1 | 9d | 2e | c2 | 01 | bc | c2 | a0 | 10 | 1b | f8 | b0 | 5d | 7d | 09 | c1 |
0000 0080 | 4d | e0 | a8 | 1c | 03 | 08 | 01 | cf | 9f | ec | 87 | 3b | 64 | 20 | 10 | 00 |
0000 0090 | 0f | 49 | 2f | 20 | 50 | 0c | 0f | 10 | 10 | 83 | 03 | 6a | e5 | 73 | fd | aa |
0000 00a0 | ec | 71 | bd | 5f | 6f | 91 | c9 | 39 | ca | f7 | 7e | be | 42 | 00 | 60 | 66 |
0000 00b0 | 41 | f3 | 0e | 7f | 87 | 03 | 8f | f2 | ea | 15 | 9e | fd | 7f | bf | 5d | cf |
0000 00c0 | a7 | 93 | d5 | f8 | f7 | 7c | bf | 9f | 70 | 10 | 0f | 00 | fe | 00 | 04 | 00 |
0000 00d0 | 01 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 04 | 00 | 01 | 00 | 00 | 00 |
0000 00e0 | 07 | 00 | 00 | 00 | 01 | 01 | 04 | 00 | 01 | 00 | 00 | 00 | 09 | 00 | 00 | 00 |
0000 00f0 | 02 | 01 | 03 | 00 | 03 | 00 | 00 | 00 | 84 | 01 | 00 | 00 | 03 | 01 | 03 | 00 |
0000 0100 | 01 | 00 | 00 | 00 | 05 | 00 | 00 | 00 | 06 | 01 | 03 | 00 | 01 | 00 | 00 | 00 |
0000 0110 | 02 | 00 | 00 | 00 | 11 | 01 | 04 | 00 | 01 | 00 | 00 | 00 | 08 | 00 | 00 | 00 |
0000 0120 | 15 | 01 | 03 | 00 | 01 | 00 | 00 | 00 | 03 | 00 | 00 | 00 | 16 | 01 | 04 | 00 |
0000 0130 | 01 | 00 | 00 | 00 | 09 | 00 | 00 | 00 | 17 | 01 | 04 | 00 | 01 | 00 | 00 | 00 |
0000 0140 | c2 | 00 | 00 | 00 | 1a | 01 | 05 | 00 | 01 | 00 | 00 | 00 | 8a | 01 | 00 | 00 |
0000 0150 | 1b | 01 | 05 | 00 | 01 | 00 | 00 | 00 | 92 | 01 | 00 | 00 | 1c | 01 | 03 | 00 |
0000 0160 | 01 | 00 | 00 | 00 | 01 | 00 | 00 | 00 | 28 | 01 | 03 | 00 | 01 | 00 | 00 | 00 |
0000 0170 | 02 | 00 | 00 | 00 | 3d | 01 | 03 | 00 | 01 | 00 | 00 | 00 | 02 | 00 | 00 | 00 |
0000 0180 | 00 | 00 | 00 | 00 | 08 | 00 | 08 | 00 | 08 | 00 | 00 | 77 | 01 | 00 | e8 | 03 |
0000 0190 | 00 | 00 | 00 | 77 | 01 | 00 | e8 | 03 | 00 | 00 |
對其進行簡要解析(偏移量省略高位兩個字節的0x0000):
-
0x0000 ~ 0x0007
IFH, 其中 最開始的兩個字節內容 0x4949 為 "II", 表示此文件的字節順序為小字節在先;接下來的兩個個字節 0x2a 0x00 按照 Word 類型解析為 0x002a 為固定值 42,接下來的 4 個字節解析為 IFD 的偏移地址為:0x000000ca; -
0x00ca ~ 0x00cb
IFD 的起始位置,開始的兩個字節解析為 0x000f, 為 Directory Entry Count, 表示下面有 15 個 Directory Entry; -
0x00cc ~ 0x00d7
Directory Entry 0, 開始兩個字節為 Tag = 0x00fe, 表示 NewSubfileType, 具體可查看附件中的 TIFF 規范; -
0x00d8 ~ 0x00e3
DE 1, 開始的 2 個字節 Tag = 0x0100, 表示 ImageWidth, 接下來的 2 個字節 Type = 0x0004 表示數據格式為 LONG(32bits), 再接下來的 4 個字節 Length = 0x00000001, 表示數據個數為 1, 再接下來的 4 個字節表示此 ImageWidth = 0x00000007, 表示圖像寬度為 7; -
0x00e4 ~ 0x00ef
DE 2, 開始的 2 個字節 Tag = 0x0101, 表示 ImageLength, 不再詳述; -
0x00f0 ~ 0x00fb
DE 3, 開始的 2 個字節 Tag = 0x0102, 表示 BitsPerSample, 為每個組件的位數;接下來的 2 個字節 Type = 0x0003, 表示數據格式為 SHORT, 接下來的 4 個字節 Length = 0x00000003, 表示數據個數為 3; 此處應當計算 3 (數據格式字節數)= 32=6, 即數據為 6 個字節,所以接下來的 4 個字節保存數據的偏移量位置 value Offset = 0x00000184;- 0x0184 ~ 0x0189
此處存儲 BitsPerSample 的數據,可以看到,其數據為 0x0008 0x0008 0x0008, 表示圖像為3通道,每個通道的像素為 8 位深度;
- 0x0184 ~ 0x0189
-
0x00fc ~ 0x0107
DE 4, 開始的 2 個字節 Tag = 0x0103, 表示 Compression, 為壓縮的屬性,此處的值為 0x05, 表示數據是 LZW 壓縮的; -
0x0108 ~ 0x0113
DE 5, Tag = 0x0106, 表示 PhotometricInterpretation, 值為 2 表示為 RGB 圖像; -
0x0114 ~ 0x011F
DE 6, Tag = 0x0111, 表示 StripOffsets, For each strip, the byte offset of that strip. 此處 Type = 4, 表示 LONG 數據類型,Length = 0x00000001, 數據只有 1 個,其值為 0x00000008, 表示數據存放在偏移量 0x08 開始的數據處;- 0x0008~0x00c9
此處存放的數據即為圖像的像素數據,驗證圖像像素值,其並不等於文件中的數值,應該是經過壓縮的原因;
- 0x0008~0x00c9
-
0x0120 ~ 0x012B
DE 7, Tag = 0x0115, 表示 SamplesPerPixel, 每個像素的組件數,對於 RGB 圖像此屬性值為3; 查看此值確實為 3; -
0x012C ~ 0x0137
DE 8, Tag = 0x0116, 表示 RowsPerStrip, The number of rows in each strip (except possibly the last strip.) 一個 Strip 中的行數,此處值為 9 (此處只有 1 個 strip, 此值為 9); -
0x0138 ~ 0x0143
DE 9, Tag = 0x0117, 表示 StripByteCounts, For each strip, the number of bytes in the strip after compression. 字節數,此處值為 0xc2 = 194 字節; -
0x0144 ~ 0x014f
DE 10, Tag = 0x011a, 表示 XResolution, The number of pixels per ResolutionUnit in the ImageWidth direction. Type = 0x05, 表示 RATIONAL 類型(分數類型,分子分母都為LONG),length = 0x00000001, 數據個數為 1 個;Value Offset = 0x0000018a;- 0x018a~0x0191
X 方向分辨率值,分子為 0x017700 分母為 0x00003e8,所以值為:0x60=96;
- 0x018a~0x0191
-
0x0150 ~ 0x015B
DE 11, Tag = 0x011b, 表示 YResolution, Type = 0x05, length = 0x00000001, Value Offset = 0x00000192;- 0x0192~0x0199
Y 方向分辨率值,分子為 0x0x017700 分母為 0x00003e8,所以值為:0x60=96;
- 0x0192~0x0199
-
0x015C ~ 0x0167
DE 12, Tag = 0x011c, 表示 PlanarConfiguration,How the components of each pixel are stored. 值為 1 ,表示 1 = Chunky format. The component values for each pixel are stored contiguously. The order of the components within the pixel is specified by PhotometricInterpretation. For example, for RGB data, the data is stored as RGBRGBRGB…; -
0x0168 ~ 0x0173
DE 13, Tag = 0x0128, 表示 ResolutionUnit, 1 = No absolute unit of measurement. Used for images that may have a non-square aspect ratio but no meaningful absolute dimensions. 2 = Inch. 3 = Centimeter. Default = 2 (inch); 此處值為 2; -
0x0174 ~ 0x017f
DE 14, Tag = 0x013d, 表示 Predictor, 此處值為 2, 此參數是與 LZW 壓縮有關的參數; -
0x0180 ~ 0x0183
Offset to next IFD, 由於本文件中只有一幅圖像,所以只有一個 IFD, 所以此處的偏移量值為 NUL = 0x00000000;