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.
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;
unsigned char *rawData = malloc(height*width*bytesPerPixel);
CGContextRef context = CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0,0,width,height),image);
CGContextRelease(context);
int byteIndex = (bytesPerRow * 100) + 100 * bytesPerPixel;
char red = rawData[byteIndex];
char green = rawData[byteIndex+1];
char blue = rawData[byteIndex+2];
char alpha = rawData[byteIndex+3];
printf("100,100:%c,%c,%c,%c \n",red,green,blue,alpha);
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?