1

I have a piece of code that creates a mask using the alpha channel of an image. Before upgrading to Mavericks, it worked great, and I never received a single warning or error in the Xcode debug console. Since upgrading, I'm receiving this warning:

Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGBitmapInfo' (aka 'enum CGBitmapInfo')

The line marked as being the problem:

CGBitmapContextCreate(alphaData, width, height, 8, rowBytes, NULL, kCGImageAlphaOnly);

Here's what the documentation says for the last argument (aka kCGImageAlphaOnly):

bitmapInfo

Constants that specify whether the bitmap should contain an alpha channel, the alpha channel’s relative location in a pixel, and information about whether the pixel components are floating-point or integer values. The constants for specifying the alpha channel information are declared with the CGImageAlphaInfo type but can be passed to this parameter safely. You can also pass the other constants associated with the CGBitmapInfo type.

Strangely, in the documentation (Alpha Information for Images), Apple's typedef doesn't list the option I'm using. However, in the term list that follows, it does! Here's what it says:

kCGImageAlphaOnly
There is no color data, only an alpha channel. Available in OS X v10.3 and later. Declared in CGImage.h.

Considering that I want to create an alpha mask, I'm not interested in any color data, so the argument I'm using is the only one that makes sense. I'm not sure what it wants me to do. Regardless, the method works, and all is well when the app runs. With that said, the warnings are really annoying. Is there a special CLANG directive to silence it? Any help would be appreciated. Cheers.

Community
  • 1
  • 1
Ben Stock
  • 1,986
  • 1
  • 22
  • 30
  • 1
    possible duplicate of [How do I create an alpha only bitmap context](http://stackoverflow.com/questions/17245787/how-do-i-create-an-alpha-only-bitmap-context) – rmaddy Oct 28 '13 at 02:19

1 Answers1

9

The problem is that you're making the compiler make an implicit conversion. The fix is to make the conversion explicit via a cast:

CGBitmapContextCreate(alphaData, width, height, 8, rowBytes, NULL, (CGBitmapInfo)kCGImageAlphaOnly);

Just because a function is implemented to allow values of multiple types for a given parameter (as noted in the documentation) doesn't mean that it can be defined such that the compiler won't complain about it. C doesn't let you define a param as taking multiple types, you have to pick one. In this case, CGBitmapContextCreate is defined as:

CGContextRef CGBitmapContextCreate (
  void *data,
  size_t width,
  size_t height,
  size_t bitsPerComponent,
  size_t bytesPerRow,
  CGColorSpaceRef colorspace,
  CGBitmapInfo bitmapInfo
);

Note the CGBitmapInfo type at the bottom there. That means, even if CGImageAlphaInfo values are technically legal, they'll still need to be cast to make it through the type checker.

jemmons
  • 18,605
  • 8
  • 55
  • 84
  • I didn't even think to do that. I'm not very smart. Thank you, man. Any idea of why it didn't complain before Mavericks? I'm new to the Xcode game, so I'm assuming all of my settings remain the same as they did pre-Mavericks. Right? :-) I guess the compiler's just beefing up its conversion game, eh? Either way, thanks for the quick fix. – Ben Stock Oct 28 '13 at 02:23
  • @BenStock The issue has nothing to do with Mavericks. It's a compiler issue. People have seen this issue with earlier versions of Xcode. What version of Xcode were you using before upgrading to Mavericks? – rmaddy Oct 28 '13 at 02:34
  • Yeah, it's not a Mavericks thing. This sort of check was always available when using LLVM/CLANG (though I'm not sure if it was on by default or not). If you weren't seeing this before and are now, it's likely because you either just switched to LLVM/CLANG or the "Implicit Enum Conversions" option in your Build Settings just got turned on for some reason. – jemmons Oct 28 '13 at 03:04
  • @jemmons: That's what I figured. With each update a new surprise awaits! Thanks for the help though. If I ever release my app, you'll be listed under "Badasses" in the About window. :-) – Ben Stock Oct 29 '13 at 18:01
  • @rmaddy: I was using the version that had the iOS-like styling and testing bots, so I'm guessing … Xcode 5.0.0? It's nearly the exact same now, but I noticed the documentation viewer has slightly changed. Weird. – Ben Stock Oct 29 '13 at 18:05