Skip to content

LR-IMP-PREVIEW-READ: a CR3 or a TIFF raw's preview is read from the plan's ranges - #1020

Merged
bflatastic merged 2 commits into
storytold:mainfrom
lzjtugjc:perf/raw-preview-plan
Oct 13, 2026
Merged

bflatastic merged 2 commits into
storytold:mainfrom
lzjtugjc:perf/raw-preview-plan

Conversation

@lzjtugjc

@lzjtugjc lzjtugjc commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What this does

Follow-up to #755 (merged in batch 31 as e4a248c9), which reads a Fuji RAF's preview from its
header. That path answers only when the pointer is in the file's 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 still read the whole file, so an unedited CR3 thumbnail — which
is the camera's own JPEG, since #755 — pulls tens of megabytes to show a JPEG of a few. Canon is the
largest bucket in the project's own gap ranking (33 % of photographers, gaps.md).

This asks lightcraft_raw::sparse::preview_plan — the module #961 added for the import path, which
until now had no production caller — which ranges embedded_preview reads, and reads exactly those.
@radialmonster suggested the same in #832 ("#755 could also use preview_plan once it's in").

How

  • planned_preview (crates/engine/src/files.rs): a buffer of the file's
    length, the ranges the plan names read into it (Ranges), asked again until Ready; then
    embedded_preview on that buffer. The header route (bounded_preview, the RAF pointer) is tried
    first and is unchanged.
  • Only the planned ranges are read; the buffer's other pages stay zero and are never written. The
    loader's memory gate still counts the whole file, exactly as before — this path's cost 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 (2–2.5 GB during a 38k-photo import, measured in Lightroom catalog import: every original is read in full and hashed, progress stays near 0 % and Cancel waits for all of it (38k photos ≈ 25 min on a USB SSD) #832), so this makes no promise about RSS.
  • The loop gives up past 60 % of the file — the bound step 2's probe_partial uses — and past
    1 GiB; a plan that says Whole, a range that does not fit, or a window that does not decode all
    return None, and the caller reads the file whole exactly as before.
  • PreviewRead.planned and the profile line say which path answered:
    [profile] preview …/arw-sony-a7rm4a-compressed.arw: read 688128 of 49479680 bytes (plan, window 483441)

Step 2 also touches files.rs, next to fs_hooks. If its partial read and this loop should become
one helper, I'm glad to factor it in whichever of the two lands second (@ingolfteetz offered the
same in #832).

The two container facts the quick path must not get wrong (both end in the whole-file fallback)

The quick path hands the JPEG to the decoder alone, so what the container says about the preview has
to be something the bytes it is handed can show:

  • Colour space. decode_raw_preview applies a preview colour space the container declares; a
    JPEG-only decode has the JPEG's own space, else sRGB. A container that declares Adobe RGB now
    declines. One that says sRGB does not — that is what the decode assumes anyway, and a guard that
    declined every declared space took the fast path away from a real Sony ARW while I was testing it,
    so that half has its own regression test.
  • Orientation. preview_orientation falls back to 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), so an orientation the header cannot see declines
    rather than showing the preview unrotated.

Measured

Over the full CC0 corpus (111 raws, cargo xtask corpus --download), measured through
preview_window itself:

route files read
plan (sparse::preview_plan) 52 median 7.8 % (0.9 %–27.7 %): RW2 3.3 %, RWL 4.1 %, ARW 7.5 %, CR3 8.4 %, DNG 9.7 %, CR2 14.3 %
header (#755, RAF) 21 median 4.5 %
whole file 38 100 %

The 38 are NEF, NRW, ORF, PEF and SRW — the planner does not describe the first four and an SRW plans
to the whole file, so the 60 % budget keeps the whole-file read — plus the ARWs, CR2s, CR3s and DNGs
that declare Adobe RGB or whose plan wants more than that budget. Across all 111 files the plan handed
on the same preview as the whole file, every time (skipped bytes 0 and 0xFF).

A Canon CR3's preview is its JPEG track instead (issue #832 measured 3 MB of one R5 Mark II file,
~15 %), so before this change every unedited CR3 thumbnail paid for the whole file.

