1,4,8,16,24,32位颜色之间的转换

来源:百度文库 编辑:神马文学网 时间:2024/10/06 00:29:12
查看文章   1,4,8,16,24,32位颜色之间的转换2010-06-03 12:59

 

1,4,8,16,24,32位颜色之间的转换
// 灰度 .299R + .587G + .114B
#define PIXEL_GREY(r,g,b) (u8)(((u16)r*77 + (u16)g*150 + (u16)b*29) >> 8)

// 获得 R5G6B5 红色分量
INLINE u8 R565( u16 clr )
{
      return (clr &   0xF800 ) >>   8 ;
}

// 获得 R5G6B5 绿色分量
INLINE u8 G565( u16 clr )
{
       return (clr &   0x07E0 ) >>   3 ;
}

// 获得 R5G6B5 蓝色分量
INLINE u8 B565( u16 clr )
{
       return (clr &   0x001F ) <<   3 ;
}

// 获得 A1R5G5B5 红色分量
INLINE u8 R1555( u16 clr )
{
       return (clr &   0x7C00 ) >>   7 ;
}

// 获得 A1R5G5B5 绿色分量
INLINE u8 G1555( u16 clr )
{
       return (clr &   0x03E0 ) >>   2 ;
}

// 获得 A1R5G5B5 蓝色分量
INLINE u8 B1555( u16 clr )
{
       return (clr &   0x001F ) <<   3 ;
}

// 获得 A1R5G5B5 Alpha值
INLINE u8 A1555( u16 clr )
{
       return clr &   0x8000   ?   255 : 0 ;
}

// 获得 A4R4G4B4 红色分量
INLINE u8 R4444( u16 clr )
{
       return (clr &   0x0F00 ) >>   8 ;
}

// 获得 A4R4G4B4 绿色分量
INLINE u8 G4444( u16 clr )
{
       return (clr &   0x00F0 );
}

// 获得 A4R4G4B4 蓝色分量
INLINE u8 B4444( u16 clr )
{
       return (clr &   0xF000 ) <<   4 ;
}

// 获得 A4R4G4B4 Alpha值
INLINE u8 A4444( u16 clr )
{
       return (clr &   0xF000 ) >>   12 ;
}

// 由rgb组合成一个32位的RGB
INLINE u32 RGB32( u8 r, u8 g, u8 b )
{
       return u32(r) <<   16   | u32(g) <<   8   | u32(b) |   0xFF000000 ;
}

// 由argb组合成一个32位的ARGB
INLINE u32 ARGB32( u8 a, u8 r, u8 g, u8 b )
{
       return u32(a) <<   24   | u32(r) <<   16   | u32(g) <<   8   | u32(b);
}

// ------------------------------------------------------------------------------------
//   1位转换到4, 8, 16, 24, 32
// ------------------------------------------------------------------------------------
INLINE u16 _1_to_565( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 3 ) | ((pal[index].Green >> 2 ) << 5 ) | ((pal[index].Red >> 3 ) << 11 ) );
}

INLINE u16 _1_to_1555( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 3 ) | ((pal[index].Green >> 3 ) << 5 ) | ((pal[index].Red >> 3 ) << 10 ) |   0x8000 );
}

INLINE u16 _1_to_4444( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 4 ) | ((pal[index].Green >> 4 ) << 4 ) | ((pal[index].Red >> 4 ) << 8 ) |   0xF000 );
}

INLINE void _1_to_24( const PALETTE * pal, u16 index, u8 & r, u8 & g, u8 & b )
{
      b = pal[index].Blue;
      g = pal[index].Green;
      r = pal[index].Red;
}

INLINE u32 _1_to_32( const PALETTE * pal, u16 index )
{
       return ( pal[index].Blue | (pal[index].Green <<   8 ) | (pal[index].Red <<   16 ) );
}

// ------------------------------------------------------------------------------------
//   4位转换到1, 8, 16, 24, 32
// ------------------------------------------------------------------------------------
INLINE u16 _4_to_565( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 3 ) | ((pal[index].Green >> 2 ) << 5 ) | ((pal[index].Red >> 3 ) << 11 ) );
}

INLINE u16 _4_to_1555( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 3 ) | ((pal[index].Green >> 3 ) << 5 ) | ((pal[index].Red >> 3 ) << 10 ) |   0x8000 );
}

