Recent

Author Topic: [SOLVED] BGRA library: Where am I going wrong?  (Read 1456 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 398
[SOLVED] BGRA library: Where am I going wrong?
« on: February 28, 2022, 07:21:17 pm »
I am trying to adapt the aa-demo program of the BGRA library to my own use. I've included the small project I've been working with, but here's the highlight:

1. I've declared the following form:
Code: Pascal  [Select][+][-]
  1.    TForm1 = class(TForm)
  2.       Label1: TLabel;
  3.       Label2: TLabel;
  4.       Label_PixelSizeValue: TLabel;
  5.       Panel1: TPanel;
  6.       SpinEdit_Gamma: TFloatSpinEdit;
  7.       TrackBar_PixelSize: TTrackBar;
  8.       procedure FormCreate(Sender: TObject);
  9.       procedure FormPaint(Sender: TObject);
  10.    private
  11.       fpts: array[1..4] of TPointF;
  12.       fPixelSize : Integer;
  13.       fMovingPointIndex : integer;
  14.       function coordToBmp(pt: TPointF): TPointF;
  15.    public
  16.  
  17.    end;
  18.  

The relevant methods are as follows:
Code: Pascal  [Select][+][-]
  1. function TForm1.coordToBmp(pt: TPointF): TPointF;
  2. begin
  3.   result := (pt+PointF(0.5,0.5))*(1/fpixelSize)-PointF(0.5,0.5);
  4. end;
  5.  

and

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.  //Let's try and draw a rectangle.
  4.  fpts[1] := PointF(100,100);
  5.  fpts[2] := PointF(200,100);
  6.  fpts[3] := PointF(200,10 );
  7.  fpts[4] := PointF(10 , 10);
  8.  Label_PixelSizeValue.Caption := '= ' + IntToStr(TrackBar_PixelSize.Position);
  9.  fMovingPointIndex := -1;
  10.  SpinEdit_Gamma.Value := BGRAGetGamma;
  11. end;
  12.  

Finally:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormPaint(Sender: TObject);
  2. var bmp          : TBGRABitmap;
  3.     aZoomX       ,
  4.     aZoomY       : Integer    ;
  5.     tx, ty       : Integer    ;
  6. begin
  7.   aZoomX       := ClientWidth;
  8.   aZoomY       := Panel1.Top ;
  9.   fPixelSize   := TrackBar_PixelSize.Position;
  10.   tx           := (aZoomX + fPixelSize - 1) div fPixelSize;
  11.   ty           := (aZoomY + fPixelSize - 1) div fPixelSize;
  12.   //Draw Square
  13.   bmp := TBGRABitmap.Create(tx,ty,BGRAWhite);
  14.   bmp.RectangleAntialias({x =} 10, {y = } 10, {x2 =} 100, {y2 = } 100, {c = } BGRABlack, {w = } 5, {back =} BGRAWhite);
  15.   bmp.FillPolyAntialias([CoordToBMP(fpts[1]), coordToBMP(fpts[2]), coordToBMP(fpts[3]), coordToBMP(fpts[4])], BGRABlack);
  16.   bmp.Free;
  17. end;
  18.  

When I run the code, nothing gets painted on the form. What am I doing wrong?

Thanks for any tip and suggestions!

« Last Edit: March 02, 2022, 09:56:24 am by EganSolo »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: BGRA library: Where am I going wrong?
« Reply #1 on: February 28, 2022, 07:51:59 pm »
Hi!

You forgot to draw on the canvas of the Form:

Code: Pascal  [Select][+][-]
  1.   bmp := TBGRABitmap.Create(tx,ty,BGRAWhite);
  2.   bmp.RectangleAntialias({x =} 10, {y = } 10, {x2 =} 100, {y2 = } 100, {c = } BGRABlack, {w = } 5, {back =} BGRAWhite);
  3.   bmp.FillPolyAntialias([CoordToBMP(fpts[1]), coordToBMP(fpts[2]), coordToBMP(fpts[3]), coordToBMP(fpts[4])], BGRABlack);
  4.   //   now it happens
  5.   bmp.draw(canvas,0,0);
  6.   bmp.Free;
  7.  
  8.  

A good introduction is the tutorial in 16 steps:

https://wiki.freepascal.org/BGRABitmap_tutorial


Winni

EganSolo

  • Sr. Member
  • ****
  • Posts: 398
[SOLVED]: BGRA library: Where am I going wrong?
« Reply #2 on: March 02, 2022, 09:56:00 am »
Thanks Winni,

That did indeed the trick.

I'm still learning my way through BGRA. Quite a complete and beautiful library but a bit overwhelming when starting. The tutorials are of some help. I am mostly interested in drawing shapes a la visio and connecting them and the only tutorial that seems somewhat applicable is the Canvas2D. Still, I'm making progress.


winni

  • Hero Member
  • *****
  • Posts: 3197
Re: [SOLVED] BGRA library: Where am I going wrong?
« Reply #3 on: March 02, 2022, 10:10:04 am »
Hi!

If you are working with shapes then indeed the TBGRACanvas2D is your swissknife. 

Look at chapter 14 of the BGRAbitmap tutorial.

Winni

 

TinyPortal © 2005-2018