Recent

Author Topic: Is it possible to display a png using fpimage and using ptcgraph?  (Read 707 times)

TBMan

  • Sr. Member
  • ****
  • Posts: 277
Is it possible to display a png using fpimage and using ptcgraph?
« on: September 02, 2025, 07:27:17 pm »
I found something, but it isn't working. Any help would be appreciated.

Code: Pascal  [Select][+][-]
  1. program bwtest;
  2. uses ptcgraph,fpimage,fpreadpng,fpcanvas, fpimgcanv,ptccrt;
  3. var
  4.   gm,gd:smallint;
  5.   r,c,
  6.   x,y:integer;
  7.   myImage: TFPMemoryImage;
  8.   reader : TFPCustomImageReader;
  9.   canvas : tfpcustomcanvas;
  10.  
  11.   color:word;
  12.    colors:tfpcolor;
  13.  
  14.  
  15.    function RGBToInteger(R, G, B: Byte): Integer;
  16.    begin
  17.      Result := (Integer(B) shl 16) or (Integer(G) shl 8) or Integer(R);
  18.    end;
  19.  
  20.  
  21.  
  22. begin
  23.  
  24.   gd := vesa;
  25.     Gm := installusermode(800, 600, 256, 2, 10000, 8000);
  26.     WindowTitle := 'BW Design';
  27.     initgraph(gd, gm, '');
  28.     myImage := TFPMemoryImage.Create(400, 400); // larger than file dimensions
  29.     Reader := TFPReaderPNG.create;
  30.     canvas := tfpimagecanvas.create(myimage);
  31.     try
  32.       myImage.LoadFromFile('qc.png',reader);
  33.     except
  34.       // Handle file not found or other load errors
  35.       WriteLn('Error loading PNG file');
  36.     end;
  37.     x := 100; y := 100;
  38.   for r := 0 to myImage.Height - 1 do
  39.   begin
  40.     for c := 0 to myImage.Width - 1 do
  41.     begin
  42.       color := rgbtointeger(canvas.colors[c,r].red, canvas.colors[c,r].green,canvas.colors[c,r].blue);
  43.       PutPixel(x+c, y+r,color);
  44.  
  45.     end;
  46.  
  47.   end;
  48.  
  49.  
  50.  
  51.     repeat until keypressed;
  52. closegraph;
  53.  
  54. end.
  55.  
  56.  
  57.  
  58.  
I love programming.

Some things I've done using PTCgraph:

NFL Retro Football (almost finished):
https://www.youtube.com/watch?v=78mTtsd7ppk


Solitaire games:
https://www.youtube.com/watch?v=zmtxI7FdWuQ&list=PLa4BPpFl34iVhFwX1JZwVm3vE5ay_i3R2

Thausand

  • Sr. Member
  • ****
  • Posts: 392
Re: Is it possible to display a png using fpimage and using ptcgraph?
« Reply #1 on: September 03, 2025, 04:54:37 am »
Yes, can work display png fpimage use ptcgraph but have caveat.

You example have ptcgraph 256 color. png can have many more color and not fit (can, if quantize color but then need palette from you) . If png 256 color then can always make work because png also can have palette.

