Recent

Author Topic: FPImageException with Lazmapviewer  (Read 1304 times)

Bert_Plitt

  • New Member
  • *
  • Posts: 31
FPImageException with Lazmapviewer
« on: October 23, 2019, 06:55:31 pm »
I downloaded and installed the packages from  lazarus-ccr-svn-r7167-components-lazmapviewer into Lazarus ver. 2.0.4, FPC ver. 3.0.4. (Windows 10)  I Opened, built, and ran the example project.  While experimenting with the example, I right clicked on the map to add a GPS point.  I entered a name and, as I expected, the point showed up on the map as a + sign and the name I entered.  Using the mouse to drag the map, the GPS point tracked the map movement.  But when the GPS point got to the left edge of the map view port, the program raised a "'FPImageException' exception: Invalid horizontal pixel index -7".  The same exceptions occur if the GPS point exceeds the top, bottom or right edge of the view port.  Does anyone know how to fix this?
Windows 10, Lazarus 2.0.6, FPC 3.0.4

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #1 on: October 24, 2019, 11:42:52 am »
I could verify this
a quick fix would be:
mvMapViewer.pas line 622ff
Code: Pascal  [Select]
  1.  // Draw point marker
  2.   if Assigned(FPOIImage) and not (FPOIImage.Empty) then
  3.     if (Pt.X - FPOIImage.Width div 2 >0) and (Pt.Y - FPOIImage.Height>0) then // <--- HERE
  4.     DrawingEngine.DrawBitmap(Pt.X - FPOIImage.Width div 2, Pt.Y - FPOIImage.Height, FPOIImage, true)
  5.     else
  6.   else if (Pt.X - 5>0) and (Pt.Y - 5>0) then begin // <--- HERE
  7.     DrawingEngine.PenColor := ptColor;
  8.     DrawingEngine.Line(Pt.X, Pt.Y - 5, Pt.X, Pt.Y + 5);
  9.     DrawingEngine.Line(Pt.X - 5, Pt.Y, Pt.X + 5, Pt.Y);
  10.     Pt.Y := Pt.Y + 5;
  11.   end;
  12.  
  13.   // Draw point text
  14.   s := aPOI.Name;
  15.   if FPOITextBgColor = clNone then
  16.     DrawingEngine.BrushStyle := bsClear
  17.   else begin
  18.     DrawingEngine.BrushStyle := bsSolid;
  19.     DrawingEngine.BrushColor := FPOITextBgColor;
  20.     s := ' ' + s + ' ';
  21.   end;
  22.   extent := DrawingEngine.TextExtent(s);
  23.   if (Pt.X - extent.CX div 2 >0) and ( Pt.Y + 5 >0) then // <--- HERE
  24.     DrawingEngine.Textout(Pt.X - extent.CX div 2, Pt.Y + 5, s);          
---------------------------
I also have a drawing-engine based on BGRA-Components

OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

wp

  • Hero Member
  • *****
  • Posts: 6334
Re: FPImageException with Lazmapviewer
« Reply #2 on: October 24, 2019, 12:06:40 pm »
I fixed it (hopefully). Please be aware that the tile painting engine requires a major rewrite because it does not consider the case that the bitmap or text of a point-of-interest reaches into an adjacent tile.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

wp

  • Hero Member
  • *****
  • Posts: 6334
Re: FPImageException with Lazmapviewer
« Reply #3 on: October 24, 2019, 12:12:26 pm »
I also have a drawing-engine based on BGRA-Components
Would you mind to post it? It would be nice to include it in the mapviewer.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #4 on: October 24, 2019, 01:48:05 pm »
Actually this did not fully do the job:
2. Version:
line 607
Code: Pascal  [Select]
  1.   s: String;
  2. begin
  3.   if not PtInsideArea(aPOI.RealPoint,Area) then // <-- HERE
  4.     exit;
  5.   if Assigned(FOnDrawGpsPoint) then begin
  6.     FOnDrawGpsPoint(Self, DrawingEngine, aPOI);
  7.     exit;    
  8.  
line 625:
Code: Pascal  [Select]
  1.  if Assigned(FPOIImage) and not (FPOIImage.Empty) then
  2.     DrawingEngine.DrawBitmap(Pt.X - FPOIImage.Width div 2, Pt.Y - FPOIImage.Height, FPOIImage, true)
  3.   else if (Pt.X - 5>0) and (Pt.Y - 5>0) // <-- HERE
  4.      and (Pt.X + 5<FEngine.Width-1) and (Pt.Y + 5<FEngine.Height-1) then begin
  5.     DrawingEngine.PenColor := ptColor;
  6.     DrawingEngine.Line(Pt.X, Pt.Y - 5, Pt.X, Pt.Y + 5);
  7.  
