2005. október 24., hétfő

How to make a palette and a pf1bit bitmap not necessary in B/W


Problem/Question/Abstract:

How to make a palette and don't bother about Range Checking ON or OFF ({$R+/-}?
How to convert a bitmap to a pf1bit bitmap, but not necessary in B/W?

Answer:

// This method will give an error if Range Checking is on
// ======================================================

var
  LogPal: PLogPalette;
  Palette: HPalette;
  PalSize: LongInt;

begin
  { ... }
  PalSize := 2 * SizeOf(Word) + n_Colors * SizeOf(TPaletteEntry));
{2 * SizeOf(Word) to get space for palVersion and palNumEntries,  n_Colors is the number
of colors in the palette}
GetMem(LogPal, PalSize);
LogPal^.palVersion := $0300;
LogPal^.palNumEntries := n_Colors;
LogPal^.palPalEntry[0] := {Some colour};
LogPal^.palPalEntry[1] := {Some other colour};
{ etc. }
FreeMem(LogPal, PalSize);
{ ... }
end;

// This method will NOT give a Range Check Error!
// =============================================

var
  pal: TMaxLogPalette;
  hpal: HPalette;
  DummyImage: TImage;

begin
  pal.palVersion := $300; // Magic number
  pal.palNumEntries := 2; // Palette for 1bit images not black and white!

  // Set foreground color
  pal.palPalEntry[0].peRed := GetRValue(Color1);
  pal.palPalEntry[0].peGreen := GetGValue(Color1);
  pal.palPalEntry[0].peBlue := GetBValue(Color1);
  pal.palPalEntry[0].peFlags := 0;

  // Set backGroundColor
  pal.palPalEntry[1].peRed := GetRValue(Color2);
  pal.palPalEntry[1].peGreen := GetGValue(Color2);
  pal.palPalEntry[1].peBlue := GetBValue(Color2);
  pal.palPalEntry[1].peFlags := 0;

  // Create the palette
  hpal := CreatePalette(PLogPalette(@pal)^);

  // Create a new image
  DummyImage := TImage.Create(Self);
  DummyImage.Picture.Bitmap.Width := Image1.Picture.Bitmap.Width;
  DummyImage.Picture.Bitmap.Height := Image1.Picture.Bitmap.Height;
  DummyImage.Picture.Bitmap.PixelFormat := pf1bit;
  DummyImage.Picture.Bitmap.Palette := hpal; // Assign the palette
  DummyImage.Picture.Bitmap.Canvas.Draw(0, 0, Image1.Picture.Graphic); // Draw it
  {...}
  {...}
end;

// DummyImage now holds the pf1bit represenation of Image1.
// Yes! A pf1bit image has a palette and it doesn't have to be black and white either!

Nincsenek megjegyzések:

Megjegyzés küldése