Automatically Rotating JPEGs
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?
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.
The orientation tag is a numerical value between 1 and 8, and each value can be translated as follows:
[table id=13 /]
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:
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:
[table id=14 /]
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.
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.