convert between RGB565 and RGB888

    xiaoxiao2025-09-08  680

    #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ /* 请把RGB(565)格式数据改成RGB(888)格式,即用三个byte表示原来两个byte的数据 565: |           byte0         |            byte1               |      | x1 x2 x3 x4 x5 x6 x7 x8 | x9 x10 x11 x12 x13 x14 x15 x16 | 888: |         byte0        |          byte1          |          byte2           |      | 0 0 0 x1 x2 x3 x4 x5 | 0 0 x6 x7 x8 x9 x10 x11 | 0 0 0 x12 x13 x14 x15 x16 | */ /* void rgb565_2_rgb888(unsigned short rgb565); int main(int argc, char *argv[]) { unsigned short rgb565 = 0xC618;     rgb565_2_rgb888(rgb565); return 0; } void rgb565_2_rgb888(unsigned short rgb565) { unsigned long rgb888=0;     rgb888 = rgb565 & 0xf800;     rgb888 = rgb888 << 8;     rgb888 = rgb888 | ((rgb565 & 0x07e0) << 5);     rgb888 = rgb888 | ((rgb565 & 0x001f) << 3);          printf("%lu\n", rgb888); }*/ typedef unsigned short int   U16; typedef unsigned char        U8; typedef unsigned int         U32; typedef unsigned int          COLOR24; typedef unsigned short int    COLOR16; #define RGB565_RED      0xf800 #define RGB565_GREEN    0x07e0 #define RGB565_BLUE     0x001f #define RGB888_RED      0xff0000 #define RGB888_GREEN    0x00ff00 #define RGB888_BLUE     0x0000ff typedef union  _T_Color16 { struct { COLOR16    b:5; COLOR16    g:6; COLOR16    r:5; } RGB; COLOR16 uValue; } T_Color16; typedef union _T_Color24 { struct { U8  b; U8  g; U8  r; U8  res; } RGB; COLOR24 uValue;  } T_Color24; T_Color24 Color16_To_Color24(T_Color16  t_color16) { T_Color24 t_color24={0}; t_color24.RGB.r=t_color16.RGB.r << 3; t_color24.RGB.g=t_color16.RGB.g << 2; t_color24.RGB.b=t_color16.RGB.b << 3; return t_color24; } COLOR24  rgb565_to_rgb888(COLOR16 color16) { COLOR24  color24=color16&RGB565_RED; color24 <<= 8; color24 |= (color16&RGB565_GREEN) << 5; color24 |= (color16&RGB565_BLUE) << 3; return color24; } T_Color16 Color24_To_Color16(T_Color24  t_color24) { T_Color16 t_color16={0}; t_color16.RGB.r=t_color24.RGB.r >> 3; t_color16.RGB.g=t_color24.RGB.g >> 2; t_color16.RGB.b=t_color24.RGB.b >> 3; return t_color16; } COLOR16  rgb888_to_rgb565(COLOR24 color24) { COLOR16  color16=(color24&RGB888_RED) >> 8; color16 &= 0xf800; color16 |= ( (color24&RGB888_GREEN) & 0xfc00 ) >> 5 ; color16 |= (color24&RGB888_BLUE) >> 3; return color16; } int main(int argc,const char* argv[]) { COLOR24 c; T_Color16 t_color16; T_Color24 t_color24; t_color16.uValue=0X9EDD; printf("0x%x:color16(r=0x%02x g=0x%02x b=0x%02x)\r\n", t_color16.uValue,t_color16.RGB.r,t_color16.RGB.g,t_color16.RGB.b); t_color24=Color16_To_Color24(t_color16); printf("color24( r=0x%02x g=0x%02x b=0x%02x)\r\n", t_color24.RGB.r,t_color24.RGB.g,t_color24.RGB.b); printf("rgb565_to_rgb888(0x%x)=0x%x \r\n",t_color16.uValue, rgb565_to_rgb888(t_color16.uValue)); printf("rgb888_to_rgb565(0x%x)=0x%x \r\n",rgb565_to_rgb888(t_color16.uValue), rgb888_to_rgb565(rgb565_to_rgb888(t_color16.uValue))); system("pause"); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1302429.html
    最新回复(0)