OiO.lk Blog C++ Proper RGB888 to RGB565 conversion
C++

Proper RGB888 to RGB565 conversion


I’ve found three RGB888 to RGB565 conversion algorithms, but I’m not sure which is the most correct. The first two produce the same results. The third produces a different result, but does agree with an online calculator. And the third one makes sense, since it does scaling. But the first two are going to be faster since there’s no integer multiplication or division.

uint8_t r8, g8, b8;
uint16_t r5, g6, b5, val;

r8 = someredvalue;
g8 = somegreenvalue;
b8 = somebluevalue;


// first method:
b5 =  (b8  >> 3)        & 0x001F;
g6 = ((g8  >> 2) <<  5) & 0x07E0;
r5 = ((r8  >> 3) << 11) & 0xF800;
val = (r5 | g6 | b5);
printf("%d, %d, %d, %u\n", r8, g8, b8, val);


// second method:
val = ((r8 & 0b11111000) << 8) | ((g8 & 0b11111100) << 3) | (b8 >> 3);
printf("%d, %d, %d, %u\n", r8, g8, b8, val);


// third method:
r5 = r8 * 31 / 255;
g6 = g8 * 63 / 255;
b5 = b8 * 31 / 255;     
val =  (r5 << 11) | (g6 << 5) | (b5);
printf("%d, %d, %d, %u\n", r8, g8, b8, val);



You need to sign in to view this answers

Exit mobile version