Skip to content

LR-IMP-CAMERA-COVERAGE: measured image area for Nikon bodies without CropArea - #1053

Merged
echelon merged 1 commit into
storytold:mainfrom
robmdunn:fix-976-nef-masked-columns
Oct 15, 2026
Merged

echelon merged 1 commit into
storytold:mainfrom
robmdunn:fix-976-nef-masked-columns

Conversation

@robmdunn

@robmdunn robmdunn commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #976
Fixes #914
Fixes #1236

Problem

Nikon bodies from before about 2016 record no CropArea in the maker note, so the decoder keeps their whole sensor data, less trailing dark columns. Where that data holds masked lines above black or optical black, they are developed as a coloured or dark bar on that edge (the top edge in portrait):

None of these files records the picture's area anywhere. A pixel-statistics heuristic was tried first and failed on real images: deep shadow at the edge looks just like masked lines. The fix is therefore measured per body, as the D4 entry already was.

Fix

MEASURED_BODIES (crates/raw/src/vendor/nef.rs) gains 44 model / data size / depth groups, next to the D4, D7100 and D90 entries already on main. Each gets the frame of its own camera JPEG inside the sensor data:

D7000, D5100, D800, D800E, Df, D4 (12-bit added), D3, D3S, D700, D300, D300S, D600, D610, D810, D4S, D5600, Coolpix A, D3000, D40X, D60, D80, D2Xs, D200, D3200, Nikon 1 V1.

  • Best guesses, short of the evidence rule below, each with a comment naming its evidence:

    • D3100: 3 of 4 clean files agree.
    • D5000: 4 of 5 agree, and the D90 has the same sensor.
    • D40: 2 of 2 agree.
    • D50 and D70: one file each, and they agree.
    • D70s: its files align between pixels next to the D50 / D70 frame.

    Every best-guess frame keeps the masked lines out by at least 11 px.

  • An entry matches model, data size and bit depth exactly. Every other body decodes as before.

  • Bodies whose data holds only picture beyond the camera frame get no entry, so their output is unchanged: D750, D5300, D5500, D3300, D2X, D2Hs, D3X, D5200, Coolpix P6000, Nikon 1 AW1 / J3 / V2 / V3, and the D800's DX / 1.2x / 5:4 modes. raw-parity.md lists them.

  • Affected bodies' renders shrink to the camera's own framing, by 8–64 px per dimension (e.g. D800 7424 × 4924 → 7360 × 4912). Crops and masks are stored in normalised coordinates, so existing edits shift by at most a few pixels.

Measuring a body: cargo xtask raw-frame

