title: 64-bit RGBA UIImage? CGBitmapInfo for 64-bit
body: <p>I'm trying to save a 16-bit depth PNG image with P3 color space from a Metal texture on iOS. The texture has pixelformat = .rgba16Unorm, and I extract the data with this code</p>
  
  <pre><code>func dataProviderRef() -&gt; CGDataProvider? {
      let pixelCount = width * height
      var imageBytes = [UInt8](repeating: 0, count: pixelCount * bytesPerPixel)
      let region = MTLRegionMake2D(0, 0, width, height)
      getBytes(&amp;imageBytes, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)
      return CGDataProvider(data: NSData(bytes: &amp;imageBytes, length: pixelCount * bytesPerPixel * MemoryLayout&lt;UInt8&gt;.size))
  }
  </code></pre>
  
  <p>I figured out that the way to save a PNG image on iOS would be to create a UIImage first, and to initialize it, I need to create a CGImage. The problem is I don't know what to pass to CGIBitmapInfo. In the <a href="https://developer.apple.com/documentation/coregraphics/cgbitmapinfo" rel="nofollow noreferrer" title="CGIBitmapInfo documentation">documentation</a> I can see you can specify the byteOrder for 32-bit formats, but not for 64-bit.</p>
  
  <p>The function I use to convert the texture to an UIImage is this,</p>
  
  <pre><code>extension UIImage {
    public convenience init?(texture: MTLTexture) {
      guard let rgbColorSpace = texture.defaultColorSpace else {
          return nil
      }
      let bitmapInfo:CGBitmapInfo = [CGBitmapInfo(rawValue: CGImageAlphaInfo.last.rawValue)]
  
      guard let provider = texture.dataProviderRef() else {
          return nil
      }
      guard let cgim = CGImage(
          width: texture.width,
          height: texture.height,
          bitsPerComponent: texture.bitsPerComponent,
          bitsPerPixel: texture.bitsPerPixel,
          bytesPerRow: texture.bytesPerRow,
          space: rgbColorSpace,
          bitmapInfo: bitmapInfo,
          provider: provider,
          decode: nil,
          shouldInterpolate: false,
          intent: .defaultIntent
          )
      else {
          return nil
      }
      self.init(cgImage: cgim)
    }
  }
  </code></pre>
  
  <p>Note that "texture" is using a series of attributes that do not exist in MTLTexture. I created a simple extension for convenience. The only interesting bit I guess it's the color space, that at the moment is simply,</p>
  
  <pre><code>public extension MTLTexture {
    var defaultColorSpace: CGColorSpace? {
      get {
          switch pixelFormat {
          case .rgba16Unorm:
              return CGColorSpace(name: CGColorSpace.displayP3)
          default:
              return CGColorSpaceCreateDeviceRGB()
          }
      }
    }
  }
  </code></pre>
  
  <p>It looks like the image I'm creating with that code above is sampling 4 bytes per pixel, instead of 8. So I obviously end up with a funny looking image...</p>
  
  <p>How do I create the appropriate CGBitmapInfo? Is it even possible?</p>
  
  <p>P.S. If you want to see the full code with an example, it's all in github: <a href="https://github.com/endavid/VidEngine/tree/master/SampleColorPalette" rel="nofollow noreferrer">https://github.com/endavid/VidEngine/tree/master/SampleColorPalette</a></p>