Recent

Author Topic: Building a composite image  (Read 3008 times)

J-G

  • Hero Member
  • *****
  • Posts: 931
Re: Building a composite image
« Reply #45 on: June 16, 2022, 06:10:08 pm »
Thanks for the suggestion paweld, but that doesn't even give me the Gear Poly outline - totally blank canvas.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

winni

  • Hero Member
  • *****
  • Posts: 3164
Re: Building a composite image
« Reply #46 on: June 16, 2022, 06:51:45 pm »
Hi!

Learn the basics of TBGRAbitmap and see how it works.
Don't try to force BGRAbitmap the way ou want,
but look how it could be achieved with BGRA.

Do not use the Canvas of the BGRAbitmap.
It is just like every other Canvas with all the disadvantages the BGRA tries to avoid - and avoids.

Read the tutorial and do your own small tests.

Read the BGRAdefaultBitmap : The most of your questions is already answered there.

And try to be more clear with your questions.

Winni





J-G

  • Hero Member
  • *****
  • Posts: 931
Re: Building a composite image
« Reply #47 on: June 16, 2022, 07:27:09 pm »
Sorry to come across as a numpty Winni but I do have both the UNIT BGRADefaultBitmap and the BGRABitmap tutorial permanently open on my desktop and am constantly searching for solutions (without success!)

As far as I am aware I'm not trying to force BGRA to do anything, I'm simply trying to understand why an ostensibly 'transparent' image, isn't!

I'm only using the 'Canvas' because that is what has been suggested (by yourself - and others) as the means by which the object  may be achieved.  This 'Test' program is 90% your code with just very minor adjustments to bring the appearance of the Gears more in line with the real world - and add trivial functionallity such as specifiable tooth count, Fwd, Rev & speed.

How I can ask a question such as "Why does this 'transparent' Bitmap not show what's behind it?"  any clearer I really have no idea.  %)


FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

paweld

  • Hero Member
  • *****
  • Posts: 524
Re: Building a composite image
« Reply #48 on: June 16, 2022, 07:33:38 pm »
@J-G:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Gears(startAngle: single);
  2. var
  3.     step, angle     : single;
  4.     sinus, cosinus  : single;
  5.     dx1,dx2         : single;
  6.     toothPart       : single;
  7.     skewAngle       : single;
  8.     heightWidth     : integer = 600;
  9.     tmp             : TBGRAbitmap;
  10.     center          : TPointF;
  11.     poly            : ArrayOfTPointF;
  12.     i               : Integer;
  13.     OutlineCol      : TColor;
  14.     ms              : TMemoryStream;
  15. begin
  16.   with T_Img do
  17.     begin
  18.      Picture.Clear;
  19.      Transparent:=True;
  20.     end;
  21.  
  22.    center    := PointF(heightWidth/2, heightWidth/2);
  23.  
  24.    tmp       := TBGRAbitmap.create (heightWidth, heightWidth);
  25.    dx1       := 3;                   // 15      // tooth depth
  26.    dx2       := 2;                   // 10
  27.    step      := 360/numOfteeth;
  28.    ToothPart := 0.5 * step;          // Tooth/Gap is strictly 50/50     // 55% tooth 45% gap
  29.    skewAngle := Toothpart * 0.25;
  30.    //angle := startAngle;
  31.    angle := DegNormalize(startAngle);
  32.    setLength(poly,numOfteeth*6);     // 6 points per tooth
  33.  
  34.    i := 0;
  35.    while i <= high(poly) do
  36.      begin
  37.        // P1
  38.        math.sincos(angle*Deg2Rad,sinus,cosinus);
  39.        poly[i] := PointF (radius*cosinus, radius*sinus)+ center;
  40.        inc(i);
  41.        //P2
  42.        poly[i] := PointF ((radius+dx1)*cosinus, (radius+dx1)*sinus)+ center;
  43.        inc(i);
  44.        //P3
  45.        math.sincos((angle+SkewAngle)*Deg2Rad,sinus,cosinus);
  46.        poly[i] := PointF ((radius+dx1+dx2)*cosinus,(radius+dx1+dx2)*sinus)+Center;
  47.        inc(i);
  48.        //P4
  49.        Math.sincos((angle+Toothpart-skewAngle)*Deg2Rad,sinus,cosinus);
  50.        poly[i] := PointF ((radius+dx1+dx2)*cosinus,(radius+dx1+dx2)*sinus)+Center;
  51.        inc(i);
  52.        //P5
  53.        Math.sincos((angle+Toothpart)*Deg2Rad,sinus,cosinus);
  54.        poly[i] := PointF ((radius+dx1)*cosinus, (radius+dx1)*sinus)+ center;
  55.        inc(i);
  56.        // P6
  57.        poly[i] := PointF (radius*cosinus,radius*sinus)+Center;
  58.        inc(i);
  59.        // next Tooth
  60.        angle := angle + step;
  61.      end;
  62.    If Fwd then
  63.      OutlineCol := cssGreen
  64.    else
  65.      OutlineCol := cssRed;
  66.  
  67.    tmp.DrawPolygonAntialias(poly,OutlineCol,1,BGRAPixelTransparent { $00756060}{csswhite});
  68.  
  69.    tmp.FontHeight := 14;
  70.    tmp.TextOut(Center.x,Center.y+Radius/2,IntToStr(NumOfTeeth),clBlue,taCenter);
  71.  
  72.    //tmp.Draw(T_Img.canvas,0,0,false);
  73.    ms := TMemoryStream.Create;
  74.    tmp.SaveToStreamAsPng(ms);
  75.    ms.Position := 0;
  76.    T_img.Picture.LoadFromStream(ms);
  77.    ms.Free;
  78.  
  79.    tmp.free;
  80. end;
