Skip to content

fix(ImageInput): Don't leave a rejected subimage as the current one - #5470

Open
lgritz wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
lgritz:lg-subimage
Open

lgritz wants to merge 1 commit into
AcademySoftwareFoundation:mainfrom
lgritz:lg-subimage

Conversation

@lgritz

@lgritz lgritz commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

This is a continuation of PR #5468 -- after fixing the TIFF reader, I audited the other readers for the same problem and found some issues in dds, dpx, fits, gif, heif, ico, openexr, ptex.

Those readers point themselves at a subimage, parse its header into m_spec, and only then decide whether to accept it -- but on rejection they return false without undoing any of that. The logical position (m_subimage/m_miplevel) and the decoder state no longer match, and this is a problem because seek_subimage() usually begins with a "we're already there" early out, which will be wrong.

The fix is either to give up the current subimage before overwriting anything, or to adopt the new one only once it has passed every check, so that a failed seek leaves the reader on no subimage rather than on a mismatched one. Rejecting a subimage seek should not cost us the open file: a seek to a different, valid subimage still has to work afterwards.

A few needing extra explanation:

  • HEIF's subimage-to-item-id lookup was off by one: m_item_ids[0] is already the primary image, so subimage 1 mapped back to the primary and every non-primary top-level image was unreachable. Found while testing the above, where a "rejected" seek to subimage 1 turned out to be a second successful seek to subimage 0.

  • Ptex also adopted the new face's resolution before checking that the face has the requested miplevel, so a failed probe left m_faceres and m_spec describing different faces; the miplevel check now comes first.

  • GIF also fixes a bug where it would not have correctly done a seek to an earlier subimage other than the first.

  • The DICOM reader has the same problem, but untangling it there needs more than a reordering, so it will be handled in a separate PR.

Assisted-by: Claude Code / Claude Opus 5

This is a continuation of PR AcademySoftwareFoundation#5468 -- after fixing the TIFF reader, I
audited the other readers for the same problem and found some issues
in dds, dpx, fits, heif, ico, openexr, ptex.

Those readers point themselves at a subimage, parse its header into
m_spec, and only then decide whether to accept it -- but on rejection
they return false without undoing any of that. The logical position
(m_subimage/m_miplevel) and the decoder state no longer match, and
this is a problem because seek_subimage() begins with a "we're already
there" early out, which will be wrong.

The fix is either to give up the current subimage before overwriting
anything, or to adopt the new one only once it has passed every check,
so that a failed seek leaves the reader on no subimage rather than on
a mismatched one. Rejecting a subimage seek should not cost us the
open file: a seek to a different, valid subimage still has to work
afterwards.

A few needing extra explanation:

- HEIF's subimage-to-item-id lookup was off by one: m_item_ids[0] is
  already the primary image, so subimage 1 mapped back to the primary
  and every non-primary top-level image was unreachable. Found while
  testing the above, where a "rejected" seek to subimage 1 turned out
  to be a second successful seek to subimage 0.

- Ptex also adopted the new face's resolution before checking that the
  face has the requested miplevel, so a failed probe left m_faceres
  and m_spec describing different faces; the miplevel check now comes
  first.

- GIF also fixes a bug where it would not have correctly done a seek
  to an earlier subimage other than the first.

The DICOM reader has the same problem, but untangling it there needs
more than a reordering, so it is handled separately.

Assisted-by: Claude Code / Claude Opus 5

Signed-off-by: Larry Gritz <lg@larrygritz.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.

1 participant