Recent

Author Topic: [Solved] Change background color of a BMP  (Read 19048 times)

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Change background color of a BMP
« Reply #30 on: June 21, 2017, 08:51:11 am »
I did it! Key to success - is ReleaseHandle.

Code: Pascal  [Select][+][-]
  1. unit TestMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TCardTestForm }
  13.  
  14.   TCardTestForm = class(TForm)
  15.     Image1: TImage;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure FormDestroy(Sender: TObject);
  18.     procedure Image1Click(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.     CardBitmap:TBitmap;
  24.     procedure DrawBitmap;
  25.   end;
  26.  
  27. var
  28.   CardTestForm: TCardTestForm;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TCardTestForm }
  35.  
  36. type
  37.   TABGR = packed record
  38.     R, G, B, A: Byte;
  39.   end;
  40.  
  41.   TABGRColor = record
  42.     case Boolean of
  43.       False:(Color:TColor);
  44.       True:(ABGR:TABGR);
  45.   end;
  46.  
  47. const
  48.   Delta = 100;
  49.   MaxColor = 255 - Delta;
  50.  
  51. procedure TCardTestForm.FormCreate(Sender: TObject);
  52.   var MaskBitmap:TBitmap;
  53.   I, J:Integer;C:TABGRColor;
  54. begin
  55.   Randomize;
  56.   with Image1.Picture do begin
  57.     Bitmap.Assign(TBitmap.Create);
  58.     Bitmap.Width := 640;
  59.     Bitmap.Height := 640;
  60.     Bitmap.Canvas.FillRect(0, 0, Bitmap.Width, Bitmap.Height);
  61.   end;
  62.   CardBitmap := TBitmap.Create;
  63.   CardBitmap.LoadFromFile('Card.bmp');
  64.   {Can be loaded from file too}
  65.   MaskBitmap := TBitmap.Create;
  66.   MaskBitmap.Width := CardBitmap.Width;
  67.   MaskBitmap.Height := CardBitmap.Height;
  68.   for I := 0 to CardBitmap.Width - 1 do begin
  69.     for J := 0 to CardBitmap.Height - 1 do begin
  70.       C.Color := CardBitmap.Canvas.Pixels[I, J];
  71.       if (C.ABGR.R > MaxColor) and (C.ABGR.G > MaxColor) and (C.ABGR.B > MaxColor) then begin
  72.         MaskBitmap.Canvas.Pixels[I, J] := clWhite;
  73.       end
  74.       else begin
  75.         MaskBitmap.Canvas.Pixels[I, J] := clBlack;
  76.       end;
  77.     end;
  78.   end;
  79.   CardBitmap.MaskHandle := MaskBitmap.ReleaseHandle;
  80.   MaskBitmap.Free;
  81.   DrawBitmap;
  82. end;
  83.  
  84. procedure TCardTestForm.FormDestroy(Sender: TObject);
  85. begin
  86.   CardBitmap.Free;
  87. end;
  88.  
  89. procedure TCardTestForm.Image1Click(Sender: TObject);
  90. begin
  91.   DrawBitmap;
  92. end;
  93.  
  94. procedure TCardTestForm.DrawBitmap;
  95. begin
  96.   with Image1.Picture do begin
  97.     {Random background color}
  98.     Bitmap.Canvas.Brush.Color := Random(clWhite);
  99.     Bitmap.Canvas.FillRect(0, 0, CardBitmap.Width, CardBitmap.Height);
  100.     Bitmap.Canvas.Draw(0, 0, CardBitmap);
  101.   end;
  102. end;
  103.  
  104. end.
  105.  

Variant with simply loading pre-baked mask from file (Mask.bmp is saved in previous example via SaveToFile before ReleaseHandle):
Code: Pascal  [Select][+][-]
  1. unit TestMain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;
  9.  
  10. type
  11.  
  12.   { TCardTestForm }
  13.  
  14.   TCardTestForm = class(TForm)
  15.     Image1: TImage;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure FormDestroy(Sender: TObject);
  18.     procedure Image1Click(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.     CardBitmap:TBitmap;
  24.     procedure DrawBitmap;
  25.   end;
  26.  
  27. var
  28.   CardTestForm: TCardTestForm;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TCardTestForm }
  35.  
  36. procedure TCardTestForm.FormCreate(Sender: TObject);
  37.   var MaskBitmap:TBitmap;
  38. begin
  39.   Randomize;
  40.   with Image1.Picture do begin
  41.     Bitmap.Assign(TBitmap.Create);
  42.     Bitmap.Width := 640;
  43.     Bitmap.Height := 640;
  44.     Bitmap.Canvas.FillRect(0, 0, Bitmap.Width, Bitmap.Height);
  45.   end;
  46.   CardBitmap := TBitmap.Create;
  47.   CardBitmap.LoadFromFile('Card.bmp');
  48.   {Can be loaded from file too}
  49.   MaskBitmap := TBitmap.Create;
  50.   MaskBitmap.LoadFromFile('Mask.bmp');
  51.   CardBitmap.MaskHandle := MaskBitmap.ReleaseHandle;
  52.   MaskBitmap.Free;
  53.   DrawBitmap;
  54. end;
  55.  
  56. procedure TCardTestForm.FormDestroy(Sender: TObject);
  57. begin
  58.   CardBitmap.Free;
  59. end;
  60.  
  61. procedure TCardTestForm.Image1Click(Sender: TObject);
  62. begin
  63.   DrawBitmap;
  64. end;
  65.  
  66. procedure TCardTestForm.DrawBitmap;
  67. begin
  68.   with Image1.Picture do begin
  69.     {Random background color}
  70.     Bitmap.Canvas.Brush.Color := Random(clWhite);
  71.     Bitmap.Canvas.FillRect(0, 0, CardBitmap.Width, CardBitmap.Height);
  72.     Bitmap.Canvas.Draw(0, 0, CardBitmap);
  73.   end;
  74. end;
  75.  
  76. end.
  77.  
« Last Edit: June 21, 2017, 08:56:26 am by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Change background color of a BMP
« Reply #31 on: June 21, 2017, 09:03:54 am »
That's great.

You're good in graphics programming, please consider to join the Graphics Contest 2017:
http://forum.lazarus.freepascal.org/index.php/topic,35313.0.html

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 844
Re: Change background color of a BMP
« Reply #32 on: June 21, 2017, 09:30:07 am »
That's great.

You're good in graphics programming, please consider to join the Graphics Contest 2017:
http://forum.lazarus.freepascal.org/index.php/topic,35313.0.html
Eh. Sad thing about me - is that now I have to focus on other area in order to prepare tools for my application. I'm developing and testing my own custom data containers (yeah, I know, that inventing bicycle is bad thing, but I'm not satisfied with standard ones) - it's long and hard process, so I've already become hungry for programming of graphics. :'(
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 592
    • Double Dummy Solver - free download
Re: Change background color of a BMP
« Reply #33 on: June 21, 2017, 04:04:29 pm »
Very nice Mr. Madguy.
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: [Solved] Change background color of a BMP
« Reply #34 on: June 21, 2017, 04:45:59 pm »
Have you solved the issue?
Will you join the contest?
See you there.

 

TinyPortal © 2005-2018