从 CFData 读取像素字节:指向不完整类型的指针上的算术



这段代码应该让我从CGImageRef开始获取每个像素的值:

UIImage* image = [UIImage imageNamed:@"mask.bmp"];
CGImageRef aCGImageRef = image.CGImage;
CFDataRef rawData = CGDataProviderCopyData(CGImageGetDataProvider(aCGImageRef));
UInt8 * buf = (UInt8 *) CFDataGetBytePtr(rawData);
int length = CFDataGetLength(rawData);
CFRelease(rawData);
int no_of_channels = 3;
int image_width = SCREEN_WIDTH();
unsigned long row_stride = image_width * no_of_channels; // 960 bytes in this case
unsigned long x_offset = x * no_of_channels;
/* assuming RGB byte order (as opposed to BGR) */
UInt8 r = *(rawData + row_stride * y + x_offset );
UInt8 g = *(rawData + row_stride * y + x_offset + 1);
UInt8 b = *(rawData + row_stride * y + x_offset + 2);

最后三行可以解决问题,但编译器表示它不会像float一样使用 xy 来做到这一点。所以我把它们投到int,但现在它说

指向不完整类型的指针上的算术 const 结构__CFData

我该如何解决这个问题?

您希望在字节指针本身上进行算术运算,而不是对CFData结构(将字节作为成员)进行算术。这意味着使用上面的buf变量:

UInt8 r = *(buf + row_stride * y + x_offset );
UInt8 g = *(buf + row_stride * y + x_offset + 1);
UInt8 b = *(buf + row_stride * y + x_offset + 2);

最新更新