# Binding to the GD graphics library

**URL:** <https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167>\
**Category:** Announcements\
**Created:** [August 14, 2022, 8:53pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167 "2022-08-14T20:53:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [August 14, 2022, 8:53pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/1 "2022-08-14T20:53:09Z")

</div>

Hallo,

I have started a binding to the GD graphics library [here](https://github.com/johandweber/fortran-gdlib).  
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](http://nn-online.org/code/gd/) seems to be nearly 19 years old and not using iso\_c\_binding for the language binding.

Cheers,  
Johann

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [September 1, 2022, 6:49pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/2 "2022-09-01T18:49:04Z")

</div>

Small update:

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.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [February 25, 2023, 10:12pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/3 "2023-02-25T22:12:29Z")

</div>

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.

Fortunately, libgd is well-documented.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [February 26, 2023, 9:20pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/4 "2023-02-26T21:20:53Z")

</div>

Now, basic reading and processing of existing image data is supported.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 8, 2023, 7:52am UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/5 "2023-03-08T07:52:42Z")

</div>

I’ve tested it successfully.  
I suggest you post here and in your repository a picture to demonstrate what can be done with that library.

> Now, basic reading and processing of existing image data is supported.

That’s an interesting point because there are other libraries we can use in Fortran for drawing, but not so many for reading/processing.

A final question: is it possible to access directly to the picture in Fortran? (an array?)

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [March 8, 2023, 2:36pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/6 "2023-03-08T14:36:31Z")

</div>

Thanks for your feedback. I will add pictures when I have time.

I am no expert in GD lib, for my current experiments I have inquired the size of the image and read the color values pixel-wise.

I will provide more info later.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 8, 2023, 2:39pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/7 "2023-03-08T14:39:22Z")

</div>

I have seen there are some image filters:  
[https://libgd.github.io/manuals/2.3.3/files/gd\_filter-c.html](https://libgd.github.io/manuals/2.3.3/files/gd_filter-c.html)

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [March 8, 2023, 4:50pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/8 "2023-03-08T16:50:59Z")

</div>

For most of these functions I have already written interfaces, but I have not comitted them and there exist no testcases.

What already exists is a test where an image (a flower) is separated in its r, g and b values.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [March 8, 2023, 4:54pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/9 "2023-03-08T16:54:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [March 8, 2023, 9:26pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/10 "2023-03-08T21:26:13Z")

</div>

Here are some of the output image.

First three demonstrations of drawing primitives and the handling of OpenType Fonts:  
 ![tf_font](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/a086750ee54e9b003e06365158ec4938ac35b904.png)

 ![test_extended](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/1/1f390019856d8527092a8cea01671d16a03e7a01.png)  
 ![polygon](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/0/056dc400ebd908167581a607d50fae8dafc3abff.png)

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.  
 ![flowers](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/6/6884ba680bb337d863667a4bc239393bbbd3f2d8.jpeg)  
 ![flowers_red](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/f/f6112faedcc4d6ce6339693a56af30284c8bf6e4.jpeg)  
 ![flowers_green](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/4/4db7254d9e3025255be9b4c6e720e916794cd5bc.jpeg)  
 ![flowers_blue](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/9/9aedfe3baf2c396ef24d74c40a20b68740708fde.jpeg)  
 ![flowers_edge](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/a/a9126d96f27f452d4537b609216ff3b8e6b877ce.jpeg)

The code for the edge detection is really quite simple (sorry for my somewhat sloppy programmings style without error detection):

```fortran
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.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [March 8, 2023, 9:50pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/11 "2023-03-08T21:50:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 9, 2023, 8:06am UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/12 "2023-03-09T08:06:48Z")

</div>

> [@Jweber](#):
>
> 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](https://github.com/vmagnin/gtk-fortran/wiki/Fortran-interoperability-with-C)).

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 9, 2023, 8:17am UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/13 "2023-03-09T08:17:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [April 15, 2023, 7:06pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/14 "2023-04-15T19:06:56Z")

</div>

Thanks a lot for your helpful hints on how to make the library more accessible  
and the hint to mark intrinsic modules

I have tried to incorporate them into my code and documentation  
and pushed the changes to github.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 15, 2023, 7:17pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/15 "2023-04-15T19:17:45Z")

</div>

Fine, your “images gallery” makes immediately the project more attractive in my opinion.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [June 16, 2023, 2:54pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/16 "2023-06-16T14:54:04Z")

</div>

I have extended the library a bit (wrapper for the popen function to open pipes, reading the the content of the image “object” in an array and vice versa) and played around with pipes and FFMPEG (using the tutorial by Ted Burke at  
[A simple way to read and write audio and video files in C using FFmpeg (part 2: video) | ad hocumentation • n. fast documentation of ideas and solutions.](https://batchloaf.wordpress.com/2017/02/12/a-simple-way-to-read-and-write-audio-and-video-files-in-c-using-ffmpeg-part-2-video/?unapproved=28303&moderation-hash=cb254afd2ee7bfcf07ed2fe1fd0621a4#comment-28303) ).

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.

First results can been seen at [Test video editing with GDLib and FFMPEG. (Warning: Some flickering) - YouTube](https://www.youtube.com/watch?v=QVt_o6hwdp4) .

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.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [June 16, 2023, 3:05pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/17 "2023-06-16T15:05:03Z")

</div>

Impressive!

Was the processing made in real time? Else how long did the preprocessing take?

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [June 16, 2023, 3:30pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/18 "2023-06-16T15:30:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [June 16, 2023, 3:32pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/19 "2023-06-16T15:32:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jweber](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@Jweber](https://fortran-lang.discourse.group/u/Jweber)\
**Post date:** [August 15, 2023, 11:04pm UTC](https://fortran-lang.discourse.group/t/binding-to-the-gd-graphics-library/4167/20 "2023-08-15T23:04:00Z")

</div>

I have now pushed some additional subroutines/functions along with a test program  
and documentation to Github.

I have changed the demo video to be smaller (~ 1 MB, 320X240 pixels).  
Therefore it looks rather ugly, but I did not want to litter my Git repository.

Furthermore the demo program is now simpler.
