Reputation: 2026
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 theCGBitmapInfo
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 inCGImage.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.
Upvotes: 1
Views: 372
Reputation: 18657
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.
Upvotes: 9