TIFF图像文件格式详解整理

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对应最亮灰度.
1 = BlackIsZero. 应用于灰度或者二值图像. 0对应最暗灰度。
2 = RGB. 正常RGB图像,存储顺序为R,G,B.
3 = Palette color. 索引图像, ColorMap必须定义,SamplesPerPixel必须1.

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.
发布了18 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zengshaoqing/article/details/105102394
今日推荐