Solved

Bitmap Palette

Posted on 1997-05-08
2
1,551 Views
Last Modified: 2008-02-26
The following code (Delphi2) shows a bitmap graphic in a TImage.
The bitmap is generated at runtime inside a buffer.
My problem is that the generated palette is not used by the
bitmap although I assigned it...
Instead the bitmap uses the system color palette.
What's wrong?

var i:byte;
    PalSize:longint;
    pLogPalle:PLogPalette;
    PalleEntry:TPaletteEntry;
    Palle:HPalette;
    buffer:array[1..1024] of byte;

begin
     Image1.Picture.Bitmap.Width:=32;
     Image1.Picture.Bitmap.Height:=32;

     PalSize:=sizeof(TLogPalette) + 256 * sizeof(TPaletteEntry);
     pLogPalle:=MemAlloc(PalSize);
     pLogPalle^.palVersion:=$0300;
     pLogPalle^.palNumEntries:=256;
     {$R-}
     for i:=0 to 255 do
     begin
     pLogPalle^.palPalEntry[i].peRed:=i;
     pLogPalle^.palPalEntry[i].peGreen:=0;
     pLogPalle^.palPalEntry[i].peBlue:=0;
     pLogPalle^.palPalEntry[i].peFlags:=PC_EXPLICIT;
     end;
     {$R+}
     Palle:=CreatePalette(pLogPalle^);
     FreeMem(pLogPalle,PalSize);
     Image1.Picture.Bitmap.Palette:=Palle;

     {Fill the buffer...}

     SetBitmapBits(Image1.Picture.Bitmap.Handle, sizeof(buffer), addr(buffer));
     Image1.Refresh;
end;
0
Comment
Question by:tier
2 Comments
 
LVL 2

Expert Comment

by:gysbert1
Comment Utility
Are you sure it is using the system palette ?

Remember that windows reserves 20 palette entries for system colors over which you have no control. You really only have 235 palette entries to work with and if you are in 256 color mode windows makes a compromise, colorwise, as to which open window can have how many active palette entries.

You can mark some entries as being important and windows will "TRY" to use them but even that is not garuanteed.

In most cases, for this reason, it is not such a good idea to use palettes directly in windows. There are of course certain apps where it is definately necesarry but palette changing is a completely different story than it in DOS.

Try running your app in 24bit mode. If it uses the system palette then there is something wring in your code and I will have a closer look at it. If it runs ok in 24bit mode you did everything perfectly right but windows didn't feel like using your palette.

It is obvious that each window can have a palette that utilises 256 different colors in 24bit mode because the palette is only used to calc the specific RGB values ...
0
 
LVL 2

Accepted Solution

by:
javiertb earned 25 total points
Comment Utility
The following code shows how to set a bitmap's palette bitmap. The bitmap is within an image box called Image1.

procedure Form1.MakePalette;
var
  LogPal: TLogPalette;
  hPal: HPalette;
  PalEntry: array[0..255] of TPaletteEntry;
  i: Integer;
begin
  for i := 0 to 235 do
  begin
    PalEntry[i].peRed := i;   // here you set your own values        PalEntry[i].peGreen := i;
    PalEntry[i].peBlue := i;
    PalEntry[i].peFlags := PC_NOCOLLAPSE;
  end;
  LogPal.palVersion := $300;
  LogPal.palNumEntries := 1;

  //create the actual palette
 
  hPal := CreatePalette(LogPal);
  ResizePalette(hPal, 256);
  SetPaletteEntries(hPal, 0, 256, PalEntry);

  //make the palette the bitmap's palette
 
  SelectPalette(Image1.Canvas.Handle, hPal, FALSE);
 
  //make windows use the palette in the image
  RealizePalette(Image1.Canvas.Handle);
end;

If you want to change the palette of a bitmap, you can first
read the current palette of the bitmap using a similar
approach, using GetPaletteEntries (see online help). Then you
can change entries and make the changed palette effective as
described above.
This method is quite simple, but device dependent.
Another method requires a bit more work, but is device independent.It uses a so-called DIB: Device Indepenent Bitmap. I believe there is an article about this on the Borland site (www.borland.com).
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now