ProRAW to CIRAWFilter to HEIF producing borked HDR results

Following WWDC 2023 "Support HDR images in your app", I'm trying to save 48-megapixel ProRAWs (taken on an iPhone 14 Pro Max) as HDR HEICs to the Photo Library. After processing the ProRAW file using CIRAWFilter, whether I use CIContext.heif10Representation() or convert to a CGImage, then UIImage, and use UIImage.heicData(), I get photos that behave oddly in the Photo Library. They appear too dark, and visibly brighten when first viewed, but more problematic is that the photos brighten a great deal more when you edit them with the Photos editor. This is the behavior when using the itur_2100_PQ color space, but itur_2100_HLG behaves similarly, except that it gets dramatically darker when edited. This behavior occurs whether CIRAWFilter.extendedDynamicRangeAmount is set to 0.0, or 2.0, or not set at all.

So what am I doing wrong? Here is a minimal iOS app -- well, just the ContentView -- that demonstrates the issue. You also need a .dng ProRAW file included in the project directory named test.dng. I'd love to include such a file, but I can't.

Be prepared for a multi-second wait when you save the photo.

import SwiftUI
import Photos

struct ContentView: View {

    let context = CIContext()
    let hdrColorSpace = CGColorSpace(name: CGColorSpace.itur_2100_PQ)!
    
    var body: some View {
        VStack(spacing: 100) {
            Button("Save Photo From CGImage/UIImage") {
                savePhotoFromUIImage()
            }
            Button("Save Photo From CIImage") {
                savePhotoDirectFromCIImage()
            }
        }.padding(60)
    }
    
    //convert RAW with CIRAWFilter to CIImage, then convert to CGImage, then UIImage, then HEIF
    private func savePhotoFromUIImage() {
        if let ciImage = processRAW(url: Bundle.main.url(forResource:"test", withExtension: "dng")!) {
            guard let outputCGImage = context.createCGImage(ciImage, from: ciImage.extent, format: .RGB10, colorSpace: hdrColorSpace) else { return }
            let uiImage = UIImage(cgImage: outputCGImage)
            if let heicData = uiImage.heicData() {
                saveHEIFPhotoToLibrary(imageData: heicData)
            } else {
                print("Failed to convert UIImage to HEIC")
            }
        }
    }
    
    //convert RAW with CIRAWFilter to CIImage, then to HEIF
    private func savePhotoDirectFromCIImage() {
        if let ciImage = processRAW(url: Bundle.main.url(forResource:"test", withExtension: "dng")!) {
            do {
                let heif = try context.heif10Representation(of: ciImage, colorSpace: hdrColorSpace)
                saveHEIFPhotoToLibrary(imageData: heif)
            } catch {
                print("Failed to get HEIF representation from CIContext")
            }
        }
    }
    
    private func processRAW(url: URL) -> CIImage? {
        guard let coreRawFilter = CIRAWFilter(imageURL: url) else { return nil }
        coreRawFilter.extendedDynamicRangeAmount = 2.0 //the issue persists whether this is not set, or set to 0, or set to, say, 2.0
        guard let ciImage = coreRawFilter.outputImage else { return nil }
        return ciImage
    }
    
    private func saveHEIFPhotoToLibrary(imageData: Data) {
        PHPhotoLibrary.shared().performChanges({
            let creationRequest = PHAssetCreationRequest.forAsset()
            let options = PHAssetResourceCreationOptions()
            creationRequest.addResource(with: .photo, data: imageData, options: options)
        }) { success, error in
            if let error = error {
                print("Error saving photo: \(error.localizedDescription)")
            } else {
                print("Photo saved.")
            }
        }
    }
}
ProRAW to CIRAWFilter to HEIF producing borked HDR results
 
 
Q