tarmes
tarmes

Reputation: 15442

CGBitmapContextCreate: unsupported parameter combination

I'm getting this error when creating a bitmap context:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 7936 bytes/row.

Here's the code (note that the context is based on the parameters of an existing CGImage:

context = CGBitmapContextCreate(NULL,
                                (int)pi.bufferSizeRequired.width,
                                (int)pi.bufferSizeRequired.height,
                                CGImageGetBitsPerComponent(imageRef),
                                0,
                                CGImageGetColorSpace(imageRef),
                                CGImageGetBitmapInfo(imageRef));

Width is 2626, height is 3981. I've leaving bytesPerRow at zero so that it gets calculated automatically for me, and it's chosen 7936 of its own accord.

So, where on Earth is the inconsistency? It's driving me nuts.

Upvotes: 9

Views: 10312

Answers (7)

jules
jules

Reputation: 533

Late to the party, but setting CGBITMAP_CONTEXT_LOG_ERRORS=YES as part of your target's scheme will log a long list of valid combinations when CGBitmapContextCreate(...) has returned nil.

Upvotes: 0

Bryan
Bryan

Reputation: 5732

Swift Version

@tarmes's answer works great, but for modern times you'll need to translate kCGImageAlphaNoneSkipLast into Swift. The gibberish to do that is:

CGImageAlphaInfo.noneSkipLast.rawValue

Pass that for the bitmapInfo parameter.

Upvotes: 0

Antzi
Antzi

Reputation: 13424

Some pixel formats are just unsupported.

You can check in advance if any image is supported with:

extension CGImage {
  public var hasCGContextSupportedPixelFormat: Bool {
    guard let colorSpace = self.colorSpace else {
      return false
    }
    #if os(iOS) || os(watchOS) || os(tvOS)
    let iOS = true
    #else
    let iOS = false
    #endif

    #if os(OSX)
    let macOS = true
    #else
    let macOS = false
    #endif
    switch (colorSpace.model, bitsPerPixel, bitsPerComponent, alphaInfo, bitmapInfo.contains(.floatComponents)) {
    case (.unknown, 8, 8, .alphaOnly, _):
      return macOS || iOS
    case (.monochrome, 8, 8, .none, _):
      return macOS || iOS
    case (.monochrome, 8, 8, .alphaOnly, _):
      return macOS || iOS
    case (.monochrome, 16, 16, .none, _):
      return macOS
    case (.monochrome, 32, 32, .none, true):
      return macOS
    case (.rgb, 16, 5, .noneSkipFirst, _):
      return macOS || iOS
    case (.rgb, 32, 8, .noneSkipFirst, _):
      return macOS || iOS
    case (.rgb, 32, 8, .noneSkipLast, _):
      return macOS || iOS
    case (.rgb, 32, 8, .premultipliedFirst, _):
      return macOS || iOS
    case (.rgb, 32, 8, .premultipliedLast, _):
      return macOS || iOS
    case (.rgb, 64, 16, .premultipliedLast, _):
      return macOS
    case (.rgb, 64, 16, .noneSkipLast, _):
      return macOS
    case (.rgb, 128, 32, .noneSkipLast, true):
      return macOS
    case (.rgb, 128, 32, .premultipliedLast, true):
      return macOS
    case (.cmyk, 32, 8, .none, _):
      return macOS
    case (.cmyk, 64, 16, .none, _):
      return macOS
    case (.cmyk, 128, 32, .none, true):
      return macOS
    default:
      return false
    }
  }
}

See https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html for more information (and the list of supported pixel formats)

Upvotes: 0

tarmes
tarmes

Reputation: 15442

For reasons that I don't understand I solved this by setting the BitmapInfo parameter to kCGImageAlphaNoneSkipLast.

Upvotes: 21

sinewave440hz
sinewave440hz

Reputation: 1365

Heinrich gave you a good background to the answer. Just thought I'd offer up my specific case, as an alternative to tarmes' answer. The problem with that answer is that it doesn't solve the issue if you want an alpha channel present. I was using a category called UIImage+Alpha by Trevor Harmon when I encountered this issue. In the code I found this comment:

// The bitsPerComponent and bitmapInfo values are hard-coded to prevent an "unsupported parameter combination" error

Now this hardcoded fix was in one of the methods calling CGBitmapContextCreate, but not the one following it. So for me it was simply a matter of following the author's own advice to fix the problem in one of his other methods ;)

Clearly some part of the CGBitmapInfo is not getting passed correctly from the image in question, though why I don't know.

So use these constants in the bitmapInfo if you're working with the alpha channel: kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst

Otherwise, I'd just like to point out it's a really useful class if you're dealing with aliasing issues!

(Also, worth mentioning this problem only turned up in Xcode 6....)

Upvotes: 3

Heinrich Giesen
Heinrich Giesen

Reputation: 1835

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 7936 bytes/row.

In the Quartz 2D Programming documentation is a list of the supported pixel formats. The 8/3/24 combination is not supported but 8/3/32 is, independent of using alpha or not.

Upvotes: 8

Lương Quang Dũng
Lương Quang Dũng

Reputation: 11

I'm not sure it would help anybody or not, I just run into the similar issue and try anyway as suggested but no luck.

My issue is:

CCLabelTTF *header_txt = [CCLabelTTF 
   labelWithString:header 
   fontName:fontname fontSize:header_fontsize 
   dimensions:CGSizeMake(header_fontsize*9, txt_h) 
   hAlignment:kCCTextAlignmentLeft 
   vAlignment:kCCVerticalTextAlignmentCenter];

With Error:

< Error >: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 8 bits/pixel; 1-component color space; kCGImageAlphaNone; 2147483648 bytes/row.

Then I found a mistake is that header_fontsize is not assigned any values (because I mistake fontsize with header_fontsize). The error lies here: dimensions:CGSizeMake(header_fontsize*9, txt_h) with header_fontsize unassigned any value (it's not 0, assign header_fontsize = 0still ok); reassign a value to header_fontsize fixed the issue.

Hope this will help someone in similar case, such as Sprite case.

Upvotes: 1

Related Questions