數組我想做到以下幾點:轉換的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