Skip to content

PEF: masked rows and columns at the black level are trimmed without crop tags - #1288

Merged
bflatastic merged 4 commits into
storytold:mainfrom
marcoarnulfo:fix/pef-masked-border
Oct 13, 2026
Merged

bflatastic merged 4 commits into
storytold:mainfrom
marcoarnulfo:fix/pef-masked-border

Conversation

@marcoarnulfo

@marcoarnulfo marcoarnulfo commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1271.

Without crop tags, dark_trimmed compared raw column means with 5% of the interior's raw mean, so masked columns sitting at the black level (BlackPoint 64/128/256 on these bodies) only counted as dark when the interior was more than 20 times brighter than black, and it never looked at rows. It now takes the maker-note BlackPoint (already read here) and treats a line as dark when its mean is at most 1/256 of the remaining range (at least 16 DN) above it, the same tolerance cr2::masked_columns uses: masked lines sit at black plus noise, while dark real content such as a night sky is well above, whatever the interior's brightness. When the outermost line sits a little above BlackPoint (the K-70 and K-1 mask at 65 DN over it), the tolerance is measured from that line's level; saturated outer lines count when dark ones follow (the 645Z's 12 white columns outside its 46 masked ones); a frame whose interior is itself within the tolerance (a lens-cap shot) keeps its whole area. Rows get the same scan as columns, keeping even offsets so the CFA phase doesn't move. Files without BlackPoint keep the old 5% rule. Render cache version goes to 62, since these frames change size.

Bodies covered: KP, KF, K-3 Mark III (and Monochrome), K-70, K-1, K-1 Mark II, K-S1, K-S2, 645Z. On the three files from the issue: KP 6032 x 4032, KF 6024 x 4024, K-3 Mark III 6264 x 4176 (24/16 columns, 34/14 rows). Docs: the PEF row of docs/raw-parity.md, LR-IMP-CAMERA-COVERAGE in docs/parity-checklist.md, docs/whats-new.md.

Validation:

  • masked_rows_and_columns_at_the_black_level_are_trimmed_without_crop_tags (BlackPoint 256, dim interior): fails on main (0,0 504x384, only the all-zero columns go), passes here
  • dark_real_content_next_to_the_masked_border_is_kept (bright centre, content 100 DN over black inside the border): fails with a 5%-of-interior threshold (44,48 432x302), passes here
  • masked_lines_a_little_above_the_black_level_are_trimmed (K-70 layout, 320 x 288): 56,28 264x248, was 56,0 264x276
  • saturated_lines_outside_masked_ones_are_trimmed (645Z layout): 58 right columns go, was none
  • a_saturated_edge_followed_by_picture_stays and a_frame_at_the_black_level_keeps_its_whole_area (the lens-cap one was 64,64 192x160)
  • cargo test -p lightcraft-raw --lib vendor::pef, cargo test -p lightcraft-raw --test vendor_robust
  • cargo clippy -p lightcraft-raw --all-targets -- -D warnings, cargo fmt --all -- --check
  • (macOS, Apple silicon)

@AlexChambers

Copy link
Copy Markdown
Contributor

I measured this branch (a9812d0) on every CC0 PEF the all-makes sweep used: 38 files from 30 bodies. The renders are full size, default settings, compared with main. Edge ΔL* is the outermost 1% (2 px at 200 px wide) against the camera's own JPEG, as in the issue. "Expected" is the area with every masked line left out, from an independent implementation of the same idea.

