TIFF图像格式解析
详细说明文档:https://www.awaresystems.be/imaging/tiff/faq.html
1 TIFF文件结构
TIFF文件包含三个部分:图像文件头(IFH, image fileheader)、图像文件目录(IFD, imagefile dictionary)、目录项(dictionaryentry)。
1.1 IFH
一共8个字节
0-1字节为II或MM,前者表示小字节在前(4D4D),后者表示大字节在前(4949)。
2-3字节为特定的数值42
4-7字节为第一个图像文件目录IFD的偏移量(必须是2的倍数)
表一 IFH结构描述
名称 | 字节数 | 数据类型 | 说明 |
Byteorder | 2 | Integer | TIF标记,值为4D4D或4949 |
Version | 2 | Integer | 版本号,其值恒为2A00 |
Offset to first IFD | 4 | Long | 第一个IFD的偏移量 |
1.2 IFD
0-1字节表示此IFD中包含的文件目录数目
2-(n*12+1)字节为n个DE
(n*12+1)-(n*12+1)字节为下一个IFD的偏移量
注:IFD数据结构并不一定紧跟在IFH后面,相反,它常常位于第三部分图像数据的后面,即TIF图像文件的一般组织形式是:IFH--图像数据--IFD。
表二 图像文件目录
名称 | 字节数 | 类型 | 说明 |
Directory Entry Count | 2 | Integer | 本IFD中DE的数量 |
Directory Entry(1) | 12 |
| 简称DE,中文译义“目录项” |
…….. |
|
|
|
Directory Entry(N) | 12 |
|
|
Offset to next IFD | 4 | Long | 下一个IFD的偏移量 |
1.3 DE
0-1为TAG的唯一标识
2-3为数据类型
4-7为存储此TAG需要的字节数
8-11为数据(字节数小于4)或指向数据的指针(字节数大于4)
表三 目录项
名称 | 字节数 | 类型 | 说明 |
tag | 2 | Integer | 本属性的标签编号 |
type | 2 | Integer | 本属性值的数据类型 |
count | 4 | Long | 该类型数据的数量 |
valueOffset | 4 | Long | 属性值的存放偏移量 |
1.4 IFH、IFD和DE的关系图
2 标签(TAG)含义解析
2.1 type的数据类型
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 thedefinition 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.2 具体含义描述
码 | 名称 | 简短描述 | |
十进制码 | 十六进制 | ||
254 | 00FE | 新的子文件类型标识 LONG 长度为1 用比特来标识图像的类型 Bit0如果是1代表缩略图 Bit1如果是1代表多页图像中的某一页 Bit2如果是1代表它是透明度掩码图像 其余的位数暂时没有定义。与SubfileType的是,此Tag用比特位来区分文件类型而不是用值来区分 | |
255 | 00FF | 子文件类型标识 SHORT 长度为1 1 全分辨率图像 2 缩小分辨率的图像 3 多页图像的某一页 过时的Tag,已不再使用。 | |
256 | 0100 | 图像宽度 SHORT或者LONG 长度为1 | |
257 | 0101 | 图像高度 SHORT或者LONG 长度为1 | |
258 | 0102 | 每个分量的Bit数 SHORT 长度为SamplesPerPixel | |
259 | 0103 | 压缩类型 SHORT 长度为1 随着TIFF的不断扩张,目前支持多达几十种的压缩方式。就我个人看来,最需要关注的有以下两个值: Compression=1: 没有压缩 Compression=7:JPEG压缩。 如果是RGB图像并且SamplesPerPixel=3,则是标准的有损JPEG压缩。如果是CFA图像,则是Lossless JPEG | |
262 | 0106 | 颜色空间 SHORT 长度为1 0 = WhiteIsZero. 应用于灰度或者二值图像, 0对应最亮灰度. 32803 = CFA (Color Filter Array) | |
263 | 0107 | 定义了转换成二值图像的阈值,忽略之 | |
264 | 0108 | The width of the dithering or halftoning matrix used to create a dithered or halftoned bilevel file. | |
265 | 0109 | The length of the dithering or halftoning matrix used to create a dithered or halftoned bilevel file. | |
266 | 010A | 在同一字节中的逻辑顺序。一般不做定义,要定义的话建议用1 | |
270 | 010E | 字符串, 对图像的描述 | |
271 | 010F | 字符串, 生产厂商的描述 | |
272 | 0110 | 字符串 | |
273 | 0111 | 每个Strip的偏移量 SHORT或者LONG N = StripsPerImage for PlanarConfiguration equal to 1; N = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2 | |
274 | 0112 | The orientation of the image with respect to the rows and columns. | |
277 | 0115 | 每个象素的通道数 SHORT 长度为1 | |
278 | 0116 | 每个Strip有多少行 SHORT或者LONG 长度为1。Default是无限大 StripsPerImage = floor ((ImageLength + RowsPerStrip - 1) / RowsPerStrip). | |
279 | 0117 | 每个Strip的长度 SHORT或者LONG N = StripsPerImage for PlanarConfiguration equal to 1; N = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2 | |
280 | 0118 | The minimum component value used. | |
281 | 0119 | The maximum component value used. | |
282 | 011A | The number of pixels per ResolutionUnit in the ImageWidth direction. | |
283 | 011B | The number of pixels per ResolutionUnit in the ImageLength direction. | |
284 | 011C | How the components of each pixel are stored. | |
288 | 0120 | For each string of contiguous unused bytes in a TIFF file, the byte offset of the string. | |
289 | 0121 | For each string of contiguous unused bytes in a TIFF file, the number of bytes in the string. | |
290 | 0122 | The precision of the information contained in the GrayResponseCurve. | |
291 | 0123 | For grayscale data, the optical density of each possible pixel value. | |
296 | 0128 | The unit of measurement for XResolution and YResolution. | |
305 | 0131 | Name and version number of the software package(s) used to create the image. | |
306 | 0132 | Date and time of image creation. | |
315 | 013B | Person who created the image. | |
316 | 013C | The computer and/or operating system in use at the time of image creation. | |
320 | 0140 | 调色板 | |
338 | 0152 | Description of extra components. | |
33432 | 8298 | Copyright notice. |
3 C/C++代码
3.1 自己写的使用opencv读取16为TIFF图像代码
bool read16bitTiffOpencv(const char* pathname, unsigned short*& myImage)
{
IplImage* src =cvLoadImage(“chd.tif”, CV_LOAD_IMAGE_ANYDEEPTH | CV_LOAD_IMAGE_ANYCOLOR |CV_LOAD_IMAGEUNCHANGED);
unsigned short*myImage = new unsigned short[src->width * src->height];
for (int r = 0;r < src->height; r++)
{
unsigned short* iRow = (unsigned short*)(src->imageData+ i * src->widthStrip);
for(int c = 0; c < src->width; c++)
{
myImage[i * src->width + j] =*(iRow + j);
}
}
return true;
}
函数调用格式:
unsigned short* myImage =NULL;
read16bitTiffOpencv(“my.tiff”, myImage);
……
delete[] myImage;