Page 1 of 2
How to use the FreeImage DLL?
Posted: Sat Sep 26, 2009 2:38 pm
by PB
I'm trying to use the FreeImage DLL (found at
http://freeimage.sourceforge.net) to load some images to display. I've never called a third-party DLL before so I can't even get it to initialise.

Here's all I have so far:
Code: Select all
lib=OpenLibrary(#PB_Any,"FreeImage.dll")
If lib And OpenWindow(0,200,200,640,480,"FreeImage DLL test",#PB_Window_SystemMenu)
ImageGadget(0,10,10,600,400,0,#PB_Image_Border)
Debug CallFunction(lib,"FreeImage_Initialise") ; Returns 0.
Repeat
ev=WaitWindowEvent()
Until ev=#PB_Event_CloseWindow
EndIf
As you can see, the Debug Output shows 0 so the lib is obviously loaded and ready, but calling it doesn't work.
Here's what the manual says for it:
What am I doing wrong?

Re: How to use the FreeImage DLL?
Posted: Sat Sep 26, 2009 2:49 pm
by milan1612
I downloaded the DLL and looked at it with Dependency Walker. The function your looking
for exports as "_FreeImage_Initialise@4", so you have to call it like this:
Code: Select all
CallFunction(lib,"_FreeImage_Initialise@4")
EDIT: Seems like progi1984 already made a wrapper for Purebasic. See here:
URL
EDIT2: Here's the direct URL to the SVN trunk:
http://code.google.com/p/rwrappers/sour ... /FreeImage
Re: How to use the FreeImage DLL?
Posted: Sat Sep 26, 2009 2:53 pm
by Marco2007
Re: How to use the FreeImage DLL?
Posted: Sat Sep 26, 2009 2:59 pm
by Demivec
@PB: According to the documentation you posted it looks like you don't need to call FreeImage_Initilise if you are using the DLL.
You would only need to need to do that if you had created a static linked version of the library.
Re: How to use the FreeImage DLL?
Posted: Sat Sep 26, 2009 3:05 pm
by PB
Thanks for replying guys. I prefer not to use wrappers. I want to keep my app small and clean without the need to import libs and so on. I just want to call the DLL clean by itself. Anyone still interested in helping now, with an example?

Re: How to use the FreeImage DLL?
Posted: Sat Sep 26, 2009 3:26 pm
by milan1612
PB wrote:Thanks for replying guys. I prefer not to use wrappers. I want to keep my app small and clean without the need to import libs and so on. I just want to call the DLL clean by itself. Anyone still interested in helping now, with an example?

It's not a complete wrapper. Did you even look at the sources? :roll:
The import library that is in the download package of FreeImage does exactly what you're
trying to do manually. Progi's wrapper utilises the import library to access the functions inside
of the DLL. It makes them available in Purebasic - nothing else...
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 2:29 am
by PB
> It's not a complete wrapper. Did you even look at the sources? :roll:
Of course I did. Don't roll your eyes at me.
> Progi's wrapper utilises the import library to access the functions
And that's what I want to avoid. I don't want to use both the DLL and LIB files to make it work, and neither do I want to use IncludeFiles like the examples do. Just the DLL. It makes for a good learning experience, since I need to know how to use other DLLs too that don't have LIB files with them. Importing and all that jazz is too much, and that is what the wrapper does (proof: remove the LIB file and the samples don't work anymore). I've looked at the sources so I know that to be true.
I thought PureBasic could just use OpenLibrary to access a DLL, and then use CallFunction to use the DLL's commands. Tell me I'm wrong. Therefore, how would I do that with FreeImage.dll alone?
Just to show that I've been trying and am not asking for a total handout of help, here's my work so far:
Code: Select all
Enumeration -1
#FIF_UNKNOWN
#FIF_BMP
#FIF_ICO
#FIF_JPEG
#FIF_JNG
#FIF_KOALA
#FIF_LBM
#FIF_MNG
#FIF_PBM
#FIF_PBMRAW
#FIF_PCD
#FIF_PCX
#FIF_PGM
#FIF_PGMRAW
#FIF_PNG
#FIF_PPM
#FIF_PPMRAW
#FIF_RAS
#FIF_TARGA
#FIF_TIFF
#FIF_WBMP
#FIF_PSD
#FIF_CUT
#FIF_XBM
#FIF_XPM
#FIF_DDS
#FIF_GIF
#FIF_HDR
#FIF_FAXG
#FIF_SGI
#FIF_EXR
#FIF_J2K
#FIF_JP2
EndEnumeration
#FIF_IFF = #FIF_LBM
lib=OpenLibrary(#PB_Any,"FreeImage.dll")
If lib And OpenWindow(0,200,200,640,480,"FreeImage DLL test",#PB_Window_SystemMenu)
ImageGadget(0,10,10,600,400,0,#PB_Image_Border)
CallFunction(lib,"_FreeImage_Initialise@4") ; Works.
CallFunction(lib,"_FreeImage_Load@12",#FIF_GIF,"Waterfall.gif") ; Fails.
Repeat
ev=WaitWindowEvent()
Until ev=#PB_Event_CloseWindow
EndIf
When I run this, I get a "Number expected instead of string" error, but I don't know why. The manual seems to imply that I'm passing the correct data types for the CallFunction line, so I'm stumped.
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 2:39 am
by Demivec
CallFunction() and CallFunctionFast() were changed to only take integers as parameters, as of v4.40b1. You can use prototypes to handle other parameter types with only a little bit more work.
I don't have the library installed, but something like this (untested) code should work:
Code: Select all
;define the prototype structure a.k.a. it's function parameters including defaults and return type. Pseudotypes are usable too.
Prototype pf_load(fif, filename.s, flags = 0)
;create a variable with the prototype structure and assign a function's address
FreeImage_load.pf_load = GetFunction(0, "_FreeImage_Load@12")
;call the function via the variable 'FreeImage_load', notice that it uses the default parameter we set up
If FreeImage_Load
FreeImage_Load(#FIF_GIF,"Waterfall.gif")
EndIf
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 4:52 am
by netmaestro
If you're using Imports, you'll only need the .lib file to compile. The released .exe only needs the .dll to run. I recommend the wrapper from progi, it truly is the easiest way to use this dll. Otherwise you're doing a lot more coding than you need to. But - horses for courses, in the end whatever you're most comfortable with is the right way to go.
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 6:13 am
by PB
> I recommend the wrapper from progi, it truly is the easiest way to use this dll
Okay, but I don't understand any of it and I'm concerned about two things:
(1) Do I need to distribute both FreeImage.dll and FreeImage.lib with my app?
(2) Do I need to IncludeFile RW_FreeImage_Inc.pb and RW_FreeImage_Res.pb in my source?
All I really want is to load GIFs for my app (but possibly other images later) using the smallest code possible. Surely there's a smaller way without having to manage someone else's code in my own code?
Anyway, here's my latest attempt at doing it solo. No GIF image is shown in the ImageGadget. I'm 99% sure I'm just one step away from doing it myself. Can anyone help? If not, can someone show how to do the below using progi's wrapper code then? I'm really lost here.
@Fred/Freak: Please, will we ever see a UseGIFImageDecoder() command? The license requirement expired long ago.
Code: Select all
Enumeration -1
#FIF_UNKNOWN
#FIF_BMP
#FIF_ICO
#FIF_JPEG
#FIF_JNG
#FIF_KOALA
#FIF_LBM
#FIF_MNG
#FIF_PBM
#FIF_PBMRAW
#FIF_PCD
#FIF_PCX
#FIF_PGM
#FIF_PGMRAW
#FIF_PNG
#FIF_PPM
#FIF_PPMRAW
#FIF_RAS
#FIF_TARGA
#FIF_TIFF
#FIF_WBMP
#FIF_PSD
#FIF_CUT
#FIF_XBM
#FIF_XPM
#FIF_DDS
#FIF_GIF
#FIF_HDR
#FIF_FAXG
#FIF_SGI
#FIF_EXR
#FIF_J2K
#FIF_JP2
EndEnumeration
#FIF_IFF = #FIF_LBM
lib=OpenLibrary(#PB_Any,"FreeImage.dll")
If lib And OpenWindow(0,200,200,640,480,"FreeImage DLL test",#PB_Window_SystemMenu)
ImageGadget(0,10,10,600,400,0,#PB_Image_Border)
Prototype pf_load(fif,filename.s,flags=0) ; No idea what this does. :)
FreeImage_load.pf_load=GetFunction(lib,"_FreeImage_Load@12")
a=FreeImage_Load(#FIF_GIF,"Waterfall.gif")
SetGadgetState(0,a) ; No image seen. :(
Repeat
ev=WaitWindowEvent()
Until ev=#PB_Event_CloseWindow
EndIf
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 7:14 am
by netmaestro
OK, self-contained, no includes or libs just the dll. Put your GIF filename in and give it a go:
Code: Select all
filename$ = "??????" ; < =========== YOUR GIF FILENAME HERE =======
#FIF_GIF = 25
lib=OpenLibrary(#PB_Any,"FreeImage.dll")
Prototype FreeImage_Load(fif.i,filename.s,flags.l=0)
Prototype FreeImage_Unload(dib.i)
Prototype FreeImage_GetInfoHeader(dib.i)
Prototype FreeImage_GetBits(dib.i)
Prototype FreeImage_GetInfo(dib.i)
Global FreeImage_Load.FreeImage_Load=GetFunction(lib,"_FreeImage_Load@12")
Global FreeImage_Unload.FreeImage_Unload=GetFunction(lib,"_FreeImage_Unload@4")
Global FreeImage_GetInfoHeader.FreeImage_GetInfoHeader = GetFunction(lib, "_FreeImage_GetInfoHeader@4")
Global FreeImage_GetBits.FreeImage_GetBits = GetFunction(lib, "_FreeImage_GetBits@4")
Global FreeImage_GetInfo.FreeImage_GetInfo = GetFunction(lib, "_FreeImage_GetInfo@4")
If lib And OpenWindow(0,200,200,640,480,"FreeImage DLL test",#PB_Window_SystemMenu)
ImageGadget(0,10,10,0,0,0,#PB_Image_Border)
dib=FreeImage_Load(#FIF_GIF,filename$)
hdc = GetDC_(#Null)
bitmap = CreateDIBitmap_(hdc, FreeImage_GetInfoHeader(dib), #CBM_INIT, FreeImage_GetBits(dib), FreeImage_GetInfo(dib), #DIB_RGB_COLORS)
FreeImage_Unload(dib)
CloseLibrary(lib)
ReleaseDC_(#Null, hdc)
SetGadgetState(0,bitmap)
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
FreeImage_Load only loads a DIB, so you have to build the bitmap using a few more functions. It's pretty straightforward, just not quite as simple as FreeImage_Load loading a complete, ready-to-use image.
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 9:29 am
by PB
So where does my GIF go in your code?

Thanks for that, netmaestro!

Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 11:05 am
by applePi
is it possible to display animated gifs, i have tried netmaestro code with this animated gif,
http://img22.imageshack.us/img22/5043/cockroach.gif
but it displayed a static picture.
may be the webcontrol can display the animated gifs.
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 1:15 pm
by yrreti
PB I like your cat
Real serious and cute.
I saved from FireFox as a gif, which isn't a real gif.
I opened the file in paint.
Then I saved as a gif. (slight loss of detail, but true gif)
I then used netmaestro's code, and it works great.
Re: How to use the FreeImage DLL?
Posted: Sun Sep 27, 2009 6:50 pm
by Progi1984