Recent

Author Topic: Raise exception in a CreateIntFImage  (Read 5030 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
Raise exception in a CreateIntFImage
« on: August 18, 2012, 05:10:05 pm »
Hello, I trying to load a PNG file, as make the example, that come with Lazarus, OpenGlControlDemo.
So I copy the function LoadflTextImage2DfromPNG, and for Test I wrote this small program.
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes
  10.   { you can add units after this },
  11.   Graphics, IntfGraphics,Gl,Fpimage;
  12.   TYpe
  13.  
  14.   { TglTexture }
  15.  
  16.  TglTexture= class
  17.     public
  18.       width ,Height : Longint;
  19.       Data          : Pointer;
  20.       destructor Destroy;Override;
  21.  
  22.   end;
  23.  
  24. var
  25.   P: TPortableNetworkGraphic;
  26.   InfImage: TLazIntfImage;
  27.   Textura : TglTexture;
  28.   Pb: PByte;
  29.   X,Y: Integer;
  30.   c : TFpColor;
  31. { TglTexture }
  32.  
  33. destructor TglTexture.Destroy;
  34. begin
  35.   Data := Nil;
  36.   //Data.Free;
  37.   inherited Destroy;
  38. end;
  39.  
  40. begin
  41.   Textura := TglTexture.Create;
  42.   InfImage := nil;
  43.    P := TPortableNetworkGraphic.Create;
  44.   P.LoadFromFile('Texture1.png');
  45.   InfImage := P.CreateIntfImage;
  46.   Textura.width:= P.Width;
  47.    Textura.Height:=P.Height;
  48.   Getmem(Textura.Data,Textura.width * Textura.Height * 3);
  49.   Pb := PByte (Textura.Data);
  50.  
  51.   For Y := 0 To P.Height-1 do
  52.   Begin
  53.     For X := 0 To P.Width do
  54.     Begin
  55.       c:=InfImage.Colors[x,y];
  56.       Pb^:= c.red shr 8;
  57.       inc(pb);
  58.       Pb^:=c.Green shr 8;
  59.       inc(pb);
  60.       Pb^:=c.blue shr 8;
  61.       inc(pb);
  62.     end;
  63.   end;
  64.   ReadLn;
  65.   P.Free;
  66.   InfImage.Free;
  67.   Textura.Free;
  68. end.    
  69.  
But in the line
Code: Pascal  [Select][+][-]
  1. InfImage := P.CreateIntfImage;
  2.  
I got a exception of the debugger: Extenal SigSev.
But If I run the example that come with Lazarus, every thing goes ok.
I don't know what I doing wrong.
Any idea?

Thank
/BlueIcaro
« Last Edit: August 18, 2012, 11:36:08 pm by BlueIcaro »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Raise exception un CreateIntFImage
« Reply #1 on: August 18, 2012, 05:26:11 pm »
This is starnge. I didn't see the demo, but here:
Code: [Select]
  P: TPortableNetworkGraphic;
  InfImage: TLazIntfImage;
are two different types and you create like this:
Code: [Select]
InfImage := P.CreateIntfImage;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
Re: Raise exception un CreateIntFImage
« Reply #2 on: August 18, 2012, 06:59:55 pm »
Yes Blaazen, it's very strange, but I copy and paste de original function, and I got the same problema.
Also I add the same requiered packaged  %).

But it's doesn't work.

/BLueIcaro

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Raise exception un CreateIntFImage
« Reply #3 on: August 18, 2012, 10:24:20 pm »
Sorry, my mistake. "InfImage := P.CreateIntfImage;" is not a constructor.

I can see that original code is in try..except construction. You should do it too.
Code: [Select]
function LoadglTexImage2DFromPNG(PNGFilename: string; Image: TglTexture
  ): boolean;
var
  png: TPortableNetworkGraphic;
  IntfImg: TLazIntfImage;
  y: Integer;
  x: Integer;
  c: TFPColor;
  p: PByte;
begin
  Result:=false;
  png:=TPortableNetworkGraphic.Create;
  IntfImg:=nil;
  try
    png.LoadFromFile(PNGFilename);
    IntfImg:=png.CreateIntfImage;
    Image.Width:=IntfImg.Width;
    Image.Height:=IntfImg.Height;
    GetMem(Image.Data,Image.Width*Image.Height * 3);
    p:=PByte(Image.Data);
    for y:=0 to IntfImg.Height-1 do begin
      for x:=0 to IntfImg.Width-1 do begin
        c:=IntfImg.Colors[x,y];
        p^:=c.red shr 8;
        inc(p);
        p^:=c.green shr 8;
        inc(p);
        p^:=c.blue shr 8;
        inc(p);
      end;
    end;
  finally
    png.Free;
    IntfImg.Free;
  end;
  Result:=true;
