Skip to content

Read the embedded JPEG previews - #15

Merged
sputier merged 1 commit into
mainfrom
lot-3/preview
Nov 18, 2026
Merged

sputier merged 1 commit into
mainfrom
lot-3/preview

Conversation

@sputier

@sputier sputier commented Nov 18, 2026

Copy link
Copy Markdown
Owner

What and why

Lot 3: the embedded JPEG previews of a NEF file (issue #3).

  • RawFile.Previews (the previews found in the sub-directories and in the Nikon MakerNote, largest first, each place once), ReadPreviewJpeg() (the largest, or null), ReadPreviewJpeg(preview) (a new array) and ReadPreviewJpeg(preview, buffer) (no allocation).
  • A preview that does not lie inside the file, is shorter than 2 bytes or larger than 64 MiB is left out; data that does not start with a JPEG start-of-image marker is reported as CorruptRawFileException when read.
  • Review findings applied: ByteCount is an int, Previews is a read-only wrapper, disposal is idempotent and thread-safe, the sort and de-duplication allocate nothing more than needed, the result array is not zeroed first, docs and README corrected.

Checklist

  • Automated tests cover the change and pass (94 tests, 2 skipped: the optional tests on real files).
  • No build warning; every public member is documented; dotnet format --verify-no-changes is clean.
  • Documentation updated: README.md, CHANGELOG.md.
  • Bounded on hostile input: ranges are checked in long arithmetic against the file length; the fuzz tests (random damage and boundary values in every field, both byte orders, with a MakerNote preview) read every preview.
  • No camera file, no personal data and no code taken from another raw decoder.

Measurements

BenchmarkDotNet (short job): reading a 700 KB preview into a buffer takes about 30 µs and allocates nothing; into a new array about 140 µs (the array is the allocation). Opening a file allocates about 3.3 KB (budget 4 KB, checked by a test).

Not verified

  • Cold-cache reads from a slow disk.
  • Other Nikon bodies and other makers.

RawFile.Previews lists the JPEG previews the library finds in the file, in its sub-directories and in the Nikon MakerNote,
from the largest to the smallest. ReadPreviewJpeg reads the largest one, or one of them into a new array or into a buffer of
the caller, which allocates nothing. A preview that does not lie inside the file is left out, and data that does not start
with a JPEG start-of-image marker is reported as corrupt.

Reading a preview of 700 KB into a buffer takes about 30 microseconds. Opening a file now also finds its previews, which
costs about 300 bytes; the allocation budget of the open is unchanged. The fuzz tests read every preview of the damaged files.
@sputier
sputier merged commit 8dfb846 into main Nov 18, 2026
4 checks passed
@sputier
sputier deleted the lot-3/preview branch November 18, 2026 23:07
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