2024年1月14日发(作者:)

tif格式详解

TIFF(Tag Image File Format)图像文件说明:

TIFF文件是由许多的标签(tag)组成,在Adobe的有关tiff6.0的说明中,将(tag)的解释称各种标签所对应的数值,而在文件中各个标签的实际入口称为(field),暂且称为域。tiff支持黑白、灰度、彩色的图像格式,同时还可以接受RGB CMYK等色彩系统,同时支持图像数据的LZW,哈夫曼等压缩算法或者不压缩。

TIFF文件分为文件头和IFD两部分,一个IFD中存储了一幅图像的信息,IFD可以由许多标签组成。在TIFF6.0文件中,规定所有的标签必须以升序排列,通过这些标签信息,来处理文件中的图像数据。

TIFF文件头说明:

TIFF的文件头有8字节

0-1 规定为 'II' 或者 'MM' Intel /Mortorola 类型的字节顺序

2-3 TIFF文件的版本,为与以前的兼容,为42

4-7 TIFF文件的第一个IFD在文件中的偏移量,肯定大于8

IFD说明:IFD是由连续的标签组成

OFFSET:0-1 IFD中的标签数目

OFFSET:2-13 IFD中的第一个标签

OFFSET:14-25 IFD中的第二个标签

.....

OFFSET:2+12*标签数 下一个IFD的(相对于文将)偏移量,在一个TIFF文件中可以由多个IFD用于存放多个图像,如果如果该数字为0,表示已经处理完所有的IFD。

标签说明:一个标签由连续的12字节组成

OFFSET:0-1 TAG ID 标签所对应的数字编号(tag),用于定义该标签所存放的"信息"

OFFSET:2-3 TAG INFO DATA TYPE 标签中数据的数据类型

OFFSET:4-7 VALUE COUNT 2-3中说明的数据类型的数目

OFFSET:8-11 VALUE OFFSET 相对于文件的该标签值的偏移量,必须开始于 "字"边界,为了节省空间,如果该标签的数据的长度小于等于四个字节,在VALUE OFFSET 中直接存储(小于四字节数据左对齐)该标签的值而不是在文件中的偏移量。

以下列出了标签中的数据的可能的数据类型

1 = BYTE

2 = ASCII

3 = SHORT

4 = LONG

5 = RATIONAL

6 = SBYTE

7 = UNDEFINED

8 = SSHORT

9 = SLONG

10= SRATIONAL

11= FLOAT

12= DOUBLE

TIFF中的标签种类有很多,按大类可以 BASELINE TIFF 标签 和 TIFF

EXTENSIONS标签

在以前版本的TIFF文当中将TIFF文件分为

BASELINE TIFF:

在以前版本中,BASELINE TIFF 分为三种格式:

(BiLevel Images)tiff class b image,(GrayScale Images)tiff class G

image,(Palette-Color Images)class P image,(rgb image)tiff class r images

l Images 所用到的标签

标签名 标签的ID号(十进制) 标签数据类型 标签可能的数值

ImageWidth 256 SHORT/LONG

ImageLength 257 SHORT/LONG

Compression 259 SHORT 1(no

Compression),2(HuffmanCompression),32773(PackBits Compression)

PhotometricInterpretation 262 SHORT 当Compression=2 0,1(0:在BiLevel/GrayScale图形中0表示全白 1:在BiLevel/GrayScale图形中0表示全黑,即图形采用反色显示)

StripOffsets 273 SHORT/LONG 每个Strip的文件偏移量

RowsPerStrip 278 SHORT/LONG

每个Strip中的行数

StripByteCounts 279 SHORT/LONG 当采用某种压缩方式后每个Strip的字节数

XResolution 282 RATIONAL ImageWidth中每ResolutionUnit中的像素数目

YResolution 283 RATIONAL ImageLength中每ResolutionUnit中的像素数目

ResolutionUnit 296 SHORT 1(用于非四边形显示的图像),2(英寸),3(厘米)根据ImageWidth及ImageLength用于计算图像在显示时的尺寸

以下是一个BiLevel Tiff 文件的例子

偏移量 说明 数值(HEX)

0000 byteorder 4d4d

0002 42 0042

0004 1st IFD offset 00000014

IFD:

偏移量 说明 值(HEX)

0014 number of directory entries 000c(IFD中的标签数目12个)

偏移量 标签说明 TagID(HEX) 数据类型(HEX) Count(HEX) 值偏移量(HEX) 说明

0016 newsubfile type 00fe 0004 00000001 00000000

0022 imagewidth 0100 0004 00000001 000007d0 图像宽度2000

002e imagelength 0101 0004 00000001 00000bb8 图像长度3000

