Skip to content

LRC-LIB-CATALOG-IMPORT: describe raws from their headers without reading pixel data (step 1 of #832) - #961

Merged
bflatastic merged 1 commit into
storytold:mainfrom
ingolfteetz:raw-sparse-header-plan
Oct 11, 2026
Merged

bflatastic merged 1 commit into
storytold:mainfrom
ingolfteetz:raw-sparse-header-plan

Conversation

@ingolfteetz

Copy link
Copy Markdown
Contributor

What this does

Step 1 of the two proposed in #832: a self-contained building block, no behaviour change yet.

lightcraft_raw::sparse::header_plan(bytes, loaded) says which byte ranges of a raw file probe_info and lightcraft_meta::extract read. It is asked about a buffer of the file's full length in which only the ranges in loaded hold the file's bytes (the rest is zeros). The caller loads the ranges it names and asks again; once it answers Ready, probing the buffer gives what probing the whole file gives. preview_plan also covers what embedded_preview reads, for raws shown from their embedded preview.

Nothing calls it yet. Step 2, the batched catalog import from #832, will probe originals with it, and #755's preview loader could use preview_plan.

What is left out

Only bulk sample data:

  • TIFF raws (DNG, CR2, ARW, SRW, CFA TIFF): the strips and tiles of each image of 256 KiB or more, keeping the first 64 KiB of its first chunk, where the lossless-JPEG / JPEG XL frame headers the header probe validates sit. An older CR2's raw IFD without image tags is located through its strip tags. RW2: from RawDataOffset to the end.
  • CR3: everything except moov, the XMP uuid box, the first sample of each CTMD track (the as-shot white balance lives there) and the main raw track's CRX headers and QP maps (crx::header_ranges: the bytes crx::validate reads).
  • RAF: the CFA strip, minus its compression header and stripe table.
  • NEF, ORF, PEF (their header probe reads samples) and anything not recognised: Whole. So is a structure that is incomplete, inconsistent, or keeps IFD tables or out-of-line values inside sample data. The plan never guesses.

The TIFF IFD walk mirrors lightcraft_tiff's reader (chain, SubIFDs, Exif, GPS, Interop, same bounds) and asks for every IFD table and for the values that locate IFDs and chunks. The CR3 box walk mirrors parse_cr3.

Equivalence

crates/raw/tests/sparse_corpus.rs runs every corpus raw (raw.pixls.us and the Leica set) through the plan twice, with the skipped bytes 0 and again 0xFF. For each file the plan describes, probe_info, lightcraft_meta::extract and the embedded XMP must equal the whole file's; with preview_plan also embedded_preview. A reader that looked at skipped bytes would fail it.

Format Files described Bytes read
CR3 10 2.6 %
DNG 16 2.4 %
CR2 9 3.0 %
RAF 21 6.4 %
RW2 16 7.0 %
ARW 23 7.5 %
SRW 3 16.6 %
NEF, ORF, PEF 0 of 16 read whole

Through step 2's probe, 961 files from my own catalog (842 EOS R5 Mark II CR3, 24 Leica M11-P DNG, 74 JPEG, 21 ORF) also gave an identical ProbeInfo, the content hash aside.

Unit tests cover the Ranges arithmetic and hostile input (self-referencing IFDs, oversized counts, CR3 boxes past the end of the file, a RAF container outside it): each ends with an answer, never a panic.

Small changes outside the module

  • crx::header_ranges (new, pub(crate)); vendor::cr3::main_track, vendor::raf::raw_ifd and vendor::rw2::RAW_OFFSET become pub(crate).
  • lightcraft_meta::cr3::XMP_UUID is public; the parse_cr3 doc says which top-level boxes it reads (the CR3 plan relies on that, and the corpus test guards it).

Verification

cargo xtask ci on this branch: fmt, clippy, heif, parity, layers, assets and wasm pass; cargo test --workspace --no-fail-fast passes except export_progress_and_cancel (#674), which also fails on main.

Related

🤖 Generated with Claude Code

…ing pixel data

`lightcraft_raw::sparse::header_plan` says which byte ranges of a raw file the header
probe (`probe_info`, `lightcraft_meta::extract`) reads, given a buffer of the file's length
in which only some ranges are loaded: everything but the bulk sample data (large TIFF
strips/tiles minus the first chunk's coding headers, a CR3's image tracks minus the raw
track's CRX headers and QP maps, a RAF's CFA strip, an RW2's raw data). `preview_plan`
also reads what `embedded_preview` needs, for raws shown from their preview. NEF, ORF and
PEF (their header probe looks at samples) and anything else are read whole.

The corpus test checks, for every raw.pixls.us sample the plan describes, that probing the
partly loaded buffer gives exactly what the whole file gives, with the skipped bytes zero and
again 0xFF: CR3 2.7 %, DNG 2.4 %, CR2 3 %, RAF 6 %, RW2 7 %, ARW 8 % of their bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bflatastic
bflatastic merged commit 63059a4 into storytold:main Oct 11, 2026
lzjtugjc added a commit to lzjtugjc/lightcraft that referenced this pull request Oct 11, 2026
…lan's ranges

The header-only path added in storytold#755 answers for a Fuji RAF, whose preview pointer sits in the first
bytes. A Canon CR3 keeps its preview in a JPEG track, and a TIFF raw (CR2, ARW, DNG, RW2, 3FR …) can
keep its preview tag in an IFD the prefix cuts short: both fell back to `std::fs::read` of the whole
file — tens of megabytes for a JPEG of a few, on the format of the largest bucket in gaps.md (Canon,
33 % of photographers).

`preview_window` now asks `lightcraft_raw::sparse::preview_plan` — the module storytold#961 added for the
import path, which had no production caller — which ranges `embedded_preview` reads, and reads
exactly those: `planned_preview` fills a buffer of the file's length, reads the ranges the plan names
and asks again until it says `Ready`. The buffer is the file's length, which the memory gate counted
already, and its untouched pages stay zero, so what this path costs is the bytes read — on macOS a
freed buffer of that size may sit in the allocator's cache rather than go back to the OS (measured on
a 38k-photo import, storytold#832). The loop gives up past 60 % of the file, the bound step 2's partial read
uses, and a plan that does not fit, a file past 1 GiB or a window that does not decode all end in the
whole-file fallback.

The quick path hands the JPEG to the decoder alone, so two container facts the whole-file path applies
have to be visible in the bytes it is handed, or the file declines and the whole file keeps the
preview:

- a preview colour space of Adobe RGB (`decode_raw_preview` applies it; a JPEG-only decode has the
  JPEG's own space, else sRGB). A container that says sRGB does not decline — that is what the decode
  assumes anyway, and declining it took the fast path away from a real Sony ARW;
- the file's own EXIF orientation, when the preview JPEG carries none of its own: the plan's header is
  a bounded 64 KiB (the header route grows its prefix to the preview's declared extent instead), and
  an orientation tag it cannot see would show the preview unrotated.

Measured over the whole CC0 corpus (111 raws): 52 files take this route, reading a median 7.8 % of
their bytes (0.9-27.7 %) — RW2 3.3 %, Leica RWL 4.1 %, ARW 7.5 %, CR3 8.4 %, DNG 9.7 %, CR2 14.3 % —
while the 21 RAFs stay on the header route (median 4.5 %). The other 38 read whole: NEF, NRW, ORF, PEF
and SRW because the planner does not describe them (an SRW plans to the whole file, which the 60 %
budget then declines), plus the ARWs, CR2s, CR3s and DNGs that declare Adobe RGB or whose plan wants
more than that budget — the bytes they read are the ones the plan would have read anyway. Across all
111 files the plan handed on the same preview as the whole file, with the skipped bytes zeroed and
again 0xFF. `LIGHTCRAFT_PROFILE=1` prints

    [profile] preview …/arw-sony-a7rm4a-compressed.arw: read 688128 of 61832192 bytes (plan, window 483441)

Tests: a synthetic TIFF/DNG whose preview only its tags locate (identical pixels to the whole-file
path, the CFA strip unread), the same structure without the tag that makes it a raw, a file whose
orientation only the whole file shows, and a file that states Adobe RGB against one that states sRGB;
the opt-in real-file test reports which path answered.

Co-Authored-By: DeepSeek V3.2 <noreply@deepseek.com>
bogusyogi pushed a commit to Orthic-Labs/ember that referenced this pull request Oct 14, 2026
…lan's ranges

The header-only path added in storytold#755 answers for a Fuji RAF, whose preview pointer sits in the first
bytes. A Canon CR3 keeps its preview in a JPEG track, and a TIFF raw (CR2, ARW, DNG, RW2, 3FR …) can
keep its preview tag in an IFD the prefix cuts short: both fell back to `std::fs::read` of the whole
file — tens of megabytes for a JPEG of a few, on the format of the largest bucket in gaps.md (Canon,
33 % of photographers).

`preview_window` now asks `lightcraft_raw::sparse::preview_plan` — the module storytold#961 added for the
import path, which had no production caller — which ranges `embedded_preview` reads, and reads
exactly those: `planned_preview` fills a buffer of the file's length, reads the ranges the plan names
and asks again until it says `Ready`. The buffer is the file's length, which the memory gate counted
already, and its untouched pages stay zero, so what this path costs is the bytes read — on macOS a
freed buffer of that size may sit in the allocator's cache rather than go back to the OS (measured on
a 38k-photo import, storytold#832). The loop gives up past 60 % of the file, the bound step 2's partial read
uses, and a plan that does not fit, a file past 1 GiB or a window that does not decode all end in the
whole-file fallback.

The quick path hands the JPEG to the decoder alone, so two container facts the whole-file path applies
have to be visible in the bytes it is handed, or the file declines and the whole file keeps the
preview:

- a preview colour space of Adobe RGB (`decode_raw_preview` applies it; a JPEG-only decode has the
  JPEG's own space, else sRGB). A container that says sRGB does not decline — that is what the decode
  assumes anyway, and declining it took the fast path away from a real Sony ARW;
- the file's own EXIF orientation, when the preview JPEG carries none of its own: the plan's header is
  a bounded 64 KiB (the header route grows its prefix to the preview's declared extent instead), and
  an orientation tag it cannot see would show the preview unrotated.

Measured over the whole CC0 corpus (111 raws): 52 files take this route, reading a median 7.8 % of
their bytes (0.9-27.7 %) — RW2 3.3 %, Leica RWL 4.1 %, ARW 7.5 %, CR3 8.4 %, DNG 9.7 %, CR2 14.3 % —
while the 21 RAFs stay on the header route (median 4.5 %). The other 38 read whole: NEF, NRW, ORF, PEF
and SRW because the planner does not describe them (an SRW plans to the whole file, which the 60 %
budget then declines), plus the ARWs, CR2s, CR3s and DNGs that declare Adobe RGB or whose plan wants
more than that budget — the bytes they read are the ones the plan would have read anyway. Across all
111 files the plan handed on the same preview as the whole file, with the skipped bytes zeroed and
again 0xFF. `LIGHTCRAFT_PROFILE=1` prints

    [profile] preview …/arw-sony-a7rm4a-compressed.arw: read 688128 of 61832192 bytes (plan, window 483441)

Tests: a synthetic TIFF/DNG whose preview only its tags locate (identical pixels to the whole-file
path, the CFA strip unread), the same structure without the tag that makes it a raw, a file whose
orientation only the whole file shows, and a file that states Adobe RGB against one that states sRGB;
the opt-in real-file test reports which path answered.

Co-Authored-By: DeepSeek V3.2 <noreply@deepseek.com>
(cherry picked from commit 3c7f450)
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