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

圖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,這裏有幾個比較特殊的情況:

  1. 佔用的字節對於4個,那麼valueOffset存儲的是文件指針的偏移量,通過文件指針可以索引到該屬性的具體值。真正的數據,存儲是按照文件順序存儲。

  1. 佔用的字節不大於4字節,分爲兩種情況:
  2. 對於圖像的真實數據,這裏存放的是圖像真實數據的偏移量,佔4個字節,要通過索引才能正確訪問圖像數據。
  3. 對於其他的數據,如該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屬性:

SubIFDs

DNG規範中推薦使用子IFD的屬性結構

IFD0 {

SubIFD0

SubIFD1

}

IFD0通常是提供一個低分辨便於預覽的縮略圖,NewSubfileType = 1

SubIFD通常提供高分辨的圖像,如RAW數據或者YUV數據NewSubfileType = 0;

Adobe的DNG規範中不支持鏈式的存儲,因此用於預覽的IFD中至少要有一個SubIFD

PhotometricInterpretation

    顏色空間 SHORT 長度爲1

0 = WhiteIsZero. 應用於灰度或者二值圖像, 0對應最亮灰度.
1 = BlackIsZero. 
應用於灰度或者二值圖像. 0對應最暗灰度。
2 = RGB. 
正常RGB圖像,存儲順序爲RGB.
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

NewSubfileType

新的子文件類型標識 LONG 長度爲1

用比特來標識圖像的類型

Bit0如果是1代表縮略圖

Bit1如果是1代表多頁圖像中的某一頁

Bit2如果是1代表它是透明度掩碼圖像

其餘的位數暫時沒有定義。與SubfileType的是,此Tag用比特位來區分文件類型而不是用值來區分

255

00FF

SubfileType

子文件類型標識 SHORT 長度爲1

全分辨率圖像

縮小分辨率的圖像

多頁圖像的某一頁

過時的Tag,已不再使用。

256

0100

ImageWidth

圖像寬度 SHORT或者LONG 長度爲1

257

0101

ImageLength

圖像高度 SHORT或者LONG 長度爲1

258

0102

BitsPerSample

每個分量的Bit SHORT 長度爲SamplesPerPixel

259

0103

Compression

壓縮類型 SHORT 長度爲1

隨着TIFF的不斷擴張,目前支持多達幾十種的壓縮方式。最需要關注的有以下兩個值:

Compression1沒有壓縮

Compression7JPEG壓縮。如果是RGB圖像並且SamplesPerPixel3,則是標準的有損JPEG壓縮。如果是CFA圖像,則是Lossless JPEG

262

0106

PhotometricInterpretation

顏色空間 SHORT 長度爲1

0 = WhiteIsZero. 應用於灰度或者二值圖像, 0對應最亮灰度.
1 = BlackIsZero. 
應用於灰度或者二值圖像. 0對應最暗灰度。
2 = RGB. 
正常RGB圖像,存儲順序爲RGB.
3 = Palette color. 
索引圖像, ColorMap必須定義,SamplesPerPixel必須1.

32803 = CFA (Color Filter Array)

263

0107

Threshholding

定義了轉換成二值圖像的閾值,忽略之

264

0108

CellWidth

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

265

0109

CellLength

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

266

010A

FillOrder

在同一字節中的邏輯順序。一般不做定義,要定義的話建議用1

270

010E

ImageDescription

字符串,對圖像的描述

271

010F

Make

字符串,生產廠商的描述

272

0110

Model

字符串

273

0111

StripOffsets

每個Strip的偏移量 SHORT或者LONG

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

274

0112

Orientation

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

277

0115

SamplesPerPixel

每個象素的通道數 SHORT 長度爲1

278

0116

RowsPerStrip

每個Strip有多少行 SHORT或者LONG 長度爲1Default是無限大

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

279

0117

StripByteCounts

每個Strip的長度 SHORT或者LONG

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

280

0118

MinSampleValue

The minimum component value used.

281

0119

MaxSampleValue

The maximum component value used.

282

011A

XResolution

The number of pixels per ResolutionUnit in the ImageWidth direction.

283

011B

YResolution

The number of pixels per ResolutionUnit in the ImageLength direction.

284

011C

PlanarConfiguration

How the components of each pixel are stored.

288

0120

FreeOffsets

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

289

0121

