1

我的问题是一旦我有了指针就可以正确读取像素数据。

所以我有一个占据整个 iPhone 屏幕并且没有 alpha 通道的图像(每像素 24 位,每个组件 8 位,每行 960 字节),我想找出特定像素的颜色。

我有指向数据的指针

UInt8 *data = CFDataGetBytePtr(bitmapData);

但现在我不确定如何在给定坐标的情况下正确索引数据?

4

1 回答 1

2
UInt8 *data = CFDataGetBytePtr(bitmapData);

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 = *(data + row_stride * y + x_offset );
UInt8 g = *(data + row_stride * y + x_offset + 1);
UInt8 b = *(data + row_stride * y + x_offset + 2);


/* less portable but if you want to access it in as a packed UInt32, you could do */
UInt32 color = *(data + row_stride * y + x) & 0x00FFFF;  /* little endian byte ordering */
于 2009-01-12T21:59:12.763 回答