Can't CopyRect in FPC-program lazarus / 39-Graphics ---------------------------------------------------------------------- hakelm · 2025-09-04 06:32 Trying to do CopyRect in a simple FPC-program (se below) I get the following error: Error Project copyk raised exception class 'External: SIGSEGV'. Infile lclintF.inc’ at line 182: Result := WidgetSet.Rawimage_QueryDescription(AFlags,ADesc); on the line png0.Canvas.CopyRect(r,png1.Canvas,r);. The same program run as a procedure in a Lazarus application behaves as expected. What have I missed and what can I do? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program copyk; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes  ,graphics, GraphType  ; var png0,png1:TPortableNetworkGraphic; r:trect;begin   png0:=TPortableNetworkGraphic.Create;   png0.LoadFromFile('U.png');    png1:=TPortableNetworkGraphic.Create;   png1.LoadFromFile('k1.png');   r:=TRect.Create(0, 0, png1.Width, png1.Height);    png0.Canvas.CopyRect(r,png1.Canvas,r);   png0.SaveToFile('p.png');end.  I have Lazarus Version: 3.8 Date: 2025-08-13 FPC Version: 3.2.2, x86_64-linux-gtk2 on Ubuntu 22.4. ---------------------------------------------------------------------- zeljko · 2025-09-04 07:16 Include Interfaces unit ---------------------------------------------------------------------- cdbc · 2025-09-04 07:18 Hi You are missing one or some of the units, that are present in a GUI app... My guess would be: - to include package 'LCLbase' or 'LCL' in you app, /project inspector/ - add 'Interfaces' to your uses-clause... Regards Benny ---------------------------------------------------------------------- Thaddy · 2025-09-04 07:40 Interfaces is enough.Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program copyk; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  interfaces,  Classes  ,graphics, GraphType  ; var png0,png1:TPortableNetworkGraphic; r:trect;begin   png0:=TPortableNetworkGraphic.Create;   png0.LoadFromFile('U.png');    png1:=TPortableNetworkGraphic.Create;   png1.LoadFromFile('k1.png');   r:=TRect.Create(0, 0, png1.Width, png1.Height);    png0.Canvas.CopyRect(r,png1.Canvas,r);   png0.SaveToFile('p.png');   png0.free;   png1.free;end.png's must exist. ---------------------------------------------------------------------- Thausand · 2025-09-04 08:01 Also can have alternative no use LCL but use TFPImage. ---------------------------------------------------------------------- Thaddy · 2025-09-04 09:05 Quote from: Thausand on September 04, 2025, 10:01:51 amAlso can have alternative no use LCL but use TFPImage. Would be good, but does it handle png's? ---------------------------------------------------------------------- Thausand · 2025-09-04 09:29 Quote from: Thaddy on September 04, 2025, 11:05:02 amWould be good, but does it handle png's? Yes, can have png when clause uses reader/writer for png (or manual load) https://wiki.freepascal.org/fcl-image#Image_formats Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test; {$mode objfpc}{$h+} uses  {$ifdef linux}cthreads,{$endif}  types ,sysutils,fpimage,fpcanvas,fpimgcanv,fpreadpng,fpwritepng; procedure copyImgRectToImg(OldFileName,RectFileName,NewFileName:string);var CompCanvas,RectCanvas:TFPImageCanvas;r:TRect;begin  CompCanvas:=TFPImageCanvas.Create(TFPMemoryImage.Create(0,0));  RectCanvas:=TFPImageCanvas.Create(TFPMemoryImage.Create(0,0));   try  CompCanvas.Image.LoadFromFile(OldFileName);    RectCanvas.Image.LoadFromFile(RectFileName);     r:=TRect.Create(0,0,RectCanvas.Image.Width,RectCanvas.Image.Height);  CompCanvas.CopyRect(0,0,RectCanvas,r);     CompCanvas.Image.SaveToFile(NewFileName);  finally    CompCanvas.Free;    RectCanvas.Free;  end;end; // "src.png"  : https://wiki.freepascal.org/images/5/59/splash_logo.png// "rect.png" : https://wiki.freepascal.org/images/e/ee/LazarusSource.PNGbegin  copyImgRectToImg('src.png','rect.png','result.png');end.  I think that work. ---------------------------------------------------------------------- hakelm · 2025-09-04 17:13 Thanks all. fpimage did the trick, se below. Interestingly readers and writers aren't necessary. But I still can't understand why my original code works in a Lazarus application but not in a non-gui program. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program copyk2; {$mode objfpc}{$H+} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes  ,fpreadpng,fpimage,FPCanvas, FPImgCanv  ; var r:trect;  png0,png1:TFPCustomImage; canvas0, canvas1 : TFPCustomCanvas; //reader:TFPReaderPNG; begin   png0:= TFPMemoryImage.Create(10, 10);   //Reader := TFPReaderpng.Create;   //png0.LoadFromFile('U.png', Reader);   png0.LoadFromFile('U.png');   Canvas0 := TFPImageCanvas.Create(png0);    png1:= TFPMemoryImage.Create(10, 10);   png1.LoadFromFile('k1.png');   r:=TRect.Create(1, 1, png1.Width-1, png1.Height-1);   Canvas1 := TFPImageCanvas.Create(png1);    canvas0.CopyRect(0,0,canvas1,r);   png0.SaveToFile('p.png');end. ---------------------------------------------------------------------- hakelm · 2025-09-04 17:22 Now I can also confirm that the addition of unit interfaces fixes the problem. ---------------------------------------------------------------------- Thausand · 2025-09-05 02:21 Quote from: hakelm on September 04, 2025, 07:13:15 pmfpimage did the trick, se below. Interestingly readers and writers aren't necessary. You have include reader/writer png else no work png. Clause use reader/writer is make work so no have make reader/writer manual (but can make if want or if format not recognize and have explicit reader/writer). QuoteBut I still can't understand why my original code works in a Lazarus application but not in a non-gui program. Lazarus application have always clause uses interfaces in program project source-code. This not see default with lazarus ide. Please have look source lazarus project .lpr or have use project/view project source So when add interfaces for init widget it work because you have now make same as lazarus app that init widget. If ask why need uses interfaces then answer is canvas depend LCL (and TPortableNetworkGraphic depend  graphic, graphic have canvas then all need LCL). Is how is implement TPortableNetworkGraphic. This why I have make suggest and have use TFPImage because TFPImage not depend widget/LCL. I think is more easy and portable when not display picture and only make manipulate picture. Can have alternative NOGUI widget but that advanced topic (and have same interfaces and depend LCL). ---------------------------------------------------------------------- Thaddy · 2025-09-05 05:22 Quote from: hakelm on September 04, 2025, 07:13:15 pmBut I still can't understand why my original code works in a Lazarus application but not in a non-gui program. That is because the GUI code has a widgetset registered and your code relies on a registered widget set: you could see that while you were debugging. Including the interfaces unit registers a widgetset for your platform. It has only two calls: in the initialization section it calls CreateWidgetSet() and in the finalization it calls FreeWidgetSet(). The other option, using fpimage,  does not need a widgetset and I would prefer that, the example code from Thausand, for smaller console applications.