-
Notifications
You must be signed in to change notification settings - Fork 254
Fixes wrong EXIF Orientation from Irot Imir #2729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
wantehchang
left a comment
There was a problem hiding this 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. |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #2735
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.
fixes #2727