Skip to content

fix(j2k-native): preserve reversible CPU precision for images and regions - #385

Merged
jcwal1516 merged 11 commits into
review/jpeg-scan-tailfrom
fix/reversible-decode-precision
Dec 2, 2026
Merged

jcwal1516 merged 11 commits into
review/jpeg-scan-tailfrom
fix/reversible-decode-precision

Conversation

@jcwal1516

@jcwal1516 jcwal1516 commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

What and why

Reversible JPEG 2000 transforms can produce intermediate integers above 2²⁴. At that point f32 can lose low bits, and decoding a region could give different samples from decoding the same pixels as part of the whole image.

The CPU decoder uses header bounds first. When those bounds are inconclusive, it checks the coefficients or rows it has decoded and switches the affected wavelet levels and colour transform to integer arithmetic as needed. The same checks cover whole images, reduced-resolution images and streamed regions.

This PR builds on #384 so the generated API report covers both the JPEG scan warnings and the CPU precision query.

Impact

  • Changes decoded output where floating-point rounding previously lost integer precision.
  • Adds a rustdoc-hidden range query used by adapters. The implementation API snapshot and release review include it.
  • Changes memory use for images that need integer reconstruction. Exact colour samples, integer reconstruction scratch and per-worker results share the existing decode memory budget. The ordinary path continues to use floating point when the bounds prove it exact.
  • Adds a test image or fixture.

Testing

  • Compared whole-image and region output with integer reconstruction, including tiled and streamed rows.
  • Exercised near-limit scratch reservations and worker result collection with small bookkeeping tests.
  • Regression coverage and an Unreleased changelog entry are included.
  • The affected release-mode library and integration suites passed on M5 Max (1,863 tests, 10 ignored). The batch reuse test first exposed the same invalid worker-count assumption seen on public main. This branch includes the reviewed test commit from PR fix(ci): correct the workspace reuse test and JPEG spelling check #383. The failed target and all remaining targets passed with that correction.
  • Project Clippy, rustfmt, documentation, and semver review passed. The CPU exact-memory regressions also passed in the private 0.15 integration checkout.

The reversible 5/3 inverse and inverse RCT run in f32, which is exact only
while every value they compute is an integer of magnitude at most 2^24.
Quantization exponents can allow more: a fuzzed 16-bit HT stream with one
exponent of 31 decoded to 26583 on the CPU and 26584 on Metal, and on the
CPU the result depended on which lifting path, scalar (i64 sums) or SIMD
(f32 sums), handled each sample.

Now the header decides first. A component whose coefficients may not be
integers f32 holds (more than 24 magnitude bits) decodes on the existing
integer path. A tile whose header bounds every transform value within 2^24
decodes as before. Any other tile, such as the T.803 file7 family, bounds
each component again from its decoded coefficients after Tier-1, scanning
bands from the coarsest level until the bound holds; a component it does
not cover runs its IDWT in integers from the same coefficients, and a tile
whose sample bounds do not cover the RCT runs that in integers at the end of
the decode. Nothing is decoded twice. The fused RGB8 last level takes only
header-proven tiles; tile workers hand their integer-RCT tiles to the
caller. The row-streamed region IDWT (streamed rows, banded and line region
decodes) keeps its unchecked f32 transforms, so those routes and their
memory floor are unchanged; checking them is left open. The CPU batch offset
plans and direct colour plans, whose transforms assume the header bound,
skip other streams through the new hidden
Image::reversible_transforms_fit_f32.

Tests: crafted 20-bit coefficients whose inverse transform passes 2^24, an
exponent-31 stream, a component switched by a QCC and a single noisy tile
among flat ones all decode like the forced integer path on every
whole-window route (native, 8-bit, regions, planes, serial, Auto and tile
workers), on NEON and on the scalar build; the batch plans route those
streams to the image decoder.

Single-threaded A/B on an Apple M5 (6 interleaved rounds): kodak lossless
classic and HT, file7.jp2, file7_b11/b15/b19 and p0_07/p0_08 decode within
-2.7% to +0.2% of before, with identical output.
…f32 range

