DNG格式解析

时间:2023-03-10 04:54:49
DNG格式解析

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,往往遵循一个共同的模式,那就是:

  1. 一个短的文件头,它通常包含文件的字节顺序,文件标识符和主数据的文件偏移量摄像传感器元数据,用来描述图像传感器的数据,包括传感器的尺寸, 颜色滤波矩阵(color filter array)的属性和它的颜色配置文件
  2. 列入任何CMS环境或数据库中需要的图像元数据。这些措施包括曝光设定,相机/扫描仪/镜头模型,拍摄/扫描日期(和可选的位置信息),创作信息和其他。一些原始文件包含可交换图像格式的标准化元数据节;
  3. 图像缩略图;
  4. 可选的JPEG格式缩小尺寸的图像,可用于快速预览;
  5. 在电影胶片扫描中,无论是文件序列的时间码,序列码或帧编号都代表在扫描卷轴中的帧顺序。这个项目允许文件被被按帧顺序排列(不依赖于它的文件名​);
  6. 传感器图像数据;

许多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之间的关系

  1. TIFF和DNG同为Specification,分别定义了后缀名为.tif/.tiff和.dng的文件格式同时在TIFF Specification也定义个baseline及部分扩展的tag。
  2. TIFF/EP则定义并规范了在电子影像中所使用的TAG。
  3. 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

DNG格式解析

图2-1 IFH的结构

DNG格式解析

图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,这里有几个比较特殊的情况:

  1. 占用的字节对于4个,那么valueOffset存储的是文件指针的偏移量,通过文件指针可以索引到该属性的具体值。真正的数据,存储是按照文件顺序存储。

DNG格式解析

  1. 占用的字节不大于4字节,分为两种情况:
  2. 对于图像的真实数据,这里存放的是图像真实数据的偏移量,占4个字节,要通过索引才能正确访问图像数据。
  3. 对于其他的数据,如该type类型占用1个字节,lengths为4,那么总的字节为4个,这里有4个值,每个值占用1各字节,存储是按照文件顺序存储。

DNG格式解析

如果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属性:

SubIFDs

DNG规范中推荐使用子IFD的属性结构

IFD0 {

SubIFD0

SubIFD1

}

IFD0通常是提供一个低分辨便于预览的缩略图,NewSubfileType = 1

SubIFD通常提供高分辨的图像,如RAW数据或者YUV数据NewSubfileType = 0;

Adobe的DNG规范中不支持链式的存储,因此用于预览的IFD中至少要有一个SubIFD

PhotometricInterpretation

颜色空间 SHORT 长度为1

对应最亮灰度.
1 = BlackIsZero. 应用于灰度或者二值图像. 0对应最暗灰度。
2 = RGB. 正常RGB图像,存储顺序为R,G,B.
3 = Palette color. 索引图像, ColorMap必须定义,SamplesPerPixel必须1.

32803 = CFA (Color Filter Array) 表征RAW图像数据

DNG格式解析

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

名称

简短描述

十进制码

十六进制

00FE

NewSubfileType

新的子文件类型标识 LONG 长度为1

用比特来标识图像的类型

代表缩略图

代表多页图像中的某一页

代表它是透明度掩码图像

其余的位数暂时没有定义。与SubfileType的是,此Tag用比特位来区分文件类型而不是用值来区分

00FF

SubfileType

子文件类型标识 SHORT 长度为1

1 全分辨率图像

2 缩小分辨率的图像

3 多页图像的某一页

过时的Tag,已不再使用。

ImageWidth

ImageLength

BitsPerSample

每个分量的Bit数 SHORT 长度为SamplesPerPixel

Compression

压缩类型 SHORT 长度为1

随着TIFF的不断扩张,目前支持多达几十种的压缩方式。最需要关注的有以下两个值:

:没有压缩

:JPEG压缩。如果是RGB图像并且SamplesPerPixel=3,则是标准的有损JPEG压缩。如果是CFA图像,则是Lossless JPEG

PhotometricInterpretation

颜色空间 SHORT 长度为1

对应最亮灰度.
1 = BlackIsZero. 应用于灰度或者二值图像. 0对应最暗灰度。
2 = RGB. 正常RGB图像,存储顺序为R,G,B.
3 = Palette color. 索引图像, ColorMap必须定义,SamplesPerPixel必须1.

32803 = CFA (Color Filter Array)

Threshholding

定义了转换成二值图像的阈值,忽略之

CellWidth

The width of the dithering or halftoning matrix used to create a dithered or halftoned bilevel file.

CellLength

The length of the dithering or halftoning matrix used to create a dithered or halftoned bilevel file.

010A

FillOrder

010E

