0

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

user1063364
  • 791
  • 6
  • 21
  • where does that `ByteBuffer` come from? – pskink Sep 03 '18 at 09:30
  • I am porting old application to android, ByteBuffer is filled in native code and later converted to opengl texture – user1063364 Sep 03 '18 at 09:39
  • i am assuming that those 2 bytes dont contain 5 lowest bits for B, 6 middle bits G and the 5 higher bits foe R? thats why i asked if you are sure that the buffer has valid data – pskink Sep 03 '18 at 09:47
  • I think yes, because following code creates bitmap with correct image : bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); buffer.position(0); bmp.copyPixelsFromBuffer(buffer); – user1063364 Sep 03 '18 at 09:49
  • no, seems still gives wrong color. – user1063364 Sep 03 '18 at 09:57
  • Yes, I updated my post. I added how I do it – user1063364 Sep 03 '18 at 09:59
  • can you somehow "fill" your ByteBuffer with half pixels white (all bits set) and half black (all bits cleared), write it to a file and then launch `hexdump -C dump_file` command (or somethig similar)? – pskink Sep 03 '18 at 10:01
  • Normally images are horizontal scan. Ie. `(y * width) + x`. Even if yours are vertical scan, it should be `(x * height) + y`. But definitely **not** `(x * width) + y`. – Paul LeBeau Sep 03 '18 at 10:04
  • Thank you very much, you are absolutely right. How could I did it. I fixed my post. But colors are still not correct. I think there must be something more. – user1063364 Sep 03 '18 at 10:11
  • @psking I can do it. It takes me some time – user1063364 Sep 03 '18 at 10:15
  • 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. – user1063364 Sep 03 '18 at 10:57

0 Answers0