end;             

Unfortunally, this demo does not work here on my laptop.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
Re: Raise exception un CreateIntFImage
« Reply #4 on: August 18, 2012, 11:27:29 pm »
Hi Blaazen, I copied the function and it doesn't work.
But I found something interesting. I was making my test in a Program. I selected a Program from  Proyect/New Proyect, the Program and it's doesn't work.

But If I make a application, it's works find. I don't know why, but this code works
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   IntfGraphics,Gl,FPimage;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.   private
  19.     { private declarations }
  20.   public
  21.     { public declarations }
  22.   end;
  23. TYpe
  24.  
  25. { TglTexture }
  26.  
  27. TglTexture= class
  28.   public
  29.     width ,Height : Longint;
  30.     Data          : Pointer;
  31.     destructor Destroy;Override;
  32.  
  33. end;
  34.  
  35. var
  36. P: TPortableNetworkGraphic;
  37. InfImage: TLazIntfImage;
  38. Textura : TglTexture;
  39. Pb: PByte;
  40. X,Y: Integer;
  41. c : TFpColor;
  42.  
  43. var
  44.   Form1: TForm1;
  45.  
  46. implementation
  47. { TglTexture }
  48.  
  49. destructor TglTexture.Destroy;
  50. begin
  51. Data := Nil;
  52. //Data.Free;
  53. inherited Destroy;
  54. end;
  55. {$R *.lfm}
  56.  
  57. { TForm1 }
  58. function LoadglTexImage2DFromPNG(PNGFilename: string; Image: TglTexture
  59.   ): boolean;
  60. var
  61.   png: TPortableNetworkGraphic;
  62.   IntfImg: TLazIntfImage;
  63.   y: Integer;
  64.   x: Integer;
  65.   c: TFPColor;
  66.   p: PByte;
  67.   Cadena: String;
  68. begin
  69.   Result:=false;
  70.   png:=TPortableNetworkGraphic.Create;
  71.   IntfImg:=nil;
  72.   Cadena := ExpandFileNameUTF8(PNGFilename);
  73.   try
  74.     png.LoadFromFile(ExpandFileNameUTf8 (PNGFilename));
  75.     IntfImg:=png.CreateIntfImage;
  76.     Image.Width:=IntfImg.Width;
  77.     Image.Height:=IntfImg.Height;
  78.     GetMem(Image.Data,Image.Width*Image.Height * 3);
  79.     p:=PByte(Image.Data);
  80.     for y:=0 to IntfImg.Height-1 do begin
  81.       for x:=0 to IntfImg.Width-1 do begin
  82.         c:=IntfImg.Colors[x,y];
  83.         p^:=c.red shr 8;
  84.         inc(p);
  85.         p^:=c.green shr 8;
  86.         inc(p);
  87.         p^:=c.blue shr 8;
  88.         inc(p);
  89.       end;
  90.     end;
  91.   finally
  92.     png.Free;
  93.     IntfImg.Free;
  94.   end;
  95.   Result:=true;
  96. end;
  97.  
  98. procedure TForm1.Button1Click(Sender: TObject);
  99. var
  100.   Tex: TglTexture;
  101. begin
  102.   Tex := TglTexture.Create;
  103.   IF LoadglTexImage2DFromPNG('Texture1.png',Tex) Then
  104.      ShowMessage('si')
  105.   ELSE
  106.     ShowMessage ('No');
  107.   IF Tex <> nil Then
  108.      ShowMessage('No nil')
  109.   ELSE
  110.       ShowMessage('Nil');
  111. end;
  112.  
  113. end.
  114.                                                                                
  115.  

I think there is some importan diference between a Application and a Program.

/BlueIcaro


BlueIcaro

  • Hero Member
  • *****
  • Posts: 834
    • Blog personal
Re: Raise exception un CreateIntFImage
« Reply #5 on: August 18, 2012, 11:35:31 pm »
I don't know why, but if this code doesn't run into a GUI Application (Application with forms) it doesn't work  :-[

/BlueIcaro

 

TinyPortal © 2005-2018