Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-04

Easy graphics

9 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alan Mead

What is the easiest way to create PNG or JPG images from within a FPC program? Ideally, in a cross-platform way although I mostly use Linux. Thanks!

-Alan

__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25� http://photos.yahoo.com/ph/print_splash

Alan Mead
to Alan Mead

I just asked about a way to create images. Then I saw the png unit and I got it to link.

But now I'm lost trying to translate the libpng C examples to Pascal. Does anyone have an example or two of a program that uses unit png?

-Alan

__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25� http://photos.yahoo.com/ph/print_splash

Michael Van Canneyt
to Alan Mead: What is the easiest way to create PNG or JPG images from within a FPC program? Ideally,…
Alan Mead wrote
What is the easiest way to create PNG or JPG images from within a FPC program? Ideally, in a cross-platform way although I mostly use Linux. Thanks!

Very easy.

Roughly like this:

uses fpimage,fpcanvas,fpwritepng, fpwritejpg;
Var
  Image : TMemoryImage;
  Canvas : TFPimageCanvas;
begin
  // Create image,
  Image:=TMemoryImage.Create(640,480);
  Image.PaletteBased:=False;
  // Create canvas.
  Canvas:=TFPImageCanvas.Create(Image);
  Canvas.Pen.Color:=colRed;
  Canvas.Circle(100,100,50);
  Canvas.Rectangle(50,50,150,150);
  // Free canvas
  Canvas.Free;
  Image.SaveToFile('myfile.png'); // Write as png
  Image.SaveToFile('myfile.jpg'); // Alternatively, write as jpg
  // Free Image.
  Image.Free;
end.

See the fcl/image directory for all needed units.
Supported are bmp,png,jpg,xpm and PPM/PBM.

There is no documentation yet, but I can provide some examples and explanations. This is fully cross-platform.

Michael.

Alan Mead
to Michael Van Canneyt
Michael Van Canneyt wrote 14 lines
Very easy. Roughly like this: uses fpimage,fpcanvas,fpwritepng, fpwritejpg; [...] See the fcl/image directory for all needed units. Supported are bmp,png,jpg,xpm and PPM/PBM. There is no documentation yet, but I can provide some examples and explanations. This is fully cross-platform.

Michael,

This looks great but I'm currently using 1.0.10 and I don't see these units. Do I need to upgrade to a newer version?

-Alan

__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25� http://photos.yahoo.com/ph/print_splash

Michael Van Canneyt
to Alan Mead
Alan Mead wrote 21 lines
Michael Van Canneyt wrote: > Very easy. > > Roughly like this: > > uses fpimage,fpcanvas,fpwritepng, fpwritejpg; > > [...] > > See the fcl/image directory for all needed units. > Supported are bmp,png,jpg,xpm and PPM/PBM. > > There is no documentation yet, but I can provide some examples and > explanations. > This is fully cross-platform. Michael, This looks great but I'm currently using 1.0.10 and I don't see these units. Do I need to upgrade to a newer version?

Yes. You need at least 1.9.2.
The best would even be to download the latest CVS, as it supports more formats. Or you can try to download the FCL sources and recompile them with FPC 1.0.10.

Michael.

Show quoted text 16 lines
-Alan __________________________________ Do you Yahoo!? Yahoo! Photos: High-quality 4x6 digital prints for 25� http://photos.yahoo.com/ph/print_splash _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Alan Mead
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
Yes. You need at least 1.9.2. The best would even be to download the latest CVS, as it supports more formats. Or you can try to download the FCL sources and recompile them with FPC 1.0.10.

I appreciate your help with this. I installed 1.9.2 on my other machine and I see most of the units (the writejpg is not found)... you indicated that your code was only a rough sketch but I cannot get it to compile:

 amead]$ cat j.pas
program j;
uses fpimage,fpcanvas,fpwritepng;
Var
  Image : TMemoryImage;
  Canvas : TFPimageCanvas;