If use png fpimage TFpColor then color 64-bit (4 x word, is not usual because usual is 32-bit) and need scale down. If image have palette then ptcgraph need use setpalette (https://www.freepascal.org/daily/packages/graph/ptcgraph/setpalette.html).

Can not make easy example because not know what is colors "qc.png" and not know how convert color. Maybe can tell or give (example) picture ?


PS: this simple very example 8-bit palette and 64-bit color convert for ptcgraph.

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}{$h+}
  4.  
  5. uses
  6.   {$ifdef linux}cthreads,{$endif}
  7.   sysutils,ptcgraph,ptccrt,
  8.   fpimage,fpreadpng;
  9.  
  10. type
  11.   TColorType=packed record
  12.     case byte of
  13.       0:(r,g,b,a:byte);
  14.       1:(value:longword);
  15.   end;
  16.  
  17. function fpColorToColorType(fpColor:TFPColor):TColorType;
  18. begin
  19.   result.value:=0;
  20.   result.r:=(fpColor.Red   shr 8) and $00ff;
  21.   result.g:=(fpColor.Green shr 8) and $00ff;
  22.   result.b:=(fpColor.Blue  shr 8) and $00ff;
  23. end;
  24.  
  25. procedure start;
  26. var
  27.   gm,gd:smallint;
  28. begin
  29.   gd:=vesa;
  30.   gm:=installusermode(800,600,256,2,10000,8000);
  31.   WindowTitle:='BW Design';
  32.   initgraph(gd,gm,'');
  33. end;
  34.  
  35. procedure stop;
  36. begin
  37.   repeat until keypressed;
  38.   closegraph;
  39. end;
  40.  
  41. procedure load256ColorPng(Filename:string);
  42. var
  43.   Image:TFPMemoryImage;
  44.   ColorType:TColorType;
  45.   x,y,w,h:integer;
  46.   index:integer;
  47.   Color:integer;
  48. begin
  49.   if FileExists(Filename) then
  50.   begin
  51.     Image:=TFPMemoryImage.Create(0,0);
  52.     try
  53.       // have palette load
  54.       Image.UsePalette:=true;
  55.       Image.LoadFromFile(Filename);
  56.  
  57.       // make palette color image copy to ptcpas
  58.       for index:=0 to Image.Palette.Count-1 do
  59.       begin
  60.         colorType:=fpColorToColorType(Image.Palette[Index]);
  61.         SetRGBPalette(index,ColorType.r,ColorType.g,ColorType.b);
  62.       end;
  63.  
  64.       // make pixel image copy to ptcpas
  65.       x:=100; y:=100;
  66.       for w:=0 to Image.Height-1 do
  67.       begin
  68.         for h:=0 to Image.Width-1 do
  69.         begin
  70.           Color:=Image.Pixels[w,h];
  71.           PutPixel(x+w,y+h,Color);
  72.         end;
  73.       end;
  74.     except
  75.       // Handle file not found or other load errors
  76.       WriteLn('Error loading PNG file');
  77.     end;
  78.   end
  79.   else writeln(Filename,' not exist!');
  80. end;
  81.  
  82. begin
  83.   start;
  84.   // picture: https://bp0.blogger.com/_5Z1NzpeMhcs/Rut53DRL_DI/AAAAAAAAAdI/XxZbdMkDS1I/s1600-h/color_palette_256_deluxepaint.png
  85.   load256ColorPng('color_palette_256_deluxepaint.png');
  86.   stop;
  87. end.
  88.  
« Last Edit: September 03, 2025, 05:52:35 am by Thausand »

TBMan

  • Sr. Member
  • ****
  • Posts: 277
Re: Is it possible to display a png using fpimage and using ptcgraph?
« Reply #2 on: September 03, 2025, 03:12:46 pm »
Thanks!
I love programming.

Some things I've done using PTCgraph:

NFL Retro Football (almost finished):
https://www.youtube.com/watch?v=78mTtsd7ppk


Solitaire games:
https://www.youtube.com/watch?v=zmtxI7FdWuQ&list=PLa4BPpFl34iVhFwX1JZwVm3vE5ay_i3R2

Thausand

  • Sr. Member
  • ****
  • Posts: 392
Re: Is it possible to display a png using fpimage and using ptcgraph?
« Reply #3 on: September 03, 2025, 11:10:29 pm »
You welcome TBMan  :)

I tell and sorry, I have make copy-paste you code then adjust and (you and me) forget to have free Image so have leak memory. Please make add Image.Free to have fix.

TBMan

  • Sr. Member
  • ****
  • Posts: 277
Re: Is it possible to display a png using fpimage and using ptcgraph?
« Reply #4 on: September 04, 2025, 03:58:18 am »
Thanks again, much appreciated!
I love programming.

Some things I've done using PTCgraph:

NFL Retro Football (almost finished):
https://www.youtube.com/watch?v=78mTtsd7ppk


Solitaire games:
https://www.youtube.com/watch?v=zmtxI7FdWuQ&list=PLa4BPpFl34iVhFwX1JZwVm3vE5ay_i3R2

 

TinyPortal © 2005-2018