Recent

Author Topic: TBGRABitmap image palette converting  (Read 323 times)

SteenJorgensen

  • Jr. Member
  • **
  • Posts: 51
TBGRABitmap image palette converting
« on: July 27, 2019, 09:22:49 am »
I need help for converting a TBGRABitmap to a customize palette.

In my code i end up with a image in correct size but i also need to converted all the color to 20 different TColor i have pre-defined.

This give me trouble, any idea?
 

I use BGRABitmap v10.4.
 
----------------------------------------
Lazarus version 2.0.2
FPC 3.0.4

circular

  • Hero Member
  • *****
  • Posts: 2954
    • Personal webpage
Re: TBGRABitmap image palette converting
« Reply #1 on: July 27, 2019, 10:58:42 pm »
Ok. So you can supply your palette using a TBGRAPalette object (unit BGRAPalette). You can add your color using AddColor.

Then you can use a TBGRAColorQuantizer object (unit BGRAColorQuantization). Create it using your palette as a parameter.

With ApplyDitheringInplace, it replaces the colors directly. Otherwise GetDitheredBitmap will return a new bitmap.
Conscience is the debugger of the mind

SteenJorgensen

  • Jr. Member
  • **
  • Posts: 51
Re: TBGRABitmap image palette converting
« Reply #2 on: July 29, 2019, 07:51:34 am »
Thanks, great!

I have made a little test procedure and this works with 20 color. BUT not with my colorpallet. I can't find where to put the pallette parameter in, in the TBGRAColorQuantizer.create.
 
Code: Pascal  [Select]
  1. procedure TForm1.bTestConvClick(Sender: TObject);
  2. var
  3.   ImageP: TBGRABitmap;
  4.   Quant: TBGRAColorQuantizer;
  5.   WorkPalette: TBGRAPalette;
  6. begin
  7.   Try
  8.     WorkPalette := TBGRAPalette.Create;
  9.     If FileExists('WorkPallet.pal') then
  10.       WorkPalette.LoadFromFile('WorkPallet.pal');
  11.  
  12.     ImageP  := TBGRABitmap.Create(200, 200, ColorToBGRA(ColorToRGB(clBtnFace)));
  13.     BGRAReplace(ImageP, ImageCrop.Resample(200, 200, rmFineResample));
  14.  
  15.     Quant := TBGRAColorQuantizer.Create(ImageP, acFullChannelInPalette, 20);
  16.     Quant.ApplyDitheringInplace(daNearestNeighbor, ImageP);
  17.  
  18.     ImageP.Draw(Panel10.Canvas, 0, 0, True);
  19.  
  20.   finally
  21.     ImageP.Free;
  22.     WorkPalette.Free;
  23.   end;
  24. end;
  25.  
----------------------------------------
Lazarus version 2.0.2
FPC 3.0.4

circular

  • Hero Member
  • *****
  • Posts: 2954
    • Personal webpage
Re: TBGRABitmap image palette converting
« Reply #3 on: July 29, 2019, 12:43:33 pm »
When creating TBGRAColorQuantizer, change the parameter of Create to your palette WorkPalette instead of ImageP.

Note that you can also replace
Code: Delphi  [Select]
  1.     ImageP  := TBGRABitmap.Create(200, 200, ColorToBGRA(ColorToRGB(clBtnFace)));
  2.     BGRAReplace(ImageP, ImageCrop.Resample(200, 200, rmFineResample));
by
Code: Delphi  [Select]
  1.     ImageP  := ImageCrop.Resample(200, 200, rmFineResample) as TBGRABitmap;
Conscience is the debugger of the mind