003a compression 0103 0003 00000001 80050000 数据压缩由于数据短于四字节,偏移量中直接为值(左对齐)32773

0046 photometricinterpretation 0106 0003 00000001 00010000 图像采用反色显示

0052 stripoffsets 0111 0004 000000bc 000000b6 图像Strip偏移量0x00B6,共188(0xBC)个Strip

005e rowsperstrip 0116 0004 00000001 00000010 每Strip中行数16

006a stripbytecounts 0117 0003 000000bc 000003a6 每Strip中的字节数,共188个,偏移量0x03A6

0076 xresolution 011b 0005 00000001 00000696

0082 yresolution 011b 0005 00000001 0000069e

008e software 0131 0002 0000000e 000006a6 用于标记生成该图像的软件,文本类型,偏移量0x6a6,字节数14

009a datetime 0132 0002 00000014 000006b6 用于标记该图像的生成日期,文本类型,偏移量0x6b6,字节数20

00a6 nextIFDoffset 00000000 没有下一个IFD

值大于4字节:

偏移量 标签说明: 值: 说明:

00b6 StripOffsets Offset0,Offset1,...Offset187

03a6 StripByteCounts Count0,Count1,...Count187

0696 XResolution 0000012C00000001 每ResolutionUnit中300像素

069e YResolution 0000012C00000001 每ResolutionUnit中300像素

06a6 Software "PageMaker4.0" 生成该图像的软件名称,长度应该为字符串长度加NULL结尾 应为13 注: 本例子来自Adobe

Tiff Revision 6.0,其中的字节数为14,可能其中有错误

06b6 DateTime "1988-02-18 13:59:59" 生成该图像的时间,长度为20字节

Image Data:

00000700 Compressed data for strip 10

xxxxxxxx Compressed data for strip 179

xxxxxxxx Compressed data for strip 53

xxxxxxxx compressed data for strip 160

.

.

文件结束

以上例子中的software,datatime为可选标签,同时各个Strip可以不安顺序在文件中存储,每个Strip的偏移量必须在文件中指定,而不能缺省认为一个Strip的结尾是下一个Strip的开始,或者Strip是按顺序在文件中存储的

注:以上的例子中没有列出ResolutionUnit,可能是Adobe文档中的错误

Bilevel Images 中需要的标签:

TagName Decimal Hex Type Value

Ima

geWidth 256 100 short/long

ImageLength 257 101 short/long

Compression 259 103 short 1,2,32773

PhotometricInterpretation 262 106 short 0,1

StripOffsets 273 111 short/long

RowsPerStrip 278 116 short/long

StripByteCounts 279 117 short/long

XResolution 282 11a rational

YResolution 283 11b rational

ResolutionUnit 296 128 short

GrayScale Image:

灰度图像是黑白图像的一般化,灰度图像需要的标签基本同上,但是用户需要修改或者添加以下标签

Compression=1 or 32773

FieldName TagID DataType Value Description

BitPerSample 258 SHORT 4/8 the number of bits per component 16 or

256 distinct shades of gray

GrayScale Image 所需的标签名称:

FieldName TagID(Decimal) DataType Value

ImageWidth 256 SHORT/LONG

ImageLength 257 SHORT/LONG

BitsPerSample 258 SHORT/LONG 4/8

Compression 259 SHORT 1/32773

PhotometricInterpretation 262 SHORT 0/1

StripOffsets 273 SHORT/LONG

RowsPerStrip 278 SHORT/LONG

StripByteCounts 279 LONG/SHORT

XResolution 282 RATIONAL

YResolution 283 RATIONAL

ResolutionUnit 296 SHORT 1/2/3

Palette-color Images:

调色板图形于灰度图形类似,它每个像素的颜色采用RGB表的查找,需要修改或者添加以下标签,其他与GrayScale Images类似

PhotometricInterpretation=3 (表示该图像为Palette-color Image)

FieldName TagID DataType Count Value Description

ColorMap 320 SHORT 3*(2的BitsPerSample次方) 调色板顺序为RGB

Palette-color Image 所需的标签名称:

FieldName TagID(Decimal) DataType Value

ImageWidth 256 SHORT/LONG

ImageLength 257 SHORT/LONG

BitsPerSample 258 SHORT 4/8

Compression 259 SHORT 1/32773

PhotometricInterpretation 262 SHORT 3

StripOffsets 273 SHORT/LONG

RowsPerStrip 278 SHORT/LONG

StripByteCounts 279 SHORT/LONG

XResolution 282 RATIONAL

YResolution 283 RATIONAL

ResolutionUnit 296 SHORT 1/2/3

ColorMap 320 SHORT

RGB Full Color Image:

一个RGB图像,每个像素由3部分组成,RGB,在图像中没有ColorMap标签,需要修改或者添加以下几个标签

