Reputation: 12496
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.
Upvotes: 4
Views: 1167
Reputation: 6847
The framework that you are using does not support 24bpp image data. That's the implication of the "some circumstances" link in your question.
(You can see this table in Apple's documentation at https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_context/dq_context.html#//apple_ref/doc/uid/TP30001066-CH203-CJBEAGHH).
You will need to extract the bytes that you want from your rawData
and copy them into a separate buffer. (Or change your image-processing algorithm, whatever that is, to handle 32bpp.)
Upvotes: 2