views:

932

answers:

2

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 that 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 pixel at a particular XY coordinate.

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 an X,Y coordinate, any help on this would be appreciated

-jeff

+1  A: 
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 */
codelogic
hello thanks for the help, ima try it tonight when i get off work!
A: 

Thanks for your idea, but the problem is that r,g,b all return the value of 240. Any ideas why RGB values are not the same with the image?

iPhoney