 |
 |
|
 |
06-28-2009, 12:07 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 88
|
Bitmaps, Quartz, confusion...
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?
|
|
|
06-28-2009, 12:48 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Apr 2008
Posts: 329
|
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.
|
|
|
06-28-2009, 05:01 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 88
|
Quote:
Originally Posted by Delirium
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);
}
}
|
|
|
06-28-2009, 05:23 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Feb 2009
Posts: 88
|
Figured it out. Switched my x and y, of course
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 446 |
| 49 members and 397 guests |
| activ8, AdyCoder, Cathy, chinhminhdo, codeMonkey, crabFish, csnplt, DaveHolzer, dda, DorkyMohr, Eagle11, endy13, GarLe722, ggalante, gjosef, gomugroup, greenuns, Gudus, hawksz, healthyutech, hyang, imsatasia, iPhoneDevelopment, Jeremy1026, Jupiterus, Kryckter, learnSomething, luigi1977, mgales, mobi_dev, PeddlePower, Picoman, rocotilos, Sckah, scotopia, seriessix, soga, sojourner, StevenD, tateyaku, themathminister, tralhavet, treazer, TunaNugget, vcanic, Vivek Nirkhe, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,496
Threads: 35,780
Posts: 156,745
Top Poster: smasher (2,448)
|
| Welcome to our newest member, GarLe722 |
|