Rotation destroys Preview images and leaves invalid MPF marker section

Started by Henk Demper, October 03, 2022, 04:26:26 PM

Previous topic - Next topic

Henk Demper

Hello Phil (I guess might answer),

I'm a developer having a large amount of JPEG photos and developing/improving an ultra-fast multi-platform (Mac, Windows, ...) viewing and manipulation application (private project, see in-progress snapshot) for (mainly) JPEG images in C++ using wxWidgets. I have created own code to parse the JPEG EXIF header and have used exiftool to analyse images and help out to decipher some of the encountered issues/details, thanks for all the good work on that ! For fast-displaying many images, I want to use the JPEG Thumbnail/Preview images (if present) first (which one based on current thumbnail viewing size), then revert to the (large) original image only if needed.

PhotoFlow.png

For years I have been rotating my images using jpegtran & jhead, because the older OS-ses (I just checked and on Windows that was in Explorer of Windows XP and 7) and browsers neglected the EXIF 'Orientation' tag. So I was under the assumption, and unaware of the specific JPEG/EXIF details at that time that I made a perfect (automated) rotation of any portrait image I made with one of my Nikon (D9 & 7200) cameras.

But now I see it a little different after recently tracing things back with exiftool, hex editors, own code and some Google searching. I believe this is more or less what happens:

  • I have a large amount of Nikon JPEG files with main image of say 6000x4000
  • Each image has a 'standard' (EXIF) Thumbnail of around 160x120 at the start
  • In addition, each image contains 2 Preview images of 640x424 and 1620x1080 (HD-like) resolution at the end of the file (after the main image)
  • Both main image plus the 2 Preview images are described correctly in APP2 MPF Marker
  • All images are always stored in landscape mode when written by the (Nikon) camera
  • There is a single Orientation 0x0112 tag in the EXIF, assumed to be applied to all above images

So far so good and so I have a main image, a small thumbnail and 2 usable Preview images... but:

  • If I (for a portrait image) perform jpegtran, then the main image is losslessly rotated Ok
  • But with that, the 2 Preview images are deleted/excluded, the file is now roughly 10% smaller because of this
  • The Orientation tag is not changed, but can be corrected with jhead (or exiftool, ...)
  • The (standard) thumbnail is still in landscape, but can be recreated with jhead
  • In the EXIF, the APP2 MPF Marker is still present, but untampered as original
  • That means that the MPF size of the main image is incorrect, and that the Preview entries point to nothing

So effectively the main image is nicely rotated and the small, standard thumbnail is also rotated.
But I lost all my Preview images and the EXIF contains a bogus APP2 MPF Marker that I need to ignore.
This means that if I want to use Preview images for fast viewing (and assuming the standard marker is too small to be useful), I need to load the main image for all rotated JPEG files instead with the associated performance penalty. Maybe less of an issue on modern hardware, but even then the JPEG files are metadata invalid. If I would have known before I would never have rotated any image in the past. Now preview pages with large amount of (portrait) images will be less snappy to load.

My believe/advice would be to not rotate any JPEG image with jpegtran/jhead/.... It is a bad thing, unless you are not concerned about Preview images and/or correct metadata. This is certainly not a big issue nowadays while most viewers respect the Orientation tag in the EXIF.

The question at the end (and assuring that I follow the correct coding path now): would you agree with above text: what is happening and the consequences ? Any additional, related information is appreciated.

Thanks in advance,

Best regards,

Henk Demper

Phil Harvey

Henk,

I think you are correct, but I have a question:

Why do you rotate the image and change the Orientation tag.  Just changing the Orientation tag should have the effect of rotating the main image.  Of course, rotation of the thumbnails and preview is another issue, but other than this, why doesn't just changing the Orientation do what you want?

- Phil

I see.  You explained that you want compatibility with older browsers. (I scanned over that, TLDR)
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Henk Demper

Hi Phil,

Thanks for the reply.

Indeed I started rotating 'back then' because of compatibility with older OS-ses (Explorer)/browsers. I kept doing this till date because I assumed it was not harmful and only made the files better, and it saved me implementing using the Orientation tag in my older version of the photo handling application...

From now on I will not rotate any more and exclusively (in existing & own apps) rely on the Orientation setting.

PS: Mentioned Nikon D9 in previous message, that should be Nikon D90...

Thanks again,

Henk Demper

blue-j

exiftran is an alternative to jpegtran that is also lossless but includes editing the EXIF orientation tag and thumbnails, should you decide to want that.

https://linux.die.net/man/1/exiftran

Quoteexiftran is a command line utility to transform digital camera JPEG images. It can do lossless rotations like jpegtran, but unlike jpegtran it can process multiple images at once, and it cares about the Exif data: it can rotate images automatically by checking the Exif orientation tag, it updates the Exif information (image dimensions/orientation) if needed, and it also rotates the Exif thumbnail.

I believe it may be Linux only, but am unsure, so perhaps not usable for you.

- J