Display an image lazarus / 39-Graphics ---------------------------------------------------------------------- AlanTheBeast · 2016-01-16 15:20 I'm not using any frameworks at all - command line only. I would like to write a simple file viewer that upon me hitting a command key (no issue there) that a particular image (JPG, PNG, GIF, TIF) would be displayed.  Hitting the command key would advance to display the next image.  I can obviously do the keyboard and the files easily enough, but I don't know how to cause an image designated by the filepath/name to display. Over simplistic code follows: 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 Repeat   WaitCommandKey;   DisplayImage (path+filename);    <----- this bit is what I need.   filename := GetNextFilename;Until QuitSig or NoMoreFiles; ---------------------------------------------------------------------- balazsszekely · 2016-01-16 15:59 Display where? TForm/TImage is allowed? ---------------------------------------------------------------------- AlanTheBeast · 2016-01-17 15:41 I don't even know how to answer your question.  I looked up Tform/Timage and I get the sense that the things I need are there.  Bear in mind most things I do do not use windows other than the basic terminal.  I process gobs of data and they output reductions of that data or output kml to load into Google Earth. In the simplest sense I would want to:         Make the entire display black         Then "tell" some procedure to display image file x on the display. From there if you could tell me which procedures/objects I need to use to do that then I'll begin reading there.  At present I'm not sure where to start. So if your reply was in the form of: - first you need a handle to XYZ and initiate object Q. - then you need to initiate ABC - then you pass the handle to QRZ.init - then you pass the filename to QRZ.display Then I'd be on my way to figuring it all out.  Maybe. ---------------------------------------------------------------------- Bart · 2016-01-17 16:05 Display it where? On your console/terminal? Does your terminal support some type of "graphics mode"? On which OS? Take a look at the FPImage and related fpc units to get started. Why not use a GUI program? It would be as simple as Image1.Picture.LoadFromFile('foobar.png'); Bart ---------------------------------------------------------------------- molly · 2016-01-17 16:34 tbh i am also a bit confused, just as Bart is. I understand that OP is not able to answer the questions as OP seems not that familiar/confident about our used terminology. Is OP using lazarus or plain FPC ? e.g. What does commandline mean exactly ? That OP is using the commandline compiler only ? Or does it mean that OP wants to display graphics inside a console/terminal ? FWIW: The latter is about causing yourself pain, even more so when not using an existing framework (if there actually exists one for displaying graphics inside a terminal). In case it might help, if OP wants, i have a ready to go example for lazarus displaying a picture inside a paintbox (loaded through using a button and storing last selected picture filename to automatically load on startup). Going full-screen (or faking that) requires some extra work (most probably depending to the underlying OS). ---------------------------------------------------------------------- AlanTheBeast · 2016-01-17 21:23 Quote from: Bart on January 17, 2016, 05:05:01 pmDisplay it where? On your console/terminal? Does your terminal support some type of "graphics mode"? On which OS? Take a look at the FPImage and related fpc units to get started. Why not use a GUI program? It would be as simple as Image1.Picture.LoadFromFile('foobar.png'); Bart Thanks for your reply.  Indeed I wasn't very forthcoming with info.  I don't use Lazarus or other GUI framework.  I compile with fpc (using Lightweight as an IDE). OS X 10.11 i7 27" iMac NVIDIA GeForce GTX 680MX with 2GB of GDDR5 memory. I'll take a look at FPImage and see what pops ... Thanks again ---------------------------------------------------------------------- Bart · 2016-01-17 22:40 Any reason why you make things so complicated? What you want takes less than 5 minutes to make in Lazarus. (A TImage, a TButton, very few lines of code.) You're on a machine where users expect to have a GUI program anyway. Bart ---------------------------------------------------------------------- molly · 2016-01-17 23:22 Quote from: AlanTheBeast on January 17, 2016, 10:23:41 pmI compile with fpc (using Lightweight as an IDE). fwiw: FPC is able to compile lazarus applications (as well as lazbuild command line compiler). No fancy GUi designer in that case though. QuoteI'll take a look at FPImage and see what pops ... Nothing, other then being able to store an image into memory. Are you at least 'allowed' to use an existing framework (whether written in fpc or not) ? I have no idea what unit graph or ptcpas does under MacOS. In case you intend to directly talk to your videocard -> no go unless using drivers. You would at least require a special framebuffer driver and write a fpc interface for it in order to display graphics that way. For 'generic' linux i spotted project fbida, but no idea if something like that's working for macOS (and would require a rewrite to pascal). ---------------------------------------------------------------------- AlanTheBeast · 2016-01-18 00:52 Quote from: Bart on January 17, 2016, 11:40:09 pmAny reason why you make things so complicated? What you want takes less than 5 minutes to make in Lazarus. (A TImage, a TButton, very few lines of code.) You're on a machine where users expect to have a GUI program anyway. Bart To you complicated, to me simple. Well, I'm the user.  There's not much about processing thousands of data points and coming up with a summary of the statistics of them or other reductions that is magically improved in a GUI over a .txt file or in terminal. I want to do something that I think (hope, believe, wish ...) is simple but I don't want to face the learning curve of Lazarus (I installed it a few years ago and was lost for hours just trying to do something quite simple - a real turn off.  XCode is even worse). If I had some starting point and could get _that_ to compile and run via Lazarus then I could happily go off and do my little project. OTOH, if I can do it from a command line program and thence have my windows pop up with the images I want, I'd be just as happy. Maybe (probably) I just don't understand the scope of what it is I'm attempting to do. But I'll delve into TImage and the other units (?) as soon as I have some free time. Thanks. ---------------------------------------------------------------------- AlanTheBeast · 2016-01-18 00:55 Quote from: molly on January 18, 2016, 12:22:28 amNothing, other then being able to store an image into memory. Are you at least 'allowed' to use an existing framework (whether written in fpc or not) ? I'm allowed to do what I want.  OTOH, I want to avoid making this bigger than need be.  Maybe that's my main problem! Quote from: molly on January 18, 2016, 12:22:28 am I have no idea what unit graph or ptcpas does under MacOS. In case you intend to directly talk to your videocard -> no go unless using drivers. You would at least require a special framebuffer driver and write a fpc interface for it in order to display graphics that way. No - I don't need to do that.  I assume (!) that if there is the right stuff in the right units that it handles talking to the OS graphics department for me. Quote from: molly on January 18, 2016, 12:22:28 amFor 'generic' linux i spotted project fbida, but no idea if something like that's working for macOS (and would require a rewrite to pascal). I'm sure that that's not where I'm heading ... ---------------------------------------------------------------------- bylaardt · 2016-01-18 01:57 Have you install the Lazarus? if not, then install it. What you looking for is there, ready to be used. the component name is TImage. drop it to on the Form and load the image with "LoadFromFile(imagename)". unless this is a homework... ---------------------------------------------------------------------- molly · 2016-01-18 02:34 Quote from: AlanTheBeast on January 18, 2016, 01:55:36 amI'm allowed to do what I want.  OTOH, I want to avoid making this bigger than need be.  Maybe that's my main problem! Well, the smallest executable size for displaying graphics inside a console would be achieved using a framebuffer. There should be one available on the webs that suits your videocard. In terms of small regarding (own written) source-code: please consider using lazarus. QuoteNo - I don't need to do that. Hmz, i wonder how you would know, because... QuoteI assume (!) that if there is the right stuff in the right units that it handles talking to the OS graphics department for me. ... tells me (wrongly?) that you don't know exactly. fwiw: Yes, it is called lazarus which offers units/interfaces to your desktop's GUI by 'communicating' to it using a particular widgetset. But, afaik we are still talking console/terminal mode. Other means of displaying graphics is using frameworks like SDL, OpenGL, PTCPAS etc etc. QuoteI'm sure that that's not where I'm heading ... OK, let me try another route then. Is there anywhere on this planet (read web(-link)): - an image/screenshot of an application for any OS out there that can show us exactly what it is that you envisioned yourself ? - a piece of code (in any given programming language) that would be able to tell which interface to use ? You might perhaps not be familiar with FPC/Lazarus, but i am not familiar (at all) with MacOSX, and right now it's more like a guessing game of trying to figure out what is inside your head ;-) This is how the code for the main form using lazarus could look like (and was faster to create then attempting to figure out what you envidioned  ): 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 unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    OpenDialog1: TOpenDialog;    PaintBox1: TPaintBox;    Panel1: TPanel;    procedure Button1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);    procedure PaintBox1Paint(Sender: TObject);  private    { private declarations }    FPicture  : TPicture;  protected    Function  GetLastFileName: String;    Procedure SetlastFileName(sFilename: String);  public    { public declarations }    procedure OpenPictureFileName(sFilename: String);  procedure OpenPictureDialog;    property  LastFileName: String read GetLastFileName write SetlastFileName;  end; var  Form1: TForm1;   implementation {$R *.lfm} { TForm1 } const  StorageExt = '.LastSelectedPicture';var  StorageFileName : String;  function TForm1.GetLastFileName: String;var  sl              : TStringList;  ThisLastPicture : String;begin  // Make sure StorageFilename actually exist on disk  if FileExists(StorageFilename) then  begin    // Create stringlist.    sl := TStringList.Create;     // Load data stored on disk into stringlist    sl.LoadFromFile(StorageFilename);     // Check if there is at least one entry inside the stringlist    if (sl.Count > 0) then    begin      // Assume one entry, first being last used filename of picture      ThisLastPicture := sl.Strings[0];       // Check if the entry from the stringlist contains a valid (existing)    // filename and return the result      if FileExists(ThisLastPicture)      then Result := ThisLastPicture      else Result := '';    end;     // Free Stringlst resources.    sl.Free;  end;end;  procedure TForm1.SetlastFileName(sFilename: String);var  sl : TStringList;begin  // Create the stringlist.  sl := TStringList.Create;   // Add the given sFilename to the stringlist  sl.Add(sFilename);   // Store the stringlist with lastfilename to disk  sl.SaveToFile(StorageFileName);   // Free stringlist resources.  sl.Free;end;  procedure TForm1.FormCreate(Sender: TObject);var  LastPicture: String;begin  // Construct the picture  FPicture := TPicture.Create;   // Construct filename used for storing text  // This filename/path assumes you are allowed to read/write in your  // application directory. If not then change accordingly.  StorageFileName := ChangeFileExt(Application.ExeName, StorageExt);   // Retrieve the last used picture filename (if any).  LastPicture := LastFileName;   // Only if LastPicture was valid we can load/display it  if (LastPicture <> '') then  begin    OpenPictureFileName(LastPicture);  end;end;  procedure TForm1.FormDestroy(Sender: TObject);begin  FreeAndNil(FPicture);end;  procedure TForm1.PaintBox1Paint(Sender: TObject);begin  // Paint the bitmap whenever the system tells us to  PaintBox1.Canvas.Draw(0, 0, FPicture.Bitmap);end;  procedure TForm1.OpenPictureFileName(sFilename: String);var  sFullFilename : String;  sFilenameOnly : String;begin  sFullFilename := sFilename;   // Construct filename without path to show in a label  sFilenameOnly := extractfilename(sFullFilename);  Label1.Caption := sFilenameOnly;   // Clear picture and load file from disk  FPicture.Clear;  FPicture.LoadFromFile(sFullFilename);   // Prepare paintbox to display picure  Paintbox1.Canvas.Clear;  Paintbox1.Width  := FPicture.Bitmap.Width;  Paintbox1.height := FPicture.Bitmap.Height;   // Invalidate to force drawing of picture  Paintbox1.invalidate;end;  procedure TForm1.OpenPictureDialog;var  sFullFilename: String;begin  // Open a dialog for user to select a file  if OpenDialog1.Execute then  begin    // Retrieve the selected filename    sFullFilename := OpenDialog1.FileName;     // Attempt to open the picture    OpenPictureFilename(sFullFilename);     // Make sure the filename of the last chosen picure is stored to disk.    LastFilename := sFullFilename;  end;end; procedure TForm1.Button1Click(Sender: TObject);begin  // User clicked a button, so act on it.  OpenPictureDialog;end;  Maybe that will give you an idea ? ---------------------------------------------------------------------- Bart · 2016-01-18 14:15 Reading and displaying images is not an easy task, but it can be done. TImage can only be used to display an image if you write a GUI program, so if you want it in a console: that cannot be done. To use graphics in console mode you need to use the graph unit and possibly install libsvga and libsvga-dev. This program, using graph unit, then will try to access hardware directly and needs to be run as root (and on my Linux tryng that simply froze my system). If you can program in Pascal, you can program in Lazarus (it is Pascal!). And yes, compared to freepascal and console, Lazarus has a larger footprint and is more resource hungry, but up until 2 years ago I was able to run it on a 700 Mhz Celeron with 512 Mb memory using Suse 10.0. (If you ask, why can't you run it anymore? Tthe harddrive died on me.) Setting up Lazarus on a recent Mac may be a little tricky, since the debugger (gdb) is no longer installed on new Mac's, so you need to install that seperately and get it code-signed). Once you take this hurdle, you will find your current task (displaying images) so much more easier than in plain console programming, you will never want to revert. And you can use Lazarus to build console programs as well. In that case you just use the many features of the Lazarus IDE (code completion to name just one) and write a plain fpc console program. I write console programs myself, but never use the fp-IDE, only Lazarus. Bart