How to convert 24 bit RGB to 8 bit RGB

16,049

Solution 1

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.

Solution 2

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

Share:
16,049

Related videos on Youtube

Gootik
Author by

Gootik

Software Engineer @Twitter

Updated on June 13, 2022

Comments

  • Gootik
    Gootik almost 2 years

    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.

  • Alin Purcaru
    Alin Purcaru over 12 years
    Are you sure you should discard less significant bytes instead of approximating?
  • Roman R.
    Roman R. over 12 years
    There are options out there, such as dithering too. The simplest for understanding and implementation is to discard bits and reduce detail in order to fit into fixed number of bits. Alterations on top of that are to add more noise yet to leave better subjective quality. Anyway, I'd bet that if OP would go one step back to clarify the question, an indexed color format would come up.

Related