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 PhotographyISO
RAW
RAW文件包含创建一个可视图像所必须的相机传感器数据信息。RAW文件的结构,包括ISO标准的RAW图像格式ISO 12234-2 ,TIFF / EP,往往遵循一个共同的模式,那就是:
- 一个短的文件头,它通常包含文件的,文件标识符和主数据的文件偏移量摄像传感器,用来描述图像传感器的数据,包括传感器的尺寸, 颜色滤波矩阵(color filter array)的属性和它的颜色配置文件
- 列入任何CMS环境或中需要的图像。这些措施包括曝光设定,相机/扫描仪/镜头模型,拍摄/扫描日期(和可选的位置信息),创作信息和其他。一些原始文件包含可交换图像格式的标准化元数据节;
- 图像缩略图;
- 可选的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个,则这里存放的是指向实际数据的指针。
- 占用的字节对于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
SubIFD通常提供高分辨的图像,如RAW数据或者YUV数据NewSubfileType
Adobe的DNG规范中不支持链式的存储,因此用于预览的IFD中至少要有一个SubIFD
1
.
. 0
姝e父RGBRGB.
, ColorMapSamplesPerPixel1.
RAW
CFA矩阵
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
CFAPatterncfa_patR G G B 4
表 2-1 TIFF文件IFD中的tag属性
tag | |||
254 | 00FE | 1 Bit01 Bit11 Bit21 SubfileTypeTag | |
255 | 00FF | 1 Tag | |
256 | 0100 | 1 | |
257 | 0101 | 1 | |
258 | 0102 | BitSamplesPerPixel | |
259 | 0103 | 1 TIFF Compression1 Compression7JPEGRGBSamplesPerPixel3JPEGCFALossless JPEG | |
262 | 0106 | 1 . 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 | StripLONG 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 | 1 | |
278 | 0116 | Strip1Default StripsPerImage = floor ((ImageLength + RowsPerStrip - 1) / RowsPerStrip). | |
279 | 0117 | StripLONG 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 |