Skip to content

Read the full image size as FillPreviewImageData does - #12

Closed
kunpai wants to merge 1 commit into
google:masterfrom
kunpai:full-size-as-fill-preview-image-data
Closed

kunpai wants to merge 1 commit into
google:masterfrom
kunpai:full-size-as-fill-preview-image-data

Conversation

@kunpai

@kunpai kunpai commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Read the full image size as FillPreviewImageData does

Problem

GetPreviewImageData fills a directory's fields with FillPreviewImageData (C++, Rust), and the two read full_width and full_height differently.

C++ does success &= GetFullDimension32(...) on every directory:

  • the size is written straight into full_width and full_height, whatever was there;
  • if the directory has size tags that do not read (a DefaultCropSize of 0, an ImageWidth with two values), the directory fails, and TiffParser::GetPreviewImageData then skips that IFD's Exif, GPS and SubIFDs.

The port ignores a size that does not read, writes it only when both values are larger than what is there, and does not take the size from directories whose NewSubfileType is not 0. For DNG it is the only side that reads NewSubfileType at all: DNG_TAGS asks for it, and C++'s tag set does not.

For example:

# C++ Rust
1 a DNG whose IFD0 ImageWidth has 103 values kFail; no preview, iso 0, GPS not valid kOk; preview 1024×768, iso 200, GPS valid
1 a DNG whose IFD0 DefaultCropSize points at zeros kFail; no preview, no Exif or GPS kFail; preview 1024×768, Exif and GPS filled
2 a caller's PreviewImageData with full_width 1024, full_height 0 full_width × full_height 1024×768 1024×0
3 a DNG as converters write it: IFD0 a thumbnail, SubIFDs the raw and a 1024×768 preview 1024×768 6000×4000

C++ is on the left and the Rust port on the right in the pictures below. Each panel shows what a viewer would get from that side's fields.

1. A size that does not read

When IFD0's size tags do not read, C++ fails IFD0 and leaves its Exif, GPS and SubIFDs unread. In a DNG this ends in kFail. In a NEF whose IFD0 ImageWidth has two values, C++ goes on to IFD1, which has the thumbnail, and returns kOk with that thumbnail and no preview. The port reads the preview and returns kUnsupported, because it has no full size to compare the preview with:

P4-1-nef-imagewidth

2. A size already in the caller's PreviewImageData

GetPreviewImageData writes into the caller's PreviewImageData. C++ overwrites full_width and full_height with each size it reads. The port keeps whatever was there unless both new values are larger, so a reused PreviewImageData with full_width 1024 comes back as 1024×0 from a 1024×768 file.

3. A DNG as converters write it

A DNG converter writes IFD0 as a thumbnail (NewSubfileType 1), and in its SubIFDs puts the raw image (NewSubfileType 0, DefaultCropSize 6000×4000) and then a JPEG preview (NewSubfileType 1, ImageWidth 1024, ImageLength 768). C++ does not read NewSubfileType here. It overwrites the size at every directory, and the preview's directory comes last, so C++ reports the preview's 1024×768 as the full image. The port does not take the size from the two NewSubfileType 1 directories and reports the raw's 6000×4000. The red frame is the full image each side reports, with the preview drawn to scale inside it:

P4-3-converter-dng

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically:

  1. should a directory whose size tags do not read fail, skipping its Exif, GPS and SubIFDs (like the C++ library), or be filled as if it had none (like the Rust port)?
  2. should each size read replace full_width and full_height (like the C++ library), or only a larger one (like the Rust port)?
  3. for DNG, should NewSubfileType be left unread, so that every directory's size counts (like the C++ library), or read, so that reduced-size directories do not (like the Rust port)?

This PR implements C++'s behaviour.

Fix

This PR changes the port to follow C++'s FillPreviewImageData and DngGetPreviewData:

  • A size that does not read fails the directory (1): in fill_preview_image_data, an error from get_full_dimension32 sets success = false. TagNotFound, the port's answer when there are no size tags at all, does not: C++'s GetFullDimension32 returns true then.
  • Every size read is written (2): full_width and full_height take the size whenever it reads, larger or not.
  • DNG does not read NewSubfileType (3): TIFF_TAG_SUB_FILE_TYPE leaves DNG_TAGS, which then holds C++'s tag set. No preview tag set reads NewSubfileType after this, so is_secondary stays false, as in C++.

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

# C++ Rust main Rust patched
1 ImageWidth with 103 values kFail, no preview kOk, preview 1024×768 kFail, no preview
1 DefaultCropSize at zeros kFail, no preview, iso 0 kFail, preview, iso 200 kFail, no preview, iso 0
1 NEF, ImageWidth with 2 values (picture 1) kOk, thumbnail only kUnsupported, preview 1024×768 kOk, thumbnail only
2 full_width 1024 handed in 1024×768 1024×0 1024×768
3 converter-style DNG (picture 3) 1024×768 6000×4000 1024×768

Notes for review:

  • Picture 3 shows C++ reporting a preview's size as the full image. For DNGs laid out like this, C++'s full size is whichever directory with a size comes last. This PR makes the port do the same. If the port's 6000×4000 is the behavior you want, the question is whether C++ should change instead.
  • One difference is left: C++'s GetFullDimension32 writes full_width before it fails on an ImageLength that does not read, and the port's get_full_dimension32 returns both values or neither. A NEF with that shape still differs in full_width. Write full_width before failing on the height, as GetFullDimension32 does #18 covers it.
  • The port's tests pass unchanged (44 + 174).

C++ fails a directory whose size tags do not read, writes every size it
reads, and does not read NewSubfileType for DNG. The port ignored a size
that did not read, kept only a larger one, and skipped reduced-size DNG
directories.
@ShaddyDC

ShaddyDC commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

I think this is outdated after updating the C++ code export. Will close, but please reopen if I'm wrong.

@ShaddyDC ShaddyDC closed this Nov 6, 2026
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