macoscolorsquartz-graphicsquartz-2dcgcolorspace

RGB colors are getting displayed as BRG after rendering on MAC OS X


When we are trying to render the image the image is getting displayed where the Red and Blue values are getting interchanged,

      {

    colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

        ...

    cgctx = CGBitmapContextCreate (bitmapData,
                                pixelsWide,
                                pixelsHigh,
                                8,      // bits per component
                                bitmapBytesPerRow,
                                colorSpace,
                                kCGImageAlphaPremultipliedLast);

 }

if i use kCGImageAlphaPremultipliedFirst instead of kCGImageAlphaPremultipliedLast the image different altogether and now i find the Green and Red and getting a bit interchanged.

Please Advice.


Solution

  • The above problem of color distortion was happening due to the way pixels have been read from the Bitmap context, it is better advised to overcome this situations by making the following changes,

    #if __BIG_ENDIAN__ bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big; #else bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little; #endif