Skip to content

opj_j2k: fix invalid tile part index error when decoding tiles with interleaved tile-parts#1644

Merged
rouault merged 1 commit into
uclouvain:masterfrom
mbklein:fix-interleaved-tile-parts
Jun 20, 2026
Merged

opj_j2k: fix invalid tile part index error when decoding tiles with interleaved tile-parts#1644
rouault merged 1 commit into
uclouvain:masterfrom
mbklein:fix-interleaved-tile-parts

Conversation

@mbklein

@mbklein mbklein commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #1558

Problem

opj_get_decoded_tile() failed with "Invalid tile part index for tile number N. Got X, expected 0" on codestreams whose tile-parts are interleaved across tiles (all tiles' first tile-part, then all tiles' second tile-part, etc.) and that only declare the total tile-part count (TNsot) in each tile's last tile-part.

Cause

When the tile is decoded, the prior tile decode stops as soon as its own last tile-part is consumed, which can be reached before the target tile's final tile-part (the one containing TNsot). The target tile's nb_tps is never set and its tile-part start positions are never recorded, so opj_j2k_decode_one_tile() takes the "index not built" fallback. That fallback seeks to m_last_sot_read_pos, which for an interleaved layout points past the tile's first tile-part, causing the decoder to encounter a non-zero TPsot first and abort.

This only occurs when multiple tiles are decoded from the same codec (e.g. j2k_random_tile_access). The first tile always succeeds because the index hasn't been built yet.

Fix

  • Seek to the start of the codestream (main_head_end + 2) instead, so the target tile's tile-parts are read in order. The faster index-based path used by ordered codestreams is unchanged.
  • Add a self-contained non-regression test (rta_interleaved_tile_parts): it encodes a small multi-tile image, rewrites it into the interleaved layout, then decodes every tile and checks the samples against a full-image decode. The test fails before this fix and passes after it, with no external test data dependency.

…nterleaved tile-parts (uclouvain#1558)

When a tile's tile-parts are interleaved across tiles and TNsot is only
declared in its last tile-part, opj_j2k_decode_one_tile() could not build a
reliable index and seeked to m_last_sot_read_pos, which may point past the
tile's first tile-part. Seek to the start of the codestream instead so the
tile-parts are read in order, and add a self-contained non-regression test.
@mbklein

mbklein commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

That one failing build action is due to a download hiccup, but it doesn't seem to be something I can retry myself.

@rouault rouault merged commit 9dd4b3c into uclouvain:master Jun 20, 2026
23 of 24 checks passed
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.

Invalid tile part index error on some JPEG 2000 files

2 participants