출처 :
http://www.davechapman.f2s.com/rockbox/rgb.c코드를 그대로 가져 왔습니다.
------------------------------------------------------
#include <stdio.h>
#define swap16(x) (((x) >> 8) || ((x) << 8))
#define _RGBPACK(r, g, b) ( ((((r) * 31 + 127) / 255) << 11) \
|((((g) * 63 + 127) / 255) << 5) \
| (((b) * 31 + 127) / 255))
#define _RGB_UNPACK_RED(x) (((x) >> 8) & 0xf8) | ((x) >> 13);
#define _RGB_UNPACK_GREEN(x) (((x) >> 3) & 0xfc) | (((x) >> 9) & 0x03);
#define _RGB_UNPACK_BLUE(x) (((x) << 3) & 0xf8) | (((x) >> 2) & 0x07);
#if 1 /* RGB565 */
#define RGBPACK(r,g,b) _RGBPACK(r,g,b)
#define RGB_UNPACK_RED(x) _RGB_UNPACK_RED(x)
#define RGB_UNPACK_GREEN(x) _RGB_UNPACK_GREEN(x)
#define RGB_UNPACK_BLUE(x) _RGB_UNPACK_BLUE(x)
#else /* RGB565 Byte-swapped */
#define RGBPACK(r,g,b) swap16(_RGBPACK(r,g,b))
#define RGB_UNPACK_RED(x) _RGB_UNPACK_RED(swap16(x))
#define RGB_UNPACK_GREEN(x) _RGB_UNPACK_GREEN(swap16(x))
#define RGB_UNPACK_BLUE(x) _RGB_UNPACK_BLUE(swap16(x))
#endif
int main (int argc, char* argv[])
{
int c;
int r,g,b;
unsigned short rgb565a;
unsigned short rgb565b;
unsigned int rgb888;
for (c=0;c<256;c++) {
/* Create RGB565 */
rgb565a=RGBPACK(c,c,c);
/* Unpack RGB565 to RGB888 and then re-pack to RGB565 */
r = RGB_UNPACK_RED(rgb565a);
g = RGB_UNPACK_GREEN(rgb565a);
b = RGB_UNPACK_BLUE(rgb565a);
/* Convert back to RGB565 */
rgb565b = RGBPACK(r,g,b);
printf("%3d -> 0x%04x -> (%3d, %3d, %3d) -> 0x%04x %s\n",
c,rgb565a,r,g,b,rgb565b,((rgb565a == rgb565b) ? "OK" : "BAD"));
}
}