begin
  Image := TMemoryImage.Create(640,480);
  Image.PaletteBased:=False;
  Canvas:=TFPImageCanvas.Create(Image);
  Canvas.Pen.Color:=colRed;
  Canvas.Circle(100,100,50);
  Canvas.Rectangle(50,50,150,150);
  Canvas.Free;
  Image.SaveToFile('myfile.png');
  Image.Free;
end.
 amead]$ fpc j.pas
Free Pascal Compiler version 1.9.2 [2004/01/07] for i386
Copyright (c) 1993-2002 by Florian Klaempfl
Target OS: Linux for i386
Compiling j.pas
j.pas(7,11) Error: Identifier not found "TMemoryImage"
j.pas(7,23) Error: Error in type definition
j.pas(8,12) Error: Identifier not found "TFPimageCanvas"
j.pas(8,26) Error: Error in type definition
j.pas(11,12) Error: Identifier not found "TMemoryImage"
j.pas(11,31) Fatal: Syntax error, ";" expected but "(" found

-Alan

__________________________________
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25� http://photos.yahoo.com/ph/print_splash

Marco van de Voort
to Alan Mead

Some grepping revealed:

Target OS: Linux for i386 Compiling j.pas j.pas(7,11) Error: Identifier not found "TMemoryImage"

Make it: tfpmemoryimage (2x)

j.pas(7,23) Error: Error in type definition j.pas(8,12) Error: Identifier not found "TFPimageCanvas"

Include unit fpimgcanv

Michael Van Canneyt
to Alan Mead: I appreciate your help with this. I installed 1.9.2 on my other machine and I see most…
Alan Mead wrote 12 lines
Michael Van Canneyt wrote: > Yes. You need at least 1.9.2. > The best would even be to download the latest CVS, as it supports > more formats. > Or you can try to download the FCL sources and recompile them with > FPC 1.0.10. I appreciate your help with this. I installed 1.9.2 on my other machine and I see most of the units (the writejpg is not found)... you indicated that your code was only a rough sketch but I cannot get it to compile:

Here is some code from an existing CGI project:

{$mode objfpc}
{$h+}
unit utests;
interface
uses db,Classes,ftFont,fpimage,fpimgcanv,fpWritePng,fpcanvas;
Procedure TTestSuite.CreateRunPie;
Var
  I : TFPMemoryImage;
  M : TMemoryStream;
begin
  ftFont.InitEngine;
  FontMgr.SearchPath:='/usr/lib/X11/fonts/truetype';
  I:=TFPMemoryImage.Create(320,320);
  try
    If FRunCount=0 Then
      Raise Exception.Create('Invalid parameters passed to script: No total count');
    DoDrawPie(I,FRunSkipCount,FRunFailedCount,FRunCount);
    M:=TMemoryStream.Create;
    Try
      With TFPWriterPNG.Create do
        try
          UseAlpha:=True;
          ImageWrite(M,I);
        Finally
          Free;
        end;
      ContentType:='image/png';
      EmitContentType;
      M.Position:=0;
      Response.CopyFrom(M,M.Size);
    Finally
      M.Free;
    end;
  Finally
    I.Free;
  end;
end;
Procedure TTestSuite.DoDrawPie(Img : TFPCustomImage; Skipped,Failed,Total : Integer);
Var
  Cnv : TFPImageCanvas;
  W,H,FH,CR,ra : Integer;
  A1,A2,FR,SR,PR : Double;
  R : TRect;
  F : TFreeTypeFont;
  Procedure AddPie(X,Y,R : Integer; AStart,AStop : Double; Col : TFPColor);
  Var
    DX,Dy : Integer;
  begin
    DX:=Round(R*Cos(A1));
    DY:=Round(R*Sin(A1));
    Cnv.Line(X,Y,X+DX,Y-DY);
    DX:=Round(Ra*Cos(A2));
    DY:=Round(Ra*Sin(A2));
    Cnv.Line(X,Y,X+DX,Y-Dy);
    DX:=Round(R/2*Cos((A1+A2)/2));
    DY:=Round(R/2*Sin((A1+A2)/2));
    Cnv.Brush.Color:=Col;
    Cnv.FloodFill(X+DX,Y-DY);
  end;
  Function FractionAngle(F,T : Integer): Double;
  begin
    Result:=(2*Pi*(F/T))
  end;
