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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 12-26-2011, 03:58 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default CGBitmapContextGetData not correct

Hi,
I'm trying to get the pixel color of a color wheel at a touch point. I set the color wheel image in drawRect, along with the bitmap context and data. I need to set the color wheel image (vs using an image on file) because I use an overlay to make the color wheel darker to get black. Then in the touches____ methods, I find the pixel in the bitmap data. The code is here:
Code:
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGImageRef wheel = [UIImage imageNamed:@"ColorWheel"].CGImage;
	CGContextTranslateCTM(context, 0.0, rect.size.height);
	CGContextScaleCTM(context, 1.0, -1.0);
	CGContextDrawImage(context, CGRectMake(rect.size.width / 2 - 140.0, rect.size.height / 2 - 140.0, 280.0, 280.0), wheel);

	UIGraphicsBeginImageContext(rect.size);
	currentColorWheel = UIGraphicsGetImageFromCurrentImageContext().CGImage;
	UIGraphicsEndImageContext();
	
	//Thanks to http://www.markj.net/iphone-uiimage-pixel-color/
	CGColorSpaceRef colorSpace;
	int bitmapByteCount, bitmapBytesPerRow;
	size_t width = CGImageGetWidth(currentColorWheel),
	height = CGImageGetHeight(currentColorWheel);
	bitmapBytesPerRow = width * 4;
	bitmapByteCount = bitmapBytesPerRow * height;
	colorSpace = CGColorSpaceCreateDeviceRGB();
	if (colorSpace == NULL)
	{
		NSLog(@"Color space could not be created.");
		return;
	}
	currentBitmapContext = CGBitmapContextCreate(NULL, width, height,  8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst);
	if (currentBitmapContext == NULL)
		NSLog(@"Bitmap context could not be created.");
	
	CGContextDrawImage(currentBitmapContext, CGRectMake(0.0, 0.0, width, height), currentColorWheel);
	imageData = CGBitmapContextGetData(currentBitmapContext);
	if (imageData == NULL)
	{
		NSLog(@"Image data could not be created.");
	}
	
	CGColorSpaceRelease(colorSpace);
}

//...

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	CGPoint touchPoint = [[touches anyObject] locationInView:self];
	if (DistanceBetweenPoints(CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2), touchPoint) <= 138.0)	//Check if the point is within the radius
	{
		if (currentBitmapContext != NULL && imageData != NULL)
		{
			size_t width = CGImageGetWidth(currentColorWheel);
			int index = 4 * (width * round(touchPoint.y) + round(touchPoint.x));
			NSLog(@"Index: %i size: %lu", index, sizeof(imageData) / sizeof(unsigned char));
			UIColor *color = [UIColor colorWithRed:imageData[index] green:imageData[index + 1] blue:imageData[index + 2] alpha:imageData[index + 3]];
			NSLog(@"Color: %@", color);
		}
	}	
}
Strangely, the log about the index and size in touchesEnded shows that imageData has a size of only 4 unsigned chars! As a result, the color is 0, 0, 0, 0. Any ideas on why this is happening and how I can get a correct color?

Thanks!
architectpianist is offline   Reply With Quote
Old 12-26-2011, 07:43 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by architectpianist View Post
Hi,
I'm trying to get the pixel color of a color wheel at a touch point. I set the color wheel image in drawRect, along with the bitmap context and data. I need to set the color wheel image (vs using an image on file) because I use an overlay to make the color wheel darker to get black. Then in the touches____ methods, I find the pixel in the bitmap data. The code is here:
Code:
- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGImageRef wheel = [UIImage imageNamed:@"ColorWheel"].CGImage;
	CGContextTranslateCTM(context, 0.0, rect.size.height);
	CGContextScaleCTM(context, 1.0, -1.0);
	CGContextDrawImage(context, CGRectMake(rect.size.width / 2 - 140.0, rect.size.height / 2 - 140.0, 280.0, 280.0), wheel);

	UIGraphicsBeginImageContext(rect.size);
	currentColorWheel = UIGraphicsGetImageFromCurrentImageContext().CGImage;
	UIGraphicsEndImageContext();
	
	//Thanks to http://www.markj.net/iphone-uiimage-pixel-color/
	CGColorSpaceRef colorSpace;
	int bitmapByteCount, bitmapBytesPerRow;
	size_t width = CGImageGetWidth(currentColorWheel),
	height = CGImageGetHeight(currentColorWheel);
	bitmapBytesPerRow = width * 4;
	bitmapByteCount = bitmapBytesPerRow * height;
	colorSpace = CGColorSpaceCreateDeviceRGB();
	if (colorSpace == NULL)
	{
		NSLog(@"Color space could not be created.");
		return;
	}
	currentBitmapContext = CGBitmapContextCreate(NULL, width, height,  8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst);
	if (currentBitmapContext == NULL)
		NSLog(@"Bitmap context could not be created.");
	
	CGContextDrawImage(currentBitmapContext, CGRectMake(0.0, 0.0, width, height), currentColorWheel);
	imageData = CGBitmapContextGetData(currentBitmapContext);
	if (imageData == NULL)
	{
		NSLog(@"Image data could not be created.");
	}
	
	CGColorSpaceRelease(colorSpace);
}

