Skip to content

Instantly share code, notes, and snippets.

@kiding
Last active April 19, 2025 03:13
Show Gist options
  • Save kiding/fa4876ab4ddc797e3f18c71b3c2eeb3a to your computer and use it in GitHub Desktop.
Save kiding/fa4876ab4ddc797e3f18c71b3c2eeb3a to your computer and use it in GitHub Desktop.
Extracting HDR Gain Map from iOS 14.1+ (iPhone 12+) photos
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)
}
@rogino
Copy link

rogino commented Jan 17, 2025

Hmmm, I’m still leaning towards the photo having HDR metadata but it not being read for some reason. I’m running out of ideas though.

If you haven’t already, can you try watch this video and use the sample app to see if the photos captured in your app have HDR data?
https://developer.apple.com/videos/play/wwdc2023/10181/
(They don’t have screenshots of the sample app and I’m on my phone so I might have linked to the wrong one)

@alexfoxy
Copy link

Just to report back on this. I have managed to get the HDRGainMap from a AVCapturePhoto with the following:

let data = photo.fileDataRepresentation()!
let gainMap = CIImage(data: data, options: [.auxiliaryHDRGainMap : true])

The key thing was that the capture device was configured correctly. From an Apple DTS Engineer:

Make sure your capture device's activeFormat reports true for isHighestPhotoQualitySupported. (or use the ".photo" sessionPreset on the capture session).

The last hurdle I'm encountering is that this still returns nil on an iPhone 13. I'm assuming the older hardware does not support it, but it's strange because I can access some sort of HDRGainMap via the CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap)! as Dictionary method with a photo captured by the default camera app on the iPhone 13. It does appear different to the iPhone 16 Pro however, so I assume there is some difference in hardware output. See below:

Screenshot 2025-01-19 at 16 03 03

@isenberg
Copy link

I found this while searching for something else, but like to add a comment which may help others hitting the following.

In case anyone is trying this and just gets nil instead of the gainmap:
let gainmap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap)! as Dictionary

Try this as most HDR JPEGs today are already using the more standard ISO gainmap format:
let isogainmap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeISOGainMap)! as Dictionary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment