How to create CGContext with 10 bits per component?

I’m trying to create a CGContext that matches my screen using the following code

let context = CGContext(
    data: pixelBuffer,
    width: pixelWidth,    // 3456
    height: pixelHeight,  // 2234
    bitsPerComponent: 10, // PixelEncoding = "--RRRRRRRRRRGGGGGGGGGGBBBBBBBBBB"
    bytesPerRow: bytesPerRow, // 13824
    space: CGColorSpace(name: CGColorSpace.extendedSRGB)!,
    bitmapInfo: CGImageAlphaInfo.none.rawValue
      | CGImagePixelFormatInfo.RGBCIF10.rawValue
      | CGImageByteOrderInfo.order16Little.rawValue
  )

But it fails with an error

CGBitmapContextCreate: unsupported parameter combination:
 	RGB 
	10 bits/component, integer
 	13824 bytes/row
	kCGImageAlphaNone
	kCGImageByteOrder16Little
	kCGImagePixelFormatRGBCIF10
	Valid parameters for RGB color space model are:
	16  bits per pixel,		 5  bits per component,		 kCGImageAlphaNoneSkipFirst
	32  bits per pixel,		 8  bits per component,		 kCGImageAlphaNoneSkipFirst
	32  bits per pixel,		 8  bits per component,		 kCGImageAlphaNoneSkipLast
	32  bits per pixel,		 8  bits per component,		 kCGImageAlphaPremultipliedFirst
	32  bits per pixel,		 8  bits per component,		 kCGImageAlphaPremultipliedLast
	32  bits per pixel,		 10 bits per component,		 kCGImageAlphaNone|kCGImagePixelFormatRGBCIF10|kCGImageByteOrder16Little
	64  bits per pixel,		 16 bits per component,		 kCGImageAlphaPremultipliedLast
	64  bits per pixel,		 16 bits per component,		 kCGImageAlphaNoneSkipLast
	64  bits per pixel,		 16 bits per component,		 kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents|kCGImageByteOrder16Little
	64  bits per pixel,		 16 bits per component,		 kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents|kCGImageByteOrder16Little
	128 bits per pixel,		 32 bits per component,		 kCGImageAlphaPremultipliedLast|kCGBitmapFloatComponents
	128 bits per pixel,		 32 bits per component,		 kCGImageAlphaNoneSkipLast|kCGBitmapFloatComponents
See Quartz 2D Programming Guide (available online) for more information.

Why is it unsupported if it matches the 6th option? (32 bits per pixel, 10 bits per component, kCGImageAlphaNone|kCGImagePixelFormatRGBCIF10|kCGImageByteOrder16Little)

Answered by DTS Engineer in 820133022

Hello. Thanks for asking.

This snippet demonstrates how to allocate a CGGBitmapContext with ten bit pixel components. Note that pixel values are aligned on 32-bit word boundaries.

        // set up CG parameters
        let bitsPerComponent: Int = 10
        let bytesPerPixel: Int = 4
        let bytesPerRow: Int = imageWidth  * bytesPerPixel
        let rasterBufferSize: Int = imageWidth * imageHeight * bytesPerPixel

        // Allocate data for the raster buffer.
        let rasterBufferPtr: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: rasterBufferSize)

        let bitsInfo = (CGImageAlphaInfo.none.rawValue
                        | CGImagePixelFormatInfo.RGBCIF10.rawValue
                        | CGImageByteOrderInfo.order32Little.rawValue)
        
        // Create a CGBitmapContext for drawing 
        let context: CGContext = CGContext(data: rasterBufferPtr,
                                    width: imageWidth,
                                    height: imageHeight,
                                    bitsPerComponent: bitsPerComponent,
                                    bytesPerRow: bytesPerRow,
                                           space: CGColorSpace(name: CGColorSpace.extendedSRGB)!,
                                    bitmapInfo: bitsInfo)!

Hello. Thanks for asking.

This snippet demonstrates how to allocate a CGGBitmapContext with ten bit pixel components. Note that pixel values are aligned on 32-bit word boundaries.

        // set up CG parameters
        let bitsPerComponent: Int = 10
        let bytesPerPixel: Int = 4
        let bytesPerRow: Int = imageWidth  * bytesPerPixel
        let rasterBufferSize: Int = imageWidth * imageHeight * bytesPerPixel

        // Allocate data for the raster buffer.
        let rasterBufferPtr: UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: rasterBufferSize)

        let bitsInfo = (CGImageAlphaInfo.none.rawValue
                        | CGImagePixelFormatInfo.RGBCIF10.rawValue
                        | CGImageByteOrderInfo.order32Little.rawValue)
        
        // Create a CGBitmapContext for drawing 
        let context: CGContext = CGContext(data: rasterBufferPtr,
                                    width: imageWidth,
                                    height: imageHeight,
                                    bitsPerComponent: bitsPerComponent,
                                    bytesPerRow: bytesPerRow,
                                           space: CGColorSpace(name: CGColorSpace.extendedSRGB)!,
                                    bitmapInfo: bitsInfo)!
How to create CGContext with 10 bits per component?
 
 
Q