Reply-To: "Earl F. Glynn" From: "Earl F. Glynn" Newsgroups: borland.public.delphi.graphics References: Subject: Re: Setting palette of a 256 colour bitmap. Date: Fri, 19 Jul 2002 18:07:54 -0500 Lines: 94 Organization: efg's Computer Lab X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300 NNTP-Posting-Host: 65.64.125.202 Message-ID: <3d389c74$1_2@dnews> X-Trace: dnews 1027120244 65.64.125.202 (19 Jul 2002 16:10:44 -0700) Path: dnews Xref: dnews borland.public.delphi.graphics:50209 "Phill Harvey-Smith" wrote in message news:Xns92429D2F12E0Fphillbiowarwickacuk@207.105.83.65... > Hi I'm trying to write a program that will extract graphics data from a > game. The data is stored in a propriotry format which I am able to read. > > Since the game in question has a palette of 256 colours I want to be able > to extract any images to a 256 colour bitmap. > > To do this I have created a form with a 64x64 pixel TImage on it I can > extract the data and plot it by using :- > > TImage.Canvas.Pixels[x,y]:=pixel value 0..255 Pixels is slow. Scanline is usually better. Here's what I suggest. Create an in-memory TBitmap the size you need. Set the Bitmap's height, width, and PixelFormat := pf8bit; Define a TMaxLogPalette structure and define the 256-color palette you want. Be aware that you can create a TBitmap with a 256 color palette, but you will have problems if you try to display such a TBitmap in 256 color mode. Doesn't make any sense? In 256 color mode, Windows reserves 20 colors for the display of buttons, panels, etc. In high color or true color display mode, you can see all 256 colors in your palette. Assign the Bitmap.Palette using the CreatePalette API call. After defining the palette, fill-in the scanlines with the data you have. Once you've defined your bitmap, save it to a file, or display it in a TImage. Here's an example that hopefully will get you started: procedure TForm1.ButtonGraysClick(Sender: TObject); TYPE TByteArray = ARRAY[WORD] OF BYTE; pByteArray = ^TByteArray; VAR Bitmap : TBitmap; i : INTEGER; j : INTEGER; NewPalette: TMaxLogPalette; row : pByteArray; begin Bitmap := TBitmap.Create; TRY Bitmap.PixelFormat := pf8bit; // Create bitmap same size as TImage (could be any size) Bitmap.Width := Image1.Width; Bitmap.Height := Image1.Height; // Define 256 shades of gray palette NewPalette.palVersion := $0300; // "Magic Number" for Windows LogPalette NewPalette.palNumEntries := 256; FOR i := 0 TO 255 DO BEGIN NewPalette.palPalEntry[i].peRed := i; NewPalette.palPalEntry[i].peGreen := i; NewPalette.palPalEntry[i].peBlue := i; NewPalette.palPalEntry[i].peFlags := PC_NOCOLLAPSE; END; Bitmap.Palette := CreatePalette(pLogPalette(@NewPalette)^); // Define Scanlines for Bitmap FOR j := 0 TO Bitmap.Height-1 DO BEGIN row := Bitmap.Scanline[j]; FOR i := 0 TO Bitmap.Width - 1 DO BEGIN row[i] := (i + j) MOD 256 END END; Bitmap.SaveToFile('Sample.BMP'); Image1.Picture.Graphic := Bitmap FINALLY Bitmap.Free END; -- efg -- Earl F. Glynn, Overland Park, KS USA efg's Computer Lab Mirror: http://homepages.borland.com/efg2lab