## Friday, June 26, 2009

### RGB, aRGB and converter

Three common display formats for RGB are: RGB888 format (8 bits per channel), RGB666 (6 bits per channel), and RGB565 (5 bits per channel for R,B, and 6 bit for G, RRRR RGGG GGGB BBBB). RGB888 has 24 bits of resolution with 16 million colors. RGB666 is usually used in portable electronics. But the 18 bits data is not good for regular 16-bit DSP. So RGB565 is popular in industry.

Convert R8 to R5 by dropping the last bits:
R8 >> 3, (255 >> 3 = 31)

From R5 to R8, you can either use scaling or dup bits:
R5*255/31, R5 << 3 | R5 >>2

aRGB is with an alpha channel (0xAARRGGBB). Alpha specifies the transparency of the pixel with 0x00 to be fully transparent and 0xff to be white background.

output = a x (foreground pixel) + (1-a) x (background pixel)

Convert RGB888 to aRGB:
r = (rgb >> 16) & 0xFF (or mask first, then shift: 0xff0000, >>16)
g = (rgb >> 8) & 0xFF
b = (rgb >> 0) & 0xFF
aRGB = 0xff000000 | (r << 16) | (g << 8) | (b)

aRGB to RGB888:
r = (aRGB >> 16) &0xFF
g = (aRGB >> 8) &0xFF
b = (aRGB >> 0) &0xFF
a = (aRGB >> 24) &0xFF
RGB888 = (r << 16) | (g << 8) | (b)

aRGB to RGB565: this question has been asked before, (should be 565 instead of 454 here). From RGB888

r >> 3
g >> 2
b >> 3
(r << 11) | (g << 6) | (b)

#### 1 comment:

1. Hi people,

This post is really useful, I exactly needed the ARGB->RGB565 convert method.

I think when converting to RGB565, one has to use the (r << 11) | (g << 5) | (b) formula (after shifting the color components, of course).

Thanks for the post,
Mate