FreeImage and x64 projects : Yes you can !

A few words about FreeImage

According to the website, FreeImage (http://freeimage.sourceforge.net) is an Open Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today’s multimedia applications.

FreeImage is easy to use, fast, multithreading safe, compatible with all 32-bit versions of Windows, and cross-platform (works both with Linux and Mac OS X).

FreeImage is written in C++ but can be used in .NET by using a wrapper (which is provided on the site in the download section)

Using FreeImage in a .NET project

To use FreeImage with a .NET project, you have to include the native C++ library (FreeImage.dll) in the same folder as the executable or in a folder contained in the PATH variable (“%WINDIR%\System32” can be a good choice).
A good way to include the FreeImage dll in the executable’s folder is to add it to your visual studio projet, and set the Build Action property to “none” and the Copy to Output Directory property to “Copy always”

image

You have to add a reference to the wrapper library too, which is “FreeImageNET.dll”.

Now, using the library is easy.

Add an using directive:

using FreeImageAPI;

Check if the library is available :

if (!FreeImage.IsAvailable())
{
   Console.WriteLine("FreeImage.dll seems to be missing. Aborting");
   return;
}

Do something with your pictures:


FIBITMAP myImage= new FIBITMAP();
myImage= FreeImage.Load( [parameters] );

// some smart actions

Don’t forget to dispose your images when finished to use them

if (!myImage.IsNull
{
   FreeImage.Unload(myImage);
}
myImage.SetNull();

Well, this is nice but it still has a problem with that library (there is always one):

And with a x64 project ?

The executable you build has to be the same architecture as the library. The library is x86 so you can use it for win32 applications. What if you have (for some reasons) to build an x64 application?

In that case, you have to rebuild the library to target X64 architectures.

Here is how to do that with Visual Studio 2010 (I assume that it’s more or less the same procedure with other visual studio versions).

Grab the latest FreeImage sources on  the download site (go to the http://freeimage.sourceforge.net/download.html page and click to the link in the Source Distribution section. Once you have the sources, unzip the archive (obvious but…)

In the FreeImage folder there is a visual studio 2005 and a visual studio 2003 solutions, pick the 2005.

image

Convert the solution

image
image
image
image
image

Notice the compilation configuration

image

By default, Win32 is set : we don’t want that !

Go to the Configuration Manager

image
image

As you can see, all FreeImage libs are set to target the Win32 platform, we want 64 bits !

Add a new solution platform

image

Choose to target the x64 platform

image

Just select x64 then click OK

image

Close the manager

Build the solution (keyboard shortcut : ctrl-shift-B).

image

And at this point you will see that there is a problem, yes, you don’t really think it would be that easy ;-)

image

It seems that the compilation encounters a problem especially with the “LibOpenJpeg” project

So, to be sure, try to compile the library one at the same time

image

We see that it is the LibOpenJPEG the big source of problems :

image

A quick Google search allows to find a post on the Google user group for OpenJPEG that helps:
http://groups.google.com/group/openjpeg/browse_thread/thread/64b89cdb1a44bb3b/fd4db2f74c76db87?lnk=gst&q=x64#fd4db2f74c76db87

The problematic file is the “opj_include.h” wich use a keyword non supported by the x64 architecture (the “_asm” keyword if you want to know. It’s because the c++ compiler does not support inline x64 assembly)

image

We have to change :

/* MSVC does not have lrintf */
#ifdef _MSC_VER
static INLINE long lrintf(float f){
 int i;

 _asm{
 fld f
 fistp i
 };

 return i;
}
#endif

to

/* MSVC does not have lrintf */
#ifdef _MSC_VER
static INLINE long lrintf(float f){
#ifdef _M_X64
	return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f));
#else
	int i;

	_asm{
		fld f
		fistp i
	};

	return i;
#endif
}
#endif

Now compile

image

The project compiles pretty well, there is some remaining warnings . . . but it works.

Now you can use that library (mine can be downloaded at http://www.sambeauvois.be/Demos/FreeImage/x64/FreeImageX64.zip) in your x64 applications

Attention anyway : the library will be slower !

Additional links :

FreeImage download page : http://freeimage.sourceforge.net/download.html
FreeImage API documentation: http://freeimage.sourceforge.net/fnet/Index.html

/* MSVC does not have lrintf */

#ifdef _MSC_VER

static INLINE long lrintf(float f){

#ifdef _M_X64

return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f));

#else

int i;

_asm{

fld f

fistp i

};

return i;

#endif

}

#endif

Incoming search terms:

  • freeimage x64
  • freeimage
  • freeimage net
  • freeimage 64bit
  • freeimagenet
  • freeimage configure 32 bit
  • NULL
  • freeimage vs2010
  • FreeImageNET64 dll
  • 64-bit freeimage dll

About Sam Beauvois

Application Developer, .NET enthusiast since 2004, I'm interested in technology watch, usability, code quality, patterns & practices, UX, ...

17 comments

  1. [...] FreeImage and x64 projects : Yes you can ! – Sam Beauvois – A few words about FreeImage According to the website, FreeImage (http://freeimage.sourceforge.net) is an Open Source library project for developers …… [...]

  2. [...] FreeImage and x64 projects : Yes you can ! – Sam Beauvois – A few words about FreeImage According to the website, FreeImage (http://freeimage.sourceforge.net) is an Open Source library project for developers …… [...]

  3. drlloyd11 says:

    Thank you so freaking much!

  4. fuck says:

    I am in fact grateful to the owner of this web page
    who has shared this impressive article at at this time.

  5. Enrique says:

    Legend, thank you for offering your compiled file. 2 years after you wrote this article it’s the only x64 version I could find, good work.

  6. admin says:

    It was on smashingmagazine.com

  7. news says:

    I love your wordpress theme, where did you get a hold of it?

  8. Anders Gardebring says:

    Thanks a lot! You saved the day :)

  9. Thank you for your brief and useful ideas about installing FreeImage in x64 with vs2010, it really helps me.

    p.s. the recent version provides FreeImage.2008.sln ^_^

  10. Sussch says:

    Microsoft Visual C++ 2010 Express only managed to convert project files for 2003. For some reason it claimed that 2005, 2008 project files were corrupt.

    Thanks to this encouraging blog entry, I decided to try the project file for 2003 before giving up.

  11. N\A says:

    Thanks very much, it worked for me!!!

  12. NA SA says:

    Hi Anybody,

    Please post FreeImage.dll version 3.1.4.1 64bit.

    My computer does not install Visual C.

    Thanks u many

  13. admin says:

    Hi,

    I have no idea why it’s not out of the box and I haven’t contacted them.

    The announcement of August 9th, 2010 for the release 3.14.0 says
    “64-bit portability has been improved”
    (see http://freeimage.sourceforge.net/news.html )

    I didn’t test it, but I’m glad my article helped you :)

  14. Alexander says:

    Great work, Sam, thank you very much!
    Any idea why the project doesn’t deliver this itself? Have you contacted them?

    You saved my day. No, my week. My MONTH. Maybe even… my project. I’ll get back to you.

    Have a great day, evening and weekend!

    Bye
    A.

  15. [...] This post was mentioned on Twitter by Samuel Beauvois. Samuel Beauvois said: New blog post : FreeImage and x64 projects : Yes you can ! http://bit.ly/dwsa66 [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>