Since DNG 1.3, a file can carry processing steps as opcodes: lens distortion and
vignetting corrections, bad pixel repair, gain maps and so on. They sit in the tags
OpcodeList1, OpcodeList2 and OpcodeList3 of the
raw image directory, applied at three stages described on the
raw data page. This page follows the DNG
1.6.0.0 specification, checked against Adobe’s DNG SDK 1.7.1, which defines no further
opcodes.
Opcode list
An opcode list is always big-endian, whatever the byte order of the file, so it can be copied between files without being understood.
| Field | Type | Meaning |
|---|---|---|
| Count | LONG | Number of opcodes |
| Opcodes | varies | One after another, applied in order |
Each opcode:
| Field | Type | Meaning |
|---|---|---|
| Opcode ID | LONG | Which opcode (below) |
| DNG version | LONG | The DNG version that defined it, as four bytes, such as 01 03 00 00 |
| Flags | LONG | Bit 0 (1): optional, a reader may skip it if it does not know it. Bit 1 (2): may be skipped when making a quick preview. |
| Parameter bytes | LONG | Size of the parameters that follow |
| Parameters | varies | Depends on the opcode |
A reader may skip an optional opcode, for example one it does not know, using the
parameter size. It should never process an opcode whose DNG version is newer than the
specification it was written for. After each opcode the values are clipped to the
image’s range: 0 to 216 − 1 in OpcodeList1 (0 to
232 − 1 for data deeper than 16 bits), 0.0 to 1.0 in lists 2 and 3.
One exception to applying every opcode in order: if a WarpRectilinear2 marked optional is immediately followed in the same list by a WarpRectilinear or WarpFisheye, a reader that applies WarpRectilinear2 skips that next opcode. This lets a file carry an accurate warp for DNG 1.6 readers and a simpler one for older readers.
Opcode IDs
| ID | Opcode | Does |
|---|---|---|
| 1 | WarpRectilinear | Corrects lens distortion and lateral chromatic aberration of a normal lens |
| 2 | WarpFisheye | Unwraps a fisheye lens to a rectilinear image |
| 3 | FixVignetteRadial | Corrects radial vignetting |
| 4 | FixBadPixelsConstant | Replaces pixels that hold a given constant value |
| 5 | FixBadPixelsList | Replaces listed bad pixels and rectangles |
| 6 | TrimBounds | Crops the image |
| 7 | MapTable | Maps values through a lookup table |
| 8 | MapPolynomial | Maps values through a polynomial |
| 9 | GainMap | Multiplies by a smoothly varying gain, such as a lens shading map |
| 10 | DeltaPerRow | Adds a value to each row |
| 11 | DeltaPerColumn | Adds a value to each column |
| 12 | ScalePerRow | Multiplies each row by a value |
| 13 | ScalePerColumn | Multiplies each column by a value |
| 14 | WarpRectilinear2 | WarpRectilinear with radial terms up to r14, odd and even, an optional division form and a valid radius range (DNG 1.6) |
Opcodes 1 to 13 date from DNG 1.3 and carry version 01 03 00 00;
WarpRectilinear2 carries 01 06 00 00. ID 0 is used inside the DNG SDK
only and is never written to files.
Area specification
Opcodes 7 to 13 start their parameters with an area: which pixels and planes they act on.
| Field | Type | Meaning |
|---|---|---|
| Top | LONG | First row |
| Left | LONG | First column |
| Bottom | LONG | Bottom edge of the area |
| Right | LONG | Right edge of the area |
| Plane | LONG | First plane |
| Planes | LONG | Number of planes, at least 1 |
| RowPitch | LONG | Act on every n-th row, at least 1 |
| ColPitch | LONG | Act on every n-th column, at least 1 |
With a pitch above 1 only every RowPitch-th row from Top and every ColPitch-th column from Left is changed, which lets one opcode act on a single colour of a Bayer pattern, for example pitch 2 starting at row 0 and column 1. The DNG SDK rejects Planes, RowPitch or ColPitch below 1.
Lens corrections
The optical centre (ĉx, ĉy) is normalised from the top-left pixel: 0.5, 0.5 is the middle and 1, 0 the top-right pixel.
| Opcode | Parameters, in order |
|---|---|
| 1 WarpRectilinear | N (LONG, 1 for all planes or one set per plane); for each set 6 DOUBLE: kr0, kr1, kr2, kr3, kt0, kt1; then ĉx, ĉy (DOUBLE). 1, 0, 0, 0, 0, 0 is no warp. |
| 2 WarpFisheye | N (LONG); for each set 4 DOUBLE: kr0–kr3; then ĉx, ĉy (DOUBLE). |
| 3 FixVignetteRadial | k0–k4 (5 DOUBLE), then ĉx, ĉy (DOUBLE). All zero is no change. |
| 14 WarpRectilinear2 | N (LONG); for each set 19 DOUBLE: kr0–kr14, kt0, kt1, min_valid_radius, max_valid_radius; then ĉx, ĉy (DOUBLE) and reciprocalRadial (LONG). |
All three use the same geometry. With (x0, y0) and (x1, y1) the top-left and bottom-right pixels: cx = x0 + ĉx(x1 − x0), likewise cy; m = √(mx² + my²) where mx = max(|x0 − cx|, |x1 − cx|), likewise my; Δx = (x − cx) / m, Δy = (y − cy) / m, r = √(Δx² + Δy²). Each output pixel (x, y) takes the input pixel (x′, y′), resampled with a kernel such as a cubic spline.
- WarpRectilinear: f = kr0 + kr1r² + kr2r⁴ + kr3r⁶; x′ = cx + m(fΔx + Δxt), y′ = cy + m(fΔy + Δyt), with Δxt = kt0·2ΔxΔy + kt1(r² + 2Δx²) and Δyt = kt1·2ΔxΔy + kt0(r² + 2Δy²). The warp must be invertible and increasing in x and y, and kr0r + kr1r³ + kr2r⁵ + kr3r⁷ increasing on [0, 1].
- WarpFisheye: θ = arctan r; f = (kr0θ + kr1θ³ + kr2θ⁵ + kr3θ⁷) / r; x′ = cx + m·f·Δx, y′ = cy + m·f·Δy. The bracketed sum must increase with r on [0, 1].
- WarpRectilinear2: f(r) = Σ krprp for p = 0 to 14. With reciprocalRadial 0, rreal = rideal × f(clamp(rideal, min, max)); otherwise rreal = rideal / f(clamp(…)). f must be one-to-one on the valid range, and 0 ≤ min_valid_radius < max_valid_radius ≤ 1 (normally 0 and 1). The tangential terms are as in WarpRectilinear.
- FixVignetteRadial: each value is multiplied by g = 1 + k0r² + k1r⁴ + k2r⁶ + k3r⁸ + k4r¹⁰, with r the distance from the centre divided by m.
Separate coefficient sets per plane correct lateral chromatic aberration.
Bad pixels and trimming
| Opcode | Parameters, in order |
|---|---|
| 4 FixBadPixelsConstant | Constant (LONG): the value that marks a bad pixel; BayerPhase (LONG): position of the Bayer pattern (0 to 3). 8 bytes. |
| 5 FixBadPixelsList | BayerPhase (LONG); point count (LONG); rectangle count (LONG); each point as row, column (2 LONG); each rectangle as top, left, bottom, right (4 LONG). 12 + 8 × points + 16 × rectangles bytes. |
| 6 TrimBounds | Top, left, bottom, right (4 LONG): the area to keep. |
BayerPhase gives the colour of the top-left pixel: 0 red, 1 green in a green/red
row, 2 green in a green/blue row, 3 blue. The bad pixel opcodes interpolate over
pixels of a Bayer CFA image; the specification places them in
OpcodeList1, and the DNG SDK accepts them only on single-plane 16-bit
data.
Value mapping
| Opcode | Parameters after the area |
|---|---|
| 7 MapTable | TableSize (LONG); entries (SHORT each). Each 16-bit value v becomes entry v. The DNG SDK accepts 1 to 65536 entries and fills the rest of the 65536-entry table by repeating an end entry; the specification does not say what happens past the end. |
| 8 MapPolynomial | Degree (LONG, at most 8); Degree + 1 coefficients (DOUBLE), constant term first. |
| 9 GainMap | Points down and across (2 LONG); spacing down and across (2 DOUBLE); origin down and across (2 DOUBLE); map planes (LONG); then the gains (FLOAT), row by row, column by column, with the planes innermost. |
| 10 DeltaPerRow | Count (LONG, one per row of the area at its row pitch); values (FLOAT), added. |
| 11 DeltaPerColumn | Count (LONG, one per column at the column pitch); values (FLOAT), added. |
| 12 ScalePerRow | Count (LONG); values (FLOAT), multiplied. |
| 13 ScalePerColumn | Count (LONG); values (FLOAT), multiplied. |
The counts of the four per-row and per-column opcodes must equal the number of rows
or columns the area and pitch select. Gain map positions are relative, 1.0 being the
height or width of the image: point i down is at origin + i × spacing.
Gains are interpolated bilinearly inside the map and held at its edges outside it.
If Planes is greater than MapPlanes, the last map plane serves the remaining
planes.
A list
00 00 00 01 count: 1 opcode
00 00 00 06 ID 6, TrimBounds
01 03 00 00 DNG 1.3.0.0
00 00 00 01 flags: optional
00 00 00 10 16 bytes of parameters
00 00 00 08 00 00 00 08 00 00 0F A8 00 00 17 78 top 8, left 8, bottom 4008, right 6008
Sources
- Digital Negative (DNG) Specification 1.6.0.0 – Adobe, December 2021; chapter 7, opcode list processing (read from a copy at paulbourke.net/dataformats/dng/)
- Adobe DNG SDK 1.7.1 source – as bundled with digiKam: dng_opcodes.h, dng_misc_opcodes.cpp and dng_bad_pixels.cpp