Skip to content

feat(rendering): row reduction for images the codec cannot reduce (#362 part 4/4) - #412

Merged
michaelstonis merged 6 commits into
spec/33-broadside-v1from
362-4-row-reduction
Oct 24, 2026
Merged

michaelstonis merged 6 commits into
spec/33-broadside-v1from
362-4-row-reduction

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner

Part 4 of 4 of #362 (closes #362): images the codec cannot reduce are reduced by rows after decoding, and the measured gains of the whole issue.

What changes

  • Row reduction (ImageReducer): an image decoded at build (Flate, LZW, raw, CCITT, JBIG2, …) of at least 512 × 512 samples is reduced at the reduction the viewer asks for by an exact box average over 2^n × 2^n samples (the last row and column cover fewer), one row at a time, nothing allocated per row. Samples of fewer than 8 bits become 8-bit averages; DisplayImageSamples.DecodeMap carries the widened Decode mapping (§8.9.5.2 Table 88 is linear, so the mean of raw values maps to the mean of the component values).
  • Colour order: a reduction precedes colour conversion only when the conversion is affine (device spaces, any reduction). Curved conversions (CalRGB, CalGray, Lab, ICC) are capped at 1/2 (CalRGB at 1/4 measured 36.7 dB against converting first, short of the 45 dB the test asks), Indexed is never reduced (ImageReductions).
  • Masks: stencil and explicit masks reduce to 8-bit coverage (DisplayImageMask.GetReduction, MaskSource); a soft mask reduces on its own grid and folds into its image at the same reduction.
  • Each reduction is made once per image and cached with the image's samples, under the image cache budget.
  • ScrollFrameTests records the loading frames (first tile + 4 s: max frame, frames over 50 ms).

Measured (M4 Max)

ScaledScanBenchmarks (a letter page holding one Flate image, replay at 390 px, viewer vs page render):

Image Unscaled Scaled Allocated (replay)
2550 × 3300 8-bit gray photo 39.9 ms 12.3 ms 0 B
5100 × 6600 1-bit scan (600 dpi) 142.6 ms 12.2 ms 0 B

Whole issue (parts 1 to 4)

Catalogue-class page (2550 × 3150 DCT 4:4:4 photo with an ICC profile, CatalogueSamples.Cover, generated; never the owner's file):

Before After
Page at 390 px (build + decode + render) 211.8 ms 80.2 ms
Replay at 390 px 38.5 ms 11.8 ms
Page at 1206 px (k = 2 keeps 1/1) 232.4 ms 233.7 ms
Allocated, page at 390 px 33.6 MB 8.4 MB

JPX issue5567.pdf image: 472.8 ms → 149.6 / 41.0 / 11.1 ms at 1/2, 1/4, 1/8.

Blazor viewer (non-AOT, Chrome via Playwright, ScrollFrameTests; before = spec/33-broadside-v1 before part 1):

Device, document Worker First tile Max loading frame
phone 390×844 @3, CPU/4, catalogue cover yes 6385 → 2040 ms 1967 → 567 ms
desktop 1280×900 @2, catalogue cover yes 5489 → 1116 ms 458 → 125 ms
phone, issue5567.pdf yes 11413 → 2319 ms 9674 → 560 ms

Scrolling frames stay at 8.4 ms mean either way. Without the render worker the phone cover is unchanged: at DPR 3 the 2550-px cover is drawn at more than half its resolution, which k = 2 does not reduce (k = 1 would).

Allocation gate

No existing case changes; the only baseline change is the new ScaledScanBenchmarks.Replay(*) cases, 0 B (recorded from this PR's CI run). Getting there: the first version added about 96 B to every software render, reduced or not, and 16 B per kept-encoded image. Fixed by:

  • DisplayImageSamples no longer carries a Decode mapping (the value grew by 8 B wherever it is stored): DisplayImage.DecodeMapFor(samples) gives it, the same instance for samples of one depth;
  • the mask source remembers a soft mask by its display image and tells a reduction by the size it converted to; the reduced-mask scratch is rented per conversion;
  • the image source takes its size from the samples it remembers, leaving room for the flag that tells whether a soft mask was folded;
  • an encoded image's reduction state has its earlier fields (the largest reduction comes from the codec header); the full decode and widened mapping of a large image decoded at build live in a subclass only such images get.

Reduced-mask coverage counts are 64-bit, so a mask reduced by 2^15 cannot overflow them.

Tests

RowReductionRenderingTests (box-average exactness, the widened Decode map of a 1-bit image, the colour-order caps, small images left alone, gray / 16-bit RGB / 1-bit / stencil / soft masks of both sizes / explicit mask pages within PSNR ≥ 35 dB and ≤ 4 % over 8 levels of a full-resolution render, curved conversions within 45 dB of converting first, zero allocation replaying a reduced list), ScaledImageDecodeTests updated; the image-decode fuzz target checks reductions.

Conformance: §8.9.5.1, §8.9.5.2, §8.9.6.2, §8.9.6.3, §11.6.5.2 rows updated.

Deferred (follow-ups in the handoff)

  • Colour-key images and Matte soft masks are not reduced.
  • Native backends (Skia, CoreGraphics, Direct2D, Android, WebGL) draw full resolution.
  • The Blazor render worker uses k = 2 whatever the page's options say.

🤖 Generated with Claude Code

michaelstonis and others added 3 commits October 23, 2026 18:30
…#362)

Part 4 of 4 of issue #362: images the codec cannot reduce (Flate, LZW,
raw, CCITT, JBIG2, ...) of at least 512 x 512 samples are reduced after
decoding, by an exact box average, at the reduction the viewer asks for.

- ImageReducer: box average over 2^n x 2^n samples (the last ones fewer),
  one row at a time with no allocation per row; samples of fewer than 8
  bits become 8-bit averages and DisplayImageSamples.DecodeMap carries
  the widened Decode mapping (Table 88 is linear).
- Colour order: a reduction precedes the colour conversion only when the
  conversion is affine (device spaces); curved conversions (CalRGB,
  CalGray, Lab, ICC) are capped at 1/2, Indexed is never reduced
  (ImageReductions).
- Stencil masks and explicit masks reduce to 8-bit coverage
  (DisplayImageMask.GetReduction, MaskSource); soft masks reduce on their
  own grid and fold into the image at the same reduction.
- Reductions are decoded once per image and reduction, cached with the
  image samples as before.
- ScaledScanBenchmarks (8-bit gray photo and 1-bit scan); the
  image-decode fuzz target checks reductions; ScrollFrameTests records
  the loading frames (first tile + 4 s).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…te baseline)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 24, 2026 01:13

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

michaelstonis and others added 3 commits October 23, 2026 20:40
…eir size (#362)

The CI allocation gate still found +24 B per render and +16 B per page:
the Decode mapping in every DisplayImageSamples value, a soft-mask
reference in the image source and a reduction in the mask source. The
mapping now comes from DisplayImage.DecodeMapFor(samples) (the same
instance for samples of one depth); the image source remembers whether a
soft mask was folded, and the mask source tells a reduction by the size
it converted to (two reductions of one size are the same 1 x 1 average).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…heir earlier size (#362)

The gate still counted +8 B per render and +16 B per kept-encoded image.
The reduction state of an encoded image is back to its earlier fields:
the largest reduction comes from the codec header, and the full decode
and widened Decode mapping of a large image decoded at build live in a
subclass only such images get. The image source takes its size from the
samples it remembers, which leaves room for the flag that tells whether
a soft mask was folded (needed: a finer soft-mask reduction at hand can
change that for the same samples).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
New cases only: ScaledScanBenchmarks.Replay (0 B, scaled or not). No
existing case changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants