Is freeimage threadsafe?

BlitzMax Forums/Brucey's Modules/Is freeimage threadsafe?

skn3(Posted 2012) [#1]
Does anyone know the status of freeimage current version:
ModuleInfo "History: 1.07"
ModuleInfo "History: Updated to FreeImage 3.15.1."


is it threadsafe? Or is it able to run in a child thread?

I don't mind if it can only ever run in one child thread but is this going to be a long exercise of crashes?

This demo seems to work:
Framework maxgui.drivers
Import maxgui.xpmanifest
Import brl.max2d
Import brl.linkedlist
Import brl.eventqueue
Import brl.glmax2d
Import brl.timer
Import brl.threads

Import bah.freeimage

SuperStrict

Global Window:TGadget = CreateWindow("Sandbox!",0,0,400,400,Null,WINDOW_CENTER | WINDOW_TITLEBAR | WINDOW_CLIENTCOORDS | WINDOW_RESIZABLE)
Global canvas:TGadget = CreateCanvas(0,0,400,400,Window)
Global timer:TTimer = CreateTimer(60)
SetGadgetLayout(canvas,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED)

Global loading:Int = False
Global loadingCounter:Float = 0.0

Global image:TImage

CreateThread(Thread,"very-large-image.jpg")

Function Thread:Object(data:Object)
	' --- perform the freeimage load ---
	loading = True
	
	Local freeImage:TFreeImage = LoadFreeImage(data)
	'convert to timage
	freeImage = freeImage.rescale(320,320,FILTER_BICUBIC)
	image = LoadImage(freeImage.getPixmap())
	freeImage.Free()
	loading = False
End Function

AddHook(EmitEventHook,EventHook,Null,1)
Function EventHook:Object(id:Int,data:Object,context:Object)
	Local event:TEvent = TEvent(data)
	
	'check valid event
	If event
		Select event.id
			Case EVENT_WINDOWCLOSE
				End
				
			Case EVENT_TIMERTICK
				RedrawGadget(canvas)
				
			Case EVENT_GADGETPAINT
				Select event.source
					Case canvas
						'prepare canvas
						Local Width:Int = canvas.ClientWidth()
						Local Height:Int = canvas.ClientHeight()
						SetGraphics(canvas.CanvasGraphics())
						SetVirtualResolution(Width,Height)
						SetViewport(0,0,Width,Height)
						
						'background
						SetClsColor(0,0,0)
						Cls()
						
						If loading = True
							loadingCounter :+ 4
							SetBlend(ALPHABLEND)
							SetAlpha(Abs(Cos(loadingCounter)))
							DrawText("Loading",20,20)
							SetAlpha(1.0)
						Else
							DrawImage(image,20,20)
						EndIf
						
						'flip canvas
						Flip()
				End Select
		End Select
	EndIf
	
	'return
	Return data
EndFunction

Repeat
	WaitEvent()
Forever


but its not a complex example so Id imagine less prone to random memory crashes!

any ideas?

Last edited 2012


Brucey(Posted 2012) [#2]
The latest version of FreeImage is apparently more threadsafe… specifically relating to some singleton initialisation when it starts up.
The wrapper doesn't use any globals/statics, so you should be good to go.


skn3(Posted 2012) [#3]
Cheers :)) good to see you back on the forums!