BitPerSample=8,8,8

PhotometricInterpretation=2 (RGB Image)

FieldName TagID DataType Value Description

SamplesPerPixel 277 SHORT

RGB Image 所需的标签名称:

FieldName TagID(Decimal) DataType Value

ImageWidth 256 SHORT/LONG

ImageLength 257 SHORT/LONG

BitsPerSample 258 SHORT 8,8,8

Compression 259 SHORT 1,32773

PhotometricInterpretation 262 SHORT 2

StripOffsets 273 SHORT/LONG

SamplesPerPixel 277 SHORT 3 or more

RowsPerStrip 278 SHORT/LONG

StripByteCounts 279 SHORT/LONG

XResolution 282 RATIONAL

YResolution 283 RATIONAL

ResolutionUnit 296 SHORT 1,2,3

Baseline Tiff文件的附加要求:

MM / II 字节顺序的处理,一文件多图像的处理。同时有些标签需要注意的是:

ImageWidth,ImageLength标签数据类型可以为SHORT/LONG,程序必须处理出现图像尺寸过大的情况.

同时对于某些标签可以同时为SHORT/LONG,在程序中需要对这些标

签进行正确的处

Tiff 文件基本标签详细说明:

以下标签按字母顺序说明:

********************************************************************

Artist 图像的作者

Tag = 315

Type = ASCII

********************************************************************

BitsPerSample 每个数据的位数

Tag = 258

Type = short

N = SamplesPerPixel

DefaultValue=1

Value=位数 SamplesPerPixel<=2

Value=位数的偏移量 SamplesPerPixel>2

********************************************************************

ColorMap 调色板数据

Tag = 320

Type = short

N = 3*(2**BitsPerSample)

********************************************************************

Compression 数据压缩方式

Tag = 259

Type = short

N=1

DefaultValue=1

Value=1 数据没有压缩

Value=2 采用CCITT Group 31 压缩

Value=3 采用CITT Group 3 Fax T4 压缩

Value=4 采用CITT Group 3 Fax T6 压缩

Value=5 LZW压缩

Value=6 JPEG压缩

Value=32773 PackBits压缩

********************************************************************

Copyright 图像的版权信息

Tag = 33432

Type = ASCII

********************************************************************

DateTime 此图像生成的日期及时间

Tag = 306

Type = ASCII

N = 20

********************************************************************

ExtraSamples 每个像素的额外组成

Tag = 338

Type = SHORT

N = m

Value=0 未指定数据

Value=1 与Alpha通道有关

Value=2 与Alpha通道无关

********************************************************************

FillOrder 每字节的数据,其位的排列方式

Tag = 266

Type = SHORT

N = 1

Value=1 从左到右

Value=2 从右到左

***********************************************************

*********

FreeByteCounts 每个未使用块在文件中的字节数

Tag = 289

Type = LONG

********************************************************************

FreeOffsets 每个未使用块在文件中的偏移量

Tag = 288

Type = LONG

********************************************************************

GrayResponseCurve 灰度响应曲线

Tag = 291

Type = SHORT

N = 2**BitsPerSample

********************************************************************

GrayResponseUnit 灰度响应曲线的单位

Tag = 290

Type = SHORT

N = 1

DefaultValue=2

Value=1 1/10

Value=2 1/100

Value=3 1/1000

Value=4 1/10000

Value=5 1/100000

********************************************************************

HostComputer 制作此图像的计算机及其操作系统

Tag = 316

Type = ASCII

********************************************************************

ImageDescription

Tag = 270

Type = ASCII

********************************************************************

ImageLength 图像的高度(像素为单位)

Tag = 257

Type = SHORT/LONG

N = 1

********************************************************************

ImageWidth 图像的宽度(像素为单位)

Tag = 256

Type = SHORT/LONG

N = 1

*******

*************************************************************

Make

Tag = 271

Type = ASCII

********************************************************************

MaxSampleValue 最大取样值

Tag = 281

Type = SHORT

N = SamplePerPixel

DefaultValue=(2**BitsPerSample)-1

********************************************************************

MinSampleValue 最小取样值

Tag = 280

Type = SHORT

N = SamplePerPixel

DefaultValue=0

********************************************************************

Model 扫描仪的型号

Tag = 272

Type = ASCII

N = 根据内容来定

********************************************************************

NewSubfileType 图像形态

Tag = 254

Type = LONG

N = 1

Value=1 表示一幅全分辨率的图像

Value=2 表示一幅降低分辨率后的图像

Value=3 表示一幅多页图像中的一页

Value=4 表示一幅透明混迭(Transparency mask)图像

********************************************************************

Orientation 图像行列的编排方向

Tag = 274

Type = SHORT