INLINE u16 _4_to_4444( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 4 ) | ((pal[index].Green >> 4 ) << 4 ) | ((pal[index].Red >> 4 ) << 8 ) |   0xF000 );
}

INLINE void _4_to_24( const PALETTE * pal, u16 index, u8 & r, u8 & g, u8 & b )
{
      b = pal[index].Blue;
      g = pal[index].Green;
      r = pal[index].Red;
}

INLINE u32 _4_to_32( const PALETTE * pal, u16 index )
{
       return ( pal[index].Blue | (pal[index].Green <<   8 ) | (pal[index].Red <<   16 ) );
}

// ------------------------------------------------------------------------------------
//   8位转换到16,24,32
// ------------------------------------------------------------------------------------
INLINE u16 _8_to_565( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 3 ) | ((pal[index].Green >> 2 ) << 5 ) | ((pal[index].Red >> 3 ) << 11 ) );
}

INLINE u16 _8_to_1555( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue) >> 3   | ((pal[index].Green >> 3 ) << 5 ) | ((pal[index].Red >> 3 ) << 10 ) |   0x8000 );
}

INLINE u16 _8_to_4444( const PALETTE * pal, u16 index )
{
       return ( (pal[index].Blue >> 4 ) | ((pal[index].Green >> 4 ) << 4 ) | ((pal[index].Red >> 4 ) << 8 ) |   0xF000 );
}

INLINE void _8_to_24( const PALETTE * pal, u16 index, u8 & r, u8 & g, u8 & b )
{
      b = pal[index].Blue;
      g = pal[index].Green;
      r = pal[index].Red;
}

INLINE u32 _8_to_32( const PALETTE * pal, u16 index )
{
       return ( pal[index].Blue | (pal[index].Green <<   8 ) | (pal[index].Red <<   16 ) |   0xFF000000 );
}

// ------------------------------------------------------------------------------------
//   16位转换到8,24,32
// ------------------------------------------------------------------------------------
INLINE u8 _565_to_8( u16 clr )
{
       return PIXEL_GREY( (((clr &   0xF800 ) >>   11 ) *   0xFF ) /   0x1F ,
           (((clr &   0x7E0 ) >>   5 ) *   0xFF ) /   0x3F ,
           ((clr &   0x1F ) *   0xFF ) /   0x1F );
}

INLINE u16 _565_to_1555( u16 clr )
{
       return ( (clr &   0xF800 ) >>   1   | (clr >>   1 ) &   0x03E0   | (clr &   0x001F ) |   0x8000 );
}

INLINE u16 _565_to_4444( u16 clr )
{
       return ( ((clr &   0xF800 ) >>   4 ) | ((clr &   0x07E0 ) >>   3 ) | ((clr &   0x001F ) >>   1 ) |   0xF000 );
}

INLINE void _565_to_24( u16 clr, u8 & r, u8 & g, u8 & b )
{
      r = (u8)((clr &   0xF800 ) >>   11 );
      g = (u8)((clr &   0x07E0 ) >>   5 );
      b = (u8)((clr &   0x001F ) <<   3 );
}

INLINE u32 _565_to_32( u16 clr )
{
       return ( ((clr &   0x001F ) <<   3 ) | ((clr &   0x07E0 ) <<   5 ) | ((clr &   0xF800 ) <<   8 ) |   0xFF000000 );
}

INLINE u8 _1555_to_8( u16 clr )
{
       return PIXEL_GREY( (((clr &   0x7C00 ) >>   10 ) *   0xFF ) /   0x1F ,
           (((clr &   0x3E0 ) >>   5 ) *   0xFF ) /   0x1F ,
           ((clr &   0x1F ) *   0xFF ) /   0x1F );
}

INLINE u16 _1555_to_565( u16 clr )
{
       return (u16)( ((clr &   0x7C00 ) << 1 ) | ((clr &   0x03E0 ) << 1 ) | (clr &   0x001F ) );
}

INLINE u16 _1555_to_4444( u16 clr )
{
       return ( ((clr &   0x7C00 ) >>   3 ) | ((clr &   0x03E0 ) >>   2 ) | ((clr &   0x001F ) >>   1 ) |   0xF000 );
}