crates/engine/examples/raw_frame.rs, run through cargo xtask raw-frame [--json] FILE…:

  • Alignment: it aligns each raw's embedded full-size camera JPEG with the sensor data, using half-size luminance and gradient correlation with a sub-pixel peak. Weak, ambiguous or between-pixel peaks are flagged.
  • Edge bands: it counts the edge lines that hold no picture (masked: flat, with the two colour-filter phases agreeing within the noise or within 0.4% of white, so filler like the D90 / D5000's counts; or dark), and reports what the decoder's crop keeps beyond the camera frame.
  • Evidence rule: it prints MeasuredBody lines only when at least 3 clean files agree exactly, either
    • of the model itself (12- and 14-bit pooled), or
    • at least one of its own plus sibling models with the same readout (data size and JPEG size) that agree on the same frame. Data size alone is not enough: the D200 and D80 share 3904 × 2616 but frame it at (16, 12) and (14, 9).
  • Cross-check: on the D7500, which records its CropArea, the tool finds that area exactly. It declines the Z f, whose JPEG is distortion-corrected. On Sony, Canon CR2 / CR3, Pentax and Samsung files it finds each decoder's own crop.

The survey covered every Nikon raw on raw.pixls.us without CropArea: its 138 CC0 samples, plus 124 other publicly posted sample files for the bodies raw.pixls.us had too few of. Only the measurements are committed, never the files.

Changes since the review

Following @cookingwithsimon's notes:

  • Rebased on current main (keeps the D4, D7100 and D90 entries).
  • Applied the classifier fix for filler with a fixed CFA offset.
  • Added Fixes #1236.
  • Dropped the D750 from the list of bodies needing work: its extra lines are picture.
  • The D90 frame difference is noted: files align at (11, 10), vs LR-IMP-CAMERA-COVERAGE: Nikon D90 NEFs drop their filler columns (no pink band) #1361's (10, 10).
  • Measured more files, so the D3200, D200 and Nikon 1 V1 now have entries and the rest of the pending bodies have best-guess ones.
  • Removed the survey doc and sample manifest; each entry's comment records its evidence.

Overlap with #1770: it adds identical D600 / D610 entries, and a D5000 entry at the D90's (10, 10) where this PR's files say (11, 10). It also measures white inside the image area for measured bodies, which this PR doesn't. Whichever lands second needs to reconcile.

Testing

  • Unit tests:
    • measured_d7000_and_d5100_image_area
    • measured_older_dx_bodies_image_area (D200, D3200, Nikon 1 V1 and the best guesses; fails without the entries)
    • measured_d4_image_area_and_white_level
    • measured_bodies_are_consistent
  • Real files: re-measuring every new body's files after the change, the decoder's crop holds no masked or dark line, and bodies without an entry keep their crop.
  • cargo xtask ci: all 8 steps pass on this branch.

🤖 Generated with Claude Code

@cookingwithsimon

Copy link
Copy Markdown
Contributor

@robmdunn thanks for this. The survey and the raw-frame tool are really useful, and I'd like to see it land. Some notes after testing it against current main:

  1. Rebase needed. LR-IMP-CAMERA-COVERAGE: Nikon D90 NEFs drop their filler columns (no pink band) #1361 (D90) and LR-IMP-CAMERA-COVERAGE: Nikon D7100 NEFs develop at Lightroom's frame, so its crops line up #1387 (D7100) landed first. Between them they make white an Option and add D7100 and D90 entries, so this PR now conflicts in nef.rs, gaps.md, parity-checklist.md, raw-parity.md and whats-new.md. The nef.rs conflict is mechanical: keep main's D4 / D7100 / D90 entries and add yours on top. That builds, and all 27 NEF unit tests pass. I checked the other open and closed PRs that touch nef.rs (LR-IMP-FORMATS: decode table-less Nikon 34713 NEFs stored as packed 12/14-bit data #671, docs: parity checklist row for two-field Coolpix NEFs (E5400, E5700 CYGM, E8400, E8800); decoder is craft-libs #2235 #672, LR-IMP-FORMATS: decode Nikon small raw (sRAW) NEFs as 4:2:2 luma/chroma #673, Decode Nikon High Efficiency NEF sensor data #896, LR-RAW-NEF: Coolpix P7000 NRW files decode without stripes and with their white balance #1467), and none of them change the measured-bodies table. So after this rebase, LR-IMP-CAMERA-COVERAGE: measured image area for Nikon bodies without CropArea #1053 is the only edge-bar fix in flight.

  2. The edge-band classifier misses the D90 / D5000 filler columns. nikon-bodies.md calls the D90 "fine" and the D5000 "no masked edge lines found", but both have a ~42-column filler bar. On a user's D5000 NEF (12-bit, lossy type 2), columns 4310–4351 are flat: about 252 on one CFA colour and 256 on the other, ±3, with a black level of 0. kind() rejects them as masked only because of the even/odd test. Their fixed ~4.5-level colour offset is far above 4 · noise · √(2 / pairs) + slack (about 1.8 here). Also accepting a small fixed offset, || l.pattern < white * 0.004, makes the tool report "right 42 masked" on that file, and a D750 file doesn't change (one-line diff below). Could you re-run the survey with that change? Other "fine" verdicts may hide the same kind of filler. That D5000 file's frame is (11, 10), clean (score 0.77, margin 0.08), so the D5000 needs 2 more files for an entry of crop (11, 10, 4288, 2848).

  3. Small frame disagreement on the D90: your CC0 sample aligns at x ≈ 10.9 (11), and the D5000 file at 10.96, while LR-IMP-CAMERA-COVERAGE: Nikon D90 NEFs drop their filler columns (no pink band) #1361 uses (10, 10) from 10 files. It doesn't change this PR, but it's worth a note in the doc.

  4. One rule for the "fine" bodies. LR-IMP-CAMERA-COVERAGE: Nikon D7100 NEFs develop at Lightroom's frame, so its crops line up #1387 crops the D7100 to its camera-JPEG frame even though the extra columns are picture, so that Lightroom crops line up (Nikon D7100 NEFs develop larger than Lightroom's frame, so Lightroom crops land up to 6 px off (18 px in full frame) #1248). This PR leaves bodies with only extra picture uncropped. It would help to agree on one rule. If LR-IMP-CAMERA-COVERAGE: Nikon D7100 NEFs develop at Lightroom's frame, so its crops line up #1387's rule wins, the D3300, D5300, D5500, D40, D70, D2X, D2Hs and Nikon 1 AW1 / J3 / V3 groups could get their frames too.

  5. Strange lines when looking at NEF files in lightcraft. #1236 (D610) is the same bug, and the D610 has entries here, so Fixes Strange lines when looking at NEF files in lightcraft. #1236 could go in the description.

  6. D750: a user's 14-bit D750 NEF aligns cleanly at (8, 8), and all 8 lines beyond the frame on each side are picture, not dark. The "dark lines" in the survey are probably shadow in the one CC0 scene, so the D750 likely needs no entry.

@radialmonster the remaining pending bodies only need sample files, and your PigeonFiles drop has some of them: pakjesboot12's D3200 (lossy type 2, 12-bit, 6080 × 4012), olavsu1's D750 and D5000, and possibly lemonnik's D3100. Could you run cargo xtask raw-frame on them, or pass them to robmdunn if the owners agree? Only the measured numbers get committed, never the files. D3200, D3100 and D5000 each need 2 more clean files; D200 and the Nikon 1 V2 need 1.

raw-frame pattern fix


diff --git a/crates/engine/examples/raw_frame.rs b/crates/engine/examples/raw_frame.rs
index a1d5a86f..73cf528b 100644
--- a/crates/engine/examples/raw_frame.rs
+++ b/crates/engine/examples/raw_frame.rs
@@ -363,7 +363,7 @@ fn kind(l: &Line, black: f64, white: f64) -> Kind {
         Kind::Dark
     } else if l.mean > black + range * 0.02
         && l.mean < white * 0.25
-        && l.pattern < 4.0 * l.noise * (2.0 / l.pairs.max(1.0)).sqrt() + slack
+        && (l.pattern < 4.0 * l.noise * (2.0 / l.pairs.max(1.0)).sqrt() + slack || l.pattern < white * 0.004)
         && l.spread < 1.5 * l.noise + slack
         && l.spread < white * 0.004
     {

…CropArea

Nikon bodies from before ~2016 record no CropArea, so the decoder kept
their whole sensor data, less trailing dark columns. Where that data
holds masked lines above black or optical black, they were developed as
a coloured or dark bar on that edge: the D7000 and D5100's 36 columns at
~600 (storytold#976), the D800's 14 (storytold#914, the top in portrait), the D600 / D610's
8 (storytold#1236), the D3200's 46, and others.

MEASURED_BODIES (crates/raw/src/vendor/nef.rs) now gives 44 more model /
data size / depth groups the frame of their camera JPEG inside the data,
next to the D4, D7100 and D90 entries already there: D7000, D5100, D800,
D800E, Df, the D4's 12-bit mode, D3, D3S, D700, D300, D300S, D600, D610,
D810, D4S, D5600, Coolpix A, D3000, D40X, D60, D80, D2Xs, D200, D3200
and Nikon 1 V1; and, on best-guess frames short of the evidence rule,
each with a comment naming its evidence, the D3100 (3 of 4 clean files
agree), D5000 (4 of 5, and the D90's sensor), D40 (2 of 2), D50 and D70
(one file each, agreeing) and D70s (next to them, between pixels).
Every best-guess frame keeps the masked lines out by several pixels.
An entry matches model, data size and depth exactly. Bodies whose data
holds only picture beyond the camera frame get no entry and render as
before.

`cargo xtask raw-frame FILE...` (crates/engine/examples/raw_frame.rs)
measures a body: it aligns each raw's embedded camera JPEG with the
sensor data (half-size luminance, gradient correlation, sub-pixel peak;
weak, ambiguous or between-pixel peaks are flagged), counts the edge
lines that hold no picture and reports what the decoder's crop keeps
beyond the camera frame. A masked line is flat and its two CFA phases
agree within the noise or within 0.4% of white, so filler with a small
fixed offset between them (D90, D5000) counts. It prints entries only
with evidence: at least 3 clean files agreeing exactly, of the model
(12- and 14-bit pooled) or, with at least one of its own, with sibling
models of the same readout (data and JPEG size) that agree on the same
frame. On the D7500, which records its CropArea, it finds that area
exactly; it declines the Z f, whose JPEG is distortion-corrected. It
works for any make whose raw embeds a full-size camera JPEG: on Sony,
Canon CR2 / CR3, Pentax and Samsung files it finds the decoder's own
crop (relative to the active area).

Every Nikon raw on raw.pixls.us without CropArea was surveyed this way
(its 138 CC0 samples, plus 124 other publicly posted sample files; only
the measurements are committed). Bodies whose data holds only picture
beyond the frame need no entry (D750, D5300, D5500, D3300, D2X, D2Hs,
D3X, D5200, Coolpix P6000, Nikon 1 AW1 / J3 / V2 / V3, the D800's crop
modes); raw-parity.md lists them, and no longer claims every NEF
coding but HE decodes. measured_bodies_are_consistent checks every entry lies
inside its data and is listed once; measured_older_dx_bodies_image_area
covers the D200, D3200, Nikon 1 V1 and best-guess entries.

Fixes storytold#976
Fixes storytold#914
Fixes storytold#1236

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@robmdunn
robmdunn force-pushed the fix-976-nef-masked-columns branch from 3ccb56f to 2b3b6e7 Compare October 15, 2026 02:44
@endthestart

Copy link
Copy Markdown
Contributor

I ran every Nikon file on raw.pixls.us (244 files, 92 bodies) through main and compared each render with the camera's own JPEG, and this PR covers almost everything I found. A few it doesn't:

  • D2H and D2Hs: the last 8 columns on the right (and the first ones on the left) read about 205 while the picture next to them is about 800, with black level 0, so the dark-column trim doesn't catch them and they show as dark bands on both sides (D2H_3957.NEF, D2A_0005.NEF, DSC_5858.NEF). The PR lists the D2Hs as having only picture past the frame, so it might be worth another look.
  • Nikon 1 J1 and J2: the last row is 0, so there's a black line along the bottom (_DSC0355.NEF, _DSC7434.NEF, DSC_0451.NEF). They share the V1's 3904 x 2606 data, so the V1 entry might apply to them too.
  • Nikon 1 V2 and Coolpix A1000: on these dark frames the trailing-column trim takes off 64 columns of picture (its cap), so the renders are 4556 and 4544 wide against the camera's 4608 (Nikon_1_V2_ISO-200.NEF, DSCN0417.NRW). Since the V2 gets no entry here, it still goes through that trim.

Also, on the D600 the noise columns past the image were being read as a saturation plateau, so white_from_data returned 3182 on DSC_3296 while the picture goes up to 3450 (12729 against 14902 on the 14-bit DSC_3294). Measuring the white inside the measured area fixed that in my branch for #1627 (#1770). I'll close that one in favour of this, and I'm happy to send the white level part as a small follow-up once this is merged, if you think it's worth it.

@echelon

echelon commented Oct 15, 2026

Copy link
Copy Markdown
Member

Thank you @robmdunn! This landed on main in integration batch 36 (#1843). We took your rebased head, so the survey data stays out of the repo, and it replaces #1770 as its author suggested.

If you haven't already, please join our Discord: https://discord.gg/artcraft. We'd love to have you there.

endthestart pushed a commit to endthestart/lightcraft that referenced this pull request Oct 15, 2026
# Conflicts:
#	crates/raw/src/vendor/nef.rs
#	docs/gaps.md
#	docs/parity-checklist.md
#	docs/raw-parity.md
#	docs/whats-new.md
endthestart pushed a commit to endthestart/lightcraft that referenced this pull request Oct 15, 2026
…orytold#1770 both added them, with the same crops)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants