tags:

views:

282

answers:

1

Hello All,

i am able to read a particular pixel at given CGPoint but i have been looking to change the color of a pixel and it would be highly appreciated if anyone can help me out with a code snippet.

My Code is:

unsigned char* data = CGBitmapContextGetData (cgctx);       

if (data != NULL) {    
  offset = 4*((w*round(point.y))+round(point.x));    
  alpha =  data[offset];     
  red = data[offset+1];          
  green = data[offset+2];        
  blue = data[offset+3];     
  color = [UIColor colorWithRed:(red/255.0f) 
                   green:(green/255.0f) blue:(blue/255.0f) 
                   alpha:(alpha/255.0f)];       
}
+1  A: 

It's not clear what you are trying to do. If you want to change the pixel's color in the original CGImageRef then you would use something like:


// Set the color of the pixel to 50% grey + 50% alpha
data[offset+0] = 128;
data[offset+1] = 128;
data[offset+2] = 128;
data[offset+3] = 128;

// Create a CGBitmapImageContext 
CGContextRef bitmapContext = CGBitmapContextCreate(data, width, height, CGImageGetBitsPerComponent(), width * 4, CGImageGetColorSpace(), kCGImageAlphPremultipliedFirst);

// Draw the bitmap context back to your original context
CGContextDrawImage(bitmapContext, CGMakeRect(...), cgctx); 

You should make all of your changes to the data* at once and then write the modified bitmap buffer back to the original context.

Mark Thalman
thanx mark,Actually i am trying to implement Flood Fill but i am not able to do that, when i try to match the pixel with its near by pixel value it gives me false.the scenario is i have a Background image and there are some shapes on it user can select the color from the palette and the with single click i am trying to flood fill the object.I desperately need help because i am not able to fill the color.
xavoDev
This goes beyond the scope of a SO question. You probably need to pick up a book on image processing. If you should look at CoreImage to see if you can use that. There is probably an easier way of doing this in CoreImage.
Mark Thalman
But Mark CoreImage is not for iPhone.....check this thread.http://discussions.apple.com/thread.jspa?threadID=1552538
xavoDev
Sorry, I was thinking MacOS, not iPhone. On the iPhone I tend to use OpenGL.
Mark Thalman
Mark can you give me an example of OpenGL flood fill in a jpg Image?? please help me i am really in a trouble.
xavoDev