Recent

Author Topic: Display an image  (Read 7652 times)

AlanTheBeast

  • Jr. Member
  • **
  • Posts: 73
Display an image
« on: January 16, 2016, 04:20:19 pm »
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][+][-]
  1. Repeat
  2.    WaitCommandKey;
  3.    DisplayImage (path+filename);    <----- this bit is what I need.
  4.    filename := GetNextFilename;
  5. Until QuitSig or NoMoreFiles;
  6.  

GetMem

  • Hero Member
  • *****
  • Posts: 3782
Re: Display an image
« Reply #1 on: January 16, 2016, 04:59:04 pm »
Display where? TForm/TImage is allowed?

AlanTheBeast

  • Jr. Member
  • **
  • Posts: 73
Re: Display an image
« Reply #2 on: January 17, 2016, 04:41:50 pm »
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

  • Hero Member
  • *****
  • Posts: 4030
    • Bart en Mariska's Webstek
Re: Display an image
« Reply #3 on: January 17, 2016, 05:05:01 pm »
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

  • Hero Member
  • *****
  • Posts: 2345
Re: Display an image
« Reply #4 on: January 17, 2016, 05:34:20 pm »
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

  • Jr. Member
  • **
  • Posts: 73
Re: Display an image
« Reply #5 on: January 17, 2016, 10:23:41 pm »
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

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

  • Hero Member
  • *****
  • Posts: 4030
    • Bart en Mariska's Webstek
Re: Display an image
« Reply #6 on: January 17, 2016, 11:40:09 pm »
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

  • Hero Member
  • *****
  • Posts: 2345
Re: Display an image
« Reply #7 on: January 18, 2016, 12:22:28 am »
I 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.

Quote
I'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).
« Last Edit: January 18, 2016, 12:24:43 am by molly »

AlanTheBeast

  • Jr. Member
  • **
  • Posts: 73
Re: Display an image
« Reply #8 on: January 18, 2016, 01:52:27 am »
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

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

  • Jr. Member
  • **
  • Posts: 73
Re: Display an image
« Reply #9 on: January 18, 2016, 01:55:36 am »
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'm allowed to do what I want.  OTOH, I want to avoid making this bigger than need be.  Maybe that's my main problem!


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.

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).
I'm sure that that's not where I'm heading ...

bylaardt

  • Sr. Member
  • ****
  • Posts: 308
Re: Display an image
« Reply #10 on: January 18, 2016, 02:57:11 am »
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

  • Hero Member
  • *****
  • Posts: 2345
Re: Display an image
« Reply #11 on: January 18, 2016, 03:34:02 am »
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!
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.

Quote
No - I don't need to do that.
Hmz, i wonder how you would know, because...

Quote
I 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.

