TIFF檔案格式中文解說.txt

pamxy發表於2013-07-07

轉自:http://wenku.baidu.com/view/1e0ebe0c76c66137ef061905.html

本文根據Adobe的Tiff Revision 6.0文件整理而成,同時部分的參考了清華大學出版的<影象檔案格式>一書




本文整理了tiff檔案的基本格式及其包括的常用標籤的說明:
對於tiff檔案中使用的壓縮方法,本文沒能詳細說明


如果發現有錯誤存在,請各位指正


xuhappy   xuxuhappy@sina.com        2000-7-17








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


1.BiLevel 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
ImageWidth 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)過程直至完成一行

相關文章