如何确定和解释CGImage的像素格式

Mat*_*omi 7 iphone pixels quartz-graphics cgimage ios

我使用以下方法加载此(非常小)的图像:

UIImage* image = [UIImage named:@"someFile.png"];
Run Code Online (Sandbox Code Playgroud)

图像为4x1,按顺序从左到右包含红色,绿色,蓝色和白色像素.

接下来,我从底层CGImage中获取像素数据:

NSData* data = (NSData*)CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
Run Code Online (Sandbox Code Playgroud)

现在,出于某种原因,像素数据的布局取决于iOS设备.

当我在模拟器或iPhone 4上运行应用程序时,像素数据如下所示:

(255,0,0),(0,255,0),(0,0,255),(255,255,255)

因此,像素为每像素3个字节,蓝色为最高有效字节,红色为最低有效字节.所以我想你称之为BGR?

当我检查CGBitmapInfo时,我可以看到kCGBitmapByteOrderMask是kCGBitmapByteOrderDefault.我无法找到解释"默认"的地方.

另一方面,当我在我的第一代iPhone上运行时,像素数据如下所示:

(0,0,255,255),(0,255,0,255),(255,0,0,255),(255255255255)

因此每个通道4个字节,alpha作为最重要的字节,蓝色作为最不重要的字节.所以......这叫做ARGB?

我一直在查看CGBitmapInfo,了解如何检测布局的线索.在第一代iPhone上,kCGBitmapAlphaInfoMask是kCGImageAlphaNoneSkipFirst.这意味着忽略了最重要的位.这是有道理的.在第一代iPhone上,kCGBitmapByteOrderMask是kCGBitmapByteOrder32Little.我不知道这意味着什么或如何将它与R,G和B组件如何在内存中布局相关联.任何人都可以对此有所了解吗?

谢谢.

小智 7

为确保设备独立性,最好使用a CGBitmapContext为您填充数据.

这样的事情应该有效

// Get the CGImageRef
CGImageRef imageRef = [theImage CGImage];

// Find width and height
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);

// Setup color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

// Alloc data that the image data will be put into
unsigned char *rawData = malloc(height * width * 4);

// Create a CGBitmapContext to draw an image into
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

// Draw the image which will populate rawData
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);


for (NSUInteger y = 0; y < height; y++) {
    for (NSUInteger x = 0; x < width; x++) {        
        int byteIndex = (bytesPerRow * y) + x * bytesPerPixel;

        CGFloat red = rawData[byteIndex];
        CGFloat green = rawData[byteIndex + 1];
        CGFloat blue = rawData[byteIndex + 2];
        CGFloat alpha = rawData[byteIndex + 3];
    }
}

free(rawData);
Run Code Online (Sandbox Code Playgroud)

  • 这被标记为已接受的答案,但实际上并没有回答这个问题.它似乎有点像用大锤锤击钉子.为什么在所有人真正想做的事情是重新排序现有数据的字节顺序时,为什么要使用所有代码重绘整个图像? (11认同)