Tutorials

The FreeImage library in Linux

As there have been and still are problems using the FreeImage library with Andorra in Linux, I decided to write this small tutorial, which describes the basic process of compiling, installing and using this free graphics library and avoids possible sources of trouble.

-1. What is FreeImage?
FreeImage is an open source and free of charge graphics library with which many different graphics formats can be loaded and displayed. A complete list of the supported formats can be found at http://freeimage.sourceforge.net/features.html.

0. Determining whether FreeImage is in the packages
In some Linux distributions it is possible that the library is already in the package sources. This renders the manual compiling unnecessary and saves a lot of time.
However, I am quite sure that it does not exist neither in Debian and Ubuntu nor in SUSE Linux or Arch Linux as a precompiled package. Should it exist in your distribution nevertheless, you can skip the following steps until "Using FreeImage in Andorra"

1. Downloading FreeImage
Go to http://freeimage.sourceforge.net/download.html and download the source package (source distribution). A ZIP file named "FreeImage3100.zip" is created. The 3100 here is the version. At the moment of writing this document, 3.10.0 was the most recent version. Even if there is a more recent version available, the process described here should be applicable without further ado, only the commands containing the version number have to be adjusted.

2. Unpacking the archive
Unpack the archive either with a graphical decompressor or via command line using:

Delphi-Code:
$ unzip FreeImage3100.zip

and browse to the created directory "FreeImage".

Delphi-Code:
$ cd FreeImage

3. Compiling FreeImage
Because FreeImage does not require any configuration, it should be possible to compile it directly. To do that, just call "make" in the command line in the directory "FreeImage":
$ make
Now it's time for a small coffee break while the library is being translated. Normally you should get an output similar to the following after a few minutes showing that compiling has been completed successfully:

Delphi-Code:
cp *.a Dist
cp *.so Dist
cp Source/FreeImage.h Dist
make[1]: Leaving directory `/some/directory/FreeImage'

Should an error message be displayed instead, something went wrong. This can have several, often possibly highly diverse, reasons. In this case, try to get information about the problem from the internet or write directly to the Free Image developing team. You can also write an e-mail to me. You can find the address at the end of the tutorial.

4. Installing FreeImage
If everything went well until now, you can install the library by calling

Delphi-Code:
$ make install

in the FreeImage directory with root privileges. After that, a file named "libfreeimage.so" should have been created in /usr/lib.

5. Using FreeImage in Andorra
Now we get to the most exciting part of the tutorial: Including the library in Andorra. Thanks to modular interface system, this is very simple, a corresponding wrapper is also available already. All you have to do is to include the unit "AdFreeImage.pas" in your main unit. From now on, it is tried to load graphics files you load, with an image list, for instance, with the FreeImage library.


6. Troubleshooting

Unfortunately, errors occur, which is why I try to address the most common errors here.

"Wenn I try to load a graphics file with FreeImage, my programme crashes with the error 'SIGSEGV'."
This error is often an indication of a wrong call convention. Unfortunately, there are obviously differences between the FreeImage versions, and as a consequence of that, both the names of the supplied functions and the call conventions are partially different. Because I don't know the reasons for this yet, some compiler switches have been added to the wrapper to change the call convention as easily as possible. This switch is located in the unit "FreeImage.pas" in the subdirectory "lib" in the Andorra directory, it is called "FIcdecl" and is active in non-Windows systems by default. When it is activated with {$define Ficdecl}, the convention "cdecl" is used instead of "stdcall". Should the same error still occur, please do not hesitate and send me an e-mail.

"Wenn I try to compile the project, I get the error 'undefined reference to 'FreeImage_Initialise@4' or something like that."
This error also has to do something with the library's different versions mentioned above. Try to activate or deactivate the compiler switch "FINoPrefix" in the unit "FreeImage.pas". Should that not help, please send me an e-mail.

"When compiling, I get the error 'Fatal: Can't find unit AdPNGUtils used by AdFreeImage'"
This is apparently a bug of Lazarus or FreePascal that only occurred in my case. Should the same error happen in your case, try deleting the entry "AdPngUtils" in the "uses" clause of the "AdFreeImage" unit and adding it again and pay attention to the capitalisation. After that, it should compile.

7. If there are any more open questions...
do not hesitate and send me an e-mail to nwyderka at stud.informatik.uni-goettingen.de or take a look into the Delphi-PRAXiS at www.delphipraxis.net. We're happy to help you.


This page was generated with the help of the following PHP-Scripts: GeSHi a free PHP Syntax highlighter and StringParser_BBCode a free BBCode Parser.