Skip to content

Write full_width before failing on the height, as GetFullDimension32 does - #18

Closed
kunpai wants to merge 2 commits into
google:masterfrom
kunpai:full-width-before-length-fails
Closed

kunpai wants to merge 2 commits into
google:masterfrom
kunpai:full-width-before-length-fails

Conversation

@kunpai

@kunpai kunpai commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Write full_width before failing on the height, as GetFullDimension32 does

Problem

This PR builds on #12, which makes a directory whose size tags do not read fail, as in C++. With that change one difference is left in how the size is read.

GetFullDimension32 (C++, Rust) reads the full image's width and height:

For example, a NEF whose IFD0 has ImageWidth 40 (one LONG) and an ImageLength LONG with 107 values. Both sides return kFail, but a caller that reads the fields afterwards sees full_width 40 from C++ and 0 from the port:

C++ Rust (with #12)
return value kFail kFail
full_width × full_height 40×0 0×0

Question

Which behaviour do we want: C++'s or the Rust port's? Specifically, when a directory's width reads and its height does not, should full_width keep the width it read (like the C++ library), or be left as it was (like the Rust port)?

This PR implements C++'s behaviour.

Fix

When get_full_dimension32 fails with anything but TagNotFound, fill_preview_image_data now writes full_width as C++ would have before failing:

  • Which width: the width tag GetFullDimension32 reads first, the Exif width or else ImageWidth. With DefaultCropSize there is none, because C++ reads its width and height together.
  • Only from a SHORT or LONG entry: as C++'s Get reads it. The port's get_u32 also takes type IFD, so an IFD-typed width is skipped.
-                Err(_) => success = false,
+                Err(_) => {
+                    let width_tag = if dir.has(EXIF_TAG_DEFAULT_CROP_SIZE) {
+                        None
+                    } else if dir.has(EXIF_TAG_WIDTH) && dir.has(EXIF_TAG_HEIGHT) {
+                        Some(EXIF_TAG_WIDTH)
+                    } else if dir.has(TIFF_TAG_IMAGE_WIDTH) && dir.has(TIFF_TAG_IMAGE_LENGTH) {
+                        Some(TIFF_TAG_IMAGE_WIDTH)
+                    } else {
+                        None
+                    };
+                    let width = width_tag
+                        .filter(|&tag| dir.get_offset_and_length(tag, TiffType::Ifd).is_err())
+                        .map(|tag| dir.get_u32(tag));
+                    if let Some(Ok(fw)) = width {
+                        preview_data.full_width = fw;
+                    }
+                    success = false;
+                }

The example above then returns what C++ returns. Replayed before and after:

C++ Rust main Rust with #12 Rust with #12 and this PR
return value kFail kOk kFail kFail
full_width × full_height 40×0 0×0 0×0 40×0

Notes for review:

  • The campaigns kept 82 NEFs of this shape. With this PR, Read the full image size as FillPreviewImageData does #12 and Use piex's RawImageTypes numbers in the cxx bridge #9 (for output_type), C++ and the port agree on all 82. Without this PR, full_width (40 against 0) is the one field left on all of them.
  • Only the error path changes: a size that reads, or no size tags at all, is handled as before. A width typed IFD is not written, as C++ does not read it: on the example with its ImageWidth retyped to IFD, both sides give full_width 0. The other PRs' examples give the same results with and without this change.
  • 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.
C++'s GetFullDimension32 writes the width into full_width before it
reads the height, so a directory whose ImageWidth reads and whose
ImageLength does not fails with full_width already written. The port's
get_full_dimension32 returns both values or neither.

C++'s Get(tag, uint32*) reads only SHORT and LONG entries. get_u32 also
reads IFD, so a width typed IFD was written where C++ writes nothing.
@ShaddyDC

ShaddyDC commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

I believe this is outdated with the recent C++ code update. Please reopen if not.

@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