begin
  F:=TFreeTypeFont.Create;
  With F do
    begin
    Name:='arial';
    FontIndex:=0;
    Size:=12;
    Color:=colred;
    AntiAliased:=False;
    Resolution:=96;
    end;
  Cnv:=TFPImageCanvas.Create(Img);
  W:=Img.Width;
  H:=Img.Height;
  cnv.Brush.Style:=bsSolid;
  cnv.Brush.Color:=colTransparent;
  cnv.Pen.Color:=colWhite;
  Cnv.Rectangle(0,0,W,H);
  Cnv.Font:=F;
  FH:=CNV.GetTextHeight('A');
  If FH=0 then
    FH:=14; // 3 * 14;
  Inc(FH,3);
  R.Top:=FH*4;
  R.Left:=0;
  R.Bottom:=H;
  CR:=H-(FH*4);
  If W>CR then
    R.Right:=CR
  else
    R.Right:=W;
  Ra:=CR div 2;
  Cnv.Pen.Color:=colBlack;
  cnv.brush.color:=colRed;
  Cnv.Ellipse(R);
  cnv.font.Color:=colred;
  Inc(FH,4);
  FR:=Failed/Total;
  SR:=Skipped/Total;
  PR:=1-(FR+SR);
  Cnv.Textout(1,FH,Format('%d Failed (%3.1f%%)',[Failed,Fr*100]));
  cnv.font.Color:=colYellow;
  Cnv.Textout(1,FH*2,Format('%d Skipped (%3.1f%%)',[Skipped,SR*100]));
  A1:=(Pi*2*(failed/total));
  A2:=A1+(Pi*2*(Skipped/Total));
  AddPie(Ra,R.Top+Ra,Ra,A1,A2,ColYellow);
  cnv.font.Color:=colGreen;
  A1:=A2;
  A2:=A1+(Pi*2*((Total-(Skipped+Failed))/Total));
  Cnv.Textout(1,FH*3,Format('%d Passed (%3.1f%%',[Total-Skipped-Failed,PR*100]));
  AddPie(Ra,R.Top+Ra,Ra,A1,A2,ColGreen);
end;
end.
Marco van de Voort
to Michael Van Canneyt
Show quoted text 14 lines
On Thu, 22 Apr 2004, Alan Mead wrote: > Michael Van Canneyt wrote: > > > Yes. You need at least 1.9.2. > > The best would even be to download the latest CVS, as it supports > > more formats. > > Or you can try to download the FCL sources and recompile them with > > FPC 1.0.10. > > I appreciate your help with this. I installed 1.9.2 on my other > machine and I see most of the units (the writejpg is not found)... > you indicated that your code was only a rough sketch but I cannot get > it to compile:

I just patched the sketch:

uses fpimage,fpcanvas,fpwritepng, fpwritejpeg,fpimgcanv;
Var
  Image : TFPMemoryImage;
  Canvas : TFPimageCanvas;
begin
  // Create image,
  Image:=TFPMemoryImage.Create(640,480);
  Image.UsePalette:=False;
  // Create canvas.
  Canvas:=TFPImageCanvas.Create(Image);
  Canvas.Pen.Color:=colRed;
  Canvas.Ellipse(50,50,150,150);
  Canvas.Rectangle(50,50,150,150);
  // Free canvas
  Canvas.Free;
  Image.SaveToFile('myfile.png',TFPWriterPNG.Create); // Write as png
  Image.SaveToFile('myfile.jpg',TFPWriterPNG.Create); // Alternatively, write as jpg
  // Free Image.
  Image.Free;
end.