Tests

  • crates/engine/src/files.rs: on a synthetic TIFF/DNG whose preview only
    its tags locate, the plan path and the whole-file path produce identical pixels, the CFA strip is
    not read and read.planned is set; the same structure without the tag that makes it a raw declines;
    a file whose orientation only the whole file shows declines; a file stating Adobe RGB declines while
    one stating sRGB stays on the plan path.
  • crates/raw/tests/sparse_corpus.rs: preview_plan is now asked
    for every corpus file, not only the ones whose header plan is Ready. That closes the gap where
    a NEF's, an ORF's and a PEF's preview plan was never exercised at all (their header probe reads
    samples, so the header plan is Whole and the preview assertions were skipped with it). The preview
    the plan hands on must equal the whole file's with the skipped bytes 0 and again 0xFF; the test
    prints what each format reads for both plans.
  • crates/engine/tests/preview_read.rs (opt-in, #[ignore]):
    unchanged contract, and it now reports [plan].

Verification

cargo xtask ci: all eight steps pass except clippy on stable 1.99.0, which fails on
crates/pipeline/src/spots.rs:250 (clippy::chunks_exact_to_as_chunks). That file is untouched here
and byte-identical to main, so main fails the same way on a current stable — the repo has no Linux
CI job that would catch it. With that one lint allowed
(cargo clippy --workspace --all-targets -- -D warnings -A clippy::chunks_exact_to_as_chunks), the
whole run is green, cargo test --workspace included.

cargo test --workspace passes with the corpus in place, except for one pre-existing failure that
has nothing to do with this PR and reproduces with upstream/main's own test file, unchanged:

crates/raw/tests/sparse_corpus.rs: arw6-sony-ilce7rm6-apsc-land-hq.arw: differs from the whole file
  left:  Err(Unsupported("ARW6 first-half row count outside 2..=7"))
  right: Ok(RawInfo { format: Arw, width: 6592, … })

header_plan marks the file ready while the ARW6 probe still needs bytes the plan skipped, so the
module's equivalence promise is broken for that file — in the import probe path, not the preview
one (the preview plan reaches Ready on it and yields the whole file's preview, which the corpus pass
above checks). It only shows up when the corpus is present, which the git-ignored corpus keeps out of
CI. Happy to look at it separately if that helps; it is not something this PR changes.

What this does not fix

Related: #832 (the issue and step 1, #961), #755.

lzjtugjc and others added 2 commits October 12, 2026 00:57
…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>
`preview_plan`'s assertions sat inside the `header_plan == Ready` branch, so a NEF, an ORF or a PEF
— whose header probe reads samples and whose header plan is therefore `Whole` — never had its
preview plan looked at, and neither did any file the header plan declines. The display path wants
exactly that answer, so both plans are now asked for every corpus file, and the preview the plan
hands on must be the whole file's with the skipped bytes `0` and again `0xFF`; `Whole` stays a
legitimate answer (that format reads whole for a preview too) and is counted. The test prints what
each format reads for both plans.

Co-Authored-By: DeepSeek V3.2 <noreply@deepseek.com>
@dev-null-undefined

Copy link
Copy Markdown
Contributor

A heads-up from #1410 (claude/small-embedded-preview). Stand-ins and grid thumbnails there decode the smallest embedded JPEG that covers the requested size (lightcraft_raw::embedded_preview_covering, called from embedded_preview_srgb) instead of the largest one. On an A7R VI that is the 1616 × 1080 JPEG (0.3 MB) instead of the 9984 × 6656 one (5.6 MB), and a 512 px thumbnail takes about 14 ms instead of 108 ms.

planned_preview here calls embedded_preview(&buf), which returns the largest JPEG, and then quick_image_srgb. Once both PRs land, a TIFF raw that goes through the plan (ARW, NEF, RW2, DNG…) would decode the largest JPEG again. preview_plan already reads the strips of every preview image, so the picker works on the plan's buffer too. The fix is to pass max_edge through preview_window and call embedded_preview_covering(&buf, max_edge) in planned_preview. The colour-space and orientation checks stay as they are, because every candidate comes from the same container.

Whichever PR lands second can wire it up. If this one lands first, I'll do it in mine.

@bflatastic

Copy link
Copy Markdown
Member

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


Generated by Claude Code

dev-null-undefined added a commit to dev-null-undefined/lightcraft-fork that referenced this pull request Oct 13, 2026
… embedded JPEG that covers them

`embedded_preview_srgb` (the loupe stand-in and the grid thumbnail of an unedited raw) decoded the
largest embedded JPEG whatever the size asked for: a 512 px thumbnail of a Sony A7R VI ARW decoded
its 9984 x 6656 JPEG (5.6 MB) although the file holds a 1616 x 1080 one (0.3 MB).

`lightcraft_raw::embedded_preview_covering(bytes, min_edge)` returns the smallest of a TIFF-based
raw's JPEGs whose long edge is at least `min_edge` and that is a copy of the largest one: aspect
ratio within 1 % (not a letterboxed thumbnail of another shape) and the same number of components
(not the grey semantic masks of an iPhone ProRAW DNG); else the largest, as `embedded_preview`.
The colour-space handling stays in `decode_raw_preview` (a `covering` argument); an unsupported
raw's editing source and the camera-look fit keep the largest JPEG, so a source never depends on
its size level.

On 120 raws of every make in the corpus at 256 / 512 / 1600 px: wherever the largest JPEG is still
chosen the output is identical byte for byte; where a smaller one is (Sony, Nikon, Panasonic,
Adobe DNG) orientation matches, the size is equal or 1-2 px off, mean difference <= 4.7 / 255.

- Rebased onto storytold#1020: its plan route (`preview_window` -> `planned_preview`) now takes the box size and picks
  with `embedded_preview_covering` too, so a TIFF raw read through the sparse plan gets the whole file's JPEG.

Co-Authored-By: Claude <noreply@anthropic.com>
bogusyogi pushed a commit to Orthic-Labs/ember that referenced this pull request Oct 14, 2026
… embedded JPEG that covers them

`embedded_preview_srgb` (the loupe stand-in and the grid thumbnail of an unedited raw) decoded the
largest embedded JPEG whatever the size asked for: a 512 px thumbnail of a Sony A7R VI ARW decoded
its 9984 x 6656 JPEG (5.6 MB) although the file holds a 1616 x 1080 one (0.3 MB).

`lightcraft_raw::embedded_preview_covering(bytes, min_edge)` returns the smallest of a TIFF-based
raw's JPEGs whose long edge is at least `min_edge` and that is a copy of the largest one: aspect
ratio within 1 % (not a letterboxed thumbnail of another shape) and the same number of components
(not the grey semantic masks of an iPhone ProRAW DNG); else the largest, as `embedded_preview`.
The colour-space handling stays in `decode_raw_preview` (a `covering` argument); an unsupported
raw's editing source and the camera-look fit keep the largest JPEG, so a source never depends on
its size level.

On 120 raws of every make in the corpus at 256 / 512 / 1600 px: wherever the largest JPEG is still
chosen the output is identical byte for byte; where a smaller one is (Sony, Nikon, Panasonic,
Adobe DNG) orientation matches, the size is equal or 1-2 px off, mean difference <= 4.7 / 255.

- Rebased onto storytold#1020: its plan route (`preview_window` -> `planned_preview`) now takes the box size and picks
  with `embedded_preview_covering` too, so a TIFF raw read through the sparse plan gets the whole file's JPEG.

Co-Authored-By: Claude <noreply@anthropic.com>
(cherry picked from commit 14e0d12)
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.

3 participants