ImageDescription

字符串,对图像的描述

010F

Make

字符串,生产厂商的描述

Model

字符串

StripOffsets

每个Strip的偏移量 SHORT或者LONG

N = StripsPerImage for PlanarConfiguration equal to 1; N = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2

Orientation

The orientation of the image with respect to the rows and columns.

SamplesPerPixel

RowsPerStrip

。Default是无限大

StripsPerImage = floor ((ImageLength + RowsPerStrip - 1) / RowsPerStrip).

StripByteCounts

每个Strip的长度 SHORT或者LONG

N = StripsPerImage for PlanarConfiguration equal to 1; N = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2

MinSampleValue

The minimum component value used.

MaxSampleValue

The maximum component value used.

011A

XResolution

The number of pixels per ResolutionUnit in the ImageWidth direction.

011B

YResolution

The number of pixels per ResolutionUnit in the ImageLength direction.

011C

PlanarConfiguration

How the components of each pixel are stored.

FreeOffsets

For each string of contiguous unused bytes in a TIFF file, the byte offset of the string.

FreeByteCounts

For each string of contiguous unused bytes in a TIFF file, the number of bytes in the string.

GrayResponseUnit

The precision of the information contained in the Gray Response Curve.

GrayResponseCurve

For grayscale data, the optical density of each possible pixel value.

ResolutionUnit

The unit of measurement for XResolution and YResolution.

Software

Name and version number of the software package(s) used to create the image.

DateTime

Date and time of image creation.

013B

Artist

Person who created the image.

013C

HostComputer

The computer and/or operating system in use at the time of image creation.

ColorMap

调色板

ExtraSamples

Description of extra components.

Copyright

Copyright notice.

下面为扩展的tags(TIFF/EP)

010D

DocumentName

The name of the document from which this image was scanned.

011D

PageName

The name of the page from which this image was scanned.

011E

XPosition

X position of the image.

011F

YPosition

Y position of the image.

T4Options

Options for Group 3 Fax compression

T6Options

Options for Group 4 Fax compression

PageNumber

The page number of the page from which this image was scanned.

012D

TransferFunction

Describes a transfer function for the image in tabular style.

013D

Predictor

A mathematical operator that is applied to the image data before an encoding scheme is applied.

013E

WhitePoint

The chromaticity of the white point of the image.

013F

PrimaryChromaticities

The chromaticities of the primaries of the image.

HalftoneHints

Conveys to the halftone function the range of gray levels within a colorimetrically-specified image that should retain tonal detail.

TileWidth

The tile width in pixels. This is the number of columns in each tile.

TileLength

The tile length (height) in pixels. This is the number of rows in each tile.

TileOffsets

For each tile, the byte offset of that tile, as compressed and stored on disk.

TileByteCounts

For each tile, the number of (compressed) bytes in that tile.

BadFaxLines

Used in the TIFF-F standard, denotes the number of 'bad' scan lines encountered by the facsimile device.

CleanFaxData

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.

ConsecutiveBadFaxLines

Used in the TIFF-F standard, denotes the maximum number of consecutive 'bad' scanlines received.

014A

SubIFDs

Offset to child IFDs.

014C

InkSet

The set of inks used in a separated (PhotometricInterpretation=5) image.

014D

InkNames

The name of each ink used in a separated image.

014E

NumberOfInks

The number of inks.

DotRange

The component values that correspond to a 0% dot and 100% dot.

TargetPrinter

A description of the printing environment for which this separation is intended.

SampleFormat

Specifies how to interpret each data sample in a pixel.

SMinSampleValue

Specifies the minimum sample value.

SMaxSampleValue

Specifies the maximum sample value.

TransferRange

Expands the range of the TransferFunction.

ClipPath

Mirrors the essentials of PostScript's path creation functionality.

XClipPathUnits

The number of units that span the width of the image, in terms of integer ClipPath coordinates.

YClipPathUnits

The number of units that span the height of the image, in terms of integer ClipPath coordinates.

015A

Indexed

Aims to broaden the support for indexed images to include support for any color space.

015B

JPEGTables

JPEG quantization and/or Huffman tables.

015F

OPIProxy

OPI-related.

GlobalParametersIFD

Used in the TIFF-FX standard to point to an IFD containing tags that are globally applicable to the complete TIFF file.

ProfileType

Used in the TIFF-FX standard, denotes the type of data stored in this file or IFD.

FaxProfile

Used in the TIFF-FX standard, denotes the 'profile' that applies to this file.

CodingMethods

Used in the TIFF-FX standard, indicates which coding methods are used in the file.

VersionYear

Used in the TIFF-FX standard, denotes the year of the standard specified by the FaxProfile field.

