Logbook noting:
How to get pixel data
from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?
+ (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)x andY:(int)y count:(int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
// First get the image into
your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
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);
// Now your rawData
contains the image data in the RGBA8888 pixel format.
NSUInteger byteIndex = (bytesPerRow *
y) + x * bytesPerPixel;
for (int i = 0 ; i < count ; ++i)
{
CGFloat alpha = ((CGFloat)
rawData[byteIndex + 3] ) / 255.0f;
CGFloat red = ((CGFloat)
rawData[byteIndex] ) / alpha;
CGFloat green = ((CGFloat)
rawData[byteIndex + 1] ) / alpha;
CGFloat blue = ((CGFloat)
rawData[byteIndex + 2] ) / alpha;
byteIndex += bytesPerPixel;
UIColor *acolor = [UIColor colorWithRed:red
green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
free(rawData);
return result;
}
What is CGImageRef
CGImageRef CGImageCreate ( size_t width, size_t height, size_t bitsPerComponent, size_t bitsPerPixel,size_t bytesPerRow, CGColorSpaceRef space, CGBitmapInfo bitmapInfo, CGDataProviderRef provider, constCGFloat *decode, bool shouldInterpolate, CGColorRenderingIntent intent );
How to use
CGContextDrawImage?
I need some help using the CGContextDrawImage. I have the
following code which will create a Bitmap context and convert the pixel data to
CGImageRef. Now I need to display that image using CGContextDrawImage. I'm not
very clear on how I'm supposed to use that. The following is my code:
-
(void)drawBufferWidth:(int)width height:(int)height pixels:(unsigned
char*)pixels
{
const int area = width
*height;
const int
componentsPerPixel = 4;
unsigned char
pixelData[area * componentsPerPixel];
for(int i = 0;
i<area; i++)
{
const int offset = i * componentsPerPixel;
pixelData[offset] = pixels[0];
pixelData[offset+1] = pixels[1];
pixelData[offset+2] = pixels[2];
pixelData[offset+3] = pixels[3];
}
const size_t
BitsPerComponent = 8;
const size_t
BytesPerRow=((BitsPerComponent * width) / 8) * componentsPerPixel;
CGColorSpaceRef
colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef gtx =
CGBitmapContextCreate(pixelData, width, height, BitsPerComponent, BytesPerRow,
colorSpace, kCGImageAlphaPremultipliedLast);
CGImageRef myimage =
CGBitmapContextCreateImage(gtx);
//What code should go
here to display the image?
CGContextRelease(gtx);
CGImageRelease(myimage);
}
Comments:
These two blocks of codes are all from http://stackoverflow.com/. If you are trying to get RGB value of a picture, the
second block of codes is very helpful.
The basic idea is to set an array which contains 4
numbers representing Red, Green, Blue and Alpha. And then display a huge matrix containing
thousands of arrays.
No comments:
Post a Comment