I would like to keep the memory block allocated in the function void ManipulateImagePixelData(CGImageRef inImage) (see http://developer.apple.com/library/mac/#qa/qa1509/_index.html for the complete code)
void ManipulateImagePixelData(CGImageRef inImage)
{
// Create the bitmap context
CGContextRef cgctx = CreateARGBBitmapContext(inImage);
if (cgctx == NULL)
{
// error creating context
return;
}
// Get image width, height. We'll use the entire image.
size_t w = CGImageGetWidth(inImage);
size_t h = CGImageGetHeight(inImage);
CGRect rect = {{0,0},{w,h}};
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(cgctx, rect, inImage);
// Now we can get a pointer to the image data associated with the bitmap
// context.
void *data = CGBitmapContextGetData (cgctx);
if (data != NULL)
{
// **** You have a pointer to the image data ****
// **** Do stuff with the data here ****
}
// When finished, release the context
CGContextRelease(cgctx);
// Free image data memory for the context
if (data)
{
free(data);
}
}
I have modified the function so that I have the width and height, but I don’t manage to get the memory block data points to.
My function looks like:
void ManipulateImagePixelData(CGImageRef inImage,
unsigned long * width, unsigned long * height, void * copy)
I no longer free data at the end and take the responsability of freeing it later.
I thought I could do something simple like this:
(caller)
void * rawPixels=NULL;
ManipulateImagePixelData([obj CGImageForProposedRect:NULL context:[NSGraphicsContext currentContext] hints:nil],&imgWidth1, &imgHeight1, rawPixels)];
(ManipulateImagePixelData function)
void * pixels = CGBitmapContextGetData (cgctx);
if (pixels != NULL) {
*width=w;
*height=h;
copy=pixels;
[...]
And have rawPixels point to the aforementioned block, but rawPixels is still NULL after this call. I’m a bit confused by this and my C skills are a bit rusty.
What should I do to get the data ?
You should pass into
ManipulateImagePixelDataa pointer to the variable where you would like to get the address of the buffer allocated within the function:and call it like this:
Better yet, you could define it like this: