Victor Image Processing Library Application Notes

Color Reduction

What is Color Reduction?

Color reduction is the process of reducing the number of colors present in an image to improve display or file handling. Generally the starting image is a 24-bit RGB image that can have as many as 16 million colors. The resulting image is an 8-bit palette color image with 256 or fewer colors.

Why use Color Reduction?

The main reason to use color reduction is to cut down the image storage requirements. Converting an image from 24-bit to 8-bit reduces the space required to store the image to one-third of the original.

In a Windows application the use of color reduction for image display avoids the painfully slow and poorly rendered display of 24-bit RGB images on display adapters limited to 256 (or fewer) colors.

Color Reduction Options

When reducing the number of colors in the image you have control over the type of palette used for the resulting image . . .

standard: a 16-color palette containing the standard Windows colors.

rainbow: a 256-color rainbow palette suitable for most all images.

optimum: the color reduction function calculates the best palette containing between 2 and 256 colors to use to represent the entire image. You specify the number of colors to exist in the resulting image.

specific: the programmer specifies the palette which may contain 2 to 256 colors.

grayscale: 256 shades of gray.

You also have control over the pixel pattern used to disperse the color . . .

You can apply color reduction to 24-bit RGB images or to 8-bit palette color images.

So What's Really Important?

The choice of color reduction function and options depends upon the requirements for speed of execution, image quality, and ability to display multiple images at the same time.
Hints
For fastest speed use a standard palette.
For highest image quality use diffusion scatter.
To display multiple images simultaneously use the same palette for all images.

Color Reduction with Victor

The color reduction functions in the Victor Library give you complete control over the palette and pixel pattern of the final image. Table 1 summarizes the Victor color reduction functions and the palette and pixel pattern options available for each.

Table 1. Victor Color Reduction Functions

Color Reduction   -- Starting --  -------------- Resulting Image ----------------
Function          bits per pixel  Palette     Number of Colors  Pixel Pattern
---------------   --------------  ---------   ----------------  -----------------
colordither       8- or 24-bit    standard           16         dither
colordither       8- or 24-bit    rainbow           256         dither
colorscatter      8- or 24-bit    standard           16         diffusion scatter
colorscatter      8- or 24-bit    rainbow           256         diffusion scatter
colortogray       8- or 24-bit    grayscale         256         none
convertrgbtopal   24-bit          optimum        2 to 256       none
convertrgbtopalex 24-bit          optimum        2 to 256       none or diffusion scatter
matchcolorimage   8- or 24-bit    specific       2 to 256       diffusion scatter
matchcolorimageex 8- or 24-bit    specific       2 to 256       none or diffusion scatter
reduceimagecolors 8-bit           optimum        2 to 255       diffusion scatter
----------------------------------------------------------------------------------
Victor also has automatic color reduction built into the viewimageex function (not available in Dos version). So you can display a 24-bit RGB image without going to the trouble of making an 8-bit version of it.

Color Reduction Sample Code - Create Optimized Palette

To convert an RGB image into a palette color image with optimized palette and save it as a PCX file, use convertrgbtopal as in the following sequence:

   // Allocate an 8-bit image 
   allocimage(&image8, (int)rgbimage.bmh->biWidth,
       (int)rgbimage.bmh->biHeight, 8);

    // Convert RGB to 8-bit image with 256 colors
   convertrgbtopal(256, &rgbimage, &image8); 
   savepcx("ANY.PCX", &image8); // Save the image as a PCX file
   freeimage(&image8);

Where rgbimage is a 24-bit RGB color image and image8 is an 8-bit palette color image.

Color Reduction Sample Code - Specify an Existing Palette

To convert an RGB image into a palette color image with an existing palette and save it as a PCX file, use matchcolorimage as in the following sequence:
   // Allocate an 8-bit image 
   allocimage(&image8, (int)rgbimage.bmh->biWidth,
    (int)rgbimage.bmh->biHeight, 8);

   // Copy an existing palette to the new image
   copyimagepalette(&anotherimage, &image8);
   // Convert RGB to 8-bit image
   matchcolorimage(&rgbimage, &image8); 
   savepcx("NEW.PCX", &image8); // Save the image as PCX file
   freeimage(&image8);

Where rgbimage is a 24-bit RGB color image, anotherimage is an existing image with the palette of interest, and image8 is an 8-bit palette color image.

Color Reduction Sample Code - Displaying a 24-bit image with a Rainbow Palette and Scatter Pattern

A Victor image can be displayed easily with the viewimageex function which automatically uses color reduction to display a 24-bit image in a display mode limited to 256 or fewer colors. The rainbow palette and scatter pattern are selected by the defined constant VIEWSCATTER.

   HDC hDC;
   PAINTSTRUCT ps;
   HPALETTE hpal;

   hDC = BeginPaint(hWnd, &ps);
   // Display the image beginning at (0,0) in the image 
   //  and place it at (0,0) in the window 
   viewimageex(hWnd, hDC, &hpal, 0, 0, &rgbimage, 0, 0, VIEWSCATTER);      
   EndPaint(hWnd, &ps);

The viewimageex function also lets you specify the starting position for the image and the scroll positions.




Victor Image Processing Library

Victor Image Processing Library homepage

Copyright © 1995 - 2002 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.