Automatically Rotating JPEGs

SRPPicture

It recently came to our attention that images were getting rotated when being loaded into the SRP Picture Control. Admittedly, we were a bit skeptical we would reproduce the problem. We’ve added thousands of images over the years to our applications and never saw this behavior, but other customers confirmed the same behavior with many different images. It wasn’t long be we were able to reproduce the problem, although not all images would do this. What could possibly be going on here?

Observations

The first step in isolating the problem was to see where else this rotation seemed to be happening. Paint, Paint.NET, Snag-it, and Windows Explorer all showed the image correctly. However, OpenInsight’s BITMAP control would also rotate the image. What were we doing wrong? As we would come to find out, the problem was that we weren’t doing enough.

The next step was to try different image formats: BMPs, ICOs, and PNGs. None of these formats had a rotation problem. Only JPGs would do this, but not every time. We also noticed that if we resized the image in a tool such as Paint.NET and saved it, then everything would render right as rain in the SRP Picture Control.

Once the issue was narrowed down to JPGs, we remembered that JPGs are one of the few formats that have Exchangeable Image File Format (EXIF) metadata attached to it. Sure enough, among the many points of data available in the metadata is a tag called Orientation.

Orientation

The orientation tag is a numerical value between 1 and 8, and each value can be translated as follows:

ValueRow 0 is at the...Column 0 is at the...
1TopLeft
2TopRight
3BottomRight
4BottomLeft
5LeftTop
6RightTop
7RightBottom
8LeftBottom

What is all this nonsense about row and column zero? Imagine the image is stored on disk as a two dimensional array of pixels. Row 0 is red and column 0 is blue:

Rotate1

Thus, each value is telling anyone who cares how to orient the picture correctly by indicating the final destination of row 0 and column 0. If row 0 needs to stay on top but column 0 needs to be on the right (value 2), then we need to horizontally flip the image. Here is how each value needs to be manipulated in order to correctly display the image:

ValueRotateFlipResult
1NoneNoneRotate1
2NoneHorizontalRotate2
3180°NoneRotate3
4NoneVerticalRotate4
590°HorizontalRotate5
690°NoneRotate6
7-90°HorizontalRotate7
8-90°NoneRotate8

Explaining the Observations

The problem, in reality, was due to the fact that modern operating systems and image manipulation applications finally started respecting the orientation flag. Prior to Windows 8, images were not automatically rotated in Windows Explorer or Paint, so developers were not surprised when the image was loaded into the SRP Picture Control.

The issue is that the SRP Picture Control and OpenInsight BITMAP control both load the image as it is actually saved on disk. So, it was misleading to think the SRP Picture Control was inadvertantly rotating images when in actuality the SRP Picture Control was failing to keep up with the Jones’s.

Conclusion

OpenInsight 9.4 does not respect the orientation flag, so you need to account for that. We were able to notify Revelation, so expect OpenInsight 10 to auto rotate these images. In the meantime, a workaround is to use the built-in command JHead. For example:

JHEAD -v -autorot C:pictures***.jpg

This will orient the JPGs according the orientation flag and then remove that orientation flag so nothing else tries to auto-rotate it.

If, however, you are using the SRP Picture Control, as of version 4.0.2, it will auto rotate JPGs that have the orientation flag.

3 Responses to Automatically Rotating JPEGs

  • Chris Meyer says:

    And I thought I was the only one having this problem.
    Great work and good to see you have addressed the issue and updated the picture control.
    Just wish Revelation was as quick at posting an update.
    Thanks

    • Kevin Fournier says:

      It would have been nice, but I think this snuck up on a lot of people. If I may, I might redirect your frustration to the engineers that decided to have an orientation tag in the first place. If cameras just saved pictures in the proper orientation to begin with, then the images would have been instantly compatible with all imaging software. Instead, they made programmers play catch-up for the last several years.

Leave a Reply