I wanna achive excacly what Microsoft paint does:
it is like if I load a bmp image 24bit and then I can select "save as..."
and the change the format to 8bit colors.
Microsoft Paint achieves what it does by virtue of a programmer who wrote
the application. You want to write an application. You're the programmer.
Unfortunately, I don't know how Microsoft Paint does the color conversion. I
don't believe Microsoft has published that information. So, you're still
going to have to figure it out for yourself.
I can give you a few clues. A 24-bit format is RGB (red, green, blue) color.
This means that each pixel is 3 bytes, each of which represents a value from
0 - 255, and that indicates the intensity of the color (red, green, or
blue). This means that a pixel can represent any of 16,777,216 (256*256*256)
colors.
An 8-bit format is quite a bit different. It stores color information in a
single byte, having (of course) a value from 0 - 255. This means that a
pixel can represent any of 256 colors. What those 256 colors are is
determined by the use of a palette, an color mapping table which specifies
the 24-bit color of each of the 256 possible color values. These 256 colors
can be any 256 colors you want them to be, but they have to be 256 specific
RGB color values.
To convert an image from 24-bit to 8-bit therefore requires a series of
steps. The first is to sample the 24-bit image, and use an algorithm to
change the color of each pixel to one of 256 possible color values. How this
algorithm works is entirely up to the developer. There are good ones and bad
ones. One of the easiest (but not the best) is to use the standard 256
web-safe color palette, determine the closest match in that palette for each
pixel, and convert it to that color. Note that this is *not* easy, but just
one of the easiest ways to do this. It entails using a lot of math to
determine the hue, saturation, and luminance (HSL) of each 24-bit pixel,
find the hue, saturation, and luminance of the closest match in the web
pallette, and convert the result to RGB.
The drawback to this method becomes apparent when you realize that a 24-bit
image could be all shades of (for example) blue. When sampled using this
method, it loses much of its resolution, and becomes virtually meaningless,
since there are only a few dozen shades of blue in the web-safe color
palette.
A better method is to determine an arbitrary 256 colors that represent more
closely approximations of the "average" colors in the original image. This
again, involves a lot of math, as well as understanding how RGB and HSL are
related. Using this method, all pixels in the original image must be
compared to all other pixels in the image, to result in a 256-color "average
palette" of the colors in the original image. Each pixel can then be
converted to one of these 256 colors.
These sorts of algorithms, by virtue of their difficulty to create, are
usually closely-guarded by the developers. If you can find some, you're a
very fortunate fellow. You may be able to find a code library that has these
built into it. That may be your best bet.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.