Color
ShowDemoOne  Lab Report
Create an Optimized Palette for a Single Image

Purpose
The purpose of this program is to create an optimized palette of the best 236 colors for a single 24-bit color image for display on a 256-color monitor.

Materials and Equipment

Software Requirements
Windows 95, Delphi 3 (to recompile)
ShowDemoOne.EXE
Parrots.BMP (or other 24-bit color BMP file)

Hardware Requirements
Best when run on 800 x 600 pixel (or higher) monitor with 256-color display. You will see no difference in image quality if used on high color or true color displays.

Procedure

  1. Configure you display adapter to 256 color mode, if necessary.

  2. Double click on the ShowDemoOne.EXE icon to start the program.

  3. Select the Load Image icon and select a 24-bits/pixel BMP file to view. (The Parrots.BMP file is included for test purposes.)

  4. Check and uncheck the Use Optimized Palette checkbox to see the difference between an optimized palette and the Windows halftone palette.

  5. Select the Palette Tabsheet at any time to view the palette being use to display the image. Note the location of the 20 fixed Windows palette entries. (The palette is displayed in a paintbox.)

Discussion
After an image is loaded using LoadFromFile, the optimized palette is attached to the bitmap. With only a single image, Delphi takes care of the WMQueryNewPalette and WMPaletteChanged messages automatically. Run multiple copies with different images to test this.

This Delphi 3 example implements the Gervautz-Purgathofer Octree Color Quantization algorithm as described in Jeff Prosise's "Wicked Code," Microsoft Systems Journal, October 1997, pp. 79-84.  Download the C++ code, PalGenCode, and take a look at it.

Optimized palettes are not needed for high color or true color displays.

Look at the function TColorQuantizer.ProcessImage and its routine ProcessLowBitDIB for an idea about how to simulate "Scanline" in Delphi 2 using the GetDIBits API call.

Also, take a look at palette optimization in Anders Melander's TGIFImage.

To count the actual number of colors (RGB Triples) in a 24-bits/pixel image, look at the Show Image Lab Report.

See E-mail note from Andrew Rybenkov with Process32BitDIB addition to TColorQuantizer in the ColorQuantizationLibrary.  (Nov 2002)

Conclusions
In 256-color mode, a 24-bit color image displayed with an optimized palette looks much better than the common halftone palette, or the standard 20 Windows colors.

NOTE:
Marko Peric reports that ShowDemoOne does not work with
Win98 and  nVidia Riva TNT video card.  (3 May 99)


References and Links
efg's Palette Links on the Color Page
efg's Palette Information on the Delphi Graphics Algorithms Page

Keywords
Palettes, TOctreeNode class, TColorQuantizer class, Bitmap.Palette, Bitmap.LoadFromFile, ColorQuantizationLibrary unit, PaletteLibrary unit, TPaintBox, CreateHalftonePalette API call, CopyPalette API Call, Paintbox

Files
Delphi 3 Source and EXE (350 KB): ShowDemoOne.ZIP


Updated 26 Feb 2005


since 1 Nov 1998