14a6834a made the whole-window reversible 5/3 inverse and inverse RCT
exact, but left the row-streamed region IDWT (the line route, serial and
pool bands into planes, rows to a sink and tile workers' RGB8 lines) on
unchecked f32 for tiles whose header bounds don't prove it exact. A
20-bit stream whose coefficients reach 24 bits decoded to other samples
on the line route than on the integer path.

Each level of such a tile's reversible components now bounds what it has
read before every row, through reversible_range::sample_bound: the
largest LL, HL, LH and HH magnitudes of the rows it has taken. Levels the
header alone proves exact read nothing, and a level whose header band
bounds suffice beside an LL input of a few times the samples' range reads
only its LL rows, so conformant 16-bit streams scan a quarter of one
level. When a level's bound passes 2^24, that level and every finer one
switch to integers for the rest of the tile: their rings hold exact
integers below 2^24, which convert in place to i32 in the same memory
(32 levels of 24-bit coefficients stay below 2^31; sums run in i64), so
no row is reconstructed twice and nothing grows but one output row per
component, within the rings' charge. The last level's integer rows reach
the output rounded once to f32, with their integers beside them.

Rows streamed to a sink run the inverse RCT in integers per row when the
reconstructions' bounds, or else the rows' own magnitudes, don't show f32
exact; batches of a reader per component keep rows past 2^24 as their
i32 bits for it. Planes take each component's bound from its
reconstruction, scanning the plane only when that bound is too loose for
the RCT, and keep the integer rows of samples past 2^24 for the existing
per-tile integer RCT. Routes and memory charges are unchanged, so 17-20-bit
images keep their streamed route past the budget.

Tests: streams with raised coarse-band exponents, crafted 20-bit
coefficients at 1 and 3 guard bits, one component raised by a QCC, one
noisy tile among flat ones, noise only in the bottom half (the levels
switch after streaming rows in f32) and 8-bit RGB packed into a sink's
RGB8 region decode like the forced integer path on the line route, serial
and pool bands into planes, and rows from serial and pool bands (also
batched, and on 12 threads), also with integer samples kept for the RCT.
Without the check, the 3-guard-bit stream mismatches on the line route.
The tests that set the keep-wide-samples hook now take a lock, as they
run in parallel.

A/B on an Apple M4 Pro, interleaved (other workloads were running):
4096 x 4096 regions of 16384 x 16384 lossless images in 1024 x 1024
tiles (classic, HTJ2K) and 12-bit DX and mammography decodes, whose
headers prove f32 exact, are within -1% to +1% of before on 1 thread and
all threads (8 rounds). T.803 file7.jp2 and file7_b11/b15/b19.jph region
decodes, which take the check, are within -0.1% to +1.1% on 1 thread and
-1.2% to +1.5% on all threads (10 rounds). Output is identical.
@jcwal1516
jcwal1516 added this pull request to stack #373 December 2, 2026 04:50
Drop the append_reserved mutex guard after its last use so strict Linux Clippy does not report avoidable contention. Capacity checks and result movement are unchanged.
Base automatically changed from perf/prepared-cpu-images to main December 2, 2026 19:14
@jcwal1516
jcwal1516 force-pushed the fix/reversible-decode-precision branch 2 times, most recently from 22e7aaa to da1f5fb Compare December 2, 2026 19:16
@jcwal1516
jcwal1516 removed this pull request from stack #373 December 2, 2026 19:34
@jcwal1516
jcwal1516 changed the base branch from main to review/jpeg-scan-tail December 2, 2026 19:34
@jcwal1516
jcwal1516 added this pull request to stack #379 December 2, 2026 19:34
@jcwal1516
jcwal1516 marked this pull request as ready for review December 2, 2026 23:35
@jcwal1516
jcwal1516 merged commit 1505fbc into main Dec 2, 2026
32 of 36 checks passed
@jcwal1516
jcwal1516 deleted the fix/reversible-decode-precision branch December 2, 2026 23:35
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.

1 participant