I have ByteBuffer which contains pixels in format RGB_565. ByteBuffer is size 320x200*2. Each pixel is stored in two bytes.
I am getting first byte location for each pixel with (y * 320) + x.
Please how can I obtain RGB from these two bytes and convert it to android color?
I tried convert this post, but without luck
Here is how looks my code, but it gives wrong colors.
EDIT : Thank you friends, I found it. I did two mistakes. First was fixed by Paul, thanks to him I get correct index position. Then I indexed buffer.array() instead buffer.get(index). First way gives me pure byte [] with some extra bytes at beginning. This made incorrect indexing.
Here is correct function, maybe someone in the future can find it usefull
public int getPixel(int x, int y) {
int index = ((y * width) + x) * 2;
//wrong (or add arrayOffset to index)
//byte[] bytes = buffer.array();
//int rgb = bytes[index + 1] * 256 + bytes[index];
//correct
int rgb = buffer.get(index + 1) * 256 + buffer.get(index);
int r = rgb;
r &= 0xF800; // 1111 1000 0000 0000
r >>= 11; // 0001 1111
r *= (255/31.); // Convert from 31 max to 255 max
int g = rgb;
g &= 0x7E0; // 0000 0111 1110 0000
g >>= 5; // 0011 1111
g *= (255/63.); // Convert from 63 max to 255 max
int b = rgb;
b &= 0x1F; // 0000 0000 0001 1111
//g >>= 0; // 0001 1111
b *= (255/31.); // Convert from 31 max to 255 max
return Color.rgb(r, g, b);
}
many thanks