Reading/writing JPEG MPF (Multi-Picture Format) files

This follows a post on the ImageMagick forum: Add support for HDR gain maps · Issue #6377 · ImageMagick/ImageMagick · GitHub

JPEG files with HDR gainmaps are stored in JPEG MPF (Multi-Picture Format) files. (Similarly JPEGs with depth maps.) These have multiple instances of SOI/EOI markers. Exiftool can extract the extra images, but can’t write them. As far as I can see, exiv2 can’t read or write the extra images. jpeglib and jpeg-turbo can read the first SOI/EOI as a jpeg image, with metadata, and seem to ignore any following SOI/EOI pairs.

It would be useful if IM had a feature that could read all the images in the file. Similarly, when IM has a list of images, it should be able to write all these into a single JPEG MPF file. I am figuring out how to do this, for code to be added to IM.

A complication is that within the first instance of SOI/EOI, there is (or should be) an APP2 segment labelled “MPF”, with pointers to the images.

If we are reading a JPEG MPF and splitting it into separate images, we shouldn’t save that APP2 MPF segment. Similarly, when we are writing multiple images to a JPEG MPF, we need to create a APP2 MPF segment and write it to the appropriate place in the file.

As far as I can see, jpeglib etc has no facility for correctly handling MPF. It simply reads the first image (in the first SOI/EOI markers), including the APP2 MPF segment, and ignores the other images. I am happy to be corrected on this.

The best solution would be for jpeglib etc to handle MPF. Perhaps it already does, making IM’s job simple. If it can’t, then I can create code that reads the JPEG file (ignoring the APP2 MPF segment) and passes each SOI/EOI pair to be read as a single jpeg image. In the opposite direction, it would translate the images into SOI/EOI pairs, create the APP2 MPF segment at the appropriate place, and append the segments into a file.

Or perhaps there is some open source software that would assist.

Thoughts, anyone?

References:
JPEG - Wikipedia
https://www.w3.org/Graphics/JPEG/jfif3.pdf
https://www.w3.org/Graphics/JPEG/itu-t81.pdf Table B.1
https://www.cipa.jp/std/documents/e/DC-X008-Translation-2019-E.pdf Section 4.7.1
CIPA - Camera & Imaging Products Association: CIPA Standards

1 Like

Some potential useful hints as far as MPF support in libturbojpeg:

It’s not particularly informative, other than possibly “look at Skia’s source code”

FYI, it seems IM added support for Ultra HDR JPEGs via libultrahdr recently.

1 Like