//...

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	CGPoint touchPoint = [[touches anyObject] locationInView:self];
	if (DistanceBetweenPoints(CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2), touchPoint) <= 138.0)	//Check if the point is within the radius
	{
		if (currentBitmapContext != NULL && imageData != NULL)
		{
			size_t width = CGImageGetWidth(currentColorWheel);
			int index = 4 * (width * round(touchPoint.y) + round(touchPoint.x));
			NSLog(@"Index: %i size: %lu", index, sizeof(imageData) / sizeof(unsigned char));
			UIColor *color = [UIColor colorWithRed:imageData[index] green:imageData[index + 1] blue:imageData[index + 2] alpha:imageData[index + 3]];
			NSLog(@"Color: %@", color);
		}
	}	
}
Strangely, the log about the index and size in touchesEnded shows that imageData has a size of only 4 unsigned chars! As a result, the color is 0, 0, 0, 0. Any ideas on why this is happening and how I can get a correct color?

Thanks!
If you pass a pointer type to sizeof(), it tells you the size of the pointer, not the size of the data structure that the pointer points to. That's not your problem.


I haven't looked over your code in enough detail to tell what IS wrong with it. I find it easier to troubleshoot code by stepping through it in the debugger. That would be my suggestion.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-26-2011, 08:01 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Default

Quote:
If you pass a pointer type to sizeof(), it tells you the size of the pointer, not the size of the data structure that the pointer points to. That's not your problem.
I assume you're talking about this line?
Code:
NSLog(@"Index: %i size: %lu", index, sizeof(imageData) / sizeof(unsigned char));
So if this NSLog is representing the imageData array wrongly, how do I log the array size correctly?
Also, I tried stepping through it with the debugger. Nothing jumps out, since these are Core Graphics opaque types. But since nothing is being created in the touches___ methods, I think the cause is probably in drawRect.
Additionally, the imageData shows up in the debugger as
imgData = (unsigned char *) 0xdc5c020
*imgData = (unsigned char) 0 '\000'

Thanks for your reply!

Last edited by architectpianist; 12-26-2011 at 08:06 PM.
architectpianist is offline   Reply With Quote
Old 12-26-2011, 11:09 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 122
architectpianist is on a distinguished road
Exclamation

By repetitive logging I found that the size of the bitmapData is in fact in the 300,000s, which is the range it should be in for a 280 x 280 image with 4 bytes per pixel. Then I added this at the end of the drawRect method, just to check:
Code:
CGContextClearRect(context, rect);
CGContextDrawImage(context, rect, currentColorWheel);
Nothing shows up on the screen when I do this! So it comes down to currentColorWheel being empty, but not NULL.
I've changed the creation of currentColorWheel from
Code:
UIGraphicsBeginImageContext(rect.size);
currentColorWheel = UIGraphicsGetImageFromCurrentImageContext().CGImage;
UIGraphicsEndImageContext();
to
Code:
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
currentColorWheel = UIGraphicsGetImageFromCurrentImageContext().CGImage;
UIGraphicsEndImageContext();
but it doesn't seem to have done anything. How can I get the color wheel to show up in the currentColorWheel CGImage?
Thanks!
architectpianist is offline   Reply With Quote
Reply

Bookmarks

Tags
bitmap, color, context, image, pixel

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
» Online Users: 405
14 members and 391 guests
bignoggins, BSH, djqbert, Duncan C, epaga, flamingliquid, jcdevelopments, leighec68, markolo, mrtdmb, omagod, pinacate, revg, taylor202
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,645
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, leighec68
Powered by vBadvanced CMPS v3.1.0

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