the rest in the mvMapViewer.pas is back to the original but:
mvDE_IntfGraphics.pas line 152
Code: Pascal  [Select]
  1. procedure TMvIntfGraphicsDrawingEngine.DrawBitmap(X, Y: Integer;
  2.   ABitmap: TCustomBitmap; UseAlphaChannel: Boolean);
  3. var
  4.   intfImg: TLazIntfImage;
  5.   i, j: Integer;
  6.   cimg, cbuf: TFPColor;
  7.   alpha: Double;
  8. begin
  9.   intfImg := ABitmap.CreateIntfImage;
  10.   try
  11.     if UseAlphaChannel then begin
  12.       for j := 0 to intfImg.Height - 1 do
  13.         if (j+y>=0) and (j+y<FBuffer.Height )then // <== HERE
  14.         for i := 0 to intfImg.Width - 1 do
  15.           if (i+x>=0) and (i+x<FBuffer.Width) then // <== HERE
  16.         begin
  17.           cimg := intfImg.Colors[i, j];
  18.  //  [...]
  19.           FBuffer.Colors[i + X, j + Y] := cbuf;
  20.         end;
  21.     end else
  22.       for j := 0 to intfImg.Height - 1 do
  23.         if (j+y>=0) and (j+y<FBuffer.Height) then // <== HERE
  24.         for i := 0 to intfImg.Width - 1 do
  25.           if (i+x>=0) and (i+x<FBuffer.Width) then // <== HERE
  26.           FBuffer.Colors := intfImg.Colors[i, j];
  27.   finally
  28.     intfimg.Free;
  29.   end;
  30. end;                
  31.  
Did the job.
---
Is there possibly a memleak ?
Tiles from mvEngine line 287 are not released at the end ?
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #5 on: October 24, 2019, 01:55:51 pm »
... here's the package ...
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

wp

  • Hero Member
  • *****
  • Posts: 6334
Re: FPImageException with Lazmapviewer
« Reply #6 on: October 24, 2019, 03:21:30 pm »
Thank you, great addition. I added it to the lazmapviewer (with minor modification).

I don't fully understand your suggestions to fix the crash due to painting outside the reserved buffer. For me, it is sufficient to fix only the DrawBitmap method (although your code is more efficient than my frist version). The other case with DrawingEngine.Line is probably already handled by clipping of the canvas, and the first one (if not PtInsideArea(...)) suppresses painting of the POI bitmap from an adjacent tile reaching into the current one.

Is there possibly a memleak ?
Tiles from mvEngine line 287 are not released at the end ?
Yes, see https://forum.lazarus.freepascal.org/index.php/topic,12674.50.html.
Unfortunately that's not all, and debugging this package which heavily makes use of threads is quite difficult. Maybe you can help?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #7 on: October 24, 2019, 03:44:59 pm »
You are right, the changes in mvDE_IntfGraphics.pas do the job.
and inRange is better than my (>0) and (<Width)
But wouldn't it be better to do the bail-out right after the for-iteration ?
like in my code ?

I posted my second version before i got your fix.
the first (line 607) is not about the problem, it's something like an optimization (things that are outside the visible area don't have  to be processed). (see line 585)



OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

wp

  • Hero Member
  • *****
  • Posts: 6334
Re: FPImageException with Lazmapviewer
« Reply #8 on: October 24, 2019, 04:37:35 pm »
But wouldn't it be better to do the bail-out right after the for-iteration ?
like in my code ?
Isn't this happening in the last r7170?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #9 on: October 24, 2019, 05:31:34 pm »
Oh, it happened again,... :-[
I as referring to an older release.

Is there a reason for computing cimg and alpha, I don't see a place where they are used afterwards.
Code: Pascal  [Select]
  1.    cimg := intfImg.Colors[i, j];
  2.    alpha := cimg.Alpha / word($FFFF);
  3.    if InRange(i + X, 0, FBuffer.Width-1) then begin        
  4.  
instead of
Code: Pascal  [Select]
  1.    if InRange(i + X, 0, FBuffer.Width-1) then begin        
  2.        cimg := intfImg.Colors[i, j];
  3.        alpha := cimg.Alpha / word($FFFF);
?

OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

Bert_Plitt

  • New Member
  • *
  • Posts: 31
Re: FPImageException with Lazmapviewer
« Reply #10 on: October 24, 2019, 05:59:49 pm »
jc99 and wp  --  I downloaded the new version of lazmapviewer.  Looks like you fixed the problem, thanks!
Windows 10, Lazarus 2.0.6, FPC 3.0.4

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #11 on: October 24, 2019, 07:27:28 pm »
BTW, I think, i fixed the mem-leak !
Try the patch ...
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

wp

  • Hero Member
  • *****
  • Posts: 6334
Re: FPImageException with Lazmapviewer
« Reply #12 on: October 25, 2019, 12:01:58 am »
I wish you'd have fixed this beast, but, sorry, you did not. heaptrc still reports a variety of memory leaks when I drag the viewport of the demo program by a sufficiently large distance so that new tiles have to be loaded.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #13 on: October 25, 2019, 12:42:28 am »
That's interesting, heaptrc reports to me 0 unfreed blocks, and I did changed the vieport, the mapprovider zoomed in and out  ...
Maybe all the tiles where already in the cache ...
...Hmmm, yes I can also verify this, but this comes from someplace else ...
let me have a look at this.

OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren

jc99

  • Hero Member
  • *****
  • Posts: 536
    • My private Site
Re: FPImageException with Lazmapviewer
« Reply #14 on: October 25, 2019, 10:29:35 am »
So...
Here comes the next try.
It's much better, but there is still an issue ...
 
OS: Win XP x64, Win 7, Win 7 x64, Win 10, Win 10 x64, Suse Linux 13.2
Laz: 1.4 - 1.8.4, 2.0
https://github.com/joecare99/public
'~|    /''
,_|oe \_,are
If you want to do something for the environment: Twitter: #reduceCO2 or
https://www.betterplace.me/klimawandel-stoppen-co-ueber-preis-reduzieren