My goal is as follows.
When The player touches the screen the program should find out the values of the pixel at that location of touch and then loop through every pixel and any that are of the exact same RGBA values will be edited, at the moment i just wish to set the alpha to 0 to make them all invisible.
So far i have managed to retireve the information find out which ones correspond to the pixel touched but when i set the pixels and create a UIImage from the new data and set it. Nothing changes.
A little help would be much appreciated.
code follows
for function
(UIImage*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy
// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
//convert phone screen coords to texture coordinates.
xx *= width/[[UIScreen mainScreen] bounds].size.width;
yy *= height/[[UIScreen mainScreen] bounds].size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
/* MY STUFF */
int counter = 0;
/* MY STUFF */
// Now your rawData contains the image data in the RGBA8888 pixel format.
// Get the byteIndex of pixel tapped.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += 4;
for(int x = 0; x < width; x++)
{
for(int y = 0; y < height; y++)
{
byteIndex = (x + (y * width)) * bytesPerPixel;
CGFloat redVal = ( rawData[byteIndex] * 1.0) / 255.0;
CGFloat greenVal = ( rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blueVal = ( rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alphaVal = ( rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += 4;
if( alphaVal != 0 )
{
if( redVal == red && greenVal == green && blueVal == blue )
{
rawData[byteIndex] = 255;
rawData[byteIndex + 1] = 255;
rawData[byteIndex + 2] = 255;
rawData[byteIndex + 3] = 1;
counter ++;
}
}
}
}
UIImage *newImage = [UIImage imageWithCGImage: imageRef];
image = newImage;
NSLog(@"Pixels amount: %i", counter);
free(rawData);
return image;
and it is called like so
tempBGRef.image = [MainMenuViewController getRGBAsFromImage:[tempBGRef image] atX:touchPointInView.x andY:touchPointInView.y];
You modify rawData object but it's not the data imageRef contains. You copied imageRef data to rawData object but imageRef won't update when you change rawData values. You should call the CGImageCreate method to create a new CGImage object from your rawData array.