FreeByteCounts

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

290

0122

GrayResponseUnit

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

291

0123

GrayResponseCurve

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

296

0128

ResolutionUnit

The unit of measurement for XResolution and YResolution.

305

0131

Software

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

306

0132

DateTime

Date and time of image creation.

315

013B

Artist

Person who created the image.

316

013C

HostComputer

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

320

0140

ColorMap

調色板

338

0152

ExtraSamples

Description of extra components.

33432

8298

Copyright

Copyright notice.

下面爲擴展的tags(TIFF/EP)

269

010D

DocumentName

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

285

011D

PageName

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

286

011E

XPosition

X position of the image.

287

011F

YPosition

Y position of the image.

292

0124

T4Options

Options for Group 3 Fax compression

293

0125

T6Options

Options for Group 4 Fax compression

297

0129

PageNumber

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

301

012D

TransferFunction

Describes a transfer function for the image in tabular style.

317

013D

Predictor

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

318

013E

WhitePoint

The chromaticity of the white point of the image.

319

013F

PrimaryChromaticities

The chromaticities of the primaries of the image.

321

0141

HalftoneHints

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

322

0142

TileWidth

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

323

0143

TileLength

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

324

0144

TileOffsets

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

325

0145

TileByteCounts

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

326

0146

BadFaxLines

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

327

0147

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.

328

0148

ConsecutiveBadFaxLines

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

330

014A

SubIFDs

Offset to child IFDs.

332

014C

InkSet

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

333

014D

InkNames

The name of each ink used in a separated image.

334

014E

NumberOfInks

The number of inks.

336

0150

DotRange

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

337

0151

TargetPrinter

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

339

0153

SampleFormat

Specifies how to interpret each data sample in a pixel.

340

0154

SMinSampleValue

Specifies the minimum sample value.

341

0155

SMaxSampleValue

Specifies the maximum sample value.

342

0156

TransferRange

Expands the range of the TransferFunction.

343

0157

ClipPath

Mirrors the essentials of PostScript's path creation functionality.

344

0158

XClipPathUnits

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

345

0159

YClipPathUnits

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

346

015A

Indexed

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

347

015B

JPEGTables

JPEG quantization and/or Huffman tables.

351

015F

OPIProxy

OPI-related.

400

0190

GlobalParametersIFD

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

401

0191

ProfileType

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

402

0192

FaxProfile

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

403

0193

CodingMethods

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

404

0194

VersionYear

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

405

0195

ModeNumber

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

433

01B1

Decode

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

434

01B2

DefaultImageColor

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

512

0200

JPEGProc

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

513

0201

JPEGInterchangeFormat

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

514

0202

JPEGInterchangeFormatLength

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

515

0203

JPEGRestartInterval

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

517

0205

JPEGLosslessPredictors

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

518

0206

JPEGPointTransforms

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

519

0207

JPEGQTables

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

520

0208

JPEGDCTables

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

521

0209

JPEGACTables

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

529

0211

YCbCrCoefficients

The transformation from RGB to YCbCr image data.

530

0212

YCbCrSubSampling

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

531

0213

YCbCrPositioning

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

532

0214

ReferenceBlackWhite

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

559

022F

StripRowCounts

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

700

02BC

XMP

XML packet containing XMP metadata

32781

800D

ImageID

OPI-related.

34732

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

32932

80A4

Wang Annotation

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

33445

82A5

MD FileTag

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

33446

82A6

MD ScalePixel

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

33447

82A7

MD ColorTable

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

33448

82A8

MD LabName

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

33449

82A9

MD SampleInfo

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

33450

82AA

MD PrepDate

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

33451

82AB

MD PrepTime

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

33452

82AC

MD FileUnits

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

33550

830E

ModelPixelScaleTag

Used in interchangeable GeoTIFF files.

33723

83BB

IPTC

IPTC (International Press Telecommunications Council) metadata.

33918

847E

INGR Packet Data Tag

Intergraph Application specific storage.

33919

847F

INGR Flag Registers

Intergraph Application specific flags.

33920

8480

IrasB Transformation Matrix

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

33922

8482

ModelTiepointTag

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

34264

85D8

ModelTransformationTag

Used in interchangeable GeoTIFF files.

34377

8649

Photoshop

Collection of Photoshop 'Image Resource Blocks'.

34665

8769