ModeNumber

Used in the TIFF-FX standard, denotes the mode of the standard specified by the FaxProfile field.

01B1

Decode

Used in the TIFF-F and TIFF-FX standards, holds information about the ITULAB (PhotometricInterpretation = 10) encoding.

01B2

DefaultImageColor

Defined in the Mixed Raster Content part of RFC 2301, is the default color needed in areas where no image is available.

JPEGProc

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGInterchangeFormat

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGInterchangeFormatLength

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGRestartInterval

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGLosslessPredictors

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGPointTransforms

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGQTables

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGDCTables

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

JPEGACTables

Old-style JPEG compression field. TechNote2 invalidates this part of the specification.

YCbCrCoefficients

The transformation from RGB to YCbCr image data.

YCbCrSubSampling

Specifies the subsampling factors used for the chrominance components of a YCbCr image.

YCbCrPositioning

Specifies the positioning of subsampled chrominance components relative to luminance samples.

ReferenceBlackWhite

Specifies a pair of headroom and footroom image data values (codes) for each pixel component.

022F

StripRowCounts

Defined in the Mixed Raster Content part of RFC 2301, used to replace RowsPerStrip for IFDs with variable-sized strips.

02BC

XMP

XML packet containing XMP metadata

800D

ImageID

OPI-related.

87AC

ImageLayer

Defined in the Mixed Raster Content part of RFC 2301, used to denote the particular function of this Image in the mixed raster scheme.

 

Private TIFF Tags for raw and DNG TAGS

80A4

Wang Annotation

Annotation data, as used in 'Imaging for Windows'.

82A5

MD FileTag

Specifies the pixel data format encoding in the Molecular Dynamics GEL file format.

82A6

MD ScalePixel

Specifies a scale factor in the Molecular Dynamics GEL file format.

82A7

MD ColorTable

Used to specify the conversion from 16bit to 8bit in the Molecular Dynamics GEL file format.

82A8

MD LabName

Name of the lab that scanned this file, as used in the Molecular Dynamics GEL file format.

82A9

MD SampleInfo

Information about the sample, as used in the Molecular Dynamics GEL file format.

82AA

MD PrepDate

Date the sample was prepared, as used in the Molecular Dynamics GEL file format.

82AB

MD PrepTime

Time the sample was prepared, as used in the Molecular Dynamics GEL file format.

82AC

MD FileUnits

Units for data in this file, as used in the Molecular Dynamics GEL file format.

830E

ModelPixelScaleTag

Used in interchangeable GeoTIFF files.

83BB

IPTC

IPTC (International Press Telecommunications Council) metadata.

847E

INGR Packet Data Tag

Intergraph Application specific storage.

847F

INGR Flag Registers

Intergraph Application specific flags.

IrasB Transformation Matrix

Originally part of Intergraph's GeoTIFF tags, but likely understood by IrasB only.

ModelTiepointTag

Originally part of Intergraph's GeoTIFF tags, but now used in interchangeable GeoTIFF files.

85D8

ModelTransformationTag

Used in interchangeable GeoTIFF files.

Photoshop

Collection of Photoshop 'Image Resource Blocks'.

Exif IFD

A pointer to the Exif IFD.

ICC Profile

ICC profile data.

87AF

GeoKeyDirectoryTag

Used in interchangeable GeoTIFF files.

87B0

GeoDoubleParamsTag

Used in interchangeable GeoTIFF files.

87B1

GeoAsciiParamsTag

Used in interchangeable GeoTIFF files.

GPS IFD

A pointer to the Exif-related GPS Info IFD.

885C

HylaFAX FaxRecvParams

Used by HylaFAX.

885D

HylaFAX FaxSubAddress

Used by HylaFAX.

885E

HylaFAX FaxRecvTime

Used by HylaFAX.

935C

ImageSourceData

Used by Adobe Photoshop.

A005

Interoperability IFD

A pointer to the Exif-related Interoperability IFD.

A480

GDAL_METADATA

Used by the GDAL library, holds an XML list of name=value 'metadata' values about the image as a whole, and about specific samples.

A481

GDAL_NODATA

Used by the GDAL library, contains an ASCII encoded nodata or background pixel value.

C427

Oce Scanjob Description

Used in the Oce scanning process.

C428

Oce Application Selector

Used in the Oce scanning process.

C429

Oce Identification Number

Used in the Oce scanning process.

C42A

Oce ImageLogic Characteristics

Used in the Oce scanning process.

C612

DNGVersion

Used in IFD 0 of DNG files.

C613

DNGBackwardVersion

Used in IFD 0 of DNG files.

C614

UniqueCameraModel

