在给定指针的情况下从位图访问图像数据

时间:2009-01-12 17:51:59

标签: ios iphone image pointers bitmap

我的问题是在有指针后正确读取像素数据。

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

我有指向数据的指针

UInt8 *data = CFDataGetBytePtr(bitmapData);

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

1 个答案:

答案 0 :(得分: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 */