?
Solved

Bitmap Palette

Posted on 1997-05-08
2
Medium Priority
?
1,663 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
ID: 1336022
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 50 total points
ID: 1336023
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

588 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