Repository navigation
fix(j2k-native): preserve reversible CPU precision for images and regions - #385
Merged
jcwal1516 merged 11 commits intoDec 2, 2026
Merged
Conversation
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.
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.
jcwal1516
force-pushed
the
fix/reversible-decode-precision
branch
2 times, most recently
from
December 2, 2026 19:16
22e7aaa to
da1f5fb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What and why
Reversible JPEG 2000 transforms can produce intermediate integers above 2²⁴. At that point
f32can 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
Testing