Adding HDR Gain Map to HEIF/JPEG manually

Hi there.

I'm stuck with the creation of files with a wide dynamic range (Smart HDR?) as iPhone 12 and later creates. They look bright on the OLED screen. What I'm trying to do currently is to create a HEIF or JPEG image with an embedded auxiliary grayscale HDR Gain Map. I have a script in Swift. It works well because I can see that AuxiliaryImageType (for HEIF) or MPImage2 for (JPEG) tag but my file still looks dull and flat. I didn't find anything in the Documentation. What might be wrong?

Accepted Reply

I think I found the reason why the gain map isn't showing any effect:

There seem to be two private {MakerApple} EXIF tags controlling the HDR effect. The one @anteo83 mentioned (0x21) seems to control the global boost to brightness. I guess via some kind of gamma curve tone mapping.

But there is also the 0x30 which controls how much the embedded gain map adds to the effect. It seems to range between 0.0 and 8.0, with 0.0 having the biggest effect.

When 0x30 is present, 0x21 doesn't need to be present to show the effects of the gain map. They also seem to work in tandem, with 0x21 adding to the global value and 0x30 controlling local values via gain map.

There is some great fun to be had with this. Check out the attached image on your XDR or OLED device in Photos. 😁

Edit: I was trying to attach an example image here, but Forums seem to convert the image, losing the gain map in the process. You can find it here.

Replies

During my investigation I found that HDR Gain Map is not used to display images in the Photos app. There is a special EXIF tag to display images in HDR. Look at my automator script to change its value and make non-iPhone images bright: https://github.com/anteo/edit-hdr-gamma

  • That was a good investigation but it is half true. Apple does use an embedded HDR Gain Map, though in combination with the gamma setting. Gamma setting alone makes images brighter, but now so dramatically as iPhone does, with any value of gamma.

  • I got a similar result earlier with setting another HDR tag and command

    exiftool -overwrite_original_in_place -preserve '-CustomRendered=HDR (no original saved)' <image>

    (BTW it gives that HDR visual tag visible in Apple Photos app, top left corner). It also made photos brighter, but not enough.

  • Here's a research article what, I suppose, was the source of the idea: researchgate.net/publication/4288952_Display_HDR_Image_using_a_Gain_Map?enrichId=rgreq-0b085e96af6d797348d30128f6a1e31b-***&enrichSource=Y292ZXJQYWdlOzQyODg5NTI7QVM6MzMzMzIyMDQwNDMwNTk2QDE0NTY0ODE1NzYxOTY%3D&el=1_x_2&_esc=publicationCoverPdf

I think I found the reason why the gain map isn't showing any effect:

There seem to be two private {MakerApple} EXIF tags controlling the HDR effect. The one @anteo83 mentioned (0x21) seems to control the global boost to brightness. I guess via some kind of gamma curve tone mapping.

But there is also the 0x30 which controls how much the embedded gain map adds to the effect. It seems to range between 0.0 and 8.0, with 0.0 having the biggest effect.

When 0x30 is present, 0x21 doesn't need to be present to show the effects of the gain map. They also seem to work in tandem, with 0x21 adding to the global value and 0x30 controlling local values via gain map.

There is some great fun to be had with this. Check out the attached image on your XDR or OLED device in Photos. 😁

Edit: I was trying to attach an example image here, but Forums seem to convert the image, losing the gain map in the process. You can find it here.