Repository navigation
LR-IMP-PREVIEW-READ: a CR3 or a TIFF raw's preview is read from the plan's ranges - #1020
Conversation
…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>
f2cd783 to
1030895
Compare
|
A heads-up from #1410 (
Whichever PR lands second can wire it up. If this one lands first, I'll do it in mine. |
|
Landed on main in #1431. Thank you, @lzjtugjc! Generated by Claude Code |
… 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>
… 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)
What this does
Follow-up to #755 (merged in batch 31 as
e4a248c9), which reads a Fuji RAF's preview from itsheader. 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, whichuntil now had no production caller — which ranges
embedded_previewreads, and reads exactly those.@radialmonstersuggested the same in #832 ("#755 could also usepreview_planonce it's in").How
planned_preview(crates/engine/src/files.rs): a buffer of the file'slength, the ranges the plan names read into it (
Ranges), asked again untilReady; thenembedded_previewon that buffer. The header route (bounded_preview, the RAF pointer) is triedfirst and is unchanged.
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.
probe_partialuses — and past1 GiB; a plan that says
Whole, a range that does not fit, or a window that does not decode allreturn
None, and the caller reads the file whole exactly as before.PreviewRead.plannedand 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 tofs_hooks. If its partial read and this loop should becomeone 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:
decode_raw_previewapplies a preview colour space the container declares; aJPEG-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.
preview_orientationfalls back to the file's own EXIF orientation when the previewJPEG 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 throughpreview_windowitself:sparse::preview_plan)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
0and0xFF).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
its tags locate, the plan path and the whole-file path produce identical pixels, the CFA strip is
not read and
read.plannedis 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.
preview_planis now askedfor every corpus file, not only the ones whose header plan is
Ready. That closes the gap wherea 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
Wholeand the preview assertions were skipped with it). The previewthe plan hands on must equal the whole file's with the skipped bytes
0and again0xFF; the testprints what each format reads for both plans.
#[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 oncrates/pipeline/src/spots.rs:250(clippy::chunks_exact_to_as_chunks). That file is untouched hereand byte-identical to
main, somainfails the same way on a current stable — the repo has no LinuxCI job that would catch it. With that one lint allowed
(
cargo clippy --workspace --all-targets -- -D warnings -A clippy::chunks_exact_to_as_chunks), thewhole run is green,
cargo test --workspaceincluded.cargo test --workspacepasses with the corpus in place, except for one pre-existing failure thathas nothing to do with this PR and reproduces with
upstream/main's own test file, unchanged:header_planmarks the file ready while the ARW6 probe still needs bytes the plan skipped, so themodule's equivalence promise is broken for that file — in the import probe path, not the preview
one (the preview plan reaches
Readyon it and yields the whole file's preview, which the corpus passabove 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
preview_planreturnsWholefor them becausethey share the header plan's early exit for formats whose header probe reads samples. @ingolfteetz
thinks previews may not need that exit, so lifting it in preview mode could bring at least NEF and
PEF in — the corpus pass above is what would tell, and it is in this PR rather than a change to
sparse.rs's semantics.fs_pair_loader), as in LR-IMP-PREVIEW-READ: a preview reads the raw's header, not the raw (56.5 MB → 5.9 MB, 10 s → 1 s over SMB) #755.Related: #832 (the issue and step 1, #961), #755.