4

I am trying to write an export function for my app that allows 16-bit depth HEIF files. Apple's documentation doesn't specify whether 16-bit is supported nor if the writeHEIFRepresentation() method requires any specific option for that. I have been trying with a 16-bit per color (64-bits per pixel) format and either the extended Display P3 or the HLG color spaces, but the output is still 8-bits. The source image is a 16-bits TIFF.

This is my playground code:

import Cocoa
import CoreImage

let originalImageURL = URL(fileURLWithPath: "/Users/myself/Desktop/DSC00990-HDR-3.tif")
let exportedImageURL = URL(fileURLWithPath: "/Users/myself/Desktop/DSC00990-HDR.heic")
guard let colorSpace = CGColorSpace(name: CGColorSpace.displayP3) else {
    print("Couldn't create a color space. :(")
    exit(1)
}
let context = CIContext()
let options = [kCGImageDestinationLossyCompressionQuality as CIImageRepresentationOption: 1.0 as CGFloat]

guard let image = CIImage(contentsOf: originalImageURL) else {
    print("Couldn't create an image. :(")
    exit(1)
}

do {
    _ = try context.writeHEIFRepresentation(of: image, to: exportedImageURL, format: CIFormat.RGBA16, colorSpace: colorSpace, options: options)
    print("It worked")
}
catch {
    print("It failed.")
}

Would any of you have experience with creating HEIF files on CoreImage to share, please? Perhaps Apple's encoder doesn't support 16-bit HEIF files yet?

Thank you so much in advance.

AndreG
  • 1,948
  • 1
  • 9
  • 13
  • You’ve misunderstood the docs. This is not a depth image. You are writing in 8 bit because your format is ABGR8. – matt Aug 13 '19 at 23:12
  • Yes, I did. As I mentioned the docs aren't clear. This is what depthImage's documentation looks like. https://developer.apple.com/documentation/coreimage/ciimagerepresentationoption/2902268-depthimage – AndreG Aug 13 '19 at 23:29
  • You are right, though. That colorspace is indeed 8-bit. However, using CIFormat.RGBA16 makes no difference either. – AndreG Aug 13 '19 at 23:34
  • The docs are perfectly clear if you know what a depth image is. I’m sorry my format idea isn’t helpful but the depth image is another matter entirely. – matt Aug 14 '19 at 01:12
  • 1
    I'm not quite sure what you are trying to achieve. Does the `.tif` file contain a depth image already? Are you trying to export _only_ the depth image or add it to another image? – Frank Rupprecht Aug 14 '19 at 07:05
  • 1
    `CIImageRepresentation.depthImage` is actually used to attach a _separate_ depth image to an image when writing it into the HEIC file (so the file would contain both). You may want to check out this session from last year's WWDC (they speak about the `portraitEffectsMatt`, but it handles the same): https://developer.apple.com/videos/play/wwdc2018-503/?time=300 – Frank Rupprecht Aug 14 '19 at 07:13
  • Thank you very much for your comments. I will update my question to make it clearer based on your suggestions. – AndreG Aug 14 '19 at 16:55
  • Matt, Preview shows 16-bit as an option for HEIC exports and macOS supports 16-bit on other image formats. However, I couldn't find any document from Apple stating limitations on their HEIC encoder. So maybe they will force everything down to 8-bit. – AndreG Aug 14 '19 at 16:58
  • Frank, thank you for the explanation and the URL! I will check it out. – AndreG Aug 14 '19 at 16:59

0 Answers0