How to convert from RGB555 to RGB888 in C #?

I need to convert 16-bit XRGB1555 to 24-bit RGB888. My function for this is lower, but it is not ideal, that is, the value 0b11111 will give a value of 248 as a pixel value, not 255. This function is intended for small ones, but can be easily modified for large numbers.

public static Color XRGB1555(byte b0, byte b1)
{ 
    return Color.FromArgb(0xFF, (b1 & 0x7C) << 1, ((b1 & 0x03) << 6) | ((b0 & 0xE0) >> 2), (b0 & 0x1F) << 3); 
}

Any ideas how to make it work?

+3
source share
3 answers

Usually you copy the most significant bits to the lower bits, so if you have five bits as follows:

Bit position: 4 3 2 1 0
Bit variable: A B C D E

You would expand this to eight bits like:

Bit position: 7 6 5 4 3 2 1 0
Bit variable: A B C D E A B C

Thus, all zeros remain all zeros, all of them become all, and the values ​​between the scales, respectively.

( , A, B, C .. - , ).

+7

. 32 , -.

8- 5- : return (x<<3)||(x>>2);

. , , 1/255.

+2

Start by writing a few tests:

Color c = XRGB1555(0, 0) => expect c RGB == 0,0,0
Color c = XRGB1555(0xff, 0xff) => expect c RGB == ff,ff,ff.

The rest will follow!

0
source

Source: https://habr.com/ru/post/1779601/


All Articles