Repository navigation
perf(jpx): decode JPX images without a ColorSpace at the resolution they are drawn - #508
Merged
michaelstonis merged 2 commits intoNov 12, 2026
Merged
Conversation
…hey are drawn A JPXDecode image whose dictionary has no ColorSpace (ISO 32000-2 §7.4.9) was always decoded at full resolution when its display list was built, then box averaged when drawn small, so viewers never dropped its resolution levels. Keep it encoded when the codestream's header gives its colour space (ICC profile, colour model or channel count, resolved as ResolveColorSpace resolves decoded samples), and decode it later asking for no colour components, as the build-time decode did. ImageHeader gains IccProfile so the header can name an ICCBased space. Closes #507
The list build now takes the colour space PdfImage.Prepare resolved instead of reading the image's layout a second time through PdfImage.ColorSpace, which allocated its Decode array again: -40 B per call in the 12 ScaledImageBenchmarks.Page and SkiaFirstTileBenchmarks.FirstTile cases (figures from the pull request's CI allocation run).
This was referenced Nov 12, 2026
Merged
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.
Closes #507. Follow-up to #362 and #352.
Was the reduced decode already used when zoomed out?
Mostly, yes. Every shipped viewer path picks the reduction the same way:
SoftwareTileBackend(theDocumentSessiondefault, the Blazor render worker, and the demo's software path)WebGLTileBackend(the demo's GPU path)SkiaTileBackend(MAUI)Each one calls
DisplayImage.GetReduction(transform, k, converter)per draw andDisplayList.PrefetchImagesAsyncper tile. The choice depends on scale alone, so every tile of a page agrees.That only helps an image the list kept encoded. A JPXDecode image without a
ColorSpacewas never kept encoded (CanKeepEncodedrequiredlayout.ColorSpace). It was decoded in full when the list was built, and only box-averaged when drawn.Traced reduction for the JPX decode (software backend,
k = 2, page fitted to the width):ColorSpaceAt 390 and 1206 px,
k = 2correctly reduces none of the four. For example, 1500 -> 390 px leaves 1.92 samples per pixel at a half. issue5481 offers only one level, and its ICC space is capped at a half anyway.Change
ImageHeader.IccProfile(new; the JPX header fills it from the colour boxes it already reads).PdfImage.CanKeepEncodedresolves the colour space from the header when the dictionary has none. It uses the sameCodecColorSpacethatResolveColorSpacenow uses for decoded samples, and keeps the image encoded when the component count matches.EncodedImageremembers the colour component count the build-time decode asked for: 0 without aColorSpace. Every reduction then gets the same channels and conversions (sYCC, ICC, palette).DisplayListBuildertakes the space fromPreparedImage.SMaskInDataimages and JPX soft-mask streams are unchanged: they are still decoded at build.Output
Broadside.Core.slnfsuite passes (17,659 tests, goldens included). New tests inJpxImageTestscover sYCC, ICC and bare codestream without aColorSpace: each is kept encoded, gets the spaceResolveColorSpacegives, and matches a directJpxDecodeFilterdecode at every reduction and in full.SMaskInDatastays decoded at build.Numbers
Build plus render of page 1 on a cold document, best of 9, two interleaved base/head rounds, Apple M4. Software tile backend,
k = 2unless noted.Times are in ms. The list build of issue5481 and issue5549 drops from about 50 ms to 0.2 ms: the decode moves to the first draw, with the render's cancellation token and the image cache budget.
Conformance
Allocation gate
12 baseline entries change, each by -40 B per call: the
ScaledImageBenchmarks.PageandSkiaFirstTileBenchmarks.FirstTilecases, which are DCT pages kept encoded. The list build now takes the colour spacePreparealready resolved, instead of reading the image's layout a second time throughPdfImage.ColorSpace(which allocated its Decode array again). The figures come from this PR's CI allocation run. Every other case is exact and unchanged.