-
-
Save kiding/fa4876ab4ddc797e3f18c71b3c2eeb3a to your computer and use it in GitHub Desktop.
import UIKit | |
import MobileCoreServices.UTCoreTypes | |
if #available(iOS 14.1, *) { | |
let input = Bundle.main.url(forResource: "IMG_0037", withExtension: "HEIC")! | |
let output = FileManager().temporaryDirectory.appendingPathComponent("IMG_0037.GAIN_MAP.BMP") | |
let source = CGImageSourceCreateWithURL(input as CFURL, nil)! | |
// urn:com:apple:photo:2020:aux:hdrgainmap | |
let dataInfo = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap)! as Dictionary | |
let data = dataInfo[kCGImageAuxiliaryDataInfoData] as! Data | |
let description = dataInfo[kCGImageAuxiliaryDataInfoDataDescription]! as! [String: Int] | |
let size = CGSize(width: description["Width"]!, height: description["Height"]!) | |
let ciImage = CIImage(bitmapData: data, bytesPerRow: description["BytesPerRow"]!, size: size, format: .L8, colorSpace: nil) | |
let cgImage = CIContext().createCGImage(ciImage, from: CGRect(origin: CGPoint(x: 0, y: 0), size: size))! | |
let destRef = CGImageDestinationCreateWithURL(output as CFURL, kUTTypeBMP, 1, nil)! | |
CGImageDestinationAddImage(destRef, cgImage, [:] as CFDictionary) | |
CGImageDestinationFinalize(destRef) | |
print(output) | |
} |
Adobe, Android 14, Chrome, Edge, Brave, and Opera all support a JPG gain map standard published by Adobe/Google. That's the ideal solution now (already widely supported, backwards compatible with any viewer, can export with control over the SDR rendition via Lightroom / Adobe Camera RAW, and the spec is part of a draft ISO specification). https://gregbenzphotography.com/hdr-images/jpg-hdr-gain-maps-in-adobe-camera-raw/
Longer term, an AVIF gain map will be even better once the key pieces are in place to support encoding and viewing (this already has support in Chrome under a dev flag).
@andsimakov Shoot RAW and process for HDR. The results are much better than capturing directly as HEIF. I'd only shoot that format for very narrow situations such as sports (ie, the same scenarios where you would use JPG over RAW now for SDR). The HDR AVIF export from Lightroom is great for sharing in the Photos app on Apple devices, but the JPG gain map is better for the web (as it gives you significant control over the SDR rendition, rather than using automatic tone mapping - and it offers much better support for FireFox and Edge which don't support HDR AVIF at this time).
@gregbenz I agree with you completely. I wrote several JPEGs using the Adobe/Google format and it works well in Lightroom/Chrome/Android, as expected. But as the others on this thread have pointed out, Apple doesn't yet support it and we have no idea when they will. And most of my friends and family only use Apple devices so it'd be nice to have a format that works for them on say, iMessage or Safari.
AVIF seems to be only semi-working on both Apple and Google phones, so I wouldn't count on it as a universal solution for a while.
Any luck on your side getting Apple Photos to recognize a bespoke image?
@jiawen Indeed. It took me a while, but I was able to generate another test image that also works on macOS:
The following was needed to make it work:
- In the XMP metadata, the
HDRGainMapVersion
key needed to be present. (I set it to65536
) - A gain map auxiliary image of type
urn:com:apple:photo:2020:aux:hdrgainmap
. - The
{MakerApple}
metadata tags33
and48
need to have meaningful values according to Apple's documentation. (I set them to0.8
and0.0
for maximum effect.) - And, new to me, the image must be larger than 360 pixels in each dimension. For smaller images, the HDR effect simply doesn't show. This took me the longest to find out. 😅
The first and last point were needed on macOS, but iOS displayed HDR even without them.
I also found, on both platforms, that only Photos is capable of displaying gain map HDR images. The HDR effect appears nowhere else, neither in Messages, Preview, Files, etc.
either 65536 (0x10000) as in that thread, or 131072 (0x20000) in some newer files
Hu, I haven't seen 131072
yet. Do you have an example of where you've seen it?
@jiawen Did you write code to do that, or just use software like Adobe LR / ACR? ImageMagick has an open request for contributors who may be able to help support gain maps. ImageMagick/ImageMagick#6377. ImageMagick support would be key to support for gain maps in the WordPress media library, etc.
Another resource for this group (if the interest isn't just limited to HEIC / Swift) is the HDR JPG gain map library published by Google: https://github.com/google/libultrahdr. This is built to the Google / Adobe gain map spec (which is the basis of a draft ISO specification and is a de facto standard with support in Lightroom, Adobe Camera RAW, Chrome, Edge, Brave, and Opera already).
Hi @frankschlegel - this is amazing! I confirmed that your JPEG renders as HDR in Apple Photos my iPhone 15 Pro Max (running iOS 17.1) and M1 Macbook Pro (running macOS Sonoma). Unsurprisingly, it does not render correctly in Apple Photos on my M1 Mac Studio running macOS Ventura when connected to an Apple XDR display (you see a difference if you toggle HDR on/off in Preferences. But it doesn't show the HDR text. It only shows a flat field of bright white vs less bright white.). Surprisingly, it does render as HDR in Chrome (v118)! Even on Ventura. It looks like Google decided to parse one of the fields, probably the XMP you added.
Amazing!
Hu, I haven't seen 131072 yet. Do you have an example of where you've seen it?
I got the following in the aux image's XMP on a JPEG taken with an iPhone 15 Pro's rear camera. I extracted it using exiftool -MPImage2 -b my_image.jpg > aux.jpg
followed by exiftool -xmp -b aux.jpg
.
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="XMP Core 6.0.0">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:HDRGainMap="http://ns.apple.com/HDRGainMap/1.0/"
xmlns:apdi="http://ns.apple.com/pixeldatainfo/1.0/">
<HDRGainMap:HDRGainMapVersion>131072</HDRGainMap:HDRGainMapVersion>
<HDRGainMap:HDRGainMapHeadroom>5.651999</HDRGainMap:HDRGainMapHeadroom>
<apdi:NativeFormat>1278226488</apdi:NativeFormat>
<apdi:AuxiliaryImageType>urn:com:apple:photo:2020:aux:hdrgainmap</apdi:AuxiliaryImageType>
<apdi:StoredFormat>1278226488</apdi:StoredFormat>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
@jiawen Did you write code to do that, or just use software like Adobe LR / ACR? ImageMagick has an open request for contributors who may be able to help support gain maps. ImageMagick/ImageMagick#6377. ImageMagick support would be key to support for gain maps in the WordPress media library, etc.
Another resource for this group (if the interest isn't just limited to HEIC / Swift) is the HDR JPG gain map library published by Google: https://github.com/google/libultrahdr. This is built to the Google / Adobe gain map spec (which is the basis of a draft ISO specification and is a de facto standard with support in Lightroom, Adobe Camera RAW, Chrome, Edge, Brave, and Opera already).
I wrote bespoke code to do this. I would love to open source this code as it is simultaneously useful, esoteric, yet not too difficult when you use the right abstractions. But my manager probably wants me to spend my time elsewhere. I think my employer can be convinced to release this portion of our code and maintain it, but someone else will need to plumb it into ImageMagick as I lack expertise there.
@jiawen That's great. If you are able to share it, I think just getting it as it currently stands to ImageMagick via ImageMagick/ImageMagick#6377 may be of great value to help expedite those efforts.
I got the following in the aux image's XMP on a JPEG taken with an iPhone 15 Pro's rear camera.
@jiawen Very interesting! It seems Apple introduced a new version with the iPhone 15, including the new HDRGainMapHeadroom
XMP field. I wonder what this does... It's not documented in their gain map documentation so far.
Surprisingly, it does render as HDR in Chrome (v118)!
Also in Brave. It seems support for Apple's format was added together with the support for Adobe's format to the Chrome engine. Very nice!
I wonder where this is all going. On the developer side, Apple seems to advocate for exporting HDR images in what they call "ISO HDR" format (basically an HDR video still frame). In fact, that's the only official way for exporting HDR images. There are no simple APIs for writing an SDR image + HDR gain map.
@gregbenz You are very deep into this topic. Do you have an idea what will be the dominant standard in the future? This "ISO HDR" format seems to be the obvious candidate, as it's simply the same as HDR video. But it comes with the big disadvantage that there is no clearly defined SDR representation for those images. It's up to the displaying application how to tone-map those to SDR. The gain map approach is much more explicit.
I don’t know that I’d quite say ISO HDR is the same as a video still frame, not in a literal sense anyhow. Video tends to define a 100 nits white point, photography is using 203. There are certainly many commonalities (sadly including the lack of a common standard for tone mapping).
The “ISO HDR” refers to encoding of an HDR image. There is another ISO draft for gain maps. These are complimentary, as an ISO gain map can use an ISO HDR as the base image. You can use one without the other, but they work best together (if the base image in a gain map is HDR).
Gain maps are the obvious way forward. They offer a much better experience for viewing on any display which lacks the full HDR headroom encoded in the image. It’s vastly better than tone mapping.
JPG gain map is the obvious choice right now. Browsers representing ~75% of viewers (once fully updated) support it on systems with HDR display. And backwards compatibility is 100% (if your browser / image software doesn’t support HDR or gain maps, you gracefully fall back to a nice SDR).
However, JPG requires large files for high quality. There is a draft AVIF gain map standard (Adobe has shared sample files and Chrome/Brave support it). This is clearly the way things “should” go.
I say “should” because best doesn’t always win. As of now, Microsoft Edge (and the file explorer) doesn’t support any AVIF, and that needs to change for AVIF to be viable. Assuming that happens (they have support under a dev flag), we still need an encoder and more browsers to add support. It’s part of the same draft ISO proposal for gain maps and I hope to see it happen.
Another theoretical candidate would be JXL (JPEG XL). As browser support is zero and Chrome pulled support, it doesn’t seem promising anytime soon.
If it were all up to me: we’d use JPG gain maps for now, transition to AVIF gain maps as soon as MS Edge supports AVIF (encoded with an SDR base image for compatibility), then finally get to the ideal AVIF encoded with an HDR base image when support for AVIF gain maps is widely available.
The other thing I’d like to see is encoder support to let the artist provide the SDR rendition as well. This has many benefits, which I outlined in this feature request for Photoshop (please upvote it if you agree): https://community.adobe.com/t5/photoshop-ecosystem-ideas/allow-full-user-control-of-the-sdr-rendition-in-an-hdr-gain-map-export/idi-p/14205440#M19464
Although I'm late to the party, I must say this thread is the most informative and inspiring resource on the entire internet regarding this specific topic. I've truly learned a lot from all the discussions above and want to sincerely thank every participant and contributor. Inspired by the discussion, it seems I've figured out how to use Apple's public APIs to generate a JPEG with a gain map that renders properly on both iPhone and Mac. It can also be inspected by Adobe's demo app and other third-party software like HoneyView. I've shared the code with detailed documentation, hoping it will be helpful to others: https://github.com/grapeot/AppleJPEGGainMap
Some more information from the apple's developer site: https://developer.apple.com/videos/play/wwdc2024/10177/
I can't believe I managed to resurrect such an old thread on something so esoteric. Bravo! I love this community!
@frankschlegel Thanks for the pointer! I poked around and as usual with Apple, the documentation is maddening. There's literally no mention of it in any Apple docs besides the one you linked. I even
grep
ed all the headers and there's literally only these two:The only other mentions of
HDRGainMapVersion
on Google are related to this thread - it's about adding it to the XMP in the auxiliary gain map thread on exiftool. I poked around and found thatHDRGainMapVersion
is an integer, either65536 (0x10000)
as in that thread, or131072 (0x20000)
in some newer files. The stored format1278226488
is the integer value ofkCVPixelFormatType_OneComponent8
, which makes sense since the gain map is a JPEG that decodes to an 8-bit grayscale image encoded with a Rec. 709 transfer function.Any luck on your side getting Apple Photos to recognize a bespoke image?