数组我想做到以下几点:转换的24位PNG图像GLubytes
- 从24位PNG图像读取RGB颜色值
- 平均RGB值,并将其储存成一列Glubytes。
我提供了我希望执行这两个步骤的功能。 我的函数返回一个Glubytes数组,但是所有元素的值都是0. 所以即时猜测即时读取图像数据不正确。
我在阅读图片时出错了什么? (也许我的格式不正确)。
这里是我的功能:
+ (GLubyte *) LoadPhotoAveragedIndexPNG:(UIImage *)image numPixelComponents: (int)numComponents
{
// Load an image and return byte array.
CGImageRef textureImage = image.CGImage;
if (textureImage == nil)
{
NSLog(@"LoadPhotoIndexPNG: Failed to load texture image");
return nil;
}
NSInteger texWidth = CGImageGetWidth(textureImage);
NSInteger texHeight = CGImageGetHeight(textureImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
GLubyte *indexedData = (GLubyte *)malloc(texWidth * texHeight);
GLubyte *rawData = (GLubyte *)malloc(texWidth * texHeight * numComponents);
CGContextRef textureContext = CGBitmapContextCreate(
rawData,
texWidth,
texHeight,
8,
texWidth * numComponents,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(textureContext,
CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight),
textureImage);
CGContextRelease(textureContext);
int rawDataLength = texWidth * texHeight * numComponents;
for (int i = 0, j = 0; i < rawDataLength; i += numComponents)
{
GLubyte b = rawData[i];
GLubyte g = rawData[i + 1];
GLubyte r = rawData[i + 2];
indexedData[j++] = (r + g + b)/3;
}
return indexedData;
}
下面是测试图像IM加载(PNG格式RGB色彩空间):
希望这个链接帮助http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage- cocoa-touch-or-cgimage-core-graphics – Ram 2013-04-29 09:52:24