Nikon has not published NEF. This page describes what open source decoders (dcraw
and its successors) and ExifTool have worked out. A NEF file is a TIFF file; most are
big-endian (MM).
Directories
| Directory | Content |
|---|---|
| IFD 0 | A small uncompressed RGB thumbnail, the camera make and model, and the SubIFDs tag (0x014A) and Exif IFD pointer. |
| Sub-IFD with NewSubfileType 1 | A JPEG preview (JPEGInterchangeFormat and its length). |
| Sub-IFD with NewSubfileType 0 | The raw data: ImageWidth, ImageLength, BitsPerSample 12 or 14, Compression, PhotometricInterpretation 32803 (colour filter array), StripOffsets, StripByteCounts, CFARepeatPatternDim and CFAPattern. |
| Exif IFD | Exposure data and the Nikon maker note (0x927C). |
| Compression | Raw data |
|---|---|
| 1 | Uncompressed, possibly packed (see NEFCompression below) |
| 34713 | Nikon’s own compression |
Maker note
In NEF files the maker note starts with its own header and holds a complete TIFF structure, so its offsets are relative to that inner header, not to the file:
| Offset | Size | Content |
|---|---|---|
| 0 | 6 | Nikon and a zero byte |
| 6 | 4 | Maker note version, such as 02 10 00 00 |
| 10 | 8 | TIFF header: byte order, 42, and the offset of the maker note IFD (usually 8) |
| 18 | varies | The maker note IFD and its data |
Tags a raw decoder needs:
| Tag | Name | Use |
|---|---|---|
0x000C | WB_RBLevels | Red and blue white balance multipliers |
0x001D | SerialNumber | Key for decrypting other tags |
0x003D | BlackLevel | Four black levels, one per CFA colour |
0x0093 | NEFCompression | How the raw data is stored (below) |
0x0096 | NEFLinearizationTable | Decoder header and tone curve for compressed data (below) |
0x0097 | ColorBalance | White balance data; encrypted from version 0200 onwards |
0x00A7 | ShutterCount | Second key for decryption |
0x0E22 | NEFBitDepth | 12 or 14, or 8 × 3 and 16 × 3 for RGB data |
dcraw confirms the header: it compares the first bytes with Nikon, takes the
byte order and the value 42 from the inner TIFF header and jumps to the IFD offset
counted from that header. It reads the four black levels of 0x003D and
shifts each right by 14 minus the raw bits per sample, so on 12-bit files the stored
numbers are on the 14-bit scale. It builds the serial key from the digits of
0x001D, reads the version digits at the start of 0x0097
(the shutter count key applies to versions 200 to 216) and only records where
0x0096 starts, reading the table later. For Compression 34713 it picks
between its packed, YUV, unpacked and Nikon decoders.
Encrypted tags (ShotInfo 0x0091, ColorBalance 0x0097 and
others) are decrypted with an XOR stream whose keys come from look-up tables indexed by
the serial number and by the four bytes of the shutter count combined. The tables are
in dcraw and ExifTool.
NEFCompression
| Value | Storage |
|---|---|
| 1 | Lossy (type 1) |
| 2 | Uncompressed |
| 3 | Lossless |
| 4 | Lossy (type 2) |
| 5 | Striped packed 12 bits |
| 6 | Uncompressed, reduced to 12 bits |
| 7 | Unpacked 12 bits |
| 8 | Small (reduced size raw) |
| 9 | Packed 12 bits |
| 10 | Packed 14 bits |
| 13 | High Efficiency |
| 14 | High Efficiency ★ |
The High Efficiency modes of the Z cameras are reported to use intoPIX’s TicoRAW codec, which is not publicly documented.
Linearisation table
Tag 0x0096 starts the information a decoder needs for Compression 34713.
Numbers are in the maker note’s byte order.
| Offset | Size | Field |
|---|---|---|
| 0 | 1 | Version byte 0: 0x44 lossy, 0x46 lossless |
| 1 | 1 | Version byte 1: 0x20 for lossy type 2 with a split |
| 2 | 8 | Four 16-bit starting predictors: even rows (columns 0, 1), odd rows (columns 0, 1) |
| 10 | 2 | Number of curve points |
| 12 | 2 each | The curve. For version 44 20, outputs for evenly spaced inputs, to be interpolated; otherwise a full lookup table with one entry per input value. Lossless files (0x46) do not use it. |
| 562 | 2 | For version 44 20: the row at which the second Huffman table takes over |
When version byte 0 is 0x49 or byte 1 is 0x58, the data
above starts 2110 bytes later.
Nikon compression
- Rows are coded one after another, each pixel as a Huffman-coded difference. The Huffman code gives the number of difference bits; then come the bits of the difference, read as in lossless JPEG (a leading 0 bit means a negative value).
- The first two pixels of each row are predicted from the pixel two rows above (the starting predictors for the first rows); the others from the pixel two columns to the left. The prediction follows the 2 × 2 colour pattern.
- Lossy files map the result through the curve to the linear 12 or 14-bit value; the curve spaces bright values further apart.
- There are six fixed Huffman tables: 12-bit lossy, 12-bit lossy after the split,
12-bit lossless, and the same three for 14 bits. Version
0x46selects lossless; 14-bit data selects the 14-bit tables.
Uncompressed and packed data is read row by row: packed 12-bit data holds two values in three bytes, 14-bit data four values in seven bytes.
dcraw’s nikon_load_raw shows the details. Tree index: 0 for 12-bit lossy,
2 for lossless (version byte 0 is 0x46), plus 3 for 14-bit data. The
maximum starts at 2 to the power of the bits per sample. If the curve holds more than 1
point and the version is 44 20, the curve entries are placed at multiples of
the maximum divided by (points − 1), the gaps are filled by linear interpolation, and the
split row is read from offset 562 of the table. Otherwise, unless the file is lossless
and when there are at most 0x4001 points, the curve is read as a full lookup table and
its length becomes the maximum; trailing equal entries are trimmed. Decoding a pixel:
- Read a Huffman code i; the length is len = i & 15 and the shift shl = i >> 4.
- The difference is ((bits(len − shl) × 2 + 1) << shl) >> 1. When bit len − 1 of that value is clear (a negative difference), subtract 2len, less 1 if shl is 0.
- For the first two columns the predictor is the stored vertical predictor for that row parity and column, which is updated by the difference; for the others it is the running predictor of the same column parity.
- The result, clipped to 0 to 0x3FFF, indexes the curve.
The six Huffman trees as dcraw lists them (code length counts for lengths 1 to 16, then the leaf values in order):
| Tree | Use | Counts | Leaves |
|---|---|---|---|
| 0 | 12-bit lossy | 0,1,5,1,1,1,1,1,1,2 | 5,4,3,6,2,7,1,0,8,9,11,10,12 |
| 1 | 12-bit lossy, after the split | 0,1,5,1,1,1,1,1,1,2 | 0x39,0x5A,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 |
| 2 | 12-bit lossless | 0,1,4,2,3,1,2 | 5,4,6,3,7,2,8,1,9,0,10,11,12 |
| 3 | 14-bit lossy | 0,1,4,3,1,1,1,1,1,2 | 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 |
| 4 | 14-bit lossy, after the split | 0,1,5,1,1,1,1,1,1,1,2 | 8,0x5C,0x4B,0x3A,0x29,7,6,5,4,3,2,1,0,13,14 |
| 5 | 14-bit lossless | 0,1,4,2,2,3,1,2 | 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 |
Main maker note tags
ExifTool’s Nikon main table lists these tags. Several are sub-directories (marked “sub”). Tags above are not repeated.
| Tags | Names |
|---|---|
0x0001–0x0009 | MakerNoteVersion, ISO, ColorMode, Quality, WhiteBalance, Sharpness, FocusMode, FlashSetting, FlashType |
0x000B–0x0014 | WhiteBalanceFineTune, ProgramShift, ExposureDifference, ISOSelection, DataDump, PreviewIFD (sub), FlashExposureComp, ISOSetting, ColorBalanceA or NRWData (sub) |
0x0016–0x0025 | ImageBoundary, ExternalFlashExposureComp, FlashExposureBracketValue, ExposureBracketValue, ImageProcessing, CropHiSpeed, ExposureTuning, ColorSpace (1 sRGB, 2 Adobe RGB, 4 BT.2100), VRInfo (sub), ImageAuthentication, FaceDetect (sub), ActiveD-Lighting, PictureControlData (sub), WorldTime (sub), ISOInfo (sub) |
0x002A–0x0045 | VignetteControl, DistortInfo (sub), UnknownInfo (sub), UnknownInfo2 (sub), ShutterMode, HDRInfo (sub), MechanicalShutterCount, LocationInfo (sub), ImageSizeRAW (1 large, 2 medium, 3 small), WhiteBalanceFineTune (rational), JPGCompression, CropArea |
0x004E–0x0056 | NikonSettings (sub), ColorTemperatureAuto, MakerNotes0x51 (sub; entry 10 is another copy of NEFCompression), MakerNotes0x56 (sub) |
0x0080–0x0092 | ImageAdjustment, ToneComp, AuxiliaryLens, LensType, Lens, ManualFocusDistance, DigitalZoom, FlashMode, AFInfo (sub), ShootingMode, LensFStops, ContrastCurve, ColorHue, SceneMode, LightSource, ShotInfo (sub, by model), HueAdjustment |
0x0094–0x00AD | SaturationAdj, NoiseReduction, LensData (sub), RawImageCenter, SensorPixelSize, SceneAssist, DateStampMode, RetouchHistory, SerialNumber (text, 0x00A0), ImageDataSize, ImageCount, DeletedImageCount, FlashInfo (sub), ImageOptimization, Saturation, VariProgram, ImageStabilization, AFResponse |
0x00B0–0x00C3 | MultiExposure (sub), HighISONoiseReduction, ToningEffect, PowerUpTime, AFInfo2 (sub), FileInfo (sub), AFTune (sub), RetouchInfo (sub), PictureControlData (sub), SilentPhotography, BarometerInfo (sub) |
0x0E00–0x0E1E | PrintIM, NikonCaptureData, NikonCaptureVersion, NikonCaptureOffsets, NikonScanIFD, NikonCaptureEditVersions, NikonICCProfile, NikonCaptureOutput |
ExifTool’s page does not give the layout of the encrypted ShotInfo, ColorBalance or LensData sub-tables or the decryption algorithm in the text read here; dcraw and ExifTool’s source carry them.
Sources
- ExifTool: Nikon tags – the main tag table and NEFCompression values
- dcraw.c (ncruces mirror) –
parse_makernoteand the decoder choice inapply_tiff, read as a description nikon_load_rawand its six Huffman trees in the same file were re-read for this check.nikon_is_compressedand the packed-bit routines were not in the part of the file read, so the packed bit order is still not confirmed