Displaying an image on screen

On the Net I find an abundance of software with which a shape, graph, or whatever, can be displayed in a graphic view box. However I would like to be able to display an existing image as is in a mandatory format, in colour or in black and white shades, that is on file freely available on the same computer but elsewhere, from within a Fortran program that is in the process of being written as a parameter input to some existing module. Does anyone know of such display software? Hopefully this narrative contains the required detail to enable my question to be answerable by an esteemed contributor, cognisant of solutions to this sort of need. Many thanks in advance.

That is a rather open-ended question :slight_smile: Could you explain what you want to do exactly? FIles containing pictures can have any of a few dozen (or a few thousand) formats (I did not actually count them :slight_smile:). That is the first hurdle. A second one is: do you want to display them from within a Fortran program? A third one: do you want to produce these picture files yourself or do they already exist? And there are more questions to be answered.

So, can you elaborate a bit on your question?

As I understand the question, you have some data in a Fortran program and you want to display it as an image in a window. The are Fortran bindings for SDL (see the image example) and GTK. Both are able to do this task, however, I don’t know which one will be the easier approach.

1 Like

With respect to following line I get the subsequent compier remark

use, intrinsic :: iso_c_binding, only: c_associated, c_null_char, c_ptr


That should not happen unless you are running a very old version of gfortran that does not support the iso_c_binding module. The code

use, intrinsic :: iso_c_binding, only: c_associated, c_null_char, c_ptr

compiles for me, on a PC where gfortran --version says GNU Fortran (GCC) 13.0.0 20221218 (experimental)

My comprehension (sorry if I misinterpreted your need) is that you want to launch a viewer program to show on screen an image file that already exist somewhere in your filesystem. A very simple first step could be to use the intrinsic Fortran subroutine execute_command_line:

program main
  implicit none

  call execute_command_line("gimp /home/vmagnin/Images/pulse.png")

end program main

Here I am on a Linux Ubuntu system. gimp is the name of the command to open the Gimp program, and /home/vmagnin/Images/pulse.png is the full path in my system to a PNG image named pulse.png. With that command, my Fortran program can easily call (with one line) that external program to display that image.

Of course, you should replace gimp by the name of an image viewer that is available on your system, and if you are in a Windows environment, replace the path by a valid Windows path.

To adapt it to your system, the first step would be to successfully execute that command directly in your terminal window. Then put it in your Fortran program, as the first parameter of execute_command_line.

I don’t want to show data as a window, I would like to display an image and project data onto that image. Thus the image would be the backdrop of a meaningful user interface.

I cannot answer Beliavsky because his post has no reply option. However the Gfortan responds to his code suggesting with following:

Fatal Error: Cannot open module file ‘sdl2.mod’ for reading at (1): No such file or directory
compilation terminated.

If you talk about the image.f90 SDL2 example cited by @milancurcic , you need first to build the whole library:

after having installed its dependencies.

VMAGNIN I cannot reply to your post due to the absence of such option;

In response of your suggestion that I could simply code
. . . . .users\pader\ . . . . .\ image
That indeed shows the image, but blasts everything else away: so I cannot overlay that image with text: it thus renders that image useless, as a backdrop for a user interface.

1 Like

Please see the other examples in the repository on how to render text with SDL 2.0. As @Arjen has asked already: could you describe your project in more detail? Maybe, the gtk-fortran bindings are something more sensible for what you want to achieve.

1 Like

The questions Arjen asked me have been attended to. There isn’t much more that I can add, to describe what I am trying to do. The solution interkosmos alludes to, may well harbour the information that I seek. However my current knowledge of Fortran is insufficient to follow kosmos’ route. So I suggest I further develop my Fortran knowledge before returning to the issue at hand.

1 Like

So, you just want to place text on a backdrop, nothing more? Because, if your goal instead is to create some kind of graphical user interface with clickable text/buttons, you should better pick a toolkit like GTK + Fortran bindings.

1 Like

There are many (and I mean many) ways to do image creation, manipulation, rendering, and saving in files. Trust me, I could go on for ages and I wouldn’t cover them all. I will just mention the ways I consider the best ones, in no particular order.

  • You will need an image creation/manipulation library, at the very least. There any many libraries for this, but after quite some research, I ended up with FreeImage, in my opinion the best library for this task. You can create images and save them in many image formats. The image I posted in this thread was actually created with a Fortran program using my bindings for FreeImage. Now, if you want to display that image - either on-the-fly or once it is fully created - you will need also need OpenGL, together with a library to create windows with OpenGL contexts and user interaction. GLFW is the most advanced library I am aware for this task. I use my own Fortran bindings for all the above.
    This solution is the most complicated, but also the one that provides the most functionality and flexibility - which you may or may not need.
  • The SDL API is a great solution. @vmagnin and @milancurcic already mentioned existing Fortran bindings for SDL2, but for many reasons I use my own Fortran library for this. My implementation provides (almost) complete Fortran bindings for SDL2, together with a higher-level API to facilitate programming in SDL2. You can watch a demo of my library doing image manipulation with effects here. Whichever you will use should work for what you want.
    SDL is a way simpler solution, compared to the one above, but has less functionality. Unless you want really fancy effects, it is much more than enough for what you want to do though.
  • GTK with cairo and friends is also a great solution, and there are well-maintained Fortran bindings for this, namely gtk-fortran. You need to have a decent knowledge of the GTK4 API for that - which I am still learning, so I can’t comment much. From what I know so far though, this seems to be of intermediate difficulty programming-wise, and has the advantage you won’t need bindings for this and that, they are all-in-one.
  • A way overlooked library for what you want to do (and much more) is MGRX. It is a well-written library, and I had no issues to port it in Fortran. It is also well-maintained and regularly updated. Some time ago I uploaded a demo of image manipulation in Fortran using MGRX in a long-time-obsolete computer.
    I will rate this solution as the simplest one, programming-wise. MGRX API is clean and simple (simpler than SDL’s,) at the cost of slightly slower performance - but you probably won’t even notice that. Afterall, I can do image animations in a “potato computer” using MGRX.

Like I said, there are many other solutions as well. The one I would very much wanted to include in the list is EGGX/ProCall, because it has its own “native” Fortran API. Unfortunately, I can’t recommend it for what you want to do, because its image rendering is bare-bones basic.


Pap, what you write is exceptional and very impressive. What I do when I study a new thing, eg a new IT language, is I make a manual for myself and refer to that when I get to implementing. I only go back to the literature when there are knowledge gaps. Your text, for the time being, is a chapter in my manual. I would like to thank you very much for it!! I will use it as a guide for when I get there. Patrick de Ridder.