1 什麼是TIFF?
TIFF是Tagged Image File Format的縮寫。在現在的標準中,只有TIFF存在, 其他的提法已經捨棄不用了。做爲一種標記語言,TIFF與其他文件格式最大的不同在於除了圖像數據,它還可以記錄很多圖像的其他信息。它記錄圖像數據的方式也比較靈活, 理論上來說, 任何其他的圖像格式都能爲TIFF所用, 嵌入到TIFF裏面。比如JPEG, Lossless JPEG, JPEG2000和任意數據寬度的原始無壓縮數據都可以方便的嵌入到TIFF中去。由於它的可擴展性, TIFF在數字影響、遙感、醫學等領域中得到了廣泛的應用。TIFF文件的後綴是.tif或者.tiff
2 TIFF文件結構
TIFF文件中的三個關鍵詞是:圖像文件頭Image File Header(IFH), 圖像文件目錄Image File Directory(IFD)和目錄項Directory Entry(DE)。每一幅圖像是以8字節的IFH開始的, 這個IFH指向了第一個IFD。IFD包含了圖像的各種信息, 同時也包含了一個指向實際圖像數據的指針。
IFH的構成:
Byte 0-1: 字節順序標誌位, 值爲II或者MM。II表示小字節在前, 又稱爲little-endian。MM表示大字節在前,又成爲big-endian。
Byte 2-3: TIFF的標誌位,一般都是42
Byte 4-7: 第一個IFD的偏移量。可以在任意位置, 但必須是在一個字的邊界,也就是說必須是2的整數倍。
IFD的構成(0代表此IFD的起始位置):
Byte 0-1: 表示此IFD包含了多少個DE,假設數目爲n
Byte 2-(n*12+1): n個DE
Byte (n*12+2)-(n*12+5): 下一個IFD的偏移量,如果沒有則置爲0
DE的構成(12個 Byte):
Byte 0-1: 此TAG的唯一標識
Byte 2-3: 數據類型。
Byte 4-7: 數量。通過類型和數量可以確定存儲此TAG的數據需要佔據的字節數
Byte 8-11: 如果佔用的字節數少於4, 則數據直接存於此。 如果超過4個,則這裏存放的是指向實際數據的指針
一個TIFF文件可能包含多個IFD,每一個IFD都是一個子文件。Baseline解碼器只要求解第一個IFD所對應的圖像數據。擴展的TIFF圖像經常包含多個IFD,每一個IFD都包含了不同的信息。
下面以一幅W*H爲12*6的tif圖像爲例說明:(其中RGB分別爲150,100,50)
0000-0007:文件頭,可以看出,該圖是II字節順序,第一個IFD的偏移量爲E6。
00E0-018D:第一個IFD。Directory Entry Count的值(00E0-00E1)爲E,表示這個IFD中有14個DE,每個DE佔用12字節,14個DE共佔用168字節,加上Directory Entry Count佔用的2字節,再加上Offset to next IFD佔用的4字節,這個IFD共佔用174字節,換算爲16進制就是AE,所以,它的數據結束於018D。Offset to next IFD的值(最後4個字節)均爲0,表示整幅圖像文件只有這一個IFD。
00E2-00ED:其中00E2-00E3爲第一個IFD中的第1個DE的tag值0100h,代表圖像寬度;00EA-00ED爲圖像寬度值000Ch(12d)。
00EE-00F9:其中00EE-00EF爲第一個IFD中的第2個DE的tag值0101h,代表圖像高度;00F6-00F9爲圖像寬度值0006h(6d)。
011E-0129:011E-011F的tag值爲0111h,代表每個Strip的偏移,0126-0129爲圖像數據對應的地址0008h。
0142-014D:圖像數據量,爲00D8h(216)。
0008-00DF:圖像數據。
參考:
[1] https://www.cnblogs.com/gywei/p/3393816.html
[2] https://blog.csdn.net/han_jiang_xue/article/details/8266207
[3] https://www.cnblogs.com/qiaozhoulin/p/4556207.html
附1:TIFF 12種數據類型
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
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.
附2:Tiff Tag 參考(https://www.awaresystems.be/imaging/tiff/tifftags/baseline.html)
基本Tag是所有TIFF編解碼器必須支持的Tag
碼 | 名稱 | 簡短描述 | |
---|---|---|---|
十進制碼 | 十六進制 | ||
254 | 00FE | NewSubfileType |
新的子文件類型標識 LONG 長度爲1 用比特來標識圖像的類型 Bit0如果是1代表縮略圖 Bit1如果是1代表多頁圖像中的某一頁 Bit2如果是1代表它是透明度掩碼圖像 其餘的位數暫時沒有定義。與SubfileType的是,此Tag用比特位來區分文件類型而不是用值來區分 |
255 | 00FF | SubfileType |
子文件類型標識 SHORT 長度爲1 1 全分辨率圖像 2 縮小分辨率的圖像 3 多頁圖像的某一頁 過時的Tag,已不再使用。 |
256 | 0100 | ImageWidth |
圖像寬度 SHORT或者LONG 長度爲1 |
257 | 0101 | ImageLength | 圖像高度 SHORT或者LONG 長度爲1 |
258 | 0102 | BitsPerSample |
每個分量的Bit數 SHORT 長度爲SamplesPerPixel |
259 | 0103 | Compression |
壓縮類型 SHORT 長度爲1 隨着TIFF的不斷擴張,目前支持多達幾十種的壓縮方式。就我個人看來,最需要關注的有以下兩個值: Compression=1: 沒有壓縮 Compression=7:JPEG壓縮。 如果是RGB圖像並且SamplesPerPixel=3,則是標準的有損JPEG壓縮。如果是CFA圖像,則是Lossless JPEG |
262 | 0106 | PhotometricInterpretation |
顏色空間 SHORT 長度爲1
0 = WhiteIsZero. 應用於灰度或者二值圖像, 0對應最亮灰度. 32803 = CFA (Color Filter Array) |
263 | 0107 | Threshholding | 定義了轉換成二值圖像的閾值,忽略之 |
264 | 0108 | CellWidth | The width of the dithering or halftoning matrix used to create a dithered or halftoned bilevel file. |
265 | 0109 | CellLength | The length of the dithering or halftoning matrix used to create a dithered or halftoned bilevel file. |
266 | 010A | FillOrder | 在同一字節中的邏輯順序。一般不做定義,要定義的話建議用1 |
270 | 010E | ImageDescription | 字符串, 對圖像的描述 |
271 | 010F | Make | 字符串, 生產廠商的描述 |
272 | 0110 | Model | 字符串 |
273 | 0111 | StripOffsets |
每個Strip的偏移量 SHORT或者LONG N = StripsPerImage for PlanarConfiguration equal to 1; N = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2 |
274 | 0112 | Orientation | The orientation of the image with respect to the rows and columns. |
277 | 0115 | SamplesPerPixel | 每個象素的通道數 SHORT 長度爲1 |
278 | 0116 | RowsPerStrip |
每個Strip有多少行 SHORT或者LONG 長度爲1。Default是無限大 StripsPerImage = floor ((ImageLength + RowsPerStrip - 1) / RowsPerStrip). |
279 | 0117 | StripByteCounts |
每個Strip的長度 SHORT或者LONG N = StripsPerImage for PlanarConfiguration equal to 1; N = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2 |
280 | 0118 | MinSampleValue | The minimum component value used. |
281 | 0119 | MaxSampleValue | The maximum component value used. |
282 | 011A | XResolution | The number of pixels per ResolutionUnit in the ImageWidth direction. |
283 | 011B | YResolution | The number of pixels per ResolutionUnit in the ImageLength direction. |
284 | 011C | PlanarConfiguration | How the components of each pixel are stored. |
288 | 0120 | FreeOffsets | For each string of contiguous unused bytes in a TIFF file, the byte offset of the string. |
289 | 0121 | FreeByteCounts | For each string of contiguous unused bytes in a TIFF file, the number of bytes in the string. |
290 | 0122 | GrayResponseUnit | The precision of the information contained in the GrayResponseCurve. |
291 | 0123 | GrayResponseCurve | For grayscale data, the optical density of each possible pixel value. |
296 | 0128 | ResolutionUnit | The unit of measurement for XResolution and YResolution. |
305 | 0131 | Software | Name and version number of the software package(s) used to create the image. |
306 | 0132 | DateTime | Date and time of image creation. |
315 | 013B | Artist | Person who created the image. |
316 | 013C | HostComputer | The computer and/or operating system in use at the time of image creation. |
320 | 0140 | ColorMap | 調色板 |
338 | 0152 | ExtraSamples | Description of extra components. |
33432 | 8298 | Copyright | Copyright notice. |