Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert 24 bit RGB to 8 bit RGB

I was wondering what is the best way to convert a 24-bit RGB color (8 bits per color) into an 8-bit color (2bits Blue, 3 Green, 3 Red). I'd like C code for doing this.

like image 474
Gootik Avatar asked Oct 05 '11 06:10

Gootik


2 Answers

8 bit RGB is normally an indexed (palettized) color format, see Palette (computing).

The way you described it though, getting 8 bpp out of 24 bpp is pretty straightforward - you would need to strip least significant bits and merge the bits into single byte value, per pixel. AFAIK it is not a standard or well-known pixel color representation.

like image 185
Roman R. Avatar answered Oct 01 '22 20:10

Roman R.


Not in C but in javascript.

encodedData = (Math.floor((red / 32)) << 5) + (Math.floor((green / 32)) << 2) + Math.floor((blue / 64));

https://stackoverflow.com/a/25258278/2130509

like image 27
Jai Avatar answered Oct 01 '22 19:10

Jai