Author:Maddock
Date:2015.04.22
轉載請注明出處:http://www.cnblogs.com/adong7639/p/4446828.html
DNG格式基本概念
DNG格式是在TIFF的基礎上擴展出來的,要了解DNG,需要清楚TIFF, TIFF/EP, DNG,RAW之間的關系。
TIFF/EP
TIFF是一個靈活適應性強的文件格式。通過在文件標頭中使用"標簽",它能夠在一個文件中處理多幅圖像和數據。標簽能夠標明圖像的如圖像大小這樣的基本幾何尺寸,或者定義圖像數據是如何排列的,或者是否使用了各種各樣的圖像壓縮選項。
TIFF/EP的全稱是"Tag Image File Format / Electronic Photography "。 它是一個名為"Electronic still-picture imaging – Removable memory – Part 2: TIFF/EP image data format"ISO標准, 標准號為ISO 12234-2。TIFF/EP對TIFF文件擴展部分標記屬性。
RAW
RAW文件包含創建一個可視圖像所必須的相機傳感器數據信息。RAW文件的結構,包括ISO標准的RAW圖像格式ISO 12234-2 ,TIFF / EP,往往遵循一個共同的模式,那就是:
-
圖像縮略圖;
-
可選的JPEG格式縮小尺寸的圖像,可用於快速預覽;
-
在電影膠片掃描中,無論是文件序列的時間碼,序列碼或幀編號都代表在掃描卷軸中的幀順序。這個項目允許文件被被按幀順序排列(不依賴於它的文件名);
-
傳感器圖像數據;
許多RAW格式, 包括 3FR (Hasselblad), DCR, K25, KDC (Kodak), IIQ (Phase One), CR2 (Canon), ERF (Epson), MEF (Mamiya), MOS (Leaf), NEF (Nikon), ORF (Olympus), PEF (Pentax), RW2 (Panasonic) and ARW, SRF, SR2 (Sony), 都是基於TIFF格式。 這些文件可能在許多方面偏離TIFF標准,包括使用一個非標准的文件頭,列入額外的圖像標記和一些標簽的數據加密。
DNG
DNG(Digital Negative)是Adobe開發的一種開放的raw image file format,主要是為了統一不同廠商raw格式。里面使用的tag基本上都定義在TIFF或者TIFF/EP中, 在DNG Sepcification中只是定義或者建議了數據的組織方式,顏色空間的轉換等等。
TIFF, TIFF/EP, DNG, RAW之間的關系
-
TIFF和DNG同為Specification,分別定義了后綴名為.tif/.tiff和.dng的文件格式同時在TIFF Specification也定義個baseline及部分擴展的tag。
-
TIFF/EP則定義並規范了在電子影像中所使用的TAG。
-
DNG同時與TIFF和TIFF/EP兼容,並包含了EXIF和XMP信息。DNG實際上就是擴張的TIFF, 把DNG的擴展名改成TIF就可以直接預覽圖片。
在DNG出現以前,各個數碼相機制造商都有自己的格式,比如Canon(cr2/crw), Nikon(nef), Olympus(orf), Pentex(pef)等等。之所以出現這么多格式,一方面的原因是在這之前沒有統一的raw格式, 但更重要的是,各個廠商希望用這個只對自己公開的數據格式來保護自己的私密信息。Adobe推出DNG希望能一統raw的天下。
DNG格式的結構
TIFF6.0格式結構
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。在解析DNG文件時,讀取圖像數據需要根據ByteOrder來正確獲取數據。
Byte 2-3: TIFF的標志位,一般都是42,表示該圖像為tiff格式。
Byte 4-7: 第一個IFD的偏移量。可以在任意位置, 但必須是在一個字的邊界,也就是說必須是2的整數倍。
IFD的構成(0代表此IFD的起始位置)
IFD是TIF圖中最重要的數據結構,它包含了一個TIF文件中最重要的信息,一個TIF圖可能有多個IFD,這說明文件中有多個圖像,每個IFD標識1 個圖像的基本屬性。 IFD結構中包含了三類成員,Directory Entry Count指出該結構里面有多少個目錄入口;接下來就是N個線性排列的DE序列,數量不定(這就是為什么稱TIF格式文件為可擴充標記的文件,甚至用戶可以添加自定義的標記屬性),每個DE標識了圖像的某一個屬性;最后就是一個偏移量, 標識下一個文件目錄相對於文件開始處的位置,當然,如果該TIF文件只包含了一幅圖像,那么就只有一個IFD,顯然,這個偏移量就等於0;
Byte 0-1: 表示此IFD包含了多少個DE,假設數目為n
Byte 2-(n*12+1): n個DE
Byte (n*12+2)-(n*12+5): 下一個IFD的偏移量,如果沒有則置為0
圖2-1 IFH的結構
圖2-2 IFD與DE的結構
DE的構成
簡單說,一個DE就是一幅圖像的某一個屬性。例如圖像的大小、分辨率、是否壓縮、像素的行列數、一個像素由幾位 表示(1位代表黑白兩色,8位代表256色等等)等。其中:tag成員是該屬性的編號,在圖像文件目錄中,它是按照升序排列的。我們可以通過讀 這些編號,然后到TIF格式官方白皮書中查找相應的含義。屬性是用數據來表示的,那么type就是代表着該數據的類型。每個DE共12個字節:
Byte 0-1: 此TAG的唯一標識
Byte 2-3: type數據類型。
Byte 4-7: lenghts數量。通過類型和數量可以確定存儲此TAG的數據需要占據的字節數
Byte 8-11: valueOffset是tag標識的屬性代表的變量值相對文件開始處的偏移量。如果占用的字節數少於4,那么該值就存放在 valueOffset中即可,沒必要再另外指向一個地方了。如果超過4個,則這里存放的是指向實際數據的指針。
對於valueOffset,這里有幾個比較特殊的情況:
-
占用的字節對於4個,那么valueOffset存儲的是文件指針的偏移量,通過文件指針可以索引到該屬性的具體值。真正的數據,存儲是按照文件順序存儲。
-
占用的字節不大於4字節,分為兩種情況:
-
對於圖像的真實數據,這里存放的是圖像真實數據的偏移量,占4個字節,要通過索引才能正確訪問圖像數據。
-
對於其他的數據,如該type類型占用1個字節,lengths為4,那么總的字節為4個,這里有4個值,每個值占用1各字節,存儲是按照文件順序存儲。
如果type類型占用2個字節,lengths為1,那么總的字節為2個,這里有1個值,存儲按照文件順序存儲,后面的數據為空。
Value1 0
對於type的數據類型,TIF官方指定的有5種數據類型。
type=1就是BYTE類型(8位無標記整數)
type=2是ASCII類型(7位ASCII碼加1位二進制0)
type=3是SHORT類型 (16位無標記整數)
type=4是LONG 類型(32位無標記整數)
type=5是RATIONAL類型(2個LONG,第一個是分子,第二個是分母)。
之后新增了7種類型:
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.
TIFF的基本tags屬性
表中的tags可以在http://www.awaresystems.be/imaging/tiff/tifftags/tileoffsets.html中查詢。也可以參看TIFF6.0規范和adobe 提供的dng文檔。
比較重要的Tag屬性:
DNG規范中推薦使用子IFD的屬性結構
IFD0 {
SubIFD0
SubIFD1
}
IFD0通常是提供一個低分辨便於預覽的縮略圖,NewSubfileType = 1
SubIFD通常提供高分辨的圖像,如RAW數據或者YUV數據NewSubfileType = 0;
Adobe的DNG規范中不支持鏈式的存儲,因此用於預覽的IFD中至少要有一個SubIFD
顏色空間 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) 表征RAW圖像數據
CFA矩陣
Filters 0x16161616: 0x61616161: 0x49494949: 0x94949494:
Bayer模式1 Bayer模式2 Bayer模式3 Bayer模式4
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
0 B G B G B G 0 G R G R G R 0 G B G B G B 0 R G R G R G
1 G R G R G R 1 B G B G B G 1 R G R G R G 1 G B G B G B
2 B G B G B G 2 G R G R G R 2 G B G B G B 2 R G R G R G
3 G R G R G R 3 B G B G B G 3 R G R G R G 3 G B G B G B
CFAPlaneColor Default 0, 1, 2 (red, green, blue)
R 0 G 1 B 2
Bayer模式1
B G B G 2 1 2 1
G R G R 1 0 1 0
B G B G 2 1 2 1
CFAPattern利用cfa_pat矩陣來存儲R G G B 4個像素點
cfa_pat[0] cfa_pat[1]
cfa_pat[2] cfa_pat[3]
R 0 G 1 B 2
filters = 0x16161616
B G 2 1
G R 1 0
filters = 0x61616161
G R 1 0
B G 2 1
filters = 0x49494949
G B 1 2
R G 0 1
filters = 0x94949494
R G 0 1
G B 1 2
表 2-1 TIFF文件IFD中的tag屬性
tag |
名稱 |
簡短描述 |
|
十進制碼 |
十六進制 |
||
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 Gray Response Curve. |
|
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. |
|
下面為擴展的tags(TIFF/EP) |
|||
269 |
010D |
The name of the document from which this image was scanned. |
|
285 |
011D |
The name of the page from which this image was scanned. |
|
286 |
011E |
X position of the image. |
|
287 |
011F |
Y position of the image. |
|
292 |
0124 |
Options for Group 3 Fax compression |
|
293 |
0125 |
Options for Group 4 Fax compression |
|
297 |
0129 |
The page number of the page from which this image was scanned. |
|
301 |
012D |
Describes a transfer function for the image in tabular style. |
|
317 |
013D |
A mathematical operator that is applied to the image data before an encoding scheme is applied. |
|
318 |
013E |
The chromaticity of the white point of the image. |
|
319 |
013F |
The chromaticities of the primaries of the image. |
|
321 |
0141 |
Conveys to the halftone function the range of gray levels within a colorimetrically-specified image that should retain tonal detail. |
|
322 |
0142 |
The tile width in pixels. This is the number of columns in each tile. |
|
323 |
0143 |
The tile length (height) in pixels. This is the number of rows in each tile. |
|
324 |
0144 |
For each tile, the byte offset of that tile, as compressed and stored on disk. |
|
325 |
0145 |
For each tile, the number of (compressed) bytes in that tile. |
|
326 |
0146 |
Used in the TIFF-F standard, denotes the number of 'bad' scan lines encountered by the facsimile device. |
|
327 |
0147 |
Used in the TIFF-F standard, indicates if 'bad' lines encountered during reception are stored in the data, or if 'bad' lines have been replaced by the receiver. |
|
328 |
0148 |
Used in the TIFF-F standard, denotes the maximum number of consecutive 'bad' scanlines received. |
|
330 |
014A |
Offset to child IFDs. |
|
332 |
014C |
The set of inks used in a separated (PhotometricInterpretation=5) image. |
|
333 |
014D |
The name of each ink used in a separated image. |
|
334 |
014E |
The number of inks. |
|
336 |
0150 |
The component values that correspond to a 0% dot and 100% dot. |
|
337 |
0151 |
A description of the printing environment for which this separation is intended. |
|
339 |
0153 |
Specifies how to interpret each data sample in a pixel. |
|
340 |
0154 |
Specifies the minimum sample value. |
|
341 |
0155 |
Specifies the maximum sample value. |
|
342 |
0156 |
Expands the range of the TransferFunction. |
|
343 |
0157 |
Mirrors the essentials of PostScript's path creation functionality. |
|
344 |
0158 |
The number of units that span the width of the image, in terms of integer ClipPath coordinates. |
|
345 |
0159 |
The number of units that span the height of the image, in terms of integer ClipPath coordinates. |
|
346 |
015A |
Aims to broaden the support for indexed images to include support for any color space. |
|
347 |
015B |
JPEG quantization and/or Huffman tables. |
|
351 |
015F |
OPI-related. |
|
400 |
0190 |
Used in the TIFF-FX standard to point to an IFD containing tags that are globally applicable to the complete TIFF file. |
|
401 |
0191 |
Used in the TIFF-FX standard, denotes the type of data stored in this file or IFD. |
|
402 |
0192 |
Used in the TIFF-FX standard, denotes the 'profile' that applies to this file. |
|
403 |
0193 |
Used in the TIFF-FX standard, indicates which coding methods are used in the file. |
|
404 |
0194 |
Used in the TIFF-FX standard, denotes the year of the standard specified by the FaxProfile field. |
|
405 |
0195 |
Used in the TIFF-FX standard, denotes the mode of the standard specified by the FaxProfile field. |
|
433 |
01B1 |
Used in the TIFF-F and TIFF-FX standards, holds information about the ITULAB (PhotometricInterpretation = 10) encoding. |
|
434 |
01B2 |
Defined in the Mixed Raster Content part of RFC 2301, is the default color needed in areas where no image is available. |
|
512 |
0200 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
513 |
0201 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
514 |
0202 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
515 |
0203 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
517 |
0205 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
518 |
0206 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
519 |
0207 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
520 |
0208 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
521 |
0209 |
Old-style JPEG compression field. TechNote2 invalidates this part of the specification. |
|
529 |
0211 |
The transformation from RGB to YCbCr image data. |
|
530 |
0212 |
Specifies the subsampling factors used for the chrominance components of a YCbCr image. |
|
531 |
0213 |
Specifies the positioning of subsampled chrominance components relative to luminance samples. |
|
532 |
0214 |
Specifies a pair of headroom and footroom image data values (codes) for each pixel component. |
|
559 |
022F |
Defined in the Mixed Raster Content part of RFC 2301, used to replace RowsPerStrip for IFDs with variable-sized strips. |
|
700 |
02BC |
XML packet containing XMP metadata |
|
32781 |
800D |
OPI-related. |
|
34732 |
87AC |
Defined in the Mixed Raster Content part of RFC 2301, used to denote the particular function of this Image in the mixed raster scheme. |
|
|
|||
32932 |
80A4 |
Annotation data, as used in 'Imaging for Windows'. |
|
33445 |
82A5 |
Specifies the pixel data format encoding in the Molecular Dynamics GEL file format. |
|
33446 |
82A6 |
Specifies a scale factor in the Molecular Dynamics GEL file format. |
|
33447 |
82A7 |
Used to specify the conversion from 16bit to 8bit in the Molecular Dynamics GEL file format. |
|
33448 |
82A8 |
Name of the lab that scanned this file, as used in the Molecular Dynamics GEL file format. |
|
33449 |
82A9 |
Information about the sample, as used in the Molecular Dynamics GEL file format. |
|
33450 |
82AA |
Date the sample was prepared, as used in the Molecular Dynamics GEL file format. |
|
33451 |
82AB |
Time the sample was prepared, as used in the Molecular Dynamics GEL file format. |
|
33452 |
82AC |
Units for data in this file, as used in the Molecular Dynamics GEL file format. |
|
33550 |
830E |
Used in interchangeable GeoTIFF files. |
|
33723 |
83BB |
IPTC (International Press Telecommunications Council) metadata. |
|
33918 |
847E |
Intergraph Application specific storage. |
|
33919 |
847F |
Intergraph Application specific flags. |
|
33920 |
8480 |
Originally part of Intergraph's GeoTIFF tags, but likely understood by IrasB only. |
|
33922 |
8482 |
Originally part of Intergraph's GeoTIFF tags, but now used in interchangeable GeoTIFF files. |
|
34264 |
85D8 |
Used in interchangeable GeoTIFF files. |
|
34377 |
8649 |
Collection of Photoshop 'Image Resource Blocks'. |
|
34665 |
8769 |
A pointer to the Exif IFD. |
|
34675 |
8773 |
ICC profile data. |
|
34735 |
87AF |
Used in interchangeable GeoTIFF files. |
|
34736 |
87B0 |
Used in interchangeable GeoTIFF files. |
|
34737 |
87B1 |
Used in interchangeable GeoTIFF files. |
|
34853 |
8825 |
A pointer to the Exif-related GPS Info IFD. |
|
34908 |
885C |
Used by HylaFAX. |
|
34909 |
885D |
Used by HylaFAX. |
|
34910 |
885E |
Used by HylaFAX. |
|
37724 |
935C |
Used by Adobe Photoshop. |
|
40965 |
A005 |
A pointer to the Exif-related Interoperability IFD. |
|
42112 |
A480 |
Used by the GDAL library, holds an XML list of name=value 'metadata' values about the image as a whole, and about specific samples. |
|
42113 |
A481 |
Used by the GDAL library, contains an ASCII encoded nodata or background pixel value. |
|
50215 |
C427 |
Used in the Oce scanning process. |
|
50216 |
C428 |
Used in the Oce scanning process. |
|
50217 |
C429 |
Used in the Oce scanning process. |
|
50218 |
C42A |
Used in the Oce scanning process. |
|
50706 |
C612 |
Used in IFD 0 of DNG files. |
|
50707 |
C613 |
Used in IFD 0 of DNG files. |
|
50708 |
C614 |
Used in IFD 0 of DNG files. |
|
50709 |
C615 |
Used in IFD 0 of DNG files. |
|
50710 |
C616 |
Used in Raw IFD of DNG files. |
|
50711 |
C617 |
Used in Raw IFD of DNG files. |
|
50712 |
C618 |
Used in Raw IFD of DNG files. |
|
50713 |
C619 |
Used in Raw IFD of DNG files. |
|
50714 |
C61A |
Used in Raw IFD of DNG files. |
|
50715 |
C61B |
Used in Raw IFD of DNG files. |
|
50716 |
C61C |
Used in Raw IFD of DNG files. |
|
50717 |
C61D |
Used in Raw IFD of DNG files. |
|
50718 |
C61E |
Used in Raw IFD of DNG files. |
|
50719 |
C61F |
Used in Raw IFD of DNG files. |
|
50720 |
C620 |
Used in Raw IFD of DNG files. |
|
50721 |
C621 |
Used in IFD 0 of DNG files. |
|
50722 |
C622 |
Used in IFD 0 of DNG files. |
|
50723 |
C623 |
Used in IFD 0 of DNG files. |
|
50724 |
C624 |
Used in IFD 0 of DNG files. |
|
50725 |
C625 |
Used in IFD 0 of DNG files. |
|
50726 |
C626 |
Used in IFD 0 of DNG files. |
|
50727 |
C627 |
Used in IFD 0 of DNG files. |
|
50728 |
C628 |
Used in IFD 0 of DNG files. |
|
50729 |
C629 |
Used in IFD 0 of DNG files. |
|
50730 |
C62A |
Used in IFD 0 of DNG files. |
|
50731 |
C62B |
Used in IFD 0 of DNG files. |
|
50732 |
C62C |
Used in IFD 0 of DNG files. |
|
50733 |
C62D |
Used in Raw IFD of DNG files. |
|
50734 |
C62E |
Used in IFD 0 of DNG files. |
|
50735 |
C62F |
Used in IFD 0 of DNG files. |
|
50736 |
C630 |
Used in IFD 0 of DNG files. |
|
50737 |
C631 |
Used in Raw IFD of DNG files. |
|
50738 |
C632 |
Used in Raw IFD of DNG files. |
|
50740 |
C634 |
Used in IFD 0 of DNG files. |
|
50741 |
C635 |
Used in IFD 0 of DNG files. |
|
50778 |
C65A |
Used in IFD 0 of DNG files. |
|
50779 |
C65B |
Used in IFD 0 of DNG files. |
|
50780 |
C65C |
Used in Raw IFD of DNG files. |
|
50784 |
C660 |
Alias Sketchbook Pro layer usage description. |
DNG數據獲取
-
利用文件指針打開.DNG文件,讀取DNG的IFH信息,獲取dng數據的字節順序byteorder和第一個IFD的字節偏移。
-
定位到第一個IFD位置,獲取DE的數目。依次遍歷DE,獲取每一個DE的tags,type,count,valueOffsets.
-
根據文檔的說明查詢對應的tags屬性,獲取該IFD圖像的相關信息。根據需要處理的數據,主要獲取以下tags數據:
imagewidth
imagehight
TileOffsets
BitsPerSample
Compaession
PhotometricInterpretation
CFAPlaneColor
SubIFDs
WhiteBalance
TIFF Tag AsShotNeutral
IFD |
|
Image |
Code |
50728 (hex 0xC628) |
|
Name |
AsShotNeutral |
|
LibTiff name |
TIFFTAG_ASSHOTNEUTRAL |
|
Type |
SHORT or RATIONAL |
|
Count |
ColorPlanes |
|
Default |
None |
Used in IFD 0 of DNG files.
AsShotNeutral specifies the selected white balance at time of capture, encoded as the coordinates of a perfectly neutral color in linear reference space values. The inclusion of this tag precludes the inclusion of the AsShotWhiteXY tag.
DNG數據的第一個IFD提供了預覽圖,在這個IFD中包含了其他IFD的偏移位置,存儲在SubIFDs的信息中。因此要獲取原始大圖的RAW數據,需要通過第一個IFD的SubIFDs,索引到大圖的IFD。
數據處理偽代碼
FILE *fp = fopen("test.dng",wb);
byteorder = fread(fp,2);
firstifdpos = fread(fp,4);
firstifd = fseek(fp, firstifdpos);
denum = fread(fp,2);
//解析DNG頭文件
for i = 1 : denum
{
tags = fread(fp,2);
type = fread(fp,2);
count = fread(fp,4);
value = fread(fp,4);
switch(tags)
{
case 256:
get imgwidht;
case 257:
get imgheigh;
......
Case 324:
Get dataoffsets;
…….
Case 330:
Process subIFD1 ,subIFD2
……
}
}
Rawimg = fread(fp, dataoffsets);
……
//數據后處理
……
原始的RAW數據存儲形式
主流的數碼相機傳感器,主要有CCD、CMOS和Foveon X3。CCD或CMOS這些圖像傳感器都幾乎歸類為Mosaic Sensor(馬賽克感光元件),或稱為Color Filter Array(彩色濾鏡陣列)元件。Foveon X3成像技術的圖像傳感器,不屬於上述的類別。,這里主要只簡單地講一下CCD/ CMOS的工作方式,對我們使用raw就已經足夠了。
數碼相機傳感器是由橫豎兩個方向密集排列的感光元件(CCD或CMOS)組成的一個二維矩陣,常見的有圖2-3示的4種Bayer模式的排列方式,每個CCD就對應一個像素。其中R感應紅光、G感應綠光、B感應藍光,而在Bayer模式中G是R和B的兩倍(因為我們的眼睛對綠色更敏感)
圖2-3 四種Bayer矩陣排列形式
在矩陣內的每個CCD或CMOS只是用來感受光子的能量,因應進入光線的強度而產生對應比例的電荷,然后將這些電荷信息匯集並經過放大,儲存起來,我們可以這樣去想象一下充滿電荷的CCD/CMOS,就像下圖一樣:。而應當知道的是,RAW紀錄的只是每個像素位置的電荷值,它是沒有記錄任何的顏色信息的。所以CCD是"色盲"的,也就是說: RAW文件只是灰度文件而已,如圖2-5所示。
當Image Sensor往外逐行輸出數據時,像素的序列為GRGRGR.../BGBGBG...(順序RGB)。這樣陣列的Sensor設計,使得RGB傳感器減少到了全色傳感器的1/3,如下所示。
圖 2-4 CMOS光子感應示意圖
因此任何一個RAW Converter(如Photoshop的Camera RAW Plugin,Bibble、Phrase One C1 Pro、RawShooter essentials 2005,各廠商自帶的Raw轉換軟件等)的作用就是將這些像素所記錄的亮度信息轉換成為肉眼所能看見的顏色信息。由於現在的CCD/CMOS與Foveon X3的原理不一樣,所以對於CCD/CMOS而言,要獲得一個像素上的顏色值,必須從鄰近的像素中獲取信息來進行一種叫做"反馬賽克"的運算 (Foveon X3不需要這樣),從而得到該位置的顏色值,如圖2-6所示的效果。
圖2-5 原始的RaW數據
圖2-6 插值顏色后的彩色數據
參考文檔
dng_spec_1.4.0.0.pdf
TIFF6.pdf
http://zh.wikipedia.org/wiki/TIFF
http://en.wikipedia.org/wiki/TIFF/EP
http://blog.csdn.net/pkeel/article/details/1490823
http://blog.csdn.net/han_jiang_xue/article/details/8266207
http://www.siliconimaging.com/RGB%20Bayer.htm
http://www.awaresystems.be/imaging/tiff/tifftags/search.html