Skip to content

Conversation

@joojoooo
Copy link
Contributor

@joojoooo joojoooo commented Apr 1, 2025

fixes #2727

@y-guyon y-guyon requested a review from vrabaud April 1, 2025 14:29
@y-guyon y-guyon merged commit 0091d09 into AOMediaCodec:main Apr 1, 2025
33 of 35 checks passed
Copy link
Collaborator

@wantehchang wantehchang left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the fix!

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.

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

@wantehchang wantehchang added this to the v1.2.2 milestone Apr 1, 2025
fdintino added a commit to fdintino/Pillow that referenced this pull request Apr 3, 2025
This implements a fix for the issue identified in
AOMediaCodec/libavif#2727 and fixed in AOMediaCodec/libavif#2729. The
code to convert irot and imir properties to EXIF orientation when
decoding AVIF images in Pillow was repurposed from libavif, so it
suffers the same bug.
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.

Wrong EXIF Orientation converting from AVIF to JPG

4 participants