Used in IFD 0 of DNG files.

C615

LocalizedCameraModel

Used in IFD 0 of DNG files.

C616

CFAPlaneColor

Used in Raw IFD of DNG files.

C617

CFALayout

Used in Raw IFD of DNG files.

C618

LinearizationTable

Used in Raw IFD of DNG files.

C619

BlackLevelRepeatDim

Used in Raw IFD of DNG files.

C61A

BlackLevel

Used in Raw IFD of DNG files.

C61B

BlackLevelDeltaH

Used in Raw IFD of DNG files.

C61C

BlackLevelDeltaV

Used in Raw IFD of DNG files.

C61D

WhiteLevel

Used in Raw IFD of DNG files.

C61E

DefaultScale

Used in Raw IFD of DNG files.

C61F

DefaultCropOrigin

Used in Raw IFD of DNG files.

C620

DefaultCropSize

Used in Raw IFD of DNG files.

C621

ColorMatrix1

Used in IFD 0 of DNG files.

C622

ColorMatrix2

Used in IFD 0 of DNG files.

C623

CameraCalibration1

Used in IFD 0 of DNG files.

C624

CameraCalibration2

Used in IFD 0 of DNG files.

C625

ReductionMatrix1

Used in IFD 0 of DNG files.

C626

ReductionMatrix2

Used in IFD 0 of DNG files.

C627

AnalogBalance

Used in IFD 0 of DNG files.

C628

AsShotNeutral

Used in IFD 0 of DNG files.

C629

AsShotWhiteXY

Used in IFD 0 of DNG files.

C62A

BaselineExposure

Used in IFD 0 of DNG files.

C62B

BaselineNoise

Used in IFD 0 of DNG files.

C62C

BaselineSharpness

Used in IFD 0 of DNG files.

C62D

BayerGreenSplit

Used in Raw IFD of DNG files.

C62E

LinearResponseLimit

Used in IFD 0 of DNG files.

C62F

CameraSerialNumber

Used in IFD 0 of DNG files.

C630

LensInfo

Used in IFD 0 of DNG files.

C631

ChromaBlurRadius

Used in Raw IFD of DNG files.

C632

AntiAliasStrength

Used in Raw IFD of DNG files.

C634

DNGPrivateData

Used in IFD 0 of DNG files.

C635

MakerNoteSafety

Used in IFD 0 of DNG files.

C65A

CalibrationIlluminant1

Used in IFD 0 of DNG files.

C65B

CalibrationIlluminant2

Used in IFD 0 of DNG files.

C65C

BestQualityScale

Used in Raw IFD of DNG files.

C660

Alias Layer Metadata

Alias Sketchbook Pro layer usage description.

DNG数据获取

  1. 利用文件指针打开.DNG文件,读取DNG的IFH信息,获取dng数据的字节顺序byteorder和第一个IFD的字节偏移。
  2. 定位到第一个IFD位置,获取DE的数目。依次遍历DE,获取每一个DE的tags,type,count,valueOffsets.
  3. 根据文档的说明查询对应的tags属性,获取该IFD图像的相关信息。根据需要处理的数据,主要获取以下tags数据:

imagewidth

imagehight

TileOffsets

BitsPerSample

Compaession

PhotometricInterpretation

CFAPlaneColor

SubIFDs

    WhiteBalance

TIFF Tag AsShotNeutral

IFD

DNG格式解析

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的两倍(因为我们的眼睛对绿色更敏感)

DNG格式解析

图2-3 四种Bayer矩阵排列形式

在矩阵内的每个CCD或CMOS只是用来感受光子的能量,因应进入光线的强度而产生对应比例的电荷,然后将这些电荷信息汇集并经过放大,储存起来,我们可以这样去想象一下充满电荷的CCD/CMOS,就像下图一样:。而应当知道的是,RAW纪录的只是每个像素位置的电荷值,它是没有记录任何的颜色信息的。所以CCD是"色盲"的,也就是说: RAW文件只是灰度文件而已,如图2-5所示。

当Image Sensor往外逐行输出数据时,像素的序列为GRGRGR.../BGBGBG...(顺序RGB)。这样阵列的Sensor设计,使得RGB传感器减少到了全色传感器的1/3,如下所示。

DNG格式解析

DNG格式解析

图 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所示的效果。

DNG格式解析

图2-5 原始的RaW数据

DNG格式解析

图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

http://wenku.baidu.com/link?url=f1TyjVTjtbGEiZk9LhBdNVup42xBx35iRlU_pnm5NtxwXJtAt1uiX4SzxWRd86GyKtbl72ofSFwNrBGRcEOEzlTuDnanC7XTQE_gNGf3-wC