![]() |
NSImage and getting access into each pixel
Hi,
I'm writing an app that's operating on images (only black and white). Image is in .jpg format. First of all image is NSImage object. Then it's passed to method that should operate on pixels of that image. And here I've a problem. I haven't found a good example on the internet to understand what to do. So i made it myself but... it's not working. I have this method which is receiving an object: Code:
- (NSImage *)skeletonization: (NSImage *)imageCode:
- (IBAction)button1: (id)senderProgram received signal: “EXC_BAD_ACCESS”. sharedlibrary apply-load-rules all kill quit Does anybody know where is problem? |
Try this.
1) producedImage... you have to init it also. 2) you don't need "workingImage" 3) be careful of memory management. Any time you "alloc" something you are responsible for releasing it. Code:
- (NSImage *)skeletonization: (NSImage *)image |
Thank you. init and memory menagement was the problem.
I have one more question. How to get access to rows and columns? Do I have to make an array or there is a specific method (like: unsigned char *pixelData = [imageRep bitmapData])? Maybe I've misted something in documentation... |
If you want to access the individual pixel data, you do that with your bitmapImageRep. NSBitmapImageRep has this method... getPixel:atX:y:. So it's already in rows/columns for you. There's also colorAtX:y: to get the color values.
|
I have read about getPixel:atX:y and setColor:atX:y: but i can't figure it out how this methods work. I mean first i have to get pixel?and then setColor of this pixel? I feel like running around without a reason :/
To get to each pixel of my image i'm using 2 loops: Code:
NSColor *myColor = [[NSColor alloc] init];*** -colorSpaceName not defined for the NSColor <NSColor: 0x1001559e0>; need to first convert colorspace. |
Have you read this:
http://developer.apple.com/mac/libra...DrawColor.html |
No i have not. Man this framework is huge :/ When you think that you have read all documents there's always something more. But thanks ;)
|
A color is meaningless without a model to describe how the color's values should be rendered. That's what a color space is. An analogue is a string being only half of the equation: you need to know more information about the string (its encoding) in order to interpret it correctly.
You're alloc/init'ing an NSColor object directly, and that's generally not something you want to do. Use the methods in the NSColor class documentation to create color objects instead so their attributes (e.g., color space) are given significant values. edit: And this doesn't make sense on two levels: Code:
[myColor set:blueColor];2. Assuming it worked at all, if you have a blue color object already, why not just reuse it? |
One More Thing™
Don't forget about the cocoa-dev mailing list, which tackles these kinds of questions all the time. (Read the docs, then search the list, then post if you haven't found the answer.) |
Hi again. I have a another question. Well I've finally made a program that changes pixel. And it wasn't so hard to write ;) But NSImage/NSBitmapImageRep is a bit slow. It takes at least 2 sec to change all pixels in picture (which, actually are not so big). And it's only a part of my algorithm. So I changed NSBitmapImageRep to CGImageRef. And it's working. But i still need to get into each pixel (skeletonization algorithm) ;) I read on the dev apple site that I need to create bitmap context. And here is my question before I start digging into CGImage - what is better ? What is fast enough? Is it better stay with CGImage or start somewhere else?
|
I reiterate what Mikey-San said above - the cocoa-dev mailing list is a better venue for such questions. And I think you will find that similar questions to yours have been discusses already - see the list archives.
|
Yes i searched archives of this mailing list but without result. So i posted here. Now i only need to subscribe this mailing list...
|
I think you need to broaden your search terms. E.g. look for discussions of speeding up graphics drawing.
And I assume that you know about (and are using) Instruments and Shark. |
I know about this. But now I'm not using.
|
Quote:
|
Still one thing is on my mind in method getPixel:atX:y:. What should I pass as (NSUInteger [])p? I want to get some informations about picture and x,y coordinates are clear. But not (NSUInteger [])p. ;)
|
It would seem from that declaration that you are supposed to pass it the address of an NSUInteger array (of an appropriate size) and it will fill in that array.
|
Ok I changed my program, and now using CoreGraphic and graphics context. Doing as documentation says. I have a method:
Code:
- (NSImage *)skeletonization: (NSImage *)imageCode:
CGContextRef MyCreateBitmapContext (int pixelsWide, int pixelsHigh) |
I'm not at all familiar with CG but looking at the data flow in your "skeletonization" method, I don't see anywhere that you are copying the image data.
|
Could you say why I need to do that? Because I thought all I need is to create image from JPG or bitmap context (which I did by CGImageRef myImage = CGBitmapContextCreateImage(myBitmapContext); ).
|
Just follow the trail of data in your variables:
image -> bitmapImageRep -> width, height -> myBitmapContext -> myImage -> bitmapRep -> producedImage You see that the only thing you take from the original image is its width & height. No where do you refer to the data in the image. Clearly you need to do a lot more reading. Maybe google for more examples, or download open source programs that might be doing similar things, etc. |
| All times are GMT -5. The time now is 06:23 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.