How to use the FreeImage DLL?

Just starting out? Need help? Post your questions and find answers here.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

How to use the FreeImage DLL?

Post 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. :oops: 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:

Image

What am I doing wrong? :(
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Re: How to use the FreeImage DLL?

Post 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
Last edited by milan1612 on Sat Sep 26, 2009 2:54 pm, edited 1 time in total.
Windows 7 & PureBasic 4.4
Marco2007
Enthusiast
Enthusiast
Posts: 648
Joined: Tue Jun 12, 2007 10:30 am
Location: not there...

Re: How to use the FreeImage DLL?

Post by Marco2007 »

PureBasic for Windows
User avatar
Demivec
Addict
Addict
Posts: 4257
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to use the FreeImage DLL?

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: How to use the FreeImage DLL?

Post 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? :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Re: How to use the FreeImage DLL?

Post 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...
Windows 7 & PureBasic 4.4
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: How to use the FreeImage DLL?

Post 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. :P

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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Demivec
Addict
Addict
Posts: 4257
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: How to use the FreeImage DLL?

Post 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 
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to use the FreeImage DLL?

Post 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.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: How to use the FreeImage DLL?

Post 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.

Image

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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: How to use the FreeImage DLL?

Post 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.
Last edited by netmaestro on Sun Sep 27, 2009 7:49 pm, edited 2 times in total.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: How to use the FreeImage DLL?

Post by PB »

So where does my GIF go in your code? ;) Thanks for that, netmaestro! :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
applePi
Addict
Addict
Posts: 1404
Joined: Sun Jun 25, 2006 7:28 pm

Re: How to use the FreeImage DLL?

Post 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.
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: How to use the FreeImage DLL?

Post by yrreti »

PB I like your cat :lol:
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.
User avatar
Progi1984
Addict
Addict
Posts: 806
Joined: Fri Feb 25, 2005 1:01 am
Location: France > Rennes
Contact:

Re: How to use the FreeImage DLL?

Post by Progi1984 »

Marco2007 wrote:Progi1984 did something:
http://www.purearea.net/pb/download/dll/RWFreeImage.zip
Marco2007, the version the most updated is here : http://code.google.com/p/rwrappers/sour ... /FreeImage
Post Reply