Recent

Author Topic: Convert BGRAbitmap to 256 Color BMP and save  (Read 535 times)

winni

  • Hero Member
  • *****
  • Posts: 3197
Convert BGRAbitmap to 256 Color BMP and save
« on: November 17, 2022, 02:08:35 pm »
Hi!

Question in the german forum:

How to convert BGRAbitmap to a 256 Color BMP and save it?

It is needed by some SAP software that rejects all images except 256-Color-BMP.

Is there a solution yet?  I have not found anything.

Winni

paweld

  • Hero Member
  • *****
  • Posts: 970
Re: Convert BGRAbitmap to 256 Color BMP and save
« Reply #1 on: November 17, 2022, 05:13:52 pm »
Hi,
sample code:
Code: Pascal  [Select][+][-]
  1. uses  
  2.   BGRABitmap, BGRABitmapTypes, BGRAColorQuantization, FPWriteBmp;
  3.  
  4.  
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. var
  7.   img: TBGRABitmap;
  8.   bmpw: TFPWriterBmp;
  9.   quant: TBGRAColorQuantizer;
  10.   i: Integer;
  11. begin
  12.   img := TBGRABitmap.Create('d:\wzor80.jpg');
  13.   bmpw := TFPWriterBmp.Create;
  14.   bmpw.BitsPerPixel := 8;
  15.   //bmpw.RLECompress := True;
  16.  
  17.   quant := TBGRAColorQuantizer.Create(img, acIgnore, 256);  //reduce color to 256 without alfa
  18.   quant.ApplyDitheringInplace(daNearestNeighbor, img);
  19.   img.UsePalette := True;
  20.  
  21.   img.Palette.Clear;
  22.   for i := 0 to quant.ReducedPalette.Count - 1 do      //copy reduced colors to palette
  23.     img.Palette.Add(quant.ReducedPalette.Color[i].ToFPColor);
  24.  
  25.   img.SaveToFile('d:\wzor80_256color.bmp', bmpw);   //save as bmp
  26.   bmpw.Free;     //clean
  27.   img.Free;
  28.   quant.Free;
  29. end;        
  30.  
Best regards / Pozdrawiam
paweld

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Convert BGRAbitmap to 256 Color BMP and save
« Reply #2 on: November 17, 2022, 05:37:06 pm »
Hi!

Thanx a lot.

The Quantizer was the part that I forgot. Getting old ...

The Link in the german forum:

https://www.lazarusforum.de/viewtopic.php?f=18&t=14593&p=131460#p131460


Winni

 

TinyPortal © 2005-2018