views:

653

answers:

3

I have a 16 bit luminance value stored in two bytes, and i want to convert that to R, G, and B values. I have two questions: how do i convert those two bytes to a short, and assuming that the hue and saturation is 0, how do i turn that short into 8 bits per component RGB values?

(apologies if this sounds like a dumb question, i just can't figure it out. The Convert class doesn't have an option to take two bytes and output a short.)

+2  A: 

Try something like this:

byte luminance_upper = 23;
byte luminance_lower = 57;
int luminance = ((int)luminance_upper << 8) | (int)luminance_lower;

That will give you a value between 0-65535.

Of course, if you just want to end up with a 32-bit ARGB (greyscale) colour from that, the lower byte isnt going to matter because you're going from a 16-bit luminance to 8-bit R,G,B components.

byte luminance_upper = 255;
// assuming ARGB format
uint argb = (255u << 24)                     // alpha = 255
            | ((uint)luminance_upper << 16)  // red
            | ((uint)luminance_upper << 8)   // green
            | (uint)luminance_upper;         // blue (hope my endianess is correct)

Depending on your situation, there may be a better way of going about it. For example, the above code does a linear mapping, however you may get better-looking results by using a logarithmic curve to map the 16-bit values to 8-bit RGB components.

geofftnz
By "32-bit greyscale number" he means "alpha, R, G, B where each is 8 bits and R=G=B."
Crashworks
yep, hope it's a bit clearer now.
geofftnz
A: 

You will need the other components too (hue and saturation).

Also, 16-bit sounds a bit weird. Normally those values are floating point.

There is a very good article on CodeProject for conversions of many color spaces.

leppie
+2  A: 

If the 16 bit value is little endian and unsigned, the 2nd byte would be the one you want to repeat 3x to create the RGB value, and you'd drop the other byte. Or if want the RGB in a 32 bit integer, you could either use bit shifts and adds or just multiply that 2nd byte by 0x10101.

David
That's exactly what i need thanks!
RCIX