Best regards
paweld

J-G

  • Hero Member
  • *****
  • Posts: 931
Re: Building a composite image
« Reply #49 on: June 16, 2022, 08:33:42 pm »
!!!  @paweld !!!    :D

Hooray !  and many thanks.

I would never have thought that removing the canvas Brush & Fill would be the cause - I'd taken that as de riguer since that's what Winni had used in the initial code he kindly sent me.

I still don't understand the reason ( nor why it's necessary to add the TMemoryStream) but - for now - it seems I have working code that I may be able to transfer to my real program.

When I first ran your code - leaving the line colour and tooth count as they were - with the Gear Image in place (you don't have that in your copy of the program) I thought it still wasn't viable - the line colour was Green and the PolyLine was drawn against the Green Gear :(  Reversing the direction - which makes the line colour Red - meant that I could see it.  Changing the default colour to White and Tooth count to 80 solved those issues.

Now that I have the Gear Image and the PolyLine 'co-incident', I can see that the scaling multiplier  (PxScale in the constants) needs to be 24.1  - though no doubt I may have to review that in the real program where the tooth count can be anything between 20 & 127 in single step increments. That will be a trivial matter.

The probability is that I won't 'animate' the gears in the real program - there could be 6 gears in the train and they would all have to run at different speeds so there may be a calculation bottle-neck :) - The display of the teeth will probably be quite sufficient . . . .  and I can certainly use different colours to indicate direction of rotation.

In case there is any interest, I've attached a new version of the .ZIP file with these latest 'adjustments'.



FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 931
Re: Building a composite image
« Reply #50 on: June 18, 2022, 06:52:41 pm »
I've now tried to incorporate the creation of the polygon outlines into my main program and have mostly been successful but am now getting an 'External: ACCESS VIOLATION' error which I can't understand.

The attached image is a ScreenGrab showing the Error, along with the part of the code referenced and the Assembler window. The 'test' program has - what I consider - the same code :
Code: Pascal  [Select][+][-]
  1. CreatePolygon;
  2.  
  3.   If Clockwise then
  4.     LineCol := cssBlack
  5.   else
  6.     LineCol := cssRed;
  7.  
  8.   tmp.DrawPolygonAntialias(poly,LineCol,1,BGRAPixelTransparent);
  9.  
  10.   ms := TMemoryStream.Create;        // suggestion from paweld
  11.   tmp.SaveToStreamAsPng(ms);
  12.   ms.Position := 0;
  13.   Form1.T_img.Picture.LoadFromStream(ms);
  14.   ms.Free;
  15.  
  16.   tmp.free;

... though naturally with some subtle differences to the variable names  -  'Outline' is now a 'record' with all the variables being part of that rather than 'Local'.  As a test I did create 'P : Array of TPointF; ' as a local and equated that to Outline.Poly before calling [tmp.DrawPolygonAntialias] but that made no difference.

Since it fails at line 467 I can't tell yet whether the 'DestT' assignment  works but that question may be redundent in the light of paweld's suggestion in the 'Z Order' question in the General Forum.

I've incorporated a 'Create CSV' routine to easily view the figures created by the CreatePolygon Proc. and they look 'good'  - well certainly no 'extraordinary' outliers.

FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

paweld

  • Hero Member
  • *****
  • Posts: 524
Re: Building a composite image
« Reply #51 on: June 18, 2022, 08:03:59 pm »
Not much code, but in my opinion the error occurs because you did not declare or create a tmp variable of type tbgrabitmap, ie:
Code: Pascal  [Select][+][-]
  1. procedure CreateOutline;var
  2.   tmp: TBGRABitmap;  ms: TMemoryStream;
  3. begin
  4.   tmp := TBGRABitmap.Create(width, height);  CreatePolygon;  
  5.  
  6.   If Clockwise then
  7.     Outline.Col := cssBlack
  8.   else
  9.     Outline.Col := cssRed;
  10.  
  11.   tmp.DrawPolygonAntialias(Outline.poly, Outline.Col, 1, BGRAPixelTransparent);
  12.   ms := TMemoryStream.Create;        // suggestion from paweld
  13.   tmp.SaveToStreamAsPng(ms);
  14.   ms.Position := 0;
  15.   Form1.T_img.Picture.LoadFromStream(ms);  ms.Free;
  16.   tmp.Free;
  17. end;
Best regards
paweld

J-G

  • Hero Member
  • *****
  • Posts: 931
Re: Building a composite image
« Reply #52 on: June 18, 2022, 08:25:58 pm »
Arrggg !!!   >:(   -  I have declared both tmp & ms but as you rightly surmise, at the end of the Gear BODY creation I (correctly?)  do 'tmp.Free'.  When I then go on to create the 'OUTLINE' of course I need to [Create] again  :(

The program runs (again!!)  but the next problem has raised it's ugly head - - - -  The Gear Bodies are drawn but the tooth outline is not - ???   This may be down to them not being in the correct location of course but at least I can  move forward, Thanks paweld.

I'll start looking at your 'One Bitmap' option in more detail now which may solve even a bad position issue.
« Last Edit: June 18, 2022, 08:28:07 pm by J-G »
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018