INLINE void _1555_to_24( u16 clr, u8 & r, u8 & g, u8 & b )
{
      r = (u8)((clr &   0x7C00 ) >>   10 );
      g = (u8)((clr &   0x03E0 ) >>   5 );
      b = (u8)((clr &   0x001F ) <<   3 );
}

INLINE u32 _1555_to_32( u16 clr )
{
       return ( ((clr &   0x001F ) <<   3 ) | ((clr &   0x03E0 ) <<   6 ) | ((clr &   0x7C00 ) <<   9 ) |   0xFF000000 );
}

INLINE u16 _4444_to_565( u16 clr )
{
       return ( ((clr &   0x000F ) <<   1 ) | ((clr &   0x00F0 ) <<   3 ) | ((clr &   0x0F00 ) <<   4 ) );
}

INLINE u16 _4444_to_1555( u16 clr )
{
       return ( ((clr &   0x000F ) <<   1 ) | ((clr &   0x00F0 ) <<   2 ) | ((clr &   0x0F00 ) <<   3 ) |   0x8000 );
}

INLINE void _4444_to_24( u16 clr, u8 & r, u8 & g, u8 & b )
{
      r = (u8)((clr &   0x0F00 ) >>   8 );
      g = (u8)((clr &   0x00F0 ) >>   4 );
      b = (u8)((clr &   0x000F ) <<   4 );
}

INLINE u32 _4444_to_32( u16 clr )
{
       return ( ((clr &   0x000F ) <<   4 ) | ((clr &   0x00F0 ) <<   8 ) | ((clr &   0x0F00 ) <<   12 ) | ((clr &   0xF000 ) <<   16 ) );
}

// ------------------------------------------------------------------------------------
//   24位转换到8,16,32
// ------------------------------------------------------------------------------------
INLINE u8 _24_to_8( u8 r, u8 g, u8 b )
{
       return PIXEL_GREY( b, g, r );
}

INLINE u16 _24_to_565( u8 r, u8 g, u8 b )
{
       return ( ((u16)(r >> 3 ) << 11 ) | ((u16)(g >> 2 ) << 5 ) | ((u16)b >> 3 ) );
}

INLINE u16 _24_to_1555( u8 r, u8 g, u8 b )
{
       return ( 0x8000   | ((u16)(r >> 3 ) << 10 ) | ((u16)(g >> 3 ) << 5 ) | ((u16)b >> 3 ) );
}

INLINE u16 _24_to_4444( u8 r, u8 g, u8 b )
{
       return ( 0xF000   | ((u16)(r >> 4 ) << 8 ) | ((u16)(g >> 4 ) << 4 ) | ((u16)b >> 4 ) );
}

INLINE u32 _24_to_32( u8 r, u8 g, u8 b )
{
       return ( 0xFF000000   | ((u16)r <<   16 ) | ((u16)g <<   8 ) | (u16)b );
}

// ------------------------------------------------------------------------------------
//   32位转换到8,16,24
// ------------------------------------------------------------------------------------
INLINE u8 _32_to_8( u32 clr )
{
       return PIXEL_GREY( (clr &   0xFF ), ((clr &   0xFF00 ) >>   8 ), ((clr &   0xFF0000 ) >>   16 ) );
}

INLINE u16 _32_to_565( u32 clr )
{
       return (u16)( ((clr &   0xF8 ) >>   3 ) | ((clr &   0xFC00 ) >>   5 ) | ((clr &   0xF80000 ) >>   8 ) );
}

INLINE u16 _32_to_1555( u32 clr )
{
       return (u16)( ((clr &   0xF8 ) >>   3 ) | ((clr &   0xF800 ) >>   6 ) | ((clr &   0xF80000 ) >>   9 ) |   0x8000 );
}

INLINE u16 _32_to_4444( u32 clr )
{
       return (u16)( ((clr &   0xF0 ) >>   4 ) | ((clr &   0xF000 ) >>   8 ) | ((clr &   0xF00000 ) >>   12 ) | ((clr &   0xF0000000 ) >>   16 ) );
}

INLINE void   _32_to_24( u32 clr, u8 & r, u8 & g, u8 & b )
{
      b = (u8)(clr &   0xFF );
      g = (u8)((clr &   0xFF00 ) >>   8 );
      r = (u8)((clr &   0xFF0000 ) >>   16 );
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/check_into/archive/2007/08/17/1748630.aspx