As Justin pointed out, the best approach here is to get all the pixel data in one go, and then access that. Getting the data one pixel at a time as I was was very very slow.
FAST pixel sampling
-
28-06-2022 - |
Question
I need to grab the RBG data from an image as quickly as possible. I currently do this:
CGRect rect = CGRectMake(roundf(xCoord * self.scale), roundf(yCoord * self.scale), 1, 1);
CGImageRef cropped = CGImageCreateWithImageInRect(self.CGImage, rect);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char rawData[UIIMAGE_BYTES_PER_PIXEL];
NSUInteger bytesPerRow = UIIMAGE_BYTES_PER_PIXEL;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, 1, 1,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), cropped);
CGImageRelease(cropped);
CGContextRelease(context);
CGFloat red = rawData[0] / 255.0;
CGFloat green = rawData[1] / 255.0;
CGFloat blue = rawData[2] / 255.0;
CGFloat alpha = rawData[3] / 255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
But it's too slow.
Does anyone have a code snippet that does this using Open GL textures instead? I've taken a look at the GPUImage project to try to get a starting point, but having to learn all about OpenGL just for this one thing is overwhelming.
La solution
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow