Skip to content

perf(jpx): decode JPX images without a ColorSpace at the resolution they are drawn - #508

Merged
michaelstonis merged 2 commits into
spec/33-broadside-v1from
507-jpx-no-colorspace-reduced
Nov 12, 2026
Merged

michaelstonis merged 2 commits into
spec/33-broadside-v1from
507-jpx-no-colorspace-reduced

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

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 (the DocumentSession default, the Blazor render worker, and the demo's software path)
  • WebGLTileBackend (the demo's GPU path)
  • SkiaTileBackend (MAUI)
  • the CoreGraphics, Direct2D and Android backends

Each one calls DisplayImage.GetReduction(transform, k, converter) per draw and DisplayList.PrefetchImagesAsync per 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 ColorSpace was never kept encoded (CanKeepEncoded required layout.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):

File ColorSpace 390 px 1206 px 195 px 97 px
pdfjs/issue5567 DeviceRGB 0 0 1 2
pdfium-tests/fx/action/123 DeviceRGB 0 0 1 2
pdfjs/issue5481 none 0 (at build) 0 (at build) 0 (at build) -> 1 0 (at build) -> 1
pdfjs/issue5549 none 0 (at build) 0 (at build) 0 (at build) -> 1 0 (at build) -> 2

At 390 and 1206 px, k = 2 correctly 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.CanKeepEncoded resolves the colour space from the header when the dictionary has none. It uses the same CodecColorSpace that ResolveColorSpace now uses for decoded samples, and keeps the image encoded when the component count matches.
  • EncodedImage remembers the colour component count the build-time decode asked for: 0 without a ColorSpace. Every reduction then gets the same channels and conversions (sYCC, ICC, palette).
  • DisplayListBuilder takes the space from PreparedImage.
  • SMaskInData images and JPX soft-mask streams are unchanged: they are still decoded at build.

Output

  • Unchanged where the rule does not reduce. Renders of all four files at 390 and 1206 px are byte-identical to base, and issue5567 and 123 are identical at 195 px.
  • Reduced renders follow the existing JPX rule. issue5481 and issue5549 at 195 px now come from the JPX low-pass. They are 35.2 dB from a full-decode render. For comparison, the JPX images already kept encoded score 36.8 to 41.5 dB (issue5567) and 33.5 to 38.3 dB (123) at 195 and 97 px.
  • Tests. The full Broadside.Core.slnf suite passes (17,659 tests, goldens included). New tests in JpxImageTests cover sYCC, ICC and bare codestream without a ColorSpace: each is kept encoded, gets the space ResolveColorSpace gives, and matches a direct JpxDecodeFilter decode at every reduction and in full. SMaskInData stays 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 = 2 unless noted.

File Width JIT base -> head Native AOT base -> head Browser AOT (Node 22) base -> head
issue5481 195 52.6 -> 35.9 (-32%) 51.0 -> 36.3 (-29%) 108.2 -> 40.0 (-63%)
issue5481 390, k = 1 53.7 -> 37.3 (-31%) 52.3 -> 37.4 (-28%) 110.2 -> 42.4 (-62%)
issue5549 195 51.3 -> 33.4 (-35%) 49.6 -> 33.9 (-32%) 105.1 -> 38.2 (-64%)
issue5549 390, k = 1 52.6 -> 35.7 (-32%) 50.9 -> 34.7 (-32%) 107.8 -> 41.0 (-62%)
issue5481 / 5549 390, 1206 unchanged unchanged unchanged
issue5567 / 123 all unchanged unchanged unchanged

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

  • ISO 32000-2 §7.4.9: row updated (types, tests, notes).

Allocation gate

12 baseline entries change, each by -40 B per call: the ScaledImageBenchmarks.Page and SkiaFirstTileBenchmarks.FirstTile cases, which are DCT pages kept encoded. The list build now takes the colour space Prepare already resolved, instead of reading the image's layout a second time through PdfImage.ColorSpace (which allocated its Decode array again). The figures come from this PR's CI allocation run. Every other case is exact and unchanged.

…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).
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