A typical gray-scale image consists of 8-bits-per-pixel data (one byte for each pixel) with values which range over the entire eight bit range (0x00 through 0xFF). These values, as shown in Figure 2, are used as indices into the color table of the DIB and provide a smooth range of 256 shades of gray. Figure 3 shows a typical gray-scale image.
Contrast Enhancement
Figure 4 is a gray-scale histogram of the image. As the sample histogram shows, this particular image follows a bell-shaped curve in most of its data distribution. To enhance the contrast of this image, the data peak must be spread across a greater number of actual displayed gray-scale values. You can spread the peak by compressing the palette index mapping. Figure 9 shows the mapping done by the BuildMapping function of Listing 5. In the resulting higher contrast image, the pixel values in the range of 96 through 160 are spread out across the palette indices of 32 through 224, increasing the shading differences between image pixels. (Figure 5 is the high contrast version of the gray-scale image.) To lower the contrast of the sample image, the program maps the pixel values of most of the image to a narrower range of palette indices representing the data peak. This mapping produces the opposite contrast effect by decreasing the shading differences between image pixels (see Figure 6) .Obviously, not all image data peaks follow a bell curve. However, as a default or starting point, using the mappings described will allow some minimal image enhancements to be performed from image to image.
The main advantage of these techniques is that the processing is done on the color table of the DIB (which contains only 256 integer entries) not the bitmap (which may well contain megabytes of data).
Making an Image Darker or Lighter
Figure 7 and Figure 8 show the effects of shifting the palette indices to opposite ends of the gray scale. To lighten an image, the pixel values are mapped to the upper half of the palette indices, and to darken the image, the pixel values are mapped to the lower half of the palette indices (see Listing 5 and Figure 9) . As in the process of contrast manipulations, some palette indices are unused, resulting in the loss of some image resolution.You can also perform other operations on the bitmap color table. For example, you can invert the image shading by reversing the order of the palette indices in the color table (see Figure 12) . You could look for specific data peak groups in the histogram, or selectively leave certain pixel values out, setting them to all white or black (see Figure 10 and Figure 11) . Finally, though I have dealt solely with gray scale, you could use a color palette and highlight pixel value differences using color gradations. What can be done on the DIB color table is up to the needs and imagination of the programmer.