Board index » delphi » Changing colors in 8-bit bitmaps when in high color mode

Changing colors in 8-bit bitmaps when in high color mode

I am using D4 to draw large GIS map images (some as big as 4000 x 4000 pixels)  on a high color device by converting the images to
bitmaps that I feed to a timage component.  To reduce memory consumption I have been using only 8-bit bitmaps. I create the bitmap
from raw data by using the bitmap's scanline property and a file map to the file containing the raw data.  The data are scaled to
byte values and then written to the bitmap's data.  If I change the bitmap's palette the colors change on screen,  but
unfortunately, the bitmap's data values change as well.   The only way I can get the bitmap to display the new palette properly is
to recreate the bitmap.  This process can be quite slow.

Is there a way to maintain the palette indices of the bitmap and just change the colors that the palette indices refer to?

Any advice or suggestions regarding the process would be much appreciated.

F. Hovey
fredh@earthling_DOT_net (replace _DOT_ with period to reply)


Re:Changing colors in 8-bit bitmaps when in high color mode

After searching through the news archives I found the solution to my question.

Contrary to several posts by others you can accomplish changing 8-bit color bitmaps when in high color mode without recreating the
bitmap or even changing the palette property of the bitmap.  In fact doing the latter is a bad idea because both D3 and D4 change
the bitmap's data when the palette is changed.  This messes up subsequent palette changes.
Also, some others have posted code for color cycling that converts 8-bit bitmaps to 24-bit ones thereby eliminating the color
changing problem, but also quadrupaling the memory consumption.  This is quite an inefficient solution, and not suitable at all for
very large bitmaps.

It turns out that the answer is very simple.

Using the following code snippet as a base you can cycle or animinate the colors of 8-bit bitmaps for display in high color graphics
mode very easily and quickly.

  ColorTable        : array[Byte] of TRGBQuad;
    for i := iStart to PaletteColorCount-1 do
      with ColorTable[i] do
        rgbBlue := Blue;
        rgbGreen := Green;
        rgbRed := Red;
        rgbReserved := 0;
    Bitmap.HandleType := bmDIB;
    SetDIBColorTable(Bitmap.Canvas.Handle, iStart, PaletteColorCount, ColorTable);

Note that the palette property of the bitmap is NOT touched in anyway; but the colors displayed will be.

Thanks to an old post by Robert Rossmair ( Re: Rendering Greyscale Graphics    97/11/07 [yy/mm/dd]) for a similar snippet and others by Chris Hill.

[keywords: palette animation, color changing cycling, pf8bit, 8-bit bitmaps, 256 ]
F. Hovey
fredh@earthling_DOT_net (replace _DOT_ with period to reply)

Other Threads