Quote
I'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  ;D):
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, ExtCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Label1: TLabel;
  18.     OpenDialog1: TOpenDialog;
  19.     PaintBox1: TPaintBox;
  20.     Panel1: TPanel;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormDestroy(Sender: TObject);
  24.     procedure PaintBox1Paint(Sender: TObject);
  25.   private
  26.     { private declarations }
  27.     FPicture  : TPicture;
  28.   protected
  29.     Function  GetLastFileName: String;
  30.     Procedure SetlastFileName(sFilename: String);
  31.   public
  32.     { public declarations }
  33.     procedure OpenPictureFileName(sFilename: String);
  34.     procedure OpenPictureDialog;
  35.     property  LastFileName: String read GetLastFileName write SetlastFileName;
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.  
  41.  
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. { TForm1 }
  48.  
  49. const
  50.   StorageExt = '.LastSelectedPicture';
  51. var
  52.   StorageFileName : String;
  53.  
  54.  
  55. function TForm1.GetLastFileName: String;
  56. var
  57.   sl              : TStringList;
  58.   ThisLastPicture : String;
  59. begin
  60.   // Make sure StorageFilename actually exist on disk
  61.   if FileExists(StorageFilename) then
  62.   begin
  63.     // Create stringlist.
  64.     sl := TStringList.Create;
  65.  
  66.     // Load data stored on disk into stringlist
  67.     sl.LoadFromFile(StorageFilename);
  68.  
  69.     // Check if there is at least one entry inside the stringlist
  70.     if (sl.Count > 0) then
  71.     begin
  72.       // Assume one entry, first being last used filename of picture
  73.       ThisLastPicture := sl.Strings[0];
  74.  
  75.       // Check if the entry from the stringlist contains a valid (existing)
  76.       // filename and return the result
  77.       if FileExists(ThisLastPicture)
  78.       then Result := ThisLastPicture
  79.       else Result := '';
  80.     end;
  81.  
  82.     // Free Stringlst resources.
  83.     sl.Free;
  84.   end;
  85. end;
  86.  
  87.  
  88. procedure TForm1.SetlastFileName(sFilename: String);
  89. var
  90.   sl : TStringList;
  91. begin
  92.   // Create the stringlist.
  93.   sl := TStringList.Create;
  94.  
  95.   // Add the given sFilename to the stringlist
  96.   sl.Add(sFilename);
  97.  
  98.   // Store the stringlist with lastfilename to disk
  99.   sl.SaveToFile(StorageFileName);
  100.  
  101.   // Free stringlist resources.
  102.   sl.Free;
  103. end;
  104.  
  105.  
  106. procedure TForm1.FormCreate(Sender: TObject);
  107. var
  108.   LastPicture: String;
  109. begin
  110.   // Construct the picture
  111.   FPicture := TPicture.Create;
  112.  
  113.   // Construct filename used for storing text
  114.   // This filename/path assumes you are allowed to read/write in your
  115.   // application directory. If not then change accordingly.
  116.   StorageFileName := ChangeFileExt(Application.ExeName, StorageExt);
  117.  
  118.   // Retrieve the last used picture filename (if any).
  119.   LastPicture := LastFileName;
  120.  
  121.   // Only if LastPicture was valid we can load/display it
  122.   if (LastPicture <> '') then
  123.   begin
  124.     OpenPictureFileName(LastPicture);
  125.   end;
  126. end;
  127.  
  128.  
  129. procedure TForm1.FormDestroy(Sender: TObject);
  130. begin
  131.   FreeAndNil(FPicture);
  132. end;
  133.  
  134.  
  135. procedure TForm1.PaintBox1Paint(Sender: TObject);
  136. begin
  137.   // Paint the bitmap whenever the system tells us to
  138.   PaintBox1.Canvas.Draw(0, 0, FPicture.Bitmap);
  139. end;
  140.  
  141.  
  142. procedure TForm1.OpenPictureFileName(sFilename: String);
  143. var
  144.   sFullFilename : String;
  145.   sFilenameOnly : String;
  146. begin
  147.   sFullFilename := sFilename;
  148.  
  149.   // Construct filename without path to show in a label
  150.   sFilenameOnly := extractfilename(sFullFilename);
  151.   Label1.Caption := sFilenameOnly;
  152.  
  153.   // Clear picture and load file from disk
  154.   FPicture.Clear;
  155.   FPicture.LoadFromFile(sFullFilename);
  156.  
  157.   // Prepare paintbox to display picure
  158.   Paintbox1.Canvas.Clear;
  159.   Paintbox1.Width  := FPicture.Bitmap.Width;
  160.   Paintbox1.height := FPicture.Bitmap.Height;
  161.  
  162.   // Invalidate to force drawing of picture
  163.   Paintbox1.invalidate;
  164. end;
  165.  
  166.  
  167. procedure TForm1.OpenPictureDialog;
  168. var
  169.   sFullFilename: String;
  170. begin
  171.   // Open a dialog for user to select a file
  172.   if OpenDialog1.Execute then
  173.   begin
  174.     // Retrieve the selected filename
  175.     sFullFilename := OpenDialog1.FileName;
  176.  
  177.     // Attempt to open the picture
  178.     OpenPictureFilename(sFullFilename);
  179.  
  180.     // Make sure the filename of the last chosen picure is stored to disk.
  181.     LastFilename := sFullFilename;
  182.   end;
  183. end;
  184.  
  185. procedure TForm1.Button1Click(Sender: TObject);
  186. begin
  187.   // User clicked a button, so act on it.
  188.   OpenPictureDialog;
  189. end;
  190.  

Maybe that will give you an idea ?

Bart

  • Hero Member
  • *****
  • Posts: 4030
    • Bart en Mariska's Webstek
Re: Display an image
« Reply #12 on: January 18, 2016, 03:15:29 pm »
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

 

TinyPortal © 2005-2018