Skip to content

Issue #1377: exports bin a raw two by two when half the sensor is enough - #1561

Merged
emmertex merged 5 commits into
mainfrom
claude/project-thread-1en860-binning
Nov 27, 2026
Merged

emmertex merged 5 commits into
mainfrom
claude/project-thread-1en860-binning

Conversation

@emmertex

Copy link
Copy Markdown
Owner

Requested by Andrew · project thread

Part of #1377 (step 2 of the plan). Stacked on #1554 (the bench this measures with). This PR's own commit is 41b24651.

Problem

An export chose its raw source by size band, not by the size asked for. Up to 2560 px it took the preview level's block: binned by 2, then box-fitted to 2560, then Lanczos to the output. That's two resamples, and the box aliases fine detail. One pixel more, and it demosaiced the whole sensor, which is the 72 % CPU cliff blwfish measured at 2561 px. The step as planned ("demosaic at reduced size for small exports") turned out to be mostly in place already for 2048 px. The gaps were the soft box step and everything from 2561 px to half the sensor.

Change

  • Session::bin_for_export (media.rs): an export of a raw that isn't decoded yet asks for a source EXPORT_BIN_MARGIN (1.15) times the pixels the output needs (crop included), with LoadOptions::keep_binned. That happens only when half the sensor has them. Otherwise nothing changes: large exports, tight crops, small sensors, and a source already decoded (the loupe's) are left alone.
  • files::export_bin_factor: with keep_binned, the loader bins by the smallest block the sensor allows that leaves that many pixels. It keeps the binned size, with no box fit, so the pipeline goes to the output in one Lanczos step. It's the smallest block, not the largest, because a box over a larger block aliases (see the zone plate below).
  • DecodedSource::serves keeps an export's binned source from standing in for a view's level.
  • New bin_sharpness example: the visual check (SSIM and detail kept against a full AHD demosaic, crops written to target/bench/bin-sharpness/).

Not in this PR: anything on the GPU (step 3), lanes (step 4).

How it was checked

  • cargo xtask ci --quick: fmt, brand, docs, notes, i18n, windows and clippy pass. Two tests_preview_prep timing tests failed in the full run on this 4-core container (the background build made 1 job where the test expects 0 while the window renders). Both pass when rerun alone (28/28 in the module). They don't touch the export path. parity, layers, assets and wasm pass when run separately.
  • New tests (tests_export_binning.rs):
    • a small export bins, while a larger one or a tight crop doesn't;
    • a binned export matches the full demosaic (same size, no colour shift, < 2.5 levels mean difference);
    • export_bin_factor never leaves fewer pixels than asked.

Visual check (bin_sharpness, an RGGB 8256 × 5504 raw of each scene; SSIM against a full AHD demosaic, Lanczos to the output):

scene out px before (bin 2, box to 2560) after (bin 2, one Lanczos)
zone plate 1024 / 1600 / 2048 / 2560 0.80 / 0.69 / 0.64 / 0.72 0.998 / 0.996 / 0.993 / 0.988
3 demo scenes 1024–2560 0.995–0.9998 0.9993–1.0000
zone plate 3000 / 3840 (full demosaic) 0.981 / 0.958
3 demo scenes 3000 / 3840 (full demosaic) 0.9982–0.9999

Speed (export45_bench, 4-core container, no GPU, 3 synthetic 45 MP DNGs, two interleaved before/after runs, per photo):

wall CPU peak
3000 px 3.61 → 1.95 s 12.0 → 6.2 s 1.86 → 1.06 GB
2048 px 1.26 → 1.29 s 3.97 → 3.97 s 0.66 → 0.72 GB

At 2048 px the colour finish gets cheaper (0.21 → 0.09 s) and the render dearer (0.72 → 0.91 s, larger source). I tried fitting the binned picture to the margin in the loader with Lanczos (one more step), and it measured no faster, so I didn't keep it. Real Z9 numbers need blwfish's set (cargo xtask bench --only export45 --export-dir DIR once #1554 is in).

Merging

Manual: output/export-size-sharpen.mdx. Release notes: docs/whats-new/ and docs/changelog/unreleased/ entries.


Generated by Claude Code

Step 2 of the plan on #1377. An export of a raw not yet decoded reads it binned
by the smallest block the sensor allows that leaves 1.15x the source pixels the
output needs (EXPORT_BIN_MARGIN), kept at that size so the pipeline takes it to
the output in one Lanczos step (LoadOptions::keep_binned). Before, an output up
to 2560 px took the preview level's block and a box fit to 2560 px, and a larger
one demosaiced the whole sensor.

Measured (4-core container, no GPU, 3 synthetic 45 MP DNGs, interleaved):
3000 px 3.61 -> 1.95 s wall, 12.0 -> 6.2 CPU-s, 1.86 -> 1.06 GB peak per photo;
2048 px unchanged in time, sharper. Visual check: the new bin_sharpness example,
SSIM vs a full AHD demosaic >= 0.998 on demo scenes 1024-3840 px; zone plate
0.96-0.998 (old box path 0.64-0.80).

Manual: output/export-size-sharpen.mdx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011VFDrfcYF2xSBoosZAu8dw
@emmertex emmertex self-assigned this Nov 27, 2026
…-1en860-binning

# Conflicts:
#	crates/engine/src/media.rs
@emmertex
emmertex enabled auto-merge November 27, 2026 21:18
@emmertex
emmertex merged commit 1ee8ffa into main Nov 27, 2026
6 of 7 checks passed
@emmertex
emmertex deleted the claude/project-thread-1en860-binning branch November 27, 2026 21:23
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.

2 participants