Converting ProRAW to HEIC while retaining HDR
I'm trying to batch convert ProRAW images in my photo library to HEIC while maintaining HDR. Preview on macOS seems to export a visually identical HEIC when viewing on my iPad Pro. I'd just use Preview for conversion, but it would crash trying to handle 10,000 photos at a time.
So I'm building a utility app to handle batches appropriately, but I can't seem to get a perfect visual replica of the original photo like Preview does. I based this code off a WWDC session. The HEIC does come out with HDR, but it's a bit underexposed compared to the ProRAW and Preview's HEIC export.
Tweaking the extendedDynamicRangeAmount doesn't seem to help, so are there any other settings or methods I should consider? Thanks!
func exportRawHDR(_ url: URL) {
guard let rawFilter = CIRAWFilter(imageURL: url) else { return }
rawFilter.extendedDynamicRangeAmount = 1.0
guard let output = rawFilter.outputImage else { return }
let heifURL = url.deletingPathExtension().appendingPathExtension("heic")
try? context.writeHEIF10Representation(
of: output,
to: heifURL,
colorSpace: CGColorSpace(name: CGColorSpace.itur_2100_PQ)!
)
}
Edit with solution: CGColorSpace.itur_2100_PQ
is in fact the correct color space, which you can verify by loading the CIImage like this: CIImage(contentsOf: url, options: [.expandToHDR: true]).colorSpace
. Without the expandToHDR option, it shows up as Display P3. To compensate for exposure, I just set it on the raw filter like so: rawFilter.exposure = 0.8
. I felt 0.8 was a good balance between keeping the image bright without blowing out the highlights.