Body File Main: size, edge ΔL* l / r / t / b This PR: size, edge ΔL* Expected (masked lines left out) Camera JPEG
KP 1825 6112 x 4060, -49.5 / -28.6 / -38.5 / +1.3 6032 x 4032, -0.7 / -0.1 / -0.3 / +1.4 6032 x 4032, -0.7 / -0.1 / -0.3 / +1.4 6016 x 4000
K-3 Mark III 5832 6304 x 4224, -5.1 / -1.6 / -38.8 / +1.2 6264 x 4176, +1.2 / +1.4 / +0.1 / +0.4 6240 x 4176, +1.0 / +1.4 / +0.3 / +0.2 6192 x 4128
K-3 Mark III Monochrome 7082 6272 x 4224, +0.1 / -1.5 / -46.7 / +2.1 6272 x 4190, +0.2 / -1.3 / -0.3 / -0.2 same as this PR 6192 x 4128
KF 6664 6080 x 4064, -33.3 / +1.6 / -12.1 / +1.3 6024 x 4024, -0.4 / +0.5 / +0.7 / -0.6 same as this PR 6000 x 4000
K-1 Mark II 3346 7380 x 4950, +0.3 / +0.3 / -11.5 / +0.2 7380 x 4932, +0.4 / +0.4 / +0.1 / +0.2 same as this PR 7360 x 4912
K-1 Mark II, APS-C crop 7133 4832 x 3238, -0.8 / +0.4 / -20.6 / -2.1 4832 x 3220, -0.6 / +0.6 / -0.2 / -1.2 same as this PR 4800 x 3200
K-S2 1627 5504 x 3664, -0.1 / -10.7 / +0.7 / -3.0 5492 x 3664, -0.1 / -1.6 / +0.8 / -2.9 same as this PR 5472 x 3648
K-S1 4365 5504 x 3664, +1.9 / -4.1 / +3.0 / -2.5 5492 x 3664, +1.8 / +1.5 / +2.9 / -2.5 same as this PR 5472 x 3648
K-70 1141 6080 x 4064, -9.7 / +0.1 / -8.2 / +1.2 6080 x 4052, -9.7 / 0.0 / -8.1 / +0.2 6024 x 4024, +0.1 / -0.1 / -0.1 / -0.1 6000 x 4000
K-1 2469 7380 x 4950, -0.1 / +1.5 / -9.0 / +0.6 7380 x 4948, -0.1 / +1.5 / -7.3 / +0.6 7380 x 4932, 0.0 / +1.5 / +0.6 / +0.7 7360 x 4912
645Z 2404 8338 x 6208, +0.4 / -12.5 / -0.4 / +0.3 8338 x 6208, +0.4 / -12.5 / -0.4 / +0.3 8280 x 6208, +0.3 / +0.7 / 0.0 / +0.5 8256 x 6192
  • Fixed: the KP, KF, K-1 Mark II, K-S2 and K-S1, and all six K-3 Mark III files (14-bit, both pixel-shift modes, HDR, Monochrome). No line with a mean of 2 out of 255 or less is left at any edge. The K-S1 is not in the issue but had the K-S2's 12 masked columns.
  • Unchanged where they should be: the other 20 files render byte-identical to main (SHA-256 of the pixels): K-3 and K-3 II, and the 18 files with area tags (*ist, K100D, K110D, K10D, K200D, K2000, K20D, K-7, K-x, K-r, K-5 family, GX-1L, 645D). White and black levels are unchanged on all 38. Offsets stay even (next_multiple_of(2) and & !1), so the colour-filter phase holds.

Gaps

  1. K-70 and K-1: masked lines 65 over BlackPoint are kept (crates/raw/src/vendor/pef.rs:182-185). The K-70's 56 left columns and 28 top rows, and the K-1's 16 top rows, sit at 64 to 66 over BlackPoint 64. The tolerance is (white - black) / 256, 63.2 on the K-70 and 63.7 on the K-1, so the scan stops at the first of them. Only the K-70's 12 empty bottom rows and 2 of the K-1's 18 top rows go.
  2. 645Z: 12 saturated columns stop the scan (pef.rs:188-194). Its right edge has 12 columns at 16316, then 46 at the black level. The first column isn't dark, so none of the 58 goes.
  3. A frame at the black level loses 64 lines on every side. The threshold no longer depends on the interior. A lens-cap or dark frame, or any frame whose outer 64 lines are all within 63 DN of black, is cut by 128 px each way. On a 320 x 288 synthetic frame at the black level the area is (64, 64, 192, 160); main keeps the whole frame.
  4. Docs: docs/raw-parity.md (the PEF row), the LR-IMP-CAMERA-COVERAGE row of docs/parity-checklist.md and docs/whats-new.md aren't updated. The commits carry no task id (AGENTS.md asks for one per commit).
  5. Minor: col_mean and row_mean index d[y * w + x] (pef.rs:179-180). The buffer is w * h long here, so it can't panic today, but d.get() would follow the never-crash rule for input-derived offsets.

Suggested fix

Keep your absolute tolerance, which rightly keeps the dark real content in your second test. Measure it over the outermost line's level when that line sits a little above black. Count saturated outer lines when dark ones follow, and keep the whole area when the interior itself is within the tolerance. A sketch, replacing let dark = ... and edges in dark_trimmed:

let (base, tol) = match black {
    Some(b) => (b, ((f64::from(white) - b) / 256.0).max(16.0)),
    None => (0.0, interior * 0.05),
};
// a frame no brighter than masked lines (a lens-cap shot) has nothing to tell them from picture
if interior - base <= tol {
    return Rect::new(0, 0, w, h);
}
let range = f64::from(white) - base;
// lines from one edge that hold no picture: within `tol` of the outermost line's level when that sits a
// little above black (K-70, K-1: 65 over BlackPoint), and saturated lines when dark ones follow (645Z)
let run = |at: &dyn Fn(usize) -> f64| {
    let saturated = (0..64).take_while(|&k| at(k) - base >= range * 0.98).count();
    let outer = at(saturated) - base;
    let level = if outer > 0.0 && outer < range * 0.01 { outer } else { 0.0 };
    match (saturated..64).take_while(|&k| at(k) - base < level + tol).count() {
        0 => 0,
        dark => saturated + dark,
    }
};
let edges = |n: usize, mean: &dyn Fn(usize) -> f64| {
    let lo = run(&|k| mean(k)).next_multiple_of(2);
    let hi = n - run(&|k| mean(n - 1 - k));
    (lo, (hi - lo) & !1)
};

With this in place, both of your tests pass, and so do three more. One is a K-70 layout: masked lines at black + 65 and empty bottom rows, expected (56, 28, 264, 248) on 320 x 288. One is a 645Z layout: 12 saturated outside 46 masked columns. The third covers a blown-out edge with picture behind it, which must stay, and a lens-cap frame, which must keep its whole area. Clippy is clean. On the edge profiles of the 20 files without area tags, it trims exactly what this PR does, plus the K-70's 56 / 28 / 12, the K-1's 18 top rows and the 645Z's 58 right columns.

Not for this PR: on two of the six K-3 Mark III files (4677 and the Monochrome), up to 28 right columns and 14 bottom rows are neither picture nor black. They stay here and in any dark-line rule (edge ΔL* right -10.7, bottom -6.1 on 4677). A per-model crop to the camera frame, as #1053 does for Nikon, would cover them.

…saturated lines outside masked ones, and keeps a lens-cap frame whole (storytold#1271)

Masked lines are measured against the outermost line's level when it sits
just above BlackPoint (K-70, K-1), saturated outer lines count when dark
ones follow (645Z), and a frame whose interior is within the tolerance
keeps its whole area. Line means read samples with get(). Render cache
version 62: PEF frames change size.
@marcoarnulfo

Copy link
Copy Markdown
Contributor Author

All five gaps are in two new commits:

  • 13e803a: your sketch, almost as written. The tolerance is measured from the outermost line when it sits just above BlackPoint (K-70, K-1). Saturated outer lines count when dark ones follow (645Z). A frame whose interior is within the tolerance keeps its whole area. Line means read with d.get(). The four cases you listed are tests now: K-70 (56, 28, 264, 248 on 320 x 288), 645Z, the blown-out edge (it stays) and the lens-cap frame (whole area). Removing each branch fails its own test. I also bumped RENDER_CACHE_VERSION to 62, since these frames change size.
  • dd502ba: the PEF row in docs/raw-parity.md, LR-IMP-CAMERA-COVERAGE in docs/parity-checklist.md and docs/whats-new.md. Both commits carry the task id.

KP, KF and K-3 Mark III come out at the same sizes as before. One case I can still see over-trimming: a PEF without area tags or a masked border, whose picture reaches the edge with a dark sky within 1% of the range. That level becomes the base, and up to 64 lines could go. None of your 38 files does it, so I left it. The K-3 Mark III per-model crop stays out, as you suggested.

@AlexChambers

Copy link
Copy Markdown
Contributor

Thanks, @marcoarnulfo, that covers all five points from my review. Agreed on leaving the dark-sky edge case and the K-3 Mark III per-model crop for later. None of the 38 sample files hits the first one, and the second fits better with a per-body frame table like #1053's.

@bflatastic

Copy link
Copy Markdown
Member

Landed on main in #1431. Thank you, @marcoarnulfo!


Generated by Claude Code

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.

Pentax PEF: masked black rows and columns stay in the picture on the K-3 Mark III, KP and KF (thinner on the K-1, K-1 Mark II, K-70, K-S2 and 645Z)

3 participants