Hey all, I've been searching around for some tutorials on doing things with bitmaps with quartz. I'm very new to this but stumbled upon some tutorials and here is the code that I came up with to start off.
Here's what I understand (or at least think I understand from reading the quartz programming guide and some other things).
CGImageRef basically just holds a certain version of the UIImage on my screen that I can mess around with. I'm not sure what the color space but I assume it has to do with figuring out how to interpret RGB components. Next I get the width and height of the image. I set some fields like bytesPerPixel (how many bytes to use to represent each pixel) and bitsPerComponent. If bytes per pixel is 4, does that mean 1 byte for Red, 1 for Blue, 1 for Green, 1 for Alpha? I suppose that goes with bitspercomponent = 8 since 8 bits is a byte.
Then I make the context which I imagine basically just says, where should the drawing of this image occur, then I draw it there. So now rawData should have all of my bitmap data. Then I tried to see what was actually in the array so I picked a random coordinate (100,100) and tried to see what the RGB components were but I didn't get numbers between 0-255 which is what I thought I'd expect.
Is my interpretation of what's going on here reasonably correct?
I believe all Quartz color and alpha values are floats between 0.0 and 1.0, where 0.0 would be 0, and 1.0 would be your 255.
How does an image get turned into a data array specifically? I'm trying to figure out why I'm getting bad accesses on some images. I think it has to do with how I'm calculating the byteIndex but not sure. The problem occurs at some point during the for loop where the byteIndex is becomes the size of the rawData array which is obviously not good.
Code:
CGImageRef image = [imageView.image CGImage];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
int size = height*width*bytesPerPixel;
unsigned char *rawData = malloc(size);
CGContextRef context = CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0,0,width,height),image);
CGContextRelease(context);
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
int byteIndex = (bytesPerRow * x) + y * bytesPerPixel;
int red = rawData[byteIndex];
int green = rawData[byteIndex+1];
int blue = rawData[byteIndex+2];
int alpha = rawData[byteIndex+3];
//printf("%d,%d: %d,%d,%d,%d\n",x,y,red,green,blue,alpha);
}
}