天天看點

RGB565 to RGB24, RGB555 & RGB24

For a image is RGB565 format, sometimes we want convert it to RGB888, we can simply extract the RGB.

The following is some piece of my codes. It's no optimized, you can optimize it. Also, you can have you own way to implement it.

#define RGB565_MASK_RED 0xF800

#define RGB565_MASK_GREEN 0x07E0

#define RGB565_MASK_BLUE 0x001F

void rgb565_2_rgb24(BYTE *rgb24, WORD rgb565)

 //extract RGB

 rgb24[2] = (rgb565 & RGB565_MASK_RED) >> 11;  

 rgb24[1] = (rgb565 & RGB565_MASK_GREEN) >> 5;

 rgb24[0] = (rgb565 & RGB565_MASK_BLUE);

 //amplify the image

 rgb24[2] <<= 3;

 rgb24[1] <<= 2;

 rgb24[0] <<= 3;

USHORT rgb_24_2_565(int r, int g, int b)

{

  return (USHORT)(((unsigned(r) << 8) & 0xF800) | 

  ((unsigned(g) << 3) & 0x7E0) |

  ((unsigned(b) >> 3)));

}

the following is conversion RGB24 with RGB555 

USHORT rgb_24_2_555(int r, int g, int b)

{

  return (USHORT)(((unsigned(r) << 7) & 0x7C00) | 

  ((unsigned(g) << 2) & 0x3E0) |

  ((unsigned(b) >> 3)));

}

COLORREF rgb_555_2_24(int rgb555)

{

  unsigned r = ((rgb555 >> 7) & 0xF8);

  unsigned g = ((rgb555 >> 2) & 0xF8);

  unsigned b = ((rgb555 << 3) & 0xF8);

  return RGB(r,g,b);

}

void rgb_555_2_bgr24(BYTE* p, int rgb555)

{

  p[0] = ((rgb555 << 3) & 0xF8);

  p[1] = ((rgb555 >> 2) & 0xF8);

  p[2] = ((rgb555 >> 7) & 0xF8);

}