tags:

views:

1137

answers:

4

I want to process my image point by point, but I don't know how to obtain the color of the point from the image. And how to move the pointer point by point?

+3  A: 

You can't do it directly from the UIImage, but you can render the image into a bitmap context, with a memory buffer you supply, then test the memory directly. That sounds more complex than it really is, but may still be more complex than you wanted to hear.

If you have Erica Sadun's iPhone Developer's Cookbook there's good coverage of it from page 54. I'd recommend the book overall, so worth getting that if you don't have it.

I arrived at almost exactly the same code independently, but hit one bug that it looks like may be in Sadun's code too. In the pointInside method the point and size values are floats and are multiplied together as floats before being cast to an int. This is fine if your coordinates are discreet values, but in my case I was supplying sub-pixel values, so the formula broke down. The fix is easy once you've identified the problem, of course - just cast each coordinate to an int before multiplying - so, in Sadun's case it would be:


long startByte = ((int)point.y * (int)size.width) + (int)point.x) * 4;

Also, Sadun's code, as well as my own, are only interested in alpha values, so we use 8 bit pixels that take the alpha value only. Changing the CGBitMapContextCreate call should allow you to get actual colour values too (obviously if you have more than 8 bits per pixel you will have to multiply that in to your pointInside formula too).

[edit] This prompted me to put the code up on my blog (with a side-track while I worked out how to syntax highlight it):

Phil Nash
This is more-or less how I solved the same problem for my application.
Mark Bessey
A: 
iPhoney
iphony, did you use the <pre><code> ... </code></pre> tags around your code in that posting? Looks like the formatting has got a bit screwed up and makes it tricky to follow. I suspect that you're missing a } or something, somewhere, but difficult to tell as the code is incomplete
Phil Nash
Thanks, and I have made another edit for the code.
iPhoney
A: 

I've heard that image process can also be carried out using NSImage. But currently I'm developing the application on iPhone and it doesn't has NSImage class.

Still, I'll be appreciated to see code using NSImage if it won't cause too much trouble.

iPhoney
I just looked at the code I used to do this and indeed I remember running into troubles with NSImage and I had to recode to use the CGContext and CGColorSpace as well as the CTGradientElement classes.... maybe investigate these. Deleted old, incorrect post.
adam
A: 
iPhoney