Skip to content

Follow SubIFDs only when the entry is a LONG, as C++ does - #13

Open
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:sub-ifds-long-only
Open

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:sub-ifds-long-only

Conversation

@kunpai

@kunpai kunpai commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Follow SubIFDs only when the entry is a LONG, as C++ does

NOTE: #23 and #24 answer this on both sides: the port's parse_sub_ifds reads the SubIFDs entry as a LONG, or else as an IFD, and C++'s ParseSubIfds does the same. Neither follows a SHORT entry. On master with both, the three rows below agree: type IFD gives the 1024×768 preview on both sides, type SHORT gives no preview on both, and type IFD pointing at a SubIFD that runs off the end gives kFail on both. So this PR can be closed once #23 and #24 are merged. They need to land together: on master with #24 alone, all three rows differ.

NOTE: Since #21, the port's get_u32_vec reads only SHORT and LONG entries, so a SubIFDs entry of type IFD is no longer followed, and the first and third rows below now agree on master. A SubIFDs entry of type SHORT (the second row) still differs on master: C++ returns no preview and a full size of 160×120, and the port returns the 1024×768 preview and 6000×4000. This PR still closes that case, and the port's tests pass (44 + 174).

Problem

When it parses an IFD, ParseSubIfds (C++, Rust) follows the SubIFDs entry (tag 0x014A) into the sub-directories, where DNGs and NEFs keep their previews.

For example:

C++ Rust
a DNG whose SubIFDs entry has type IFD, pointing at a good SubIFD kOk; no preview, thumbnail 160×120 kOk; preview 1024×768, thumbnail 160×120
the same, with type SHORT kOk; no preview kOk; preview 1024×768
a DNG whose SubIFDs entry has type IFD, pointing at a SubIFD that runs off the end of the file kOk; IFD0's thumbnail, Exif and GPS kFail; nothing

C++ is on the left and the Rust port on the right. Each panel shows the image a viewer would show for that side's fields: the preview if there is one, and otherwise the thumbnail, scaled up.

When the SubIFDs entry of a DNG has type IFD, C++ never reads the SubIFD that holds the 1024×768 JPEG preview, so a viewer has only the 160×120 thumbnail. The port follows it and returns the preview:

P5-subifds

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically, should a SubIFDs entry be followed only when its type is LONG (like the C++ library), or also when it is IFD or SHORT (like the Rust port)?

This PR implements C++'s behaviour.

Fix

parse_sub_ifds follows the entry only when get_offset_and_length(EXIF_TAG_SUB_IFDS, TiffType::Long) finds it, the port's counterpart of the C++ call, in place of has(EXIF_TAG_SUB_IFDS):

-        if tiff_ifd.has(EXIF_TAG_SUB_IFDS) {
+        if tiff_ifd.get_offset_and_length(EXIF_TAG_SUB_IFDS, TiffType::Long).is_ok() {

The examples above then return what C++ returns. Replayed before and after (x86-64):

C++ Rust main Rust patched
SubIFDs type IFD, good SubIFD (picture) no preview preview 1024×768 no preview
SubIFDs type SHORT, good SubIFD no preview preview 1024×768 no preview
SubIFDs type IFD, SubIFD past the end kOk, IFD0's fields kFail, nothing kOk, IFD0's fields

Notes for review:

  • TIFF Technical Note 1 and the DNG specification both allow SubIFDs as LONG or IFD, so files with an IFD-typed entry are valid. With this PR the port, like C++, finds no preview in them.
  • For the first two rows, full_width and full_height also differ before the patch (C++ 160×120, the port 6000×4000, from the SubIFD C++ never reads). The patched port matches C++ there too.
  • The port's tests pass unchanged (44 + 174).

C++ reads SubIFDs with GetOffsetAndLength(kTiffTagSubIfd, TIFF_TYPE_LONG),
which ignores an entry of any other type. The port followed SHORT and
IFD entries too.
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