N = 1

DefaultValue=1

Value=1 1行在上方,1列在左方

Value=2 1行在上方,1列在右方

Value=3 1行在下方,1列在右方

Value=4 1行在下方,1列在左方

Value=5 1行在左方,1列在上方

Value=6 1行在右方,1列在上方

Value=7 1行在右方,1列在下方

Value=8 1行在左方,1列在下方

********************************************************************

PhotometricInterpretation 图像所采用的色彩系统

Tag = 262

Type = SHORT

N = 1

Value=0 对黑白及灰度图像而言,0为白色

Value=1 对黑白及灰度图像而言,0为黑色

Value=2 图像数据以R,G,B的方式存储

Value=3 图像数据采用调色板索引

Value=4 单色的透明混迭图像

Value=5 图像数据以C,M,Y,K的方式存储

Value=6 图像数据以Y,Cb,Cr的方式存储

Value=8 图像以L*,a*,b*的方式存储

********************************************************************

PlanarConfiguration 图像数据的平面排列方式

Tag = 284

Type = SHORT

N = 1

DefaultValue=1

Value=1 单平面格式

Value=2 多平面格式

********************************************************************

ResolutionUnit 图像的分辨率单位

Tag = 296

Type = SHORT

N = 1

DefaultValue=2

Value=1 没有指定单位

Value=2 英寸为单位

Value=3 厘米为单位

********************************************************************

RowsPerStrip 每个条状数据的图像行数

Tag = 278

Type = SHORT/LONG

N = 1

********************************************************************

SamplesPerPixel 每像素的取样数

Tag = 277

Type = SHORT

N = 1

DefaultValue=1

Value=1 表示是黑白,灰度或者调色板图像

Value=3 表示是RGB图像

********************************************************************

Software 生成图像的软件名称及版本号

Tag = 305

Type = ASCII

N = 根据内容来定

***************************

*****************************************

StripByteCounts 每个条状数据在文件中的字节数

Tag = 279

Type = SHORT/LONG

N = StripsPerImage 如果PlanarConfiguration=1

=StripsPerImage*SamplesPerPixel如果PlanarConfiguration=2

其中StripsPerImage不是标签(StripsPerImage=(ImageLength+RowPerStrip-1)/RowsPerStrip

********************************************************************

StripOffsets 每个条状数据在文件中的偏移量

Tag = 273

Type = SHORT/LONG

N = StripsPerImage 如果PlanarConfiguration=1

=StripsPerImage*SamplesPerPixel如果PlanarConfiguration=2

其中StripsPerImage不是标签(StripsPerImage=(ImageLength+RowPerStrip-1)/RowsPerStrip

********************************************************************

SubfileType 已经由NewSubfileType标签取代

Tag = 255

Type = SHORT

N = 1

********************************************************************

Threashholding 定义由非黑白图像变换至黑白图像的技术

Tag = 263

Type = SHORT

N = 1

DefaultValue=1

Value=1 未采用任何技术

Value=2 采用抖色(dither)或半色调(halftone)的技术

Value=3 采用误差扩散(error diffusion)技术

********************************************************************

XResolution 单位长度上的图像水平分辨率

Tag = 282

Type = RATIONAL

N = 1

********************************************************************

YResolution 单位长度上的图像垂直分辨率

Tag = 283

Type = RATIONAL

N = 1

压缩方法说明:

PackBits Compression

PackBits Compression 方法与Pcx图像文件中的压缩方法类似,编码原则

*图像数据是以字节为单位来进行编码

*每条扫描线必须分开压缩

*若遇到重复的图像数据,其长度值为iCount,则先存入数据长度值(iCount-1),然后再存入重复的图像数据。

*若遇到不重复的图像数据,其长度值为iCount,则先存入数据长度值(iCount-1),在存入不重复的图像数据。

假设一条扫描显得数据如下:

0x09 0x09 0x09 0x09 0x09 0x13 0xF9 0x 09 0x08 0x08 0x08

其编码结果为:

0xFC 0x09 0x02 0x13 0xF9 0x09 0xFE 0x08

*由于iCount的最大值仅能为128,所以如果(iCount>128),则必须分几次处理。

假设一条扫描线的数据为132*0x98,则其编码结果为

0x81 0x98 0xFD 0x98

由上可得,如果数据为压缩,该字节左第一位为1

PackBits解码的伪代码如下:

变量 bytevar,iCount;

1. 读一字节数据到bytevar

2. if(bytevar&0x80)

{

if(bytevar!=0x80)

{

iCount=-bytevar+1;

读下一个字节并将其重复iCount次

}

}

else

{

iCount=bytevar+1;

直接读取iCount个字节的数据

};

3.重复(1),(2)过程直至完成一行