1

I'm currently attempting to port a Java program over to iOs which utilzes BufferedImage's .setRGB method to individually manipulate images' pixels like so

image.setRGB(x, y, color);

In Objective-C I'm using UIImage in place of BufferedImage and what I'd like to know is if there is an equivalent way of doing Java's setRGB , or must I convert the image to a byte array, manipulate the pixels and then use ImageWithData?

Rob Keniger
  • 45,830
  • 6
  • 101
  • 134
Rory Harvey
  • 2,579
  • 2
  • 22
  • 27

4 Answers4

2

You have to convert the image into a bitmap, use setRGB on the bitmap, then create an image with the bitmap and draw it or assign the new image to your image views contents.

Why? The pixels are likely in an opaque format on the other side of the GPU where an app's ARM code can't access them.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

You can use following Method to access pixels and manipulating them

 -(UIImage*) applyFilterContext:(void*)context
{
CGImageRef inImage = self.CGImage;
CFDataRef m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);

int length =  CFDataGetLength(m_DataRef);

for (int i=0; i<length; i+=4)
{
    filterGreyScale(m_PixelBuf, i, context);
}


//Create Context
CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf,
                                         CGImageGetWidth(inImage),
                                         CGImageGetHeight(inImage),
                                         CGImageGetBitsPerComponent(inImage),
                                         CGImageGetBytesPerRow(inImage),
                                         CGImageGetColorSpace(inImage),
                                         CGImageGetBitmapInfo(inImage)
                                         );

CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
CGContextRelease(ctx);
UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CFRelease(m_DataRef);

return finalImage;

}

//manipulating pixels to to Greyscale values

void filterGreyScale (UInt8 *pixelBuf, UInt32 offset, void *context)
{

int r = offset;
int g = offset+1;
int b = offset+2;

int red = pixelBuf[r];
int green = pixelBuf[g];
int blue = pixelBuf[b];

uint32_t gray = 0.3 * red + 0.59 * green + 0.11 * blue;

pixelBuf[r] = gray;
pixelBuf[g] = gray;
pixelBuf[b] = gray;

}
Satish Azad
  • 2,302
  • 1
  • 16
  • 35
0

Here is an example of how to get the pixel data from an image: http://developer.apple.com/library/mac/#qa/qa1509/_index.html

Then you could change the color of the data this way:

// 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;
Michael Frederick
  • 16,664
  • 3
  • 43
  • 58
0

Here is the code I ended up using to set a pixel

-(void)setPixel:(UInt8*)buffer width:(int)width x:(int)x y:(int)y r:(int)r g:(int)g b:(int)b
{
    buffer[x*4 + y*(width*4)] = r;
    buffer[x*4 + y*(width*4)+1] = g;
    buffer[x*4 + y*(width*4)+2] = b;
}
Rory Harvey
  • 2,579
  • 2
  • 22
  • 27