I also got attracted to this topic and found @rued's answer very inspiring. As a newbie in Swift, with ChatGPT's help, I was able to make sense of the gainData and save it to an image, which looks reasonable. Here is a sample code snippet hoping to help others. This is an extremely interesting area, but the relevant docs and codes are extremely hard to find.
let imageURL = URL(fileURLWithPath: "/path/to/input/image.dng")
guard let source = CGImageSourceCreateWithURL(imageURL as CFURL, nil) else {
print("Error creating image source.")
exit(1)
}
let gainmap = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source, 0, kCGImageAuxiliaryDataTypeHDRGainMap)
let gainDict = NSDictionary(dictionary: gainmap ?? [:])
let gainData = gainDict[kCGImageAuxiliaryDataInfoData] as! Data
let gainDescription = gainDict[kCGImageAuxiliaryDataInfoDataDescription]
let gainMeta = gainDict[kCGImageAuxiliaryDataInfoMetadata]
let bitmapRep = NSBitmapImageRep(
bitmapDataPlanes: nil,
pixelsWide: Int((gainDescription as! [String: Any])["Width"] as! Int32),
pixelsHigh: Int((gainDescription as! [String: Any])["Height"] as! Int32),
bitsPerSample: 8,
samplesPerPixel: 1,
hasAlpha: false,
isPlanar: false,
colorSpaceName: .deviceWhite,
bytesPerRow: Int((gainDescription as! [String: Any])["BytesPerRow"] as! Int32),
bitsPerPixel: 8
)
gainData.copyBytes(to: bitmapRep!.bitmapData!, count: gainData.count)
if let pngData = bitmapRep!.representation(using: .png, properties: [:]) {
do {
try pngData.write(to: URL(fileURLWithPath: "/path/to/output.png"))
print("Image saved to \(outputPath)")
} catch {
print("Error saving image: \(error)")
}
} else {
print("Failed to create PNG data.")
}
Again I'm a Swift newbie, so the code might/must have not sticked to the recommended practice.
Topic:
Media Technologies
SubTopic:
General
Tags: