Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ The changes are relative to the previous release, unless the baseline is specifi
AVIF_PIXEL_FORMAT_YUV400 to be AV1 spec compatible.
* Ignore tmap items not present in `grpl` box
* Update libyuv.cmd: dc47c71b3 (1907)
* Fix wrong Exif orientation set in JPEG or PNG output by avifdec when the input
AVIF file has an ImageRotation property with angle set to 1 or 3, has no
ImageMirror property, and carries an Exif chunk. Note that Exif orientation is
usually ignored in PNG files, so this mainly impacts JPEG files.

## [1.2.1] - 2025-03-17

Expand Down
4 changes: 2 additions & 2 deletions apps/shared/avifexif.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ uint8_t avifImageGetExifOrientationFromIrotImir(const avifImage * image)
}
return 5; // 90 degrees anti-clockwise then swap top and bottom.
}
return 6; // 90 degrees anti-clockwise.
return 8; // 90 degrees anti-clockwise.
}
if ((image->transformFlags & AVIF_TRANSFORM_IROT) && (image->irot.angle == 2)) {
if (image->transformFlags & AVIF_TRANSFORM_IMIR) {
Expand All @@ -31,7 +31,7 @@ uint8_t avifImageGetExifOrientationFromIrotImir(const avifImage * image)
}
return 7; // 270 degrees anti-clockwise then swap top and bottom.
}
return 8; // 270 degrees anti-clockwise.
return 6; // 270 degrees anti-clockwise.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified the bug fix by comparing this function with the corresponding code in Chrome. You can skip the following details.

Details:

In Chrome (third_party/blink/renderer/platform/image-decoders/avif/crabbyavif_image_decoder.cc), this operation is implemented as a table lookup (I replaced the symbolic constants with numeric values in the lookup table):

  // |angle| * 90 specifies the angle of anti-clockwise rotation in degrees.
  // Legal values: [0-3].
  int angle = 0;
  if (container->transformFlags & crabbyavif::AVIF_TRANSFORM_IROT) {
    angle = container->irot.angle;
    CHECK_LT(angle, 4);
  }
  // |axis| specifies how the mirroring is performed.
  //   -1: No mirroring.
  //    0: The top and bottom parts of the image are exchanged.
  //    1: The left and right parts of the image are exchanged.
  int axis = -1;
  if (container->transformFlags & crabbyavif::AVIF_TRANSFORM_IMIR) {
    axis = container->imir.axis;
    CHECK_LT(axis, 2);
  }
  // MIAF Section 7.3.6.7 (Clean aperture, rotation and mirror) says:
  //   These properties, if used, shall be indicated to be applied in the
  //   following order: clean aperture first, then rotation, then mirror.
  //
  // In the kAxisAngleToOrientation array, the first dimension is axis (with an
  // offset of 1). The second dimension is angle.
  constexpr std::array<std::array<ImageOrientationEnum, 4>, 3>
      kAxisAngleToOrientation = {{
          // No mirroring.
          {1, 8, 3, 6},
          // Top-to-bottom mirroring. Change Top<->Bottom in the first row.
          {4, 5, 2, 7},
          // Left-to-right mirroring. Change Left<->Right in the first row.
          {2, 7, 4, 5},
      }};
  orientation_ = kAxisAngleToOrientation[axis + 1][angle];

To match the code here, we need to read each column in Chrome's lookup table from the bottom up. For example, the second column read from the bottom up is 7, 5, 8. This should match the first outermost if block in this function. This confirms the last entry should be 8, not 6.

Similarly, the third column read from the bottom up is 4, 2, 3. This matches the second outermost if block in this function.

The fourth column read from the bottom up is 5, 7, 6. This matches the new code in the third outermost if block in this function.

Finally, the first column read from the bottom up is 2, 4, 1. This matches the last outermost if block and the final return statement in this function.

}
if (image->transformFlags & AVIF_TRANSFORM_IMIR) {
if (image->imir.axis) {
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ if(AVIF_GTEST)
add_avif_gtest_with_data(avifiostatstest)
add_avif_gtest_with_data(avifkeyframetest)
add_avif_gtest_with_data(aviflosslesstest)
add_avif_gtest_with_data(avifmetadatatest)
add_avif_internal_gtest_with_data(avifmetadatatest)

if(AVIF_ENABLE_EXPERIMENTAL_MINI)
add_avif_gtest(avifminitest)
Expand Down
18 changes: 18 additions & 0 deletions tests/gtest/avifmetadatatest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "avif/avif.h"
#include "avif/avif_cxx.h"
#include "avif/internal.h"
#include "avifexif.h"
#include "avifjpeg.h"
#include "avifpng.h"
#include "aviftest_helpers.h"
Expand Down Expand Up @@ -284,6 +286,22 @@ TEST(MetadataTest, ExifOrientation) {
EXPECT_EQ(image->width, temp_image->width /* should be height here */);
}

TEST(MetadataTest, AllExifOrientations) {
const ImagePtr image =
testutil::ReadImage(data_path, "paris_exif_orientation_5.jpg");
ASSERT_NE(image, nullptr);
image->transformFlags = AVIF_TRANSFORM_NONE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: It seems better to move these four lines (290-293) into the for loop so that each iteration of the for loop starts from a clean image. Right now each iteration starts from the results of the previous iteration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #2735

for (uint8_t orientation = 1; orientation <= 8; ++orientation) {
// Check roundtrip.
ASSERT_EQ(avifSetExifOrientation(&image->exif, orientation),
AVIF_RESULT_OK);
ASSERT_EQ(avifImageExtractExifOrientationToIrotImir(image.get()),
AVIF_RESULT_OK);
ASSERT_EQ(avifImageGetExifOrientationFromIrotImir(image.get()),
orientation);
}
}

TEST(MetadataTest, ExifOrientationAndForcedImir) {
const ImagePtr image =
testutil::ReadImage(data_path, "paris_exif_orientation_5.jpg");
Expand Down
Loading