A DNG file is a TIFF 6.0 file, in either byte order, that adds the tags on the tags page and restricts some TIFF tags. This page covers how the images inside it are organised and stored, following the DNG 1.6.0.0 specification; the DNG 1.7 additions (JPEG XL and gain maps) come from Adobe’s DNG SDK 1.7.1 source.
Directories
- The file starts with a normal TIFF header:
IIorMM, the number 42, and the offset of IFD 0. Readers must accept both byte orders. - IFD 0 holds
DNGVersionand the other tags that describe the camera and its colour. The specification recommends, but does not require, that IFD 0 be a low-resolution thumbnail. - The raw image and any further images sit in a tree of sub-IFDs listed by the TIFF
tag
SubIFDs(0x014A), as in TIFF/EP. Chains of sub-IFDs (a sub-IFD’s “next IFD” link) are not supported. - Metadata can be Exif and TIFF/EP tags (the Exif IFD,
0x8769, is preferred over IFD 0), IPTC (tag 33723) and XMP (tag 700). Makers may add private tags, private IFDs and a maker note. Orientationis required. Mirrored orientations are allowed but are not valid in TIFF/EP.- The extension is
.dng; readers should also accept.tif.
Each image directory is marked by NewSubFileType (0x00FE):
| NewSubFileType | Image | Since |
|---|---|---|
| 0 | The main image: the highest-resolution, best quality raw data | 1.0 |
| 1 | The primary preview or a reduced-resolution image | 1.0 |
0x10001 | An alternative rendering, shown only on request | 1.2 |
| 4 | Transparency mask of the main image | 1.4 |
| 5 | Transparency mask of a reduced-resolution image | 1.4 |
| 8 | Depth map at the highest resolution | 1.5 |
| 9 | Depth map at a lower resolution | 1.5 |
| 16 | Enhanced image data: a processed, at least demosaiced version of the raw data | 1.5 |
| 32 | Gain map (from the DNG SDK; allowed only in output-referred images) | 1.7 |
0x10004 | Semantic mask, such as a sky or skin mask; there may be several | 1.6 |
Transparency masks have the size of their image. 0 is fully transparent; 1.0 (float) or the largest integer is opaque; values between are partly transparent, and the image keeps its unmatted colour there. Enhanced data uses PhotometricInterpretation 34892 in the raw data’s colour space, as 16-bit integers or 16, 24 or 32-bit floats, and may carry its own BaselineSharpness, NoiseProfile and NoiseReductionApplied. Semantic masks are single-channel 8-bit, 16-bit or 32-bit float images where 0 means “not part of” and the maximum means “part of”, covering ActiveArea.
Samples
BitsPerSampleis 8 to 32, the same for every sample. Sizes other than 8, 16 and 32 are packed into bytes most significant bit first (TIFF FillOrder 1), whatever the file’s byte order.SampleFormatis 1 (unsigned integer) up to DNG 1.3. DNG 1.4 adds 3, floating point, at 16, 24 or 32 bits.- Raw images are usually tiled (
TileWidth,TileLength,TileOffsets,TileByteCounts); strips are also allowed.SubTileBlockSize,RowInterleaveFactorandColumnInterleaveFactorcan reorder the pixels within a tile.
Photometric interpretation
| Value | Used for |
|---|---|
| 1 | Black is zero: monochrome previews, gamma 2.2 unless PreviewColorSpace says otherwise |
| 2 | RGB: previews, sRGB unless PreviewColorSpace says otherwise |
| 6 | YCbCr: JPEG previews |
| 32803 | Colour filter array (TIFF/EP): one sample per pixel through the filter pattern; needs CFARepeatPatternDim and CFAPattern |
| 34892 | Linear raw: every colour at each pixel, in the camera’s colour space; for cameras without a CFA, demosaiced data, reduced-resolution and enhanced images |
| 51177 | Depth map (required for depth IFDs) |
| 52527 | Semantic mask (required for mask IFDs) |
| 4 | Transparency mask (the TIFF value; the DNG SDK uses it for masks) |
| 52553 | Gain map (DNG 1.7, from the SDK; required when NewSubFileType is 32, with 1 or 3 samples) |
Compression
| Compression | Meaning in DNG | Since |
|---|---|---|
| 1 | Uncompressed. | 1.0 |
| 7 | JPEG. Baseline DCT JPEG when the data is 8-bit YCbCr or 8-bit BlackIsZero (previews); otherwise lossless Huffman JPEG, one stream per tile or strip. In lossless JPEG the stream’s width, height and component count need not match the tile, only the total sample count, so CFA data is often coded as if it had fewer, wider rows or more components. | 1.0 |
| 8 | Deflate (ZIP), for floating point data, 32-bit integer data, transparency masks and depth maps. | 1.4 |
| 34892 | Lossy JPEG: 8-bit data with PhotometricInterpretation 34892 (linear raw) or 52527 (mask), and in 1.7 gain maps. Tells the reader to use a DCT decoder where it would otherwise use lossless JPEG. | 1.4 |
| 52546 | JPEG XL, for 8 to 16-bit data with PhotometricInterpretation CFA, RGB, BlackIsZero, depth, linear raw, gain map, semantic mask or transparency mask (from the DNG SDK). | 1.7 |
Semantic masks may be uncompressed, lossless JPEG (7, 8-bit), lossy JPEG (34892, 8 and 16-bit as the 1.6 text puts it) or deflate (8, 8 and 16-bit integers and 32-bit float).
DNG files written before version 1.1 by Adobe software use a lossless JPEG coder that departs from the JPEG standard for 16-bit data: a difference of −32768 is stored with its 16 extra bits, where the standard stores none. Readers of files older than 1.1 should allow for it.
Deflate-compressed data can use these Predictor values (floating point
data has SampleFormat 3, at 16, 24 or 32 bits):
| Predictor | Meaning |
|---|---|
| 1 | None |
| 2 | Horizontal differencing (TIFF) |
| 3 | Floating point: bytes regrouped by significance, then differenced (Adobe’s TIFF technical note 3) |
| 34892 | Horizontal differencing, two samples apart |
| 34893 | Horizontal differencing, four samples apart |
| 34894 | Floating point, two samples apart |
| 34895 | Floating point, four samples apart |
64-bit files
A DNG larger than 4 GB may use the BigTIFF layout (any DNG version; writers should use it only when needed):
| Offset | Size | Value |
|---|---|---|
| 0 | 2 | II or MM |
| 2 | 2 | 43 (not 42) |
| 4 | 2 | 8 |
| 6 | 2 | 0 |
| 8 | 8 | Offset of the first IFD |
In each IFD the entry count, each entry’s element count and value field, and the next-IFD offset are 64-bit. Three types are added: 16 unsigned 64-bit, 17 signed 64-bit, 18 a 64-bit IFD offset.
Colour filter array
A CFA image uses the TIFF/EP tags CFARepeatPatternDim
(0x828D, rows and columns of the repeating pattern) and
CFAPattern (0x828E, one colour code per cell, row by row),
with the DNG tags CFAPlaneColor and CFALayout. The pattern
starts at the top left of ActiveArea. The colour codes are TIFF/EP’s, as
listed in the DNG SDK:
| CFAPattern code | Colour |
|---|---|
| 0 | Red |
| 1 | Green |
| 2 | Blue |
| 3 | Cyan |
| 4 | Magenta |
| 5 | Yellow |
| 6 | White |
A Bayer RGGB sensor has CFARepeatPatternDim 2, 2 and
CFAPattern 0, 1, 1, 2. CFAPlaneColor (default 0, 1, 2) maps
the plane numbers to these codes.
| CFALayout | Sensor grid |
|---|---|
| 1 | Rectangular (square) layout |
| 2 | Staggered: even columns offset down ½ row |
| 3 | Staggered: even columns offset up ½ row |
| 4 | Staggered: even rows offset right ½ column |
| 5 | Staggered: even rows offset left ½ column |
| 6 | Even rows offset up ½ row, even columns offset left ½ column |
| 7 | Even rows offset up ½ row, even columns offset right ½ column |
| 8 | Even rows offset down ½ row, even columns offset left ½ column |
| 9 | Even rows offset down ½ row, even columns offset right ½ column |
Rows and columns are counted from one for CFALayout, so “even” means the second, fourth and so on. Values 6 to 9 date from DNG 1.3; the specification calls 2 to 9 staggered layouts A to H.
From raw values to a picture
- Read the stored samples and apply
OpcodeList1(see opcodes). Defective pixels are either already interpolated over or fixed by a bad pixel opcode here. - Linearise: map each value through
LinearizationTable, if present; values past the end of the table take its last entry. - Subtract black: the black level of each pixel is
BlackLevel(its pattern cell) plusBlackLevelDeltaHfor its column plusBlackLevelDeltaVfor its row. - Rescale by 1 / (
WhiteLevel− the largest black level of the plane), and clip to 0.0 to 1.0. These are the linear reference values; each plane is handled separately. - Apply
OpcodeList2. - Demosaic a CFA image to full colour, then apply
OpcodeList3. - Crop to
DefaultCropOriginandDefaultCropSize, and scale byDefaultScale. - Convert camera colour to CIE XYZ (D50), below.
Camera colour to CIE XYZ
With n colour planes: CM is the n × 3 ColorMatrix, CC the n × n CameraCalibration (identity unless CameraCalibrationSignature equals the profile’s ProfileCalibrationSignature), AB the diagonal AnalogBalance, RM the 3 × n ReductionMatrix and FM the 3 × n ForwardMatrix. With two or three calibrations each is interpolated linearly in inverse correlated colour temperature between the two illuminants around the chosen white balance, or the nearest one is used outside them.
- XYZtoCamera = AB × CC × CM.
- A white balance given as x, y becomes CameraNeutral = XYZtoCamera × XYZ (with Y = 1). The reverse, from AsShotNeutral to x, y, is found by iteration, since the interpolation weight depends on the answer.
- Without ForwardMatrix: CameraToXYZ is the inverse of XYZtoCamera (n = 3), or Inverse(RM × XYZtoCamera) × RM, or the pseudo-inverse; then CameraToXYZ_D50 = CA × CameraToXYZ, where CA adapts the white balance to D50 (linear Bradford recommended).
- With ForwardMatrix: CameraToXYZ_D50 = FM × D × Inverse(AB × CC), where D is the inverse of the diagonal of ReferenceNeutral = Inverse(AB × CC) × CameraNeutral.
- Then the hue/saturation/value map, if any: XYZ to linear ProPhoto RGB, to HSV, look up hue shift, saturation and value scales by trilinear interpolation (hue wraps around; a single division is constant), add the shift, multiply and clip saturation and value to 1.0, and convert back.
With three calibrations, illuminants 1 and 2 must both be present, ColorMatrix3 is required, ForwardMatrix, ReductionMatrix and ProfileHueSatMapData come in all three or none, and the three white points must differ. The third is meant for the “as shot” or a custom illuminant.
Version compatibility
Writers set DNGBackwardVersion to at least the version a feature needs
when they use it:
| Feature | Minimum DNGBackwardVersion |
|---|---|
| Masked pixels stored (ActiveArea) | 1.1 |
| SubTileBlockSize, RowInterleaveFactor or PreviewColorSpace other than default | 1.2 |
| CFALayout 6 to 9 | 1.3 |
| An opcode not marked optional | The opcode’s DNG version |
| Floating point main image, NewSubFileType 4, compression 8 or 34892 in the main image | 1.4 |
| WarpRectilinear2 not marked optional; CalibrationIlluminant1 or 2 set to 255 | 1.6 |
| Depth maps, enhanced data, semantic masks, ProfileGainTableMap | No increase (reading them is optional) |
Sources
- Digital Negative (DNG) Specification 1.6.0.0 – Adobe, December 2021; chapters 2 to 6 and appendix A (read from a copy at paulbourke.net/dataformats/dng/)
- Adobe DNG SDK 1.7.1 source – as bundled with digiKam: dng_tag_values.h (codes, CFA colours) and dng_ifd.cpp (compression and gain map rules)