Home > Enterprise >  Reduce Image bit C
Reduce Image bit C

Time:12-01

How can I reduce the number of bits from 24 bits to a number between 0 and 8 bits and distribute the bits for the three colors Red, Green and Blue

Any idea ?

CodePudding user response:

Keep only the most significant bits of the pixel's red channel. Do likewise for the green and blue channel. Then use C 's bit manipulation operations to move those bits values into a single byte. There are multiple ways of doing so. For example, do an Internet search for "rgb332" for one example (where you keep 3 red bits, 3 green bits, and 2 blue bits).

CodePudding user response:

This is called "Color Quantization". You have 16.777.216 colors and you want to map them to a smaller number (2 to 256).

Step 1: choose the colors you want to use. First their number, then the colors themselves. You need to chose if the colors are fixed for all images, or if they change based on the image (you will need to ship a palette of colors with every image).

Step 2: substitute the colors of your image with those in the selection.

If the colors are fixed and you want to stay very simple you can use 1 bit per channel (8 colors in total) or 2 bits per channel (64 colors in total).

Slightly more complex, use these values for each channel 0, 51, 102, 153, 204, 255, in any possible way, leading to 216 different color combinations. Make a table which associates every color combination with an index. That index requires 8 bits (with some spare). This is called a "web safe palette" (this brings me back to the late 1999). Now you are ready for substitution: take every pixel in your image and the quantized color can be found as x*6//256*51 (// is integer division).

If you want a better looking palette, look for the Median cut algorithm.

  • Related