Kosmos Kosmos - 2 months ago 6
C# Question

How to read color components from ABBBBBGG GGGRRRRR two bytes format?

I have a palette that I'm trying to decode but I always fail because I can't understand how to subtract certain bit values correctly. I always end with some crazy values.

enter image description here

How can I split two bytes on needed components?

PS: I added C++ tag because the answer is not language dependent.


First, get your two bytes in, say, byte b1 and byte b0.

This would possibly be done as follows:

byte b0 = clr_bytes[0];
byte b1 = clr_bytes[1];

Of course this all depends on the endianness of your palette; it may be that you need to try the opposite:

byte b0 = clr_bytes[1];
byte b1 = clr_bytes[0];

Then your components can be computed as follows:

byte a = b1 >> 7;
byte b = (b1 >> 2) & 0x1f;
byte g = ((b1 & 0x3) << 3) | (b0 >> 5);
byte r = b0 & 0x1f;