1

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;
}
Adam
  • 913
  • 1
  • 9
  • 26
  • 1
    The color space that you show is not an indexed color space. It is an ICC-based RGB colorspace (or "Calibrated RGB"). - What trouble exactly do you have with your method? Error messages? Wrong results? – Martin R Aug 15 '12 at 05:02
  • 1
    Also, what are you trying to achieve, exactly? To "display" an image, you can just draw it into any CGContext, no matter what colorspaces the image and context use. CG will convert colors as necessary. Your CGContext doesn't have to have the same colorspace as the image. – Kurt Revis Aug 15 '12 at 05:19
  • 1
    I just want to draw img with the correct colors. The error I got with this codes is the line I included at the top. It complains about the indexed color space not being supported by CGBitmapContextCreate. I have tried just drawing image onto a CGColorSpaceCreateDeviceRGB() rather than CGImageGetColorSpace(img), and it does draw, but with the wrong colors. I tried to use CGImageCreateCopyWithColorSpace(img, CGColorSpaceCreateDeviceRGB()) to convert the image to a supported color space, but it returns NULL because there is only one component in an indexed color space, so nothing gets drawn. – Adam Aug 15 '12 at 11:49

0 Answers0