locked
16bpp Color Format Help RRS feed

  • Question

  • Hey guys! So I am working on some video drivers and working with pixel formats. I understand 16bpp pixel format is what follows here: http://en.wikipedia.org/wiki/High_color

    And 0xf800 would be a solid red. How do I convert a RGB color value that are bytes like this FF0000 to a 16bpp color as a short and convert it back to RGB with three bytes again?

    Thanks, Matt 

    Saturday, December 21, 2013 9:57 AM

Answers

  • If you have to use bit operations in your driver, then try these samples:

    {

           uint sampleRGB = 0xFF0000;

           uint R = ( sampleRGB >> 16 ) & 0xFF;

           uint G = ( sampleRGB >> 8 ) & 0xFF;

           uint B = sampleRGB & 0xFF;

           uint r = R * 31 / 255;

           uint g = G * 63 / 255;

           uint b = B * 31 / 255;

           ushort converted16bpp = (ushort)( r << ( 5 + 6 ) | ( g << 5 ) | b );

    }

    {

           ushort sample16bpp = 0xF800;

           uint r = (uint)( ( sample16bpp >> ( 5 + 6 ) ) & 31 );

           uint g = (uint)( ( sample16bpp >> 5 ) & 63 );

           uint b = (uint)( sample16bpp & 31 );

           uint R = r * 255 / 31;

           uint G = g * 255 / 63;

           uint B = b * 255 / 31;

           uint convertedRGB = ( R << 16 ) | ( G << 8 ) | B;

    }


    For better conversions, it probably has sense to use floating point operations and rounding.


    • Edited by Viorel_MVP Saturday, December 21, 2013 8:53 PM
    • Marked as answer by civilwarrock Monday, December 23, 2013 2:22 AM
    Saturday, December 21, 2013 8:47 PM

All replies

  • Use the FormatConvertedBitmap class for PixelFormat conversion.
    Saturday, December 21, 2013 11:45 AM
  • If you have to use bit operations in your driver, then try these samples:

    {

           uint sampleRGB = 0xFF0000;

           uint R = ( sampleRGB >> 16 ) & 0xFF;

           uint G = ( sampleRGB >> 8 ) & 0xFF;

           uint B = sampleRGB & 0xFF;

           uint r = R * 31 / 255;

           uint g = G * 63 / 255;

           uint b = B * 31 / 255;

           ushort converted16bpp = (ushort)( r << ( 5 + 6 ) | ( g << 5 ) | b );

    }

    {

           ushort sample16bpp = 0xF800;

           uint r = (uint)( ( sample16bpp >> ( 5 + 6 ) ) & 31 );

           uint g = (uint)( ( sample16bpp >> 5 ) & 63 );

           uint b = (uint)( sample16bpp & 31 );

           uint R = r * 255 / 31;

           uint G = g * 255 / 63;

           uint B = b * 255 / 31;

           uint convertedRGB = ( R << 16 ) | ( G << 8 ) | B;

    }


    For better conversions, it probably has sense to use floating point operations and rounding.


    • Edited by Viorel_MVP Saturday, December 21, 2013 8:53 PM
    • Marked as answer by civilwarrock Monday, December 23, 2013 2:22 AM
    Saturday, December 21, 2013 8:47 PM
  • Thank you so much! Worked like a charm.
    Monday, December 23, 2013 2:22 AM