I am trying to get a pointer access directly to the underlying RGB buffer of a CGImageRef
. My first attempt is as follow:
CGImageRef image = [...];
CFDataRef rawData = CGDataProviderCopyData(CGImageGetDataProvider(image));
const UInt8 * buf = CFDataGetBytePtr(rawData);
int length = CFDataGetLength(rawData);
However if I compare the length length
with the product of width and height here is what I get:
size_t w = CGImageGetWidth(image);
size_t h = CGImageGetHeight(image);
CGColorSpaceRef color_space = CGImageGetColorSpace(image);
size_t nsamples = CGColorSpaceGetNumberOfComponents ( color_space );
assert( length == w * h * 4 && nsamples == 3 );
I can understand under some circumstances (mostly display purposes), it is easier to manipulate an RGBA (or ARGB) buffer, but in my specific case I simply need to decompress an RGB (=3 components) JPEG/PNG/TIFF image. The above code is working nicely to extract RGBA (4 values) buffer, but really what I am searching for is another API to get access to this raw RGB (3 pixel values) buffer. In which case I would have:
assert( length == w * h * 3 && nsamples == 3 ); // only RGB
When all else fails I could simply copy 3 RGB bytes out of the 4 RGBA bytes buffer, but that feels ugly.