Objective-C: CGBitmapCreateContext with Indexed ColorSpace

1.2k views Asked by At

I know the documentation says that CGBitMapContextCreate doesn't support indexed color spaces. That's troubling for me. I have a bunch of 256bit PNG files that I need to display, which are all indexed color spaces:

<CGColorSpace 0x101301000> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; sRGB IEC61966-2.1)

How can I convert the CGImageRef to a supported colorspace so that I can make this work?

Here's my method, which is having trouble:

- (BOOL) renderToBuffer:(void*)baseAddress withBytesPerRow:(NSUInteger)rowBytes pixelFormat:(NSString*)format forBounds:(NSRect)bounds
{

    CGContextRef context = CGBitmapContextCreate(baseAddress, bounds.size.width, bounds.size.height,
                                                 8, rowBytes, CGImageGetColorSpace(img),
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGImageRef imageRef = CGImageCreateWithImageInRect(img, bounds);
    CGContextDrawImage(context, CGRectMake(0, 0, bounds.size.width, bounds.size.height), imageRef);
    CGImageRelease(imageRef);
    CGContextRelease(context);

    return YES;
}
0

There are 0 answers