Skip to content

Read the metadata of a NEF file - #14

Merged
sputier merged 1 commit into
mainfrom
lot-2/metadata
Nov 18, 2026
Merged

sputier merged 1 commit into
mainfrom
lot-2/metadata

Conversation

@sputier

@sputier sputier commented Nov 18, 2026

Copy link
Copy Markdown
Owner

What and why

Lot 2: reading the metadata of a NEF file without decoding any pixel.

  • Public API: RawFile.Open (path, seekable stream, memory), RawFile.ReadMetadata(path), RawMetadata, RawRectangle, CfaPattern, RawCompression, RawMediaTypes.NikonNef (image/x-nikon-nef).
  • The structure is read by a small internal TIFF reader behind an internal interface (INefStructureReader), so another implementation could replace it. Little- and big-endian files, the Nikon MakerNote (own header, own offsets), the largest colour filter array image of the sub-directories.
  • Failure policy: a file that is not a NEF (UnsupportedFormatException), comes from another maker (UnsupportedCameraException) or is damaged or truncated (CorruptRawFileException) fails with a RawException and nothing else. I/O errors propagate; wrong arguments give ArgumentException.
  • Review findings applied: the clock offset follows the date that is reported; zero focal length, f-number, exposure or ISO mean unknown; a bad sub-directory is skipped and reported as damage rather than "unsupported"; the serial number is not part of ToString; handle leak on failure fixed; open file share allows other readers and deletion.

Checklist

  • Automated tests cover the change and pass (77 tests, 1 skipped: the optional test on a folder of real files).
  • No build warning; every public member is documented; dotnet format --verify-no-changes is clean.
  • Documentation updated: README.md, CHANGELOG.md.
  • Code that reads a file stays bounded on hostile input: every value is range-checked in long arithmetic, allocations are bounded (directory buffer, 65 536 strips, 256 characters of text), and tests truncate the file at every length, damage it at random and put boundary values in every field, in both byte orders.
  • No camera file, no personal data and no code taken from another raw decoder.
  • Hot path measured, with an allocation budget.

Measurements

BenchmarkDotNet (short job, one machine): about 1.4 µs and about 3 KB allocated per read from memory; about 0.13 ms from a file of 28 MB already in the system cache. An optional test reads every file of a local folder of 570 real NEF files in well under 50 ms per file.

Not verified

  • Cold-cache reads from a slow disk.
  • Other Nikon bodies, other raw formats, sRAW / high-efficiency NEF (reported as unsupported).
  • The CI workflow on this branch runs for the first time on this pull request.

RawFile.Open (from a path, a seekable stream or memory) and RawFile.ReadMetadata read the structure of the file and give a
RawMetadata: camera, serial number, lens, exposure, ISO, capture date and clock offset, shutter count, image size, active
area, colour filter pattern, bit depth and compression. No pixel is decoded.

The TIFF structure is read by a small internal reader behind an internal interface. Little- and big-endian files, the Nikon
MakerNote with its own header and offsets, and the choice of the largest colour filter array image among the sub-directories
are handled. A file that is not a NEF, comes from another maker, or is damaged or truncated fails with a RawException and
nothing else: tests truncate the file at every length, damage it at random, and put boundary values in every field of its
structure, in both byte orders.

Reading the metadata of a file allocates about 3 KB (checked by a test) and takes about 1.4 microseconds from memory.
BenchmarkDotNet benchmarks are added to the benchmarks project, and an optional test reads every file of a folder named by
RAWNET_SAMPLES_DIR.
@sputier
sputier merged commit b8f6e96 into main Nov 18, 2026
4 checks passed
@sputier
sputier deleted the lot-2/metadata branch November 18, 2026 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant