Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-28-2009, 12:07 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 88
Default 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?
jcafaro10 is offline   Reply With Quote
Old 06-28-2009, 12:48 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2008
Posts: 339
Default

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.
Delirium is offline   Reply With Quote
Old 06-28-2009, 05:01 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 88
Default

Quote:
Originally Posted by Delirium View Post
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);
		}
	}
jcafaro10 is offline   Reply With Quote
Old 06-28-2009, 05:23 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 88
Default

Figured it out. Switched my x and y, of course
jcafaro10 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,768
Threads: 89,201
Posts: 380,574
Top Poster: BrianSlick (7,129)
Welcome to our newest member, lucileadair
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:58 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0