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)!
Accepted Answer

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)!

Thanks! I my example, I just needed to change CGImageByteOrderInfo.order16Little.rawValue to CGImageByteOrderInfo.order32Little.rawValue, and it worked. It seems that the error message listing valid parameters for the color space is incorrect for the 6th option: 32 bits per pixel, 10 bits per component, kCGImageAlphaNone|kCGImagePixelFormatRGBCIF10|kCGImageByteOrder16Little

Thanks for pointing that out. The information in the error message is obviously a bug. Won't you please consider filing a bug report about that using the Feedback Assistant.

Bug Reporting: How and Why? has tips on creating your bug report.

How to create CGContext with 10 bits per component?
 
 
Q