Files
pdfcraft/crates
42c0ba8679 M2: a JPX image on a rotated page draws instead of crashing (#3884) (#5438)
* M2: a JPX image on a rotated page draws instead of crashing (#3884)

The decode-size hint `image_draw_size` hands hayro-jpeg2000 mapped the
image's corners (w, 0) and (0, h) as points, so it took in the
translation. On a /Rotate 90 page (Acrobat Paper Capture scans) one
corner lands on the device origin, the hint gets a side of 0, and
hayro-jpeg2000 0.3.5 divides the image size by it: "renderer crashed on
page N: attempt to divide by zero" on every rotated JPX page.

The hint now comes from the transform's linear part (the axis lengths
the preview tiny-image check already used, now computed once), and each
side is at least 1. As a side effect, translated JPX images no longer get
an overestimated hint, so they can decode at a reduced resolution level
when drawn small, which is what the hint is for.

Regression test: a 1x1 JPX image filling a 40x40 page at /Rotate 0, 90,
180 and 270, rendered at 0.5x, 1x and 1.5x. It fails before the fix
(/Rotate 90 at 0.5x: attempt to divide by zero) and passes after.

Supersedes the guard in #1883 and does not depend on the hayro-jpeg2000
bump in #2309; both are open and conflicting with main.

Fixes #3884

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2

* M2: decode an offset JPX image area at full size (#3884 review)

hayro-jpeg2000 0.3.5 places a reduced resolution's samples using the
image area's reference-grid offset (XOsiz, YOsiz) unscaled. When a size
hint skips a level, an offset image either loses rows (offset 7, 7: no
reduced row is copied) or is sliced past a reduced row and panics
(offset 7, 0: "range start index 3 out of range for slice of length 2"
at decode.rs:486). The accurate hint from the previous commit makes
reduced decoding more common, so this matters more now.

filter::jpx::decode now passes the hint only when both offsets are 0,
read from the SIZ segment of a raw codestream or a JP2 jp2c box;
codestreams without an offset keep reduced decoding.

Tests: jpx_size_hint_is_dropped_only_for_an_offset_image_area fails
before the fix (the panic above, and 2x2 instead of 4x4 for the other
offsets) and passes after; the (0, 0) case pins that reduced decoding
is kept. image_area_offset_reads_raw_codestreams_and_jp2_files covers
raw, JP2 and truncated inputs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2

* M2: pin invalid /Rotate values in the rotated JPX test (#3884 review)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2

* M2: restore the /UserUnit test's rotations (#3884 review)

7928399 widened the rotation list in the rotated-JPX test with a sed that
also matched the identical loop in user_unit_scales_the_page_and_everything_on_it,
which then failed on /Rotate 45. Only the JPX test was meant to change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2

* M2: decode a colour-keyed JPX image at full size (#3884 review)

A colour-key /Mask (ISO 32000-2 8.9.6.4) is matched against the decoded
samples in resolve_alpha. A JPX image decoded at a reduced resolution for
the size hint has wavelet low-pass averages instead of its samples, so
keyed pixels next to unkeyed ones turned opaque, and unkeyed averages
could fall in the key range. The accurate hint from the first commit
makes reduced decodes more common.

ImageXObject::decoded_raster now drops the hint when a colour key is the
image's mask (no /SMask stream and no /SMaskInData 1, the precedence
resolve_alpha uses), so it is decoded at full size and resampled with its
mask like an image under any other filter. A /SMask keeps the reduced
decode. The decode cache key follows, since it is built from the same
hint.

Test: jpx_with_a_colour_key_mask_is_decoded_at_full_size decodes a 4x4
one-level JPX image with a 2x2 hint: without a mask, with /Mask [0 0],
and with /Mask [0 0] plus /SMask. The /Mask case gave 2x2 before the fix
and gives 4x4 after; the other two stay 2x2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C19EMwwBCMSkqew7KepPV2

* M2: pin a colour-keyed JPX image's transparency as drawn (#3884 review)

The colour-key test checked the size the image was decoded at. This one
checks the page: a 4x2 JPX checkerboard of samples 0 and 1 with
/Mask [0 0], drawn over red at 2x1 pixels, where the size hint skips a
resolution level. Half of each pixel's samples are keyed, so both pixels
come out half red (128, 1, 1).

With the fix in decoded_raster reverted, the reduced decode's averaged
samples no longer match the key and the image covers the red: both
pixels are (1, 1, 1) and the test fails.

The codestream is the one inside the 4x2 JP2 fixture in hayro-syntax's
filter::jpx tests.

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-15 22:14:10 +01:00
..
2026-11-13 18:59:56 +01:00