Convert a 24bit video in a 8bit video ?

I need to convert the format of the frames given by a digital camera, from 24 bits to 8 bits. So I can use them with
a Java applet. I tried the capGetVideoFormat and the capSetVideoFormat macros given by Visual C++ 5.0, but it doesn't work. How can I do?

This might not totally answer you question since I am not familiar with video-capture functions, but I can tell you the algorithm for converting 24-bit to palletized (8-bit) color. Seeing as nobody has even left a comment yet although this question is getting old, this might be as good as you can get. However there may be a function which does that, and saves you the trouble of coding the algorithm. The algorithm is rather standard, but I don't think there is any Windows API which does it (I may be wrong).

In short, if you would accept such an answer, leave a comment to that effect. It is not a simple algorithm and I would hate to submit it and then have you fail it.

0

jcelineAuthor Commented: 1998-07-20

Thank you to answer me. I think I need to code it because I didn't find such a function and I'm in hurry...

Let me state the problem more clearly: We have a 24-bit RGB picture, and we want to convert it to an 8-bit palettized picture. This means that, of the 16.7 million colors, we need to select 256 colors which are used most often, and use only those colors. Again, this is _not_ a simple process. You can get a feel for the complexity of the algorithm just by looking at the amount of time it takes a program like Photoshop to convert a single 24-bit picture to optimized 8-bit. This process has two stages:

1. Select the palette, i.e. the 256 colors which you will use. There are several ways of doing this, but a relatively simple method which gives pretty good results is as follows.
Look at the 24-bit color set as a three-dimensional space where the coordinates are R, G, B. Every pixel that is used has a certain value, represented as a point in this space. If, for example, this is a picture of the sky, the most of the points will be in the area of high B's and low R's and G's, so we want most of the palette to contain values with high B's and low R's and G's.
The way we do this is divide the 3-space into two separate sets, both of which have roughly the same number of points. We then say that the two sets shall each have 128 colors applied to them, and continue recursively into each of these sets, dividing them into four 64-color sets, and so on. Eventually we reach a very small set to which we need to assign just one color. This set might be, for example, the set R:23-25,G:10-14,B:11-12, in which case we can choose an average value of R:24,G:12,B:11 for the color which will be assigned to this set. So now we have 256 colors and we've assigned them in a fairly intelligent way, because X percent of the pixels in the picture will use up almost exactly X percent of the palette.

This is the most time-consumin part of the calculation. _IF_ this is too complicated for you, you could consider using a "fixed" palette: that is, choose the palette in advance, and go immediately to step 2. However, results will be much poorer. Most pictures with optimized palettes can be distinguished from true 24-bit color pictures only when looking closely at the details, whereas when you see a fixed-palette picture you will immediately recognize that it is lower-quality.

2. Convert all pixels to the nearest color in the palette. The "distance" between two colors can be defined pretty well as abs(r1-r2)+abs(g1-g2)+abs(b1-b2), where abs() is the absolute value function, and (r1,g1,b1) and (r2,g2,b2) are the two colors. What you do is, for a given pixel find the distance between that pixel and all colors in the palette. The color in the palette with minimum distance is the one you want for your pixel.