swyitch
swyitch

Reputation:

Accessing image data from a bitmap given a pointer

My problem is reading the pixel data correctly once I have the pointer.

So I have an image that takes up the whole iPhone screen and has no alpha channel (24 bits per pixel, 8 bits per component, 960 bytes per row) and I want to find out the color of a particular pixel.

I have the pointer to the data

UInt8 *data = CFDataGetBytePtr(bitmapData);

but now I am not sure how to index into the data correctly given a coordinate?

Upvotes: 1

Views: 1691

Answers (1)

codelogic
codelogic

Reputation: 73702

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 */

Upvotes: 2

Related Questions