Lazarus

Programming => Graphics => Graphics and Multimedia => BGRABitmap and LazPaint => Topic started by: crazzzypeter on May 27, 2021, 06:23:55 pm

Title: Division by Zero bug in BGRABitmap
Post by: crazzzypeter on May 27, 2021, 06:23:55 pm
Path drawing function can cause "Division by Zero" exception in Windows.
There is no error on Linux, but the drawing is not correct, the path is not closed. (see attached image).
The bug is in the ComputeWidePolylinePoints function in the BGRAPen.pas file. (see attached image).

I found a workaround that fixes the error, however this is a quick "dirty" hack:
Code: Pascal  [Select][+][-]
  1.   //compute borders
  2.   setlength(borders, length(pts)-1);
  3.   for i := 0 to high(pts)-1 do
  4.   begin
  5.     dir := pts[i+1]-pts[i];
  6.     len := VectLen(dir);
  7.     if len <> 0 then// <- hack
  8.       dir.Scale(1/len);  
Attached to the post is an example that raises a "Division by Zero" exception on Windows.

I hope the author of BGRABitmap will take a look at this bug and the possibility of fixing it.
Title: Re: Division by Zero bug in BGRABitmap
Post by: circular on May 28, 2021, 06:42:25 pm
Thanks.

Added the bug.
https://github.com/bgrabitmap/bgrabitmap/issues/141
Title: Re: Division by Zero bug in BGRABitmap
Post by: Josh on May 28, 2021, 09:13:33 pm
Hi Circular,

I had a similar problem and created a function that helped in my situation,  whether its usefull for ur needs not sure.
Code: [Select]
Function Special_Divide(ADividend ,ADivisor,ADefault:Double):Double;
var ASign:Integer=1;
begin
  if Adividend<0 then ASign:=-1;
  if abs(Adivisor)<=abs(ADefault) then Result:=ADefault*ASign
  else
    If (abs(ADividend)*abs(ADivisor))<abs(Adefault) then result:=ADefault*ASign
    else result:=ADividend/ADivisor;
end;     

It is probably not the fastest/effecient way but it helped in my scenario and my needs; it may need adjusting
Title: Re: Division by Zero bug in BGRABitmap
Post by: circular on May 30, 2021, 11:30:18 am
Thanks.

I suppose that in this case, it is a matter of skipping a point if the length is zero.
TinyPortal © 2005-2018