Exif IFD

A pointer to the Exif IFD.

34675

8773

ICC Profile

ICC profile data.

34735

87AF

GeoKeyDirectoryTag

Used in interchangeable GeoTIFF files.

34736

87B0

GeoDoubleParamsTag

Used in interchangeable GeoTIFF files.

34737

87B1

GeoAsciiParamsTag

Used in interchangeable GeoTIFF files.

34853

8825

GPS IFD

A pointer to the Exif-related GPS Info IFD.

34908

885C

HylaFAX FaxRecvParams

Used by HylaFAX.

34909

885D

HylaFAX FaxSubAddress

Used by HylaFAX.

34910

885E

HylaFAX FaxRecvTime

Used by HylaFAX.

37724

935C

ImageSourceData

Used by Adobe Photoshop.

40965

A005

Interoperability IFD

A pointer to the Exif-related Interoperability IFD.

42112

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.

42113

A481

GDAL_NODATA

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

50215

C427

Oce Scanjob Description

Used in the Oce scanning process.

50216

C428

Oce Application Selector

Used in the Oce scanning process.

50217

C429

Oce Identification Number

Used in the Oce scanning process.

50218

C42A

Oce ImageLogic Characteristics

Used in the Oce scanning process.

50706

C612

DNGVersion

Used in IFD 0 of DNG files.

50707

C613

DNGBackwardVersion

Used in IFD 0 of DNG files.

50708

C614

UniqueCameraModel

Used in IFD 0 of DNG files.

50709

C615

LocalizedCameraModel

Used in IFD 0 of DNG files.

50710

C616

CFAPlaneColor

Used in Raw IFD of DNG files.

50711

C617

CFALayout

Used in Raw IFD of DNG files.

50712

C618

LinearizationTable

Used in Raw IFD of DNG files.

50713

C619

BlackLevelRepeatDim

Used in Raw IFD of DNG files.

50714

C61A

BlackLevel

Used in Raw IFD of DNG files.

50715

C61B

BlackLevelDeltaH

Used in Raw IFD of DNG files.

50716

C61C

BlackLevelDeltaV

Used in Raw IFD of DNG files.

50717

C61D

WhiteLevel

Used in Raw IFD of DNG files.

50718

C61E

DefaultScale

Used in Raw IFD of DNG files.

50719

C61F

DefaultCropOrigin

Used in Raw IFD of DNG files.

50720

C620

DefaultCropSize

Used in Raw IFD of DNG files.

50721

C621

ColorMatrix1

Used in IFD 0 of DNG files.

50722

C622

ColorMatrix2

Used in IFD 0 of DNG files.

50723

C623

CameraCalibration1

Used in IFD 0 of DNG files.

50724

C624

CameraCalibration2

Used in IFD 0 of DNG files.

50725

C625

ReductionMatrix1

Used in IFD 0 of DNG files.

50726

C626

ReductionMatrix2

Used in IFD 0 of DNG files.

50727

C627

AnalogBalance

Used in IFD 0 of DNG files.

50728

C628

AsShotNeutral

Used in IFD 0 of DNG files.

50729

C629

AsShotWhiteXY

Used in IFD 0 of DNG files.

50730

C62A

BaselineExposure

Used in IFD 0 of DNG files.

50731

C62B

BaselineNoise

Used in IFD 0 of DNG files.

50732

C62C

BaselineSharpness

Used in IFD 0 of DNG files.

50733

C62D

BayerGreenSplit

Used in Raw IFD of DNG files.

50734

C62E

LinearResponseLimit

Used in IFD 0 of DNG files.

50735

C62F

CameraSerialNumber

Used in IFD 0 of DNG files.

50736

C630

LensInfo

Used in IFD 0 of DNG files.

50737

C631

ChromaBlurRadius

Used in Raw IFD of DNG files.

50738

C632

AntiAliasStrength

Used in Raw IFD of DNG files.

50740

C634

DNGPrivateData

Used in IFD 0 of DNG files.

50741

C635

MakerNoteSafety

Used in IFD 0 of DNG files.

50778

C65A

CalibrationIlluminant1

Used in IFD 0 of DNG files.

50779

C65B

CalibrationIlluminant2

Used in IFD 0 of DNG files.

50780

C65C

BestQualityScale

Used in Raw IFD of DNG files.

50784

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

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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章