(E-Mail Removed) wrote:

> Hi,

>

> I am trying to convert a 16 bit rgb value to 32 bit, however the new

> color generated is different from the 16 bit rgb data. Please let me

> know the formula to convert an 16 bit rgb data to 32 bit rgb data.

> Thanks!!
You need to rigidly specify the actual bitwise format for both the

16-bit and the 32-bit form.

Your subject said the 16-bit rgb value was 565, so I have assumed it's

the binary value rrrrrggggggbbbbb. Splitting this into nybbles helps for

reading off the masks required in hexadecimal.

You said nothing about the 32-bit format. Common formats include BGR0,

RGB0, 0RGB, 0BGR. In the code below I have assumed 0RGB. Changing this

is easy, just modify the shift amounts in the last line.

unsigned long rgb16_to_rgb32(unsigned short a)

{

/* 1. Extract the red, green and blue values */

/* from rrrr rggg gggb bbbb */

unsigned long r = (a & 0xF800) >> 11;

unsigned long g = (a & 0x07E0) >> 5;

unsigned long b = (a & 0x001F);

/* 2. Convert them to 0-255 range:

There is more than one way. You can just shift them left:

to 00000000 rrrrr000 gggggg00 bbbbb000

r <<= 3;

g <<= 2;

b <<= 3;

But that means your image will be slightly dark and

off-colour as white 0xFFFF will convert to F8,FC,F8

So instead you can scale by multiply and divide: */

r = r * 255 / 31;

g = g * 255 / 63;

b = b * 255 / 31;

/* This ensures 31/31 converts to 255/255 */

/* 3. Construct your 32-bit format (this is 0RGB): */

return (r << 16) | (g <<

| b;

/* Or for BGR0:

return (r <<

| (g << 16) | (b << 24);

*/

}

--

Simon.