I have started a binding to the GD graphics library here.
It currently supports only a tiny subset of the functions (mainly drawing primitives) included in this library
and will likely never be complete as it is purely a hobby project.
But as the GD library already exists for a long time, is easy to use and popular in
some fields), someone might still find it useful.
It is not the first Fortran language binding to the GD library, but the other approach I found here seems to be nearly 19 years old and not using iso_c_binding for the language binding.
Now I have added the pixel manipulation tools and very basic font support und basic support for copying areas of images.
Still missing (among others) is the support for Free Type fonts and more complex image manipulation tools (read and write).
Note that this library is not a real plotting tools but aims to be just a simple library binding for drawing things into pixel graphics (currently only *:JPG and *:PNG are supported) and read pixels from them.
The homepage of the binding now contains a list of already supported subroutines/functions.
The library now supports the use of OpenType fonts, so that
the most basic drawing functions are supported and the language
binding is almost usable for drawing.
Basically everything related reading files is still missing, but I am optimistic to
be able to integrate the most important pieces of that functionality
into the language binding soon.
One caveat: I do not think that libGD was ever intended for scuentific image analysis, but rather to generate nice pictures. For example, only 8bit color encoding is supported.
Then five images of a flower: The “normally” colored one is the original, then there is color separation in the red, green and blue channels (done manually via pixel-wise manipulation), and the result of a built-in function for edge detection.
The code for the edge detection is really quite simple (sorry for my somewhat sloppy programmings style without error detection):
program test_edge
use iso_c_binding, only: c_ptr, c_int
use fortran_libgd
implicit none
type(c_ptr) :: im ! image pointer
type(c_ptr) :: output_image ! file pointer
integer(c_int) :: status
im= gdImageCreateFromFile("inpics/flowers.jpg"//c_null_char)
status=gdImageEdgeDetectQuick(im)
output_image =gd_fopen('outpics/flowers_edge.jpg'//c_null_char, 'wb'//c_null_char)
call gdImageJpeg(im, output_image, -1)
status = gd_fclose(output_image)
end program test_edge
gd_fopen and gf_fclose do not exist in libGD, but are just wrappers around fopen and fclose of the C standard library where I have added the prefix gd_ to avoid naming conflicts.
I will push the current version of the language bindings now, but not evertyhing is documented or tested yet.
The newest changes are now committed.
I will likely take care about the repository on weekend…
Do you (as author of the GTK C-binding) have any suggestions
on how I could make the binding more accessible
for people not very experienced in the C - Fortran interface?
I think the simplest way is to give them the basics of iso_c_binding: why they must append //c_null_char when they pass a string, why _c_int, what is c_double, c_ptr, etc. Expose that without assuming they should be familiar with C.
It could be a few paragraphs in the doc, or a short tutorial. You can also add some comments on that subject in the examples (tests) programs. And give them some links toward iso_c_binding stuff (see for example Fortran interoperability with C · vmagnin/gtk-fortran Wiki · GitHub).
Concerning the iso_c_binding and iso_fortran_env intrinsic modules, it is also considered a better practice to state that the intrinsic modules must be used: use, intrinsic :: iso_c_binding
Thank you for bringing this library to the community!
I have now actually been able to do some video manipulation, although some workarounds had been necessary because of the lack of unsigned integer types in Fortran and the reading of ḿultidimensional arrays from pipes, which required mixed C-Fortran programing.
There is still some cleanup and documentation work to do and I want to contact the author of the tutorial first, so the code for the example has not been published to GitHub yet.
No, for a Ryzen 2 System with no parallelization at all of the side of my Fortran code or the C wrapper (gfortran11 -O3 -march=native) rendering the (Full-HD) video takes about 3-4 times the run time.
However, I still consider my code still rather badly optimized.
Note that the approach shown in the blog post is not related to GDlib,
so in principle FFMPEG can be wich each graphics system that can associate
a pixel with a color value.