#### Archived

This topic is now archived and is closed to further replies.

# Convert 24Bit to 8Bit

This topic is 5585 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, it may sound stupid but I can''t figure out how to convert an 24Bit-RGB pixel to 8 Bit. Hope someone can help me, please.

##### Share on other sites
Ok here is the help:

the convertion depends on your needs.If you need grayscale data simply loop through all RGB tripples and find their average.Here is sample code:

long stride = width * 3;for(int i = 0;i < height;i++){       for(int j = 0;j < width;j++)       {            new_data[i*width+j] = rgb_data[i*width+(j*3+0)] +                                  rgb_data[i*width+(j*3+1)] +                                  rgb_data[i*width+(j*3+2)]);            new_data[i*width+j] /= 3;       }}

If you want paletted 8 bit data the stuff gets complicated.You must make a table with 256 indices where u''ll have to store the most important RGB tripplets of the RGB data.Of the rules will be to store the to store tripplets with big difference.I.E u shoudn''t store RGB(255,0,0) and RGB(254,0,0) if there is RGB(253,0,0).

"Tonight we strike,there is thunder in the sky,together we''ll fight,some of us will die,but they''ll always remember that we''ve made a stand and many will die by hand!" - ManOwaR

##### Share on other sites
NeuQuant. One of the best color quantization algorithms out there, used to create a full 8-bit palette from a 24-bit image. I''ve used it with great success. Best of all, source code included

Otherwise if you already have the palette and are looking for the closes index, imagine each RGB values as being XYZ coordinates and find the index with the smallest 3D distance from the color you have.

1. 1
Rutin
67
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633419
• Total Posts
3011787
• ### Who's Online (See full list)

There are no registered users currently online

×