Know when image has loaded?


I got a problem with ofImage loadImage(). I want to load alot of image after eachother, but if i got a ofImage object and call loadImage on it, i’m not guaranteed that it’s loaded before it continues. So if i call many loadImage just after eachother it continues showing the first image in my ofImage.draw(), (until i stop loading images, then it finaly show the newest image). Is there a way where i can be sure that it doesn’t start loading a new image before the old when has shown up?

hmm. to the best of my knowledge loadImage() isn’t multi-threaded, so it will block until the image has loaded.

perhaps you’re doing something else to give you this strange behaviour. can you post the code that does the loading, and the code that does the drawing?


I got one function that does the loading

void testApp::loadImg(float xin, float yin){  
	float dist = 1;  
	XML.pushTag("IMAG", 0);  
	int numTags = XML.getNumTags("IMAGES:ID");  
	float x, y;  
	string uri;  
	int id;  
	for(int u=0; u<numTags ; u++){  
		XML.pushTag("IMAGES", u);  
		x = XML.getValue("X",0.0,0);  
		y = XML.getValue("Y",0.0,0);  
		if(sqrt((xin-x)*(xin-x) + (yin-y)*(yin-y)) < dist){  
			dist = sqrt((xin-x)*(xin-x) + (yin-y)*(yin-y));  
			uri = XML.getValue("URI", "",0);  
			id = XML.getValue("ID", 0,0);  
	if(id != curId){  
		//Load the image  
		curId = id;  
void testApp::mouseMoved(int x, int y ){  

it takes a x and y coordinate, and finds the closest image in a XML file. The draw function looks like this:

void testApp::draw(){  
	glTranslated(ofGetWidth(), 0, 0);  
	glRotated(90, 0, 0, 1);  
	images[0].draw(0,0, ofGetHeight(), ofGetWidth());  

i can see that it correctly calls the load image. But it simply doesnt update the ofImage texture before its done loading the last image. And i want it to show images when im moving the mouse aswell (doesnt have to be all images, but atleast it should load a image when the last one is done)…

hmm. my first guess would be that there’s something funky going in with the maths, and actually it’s not loading the right one.

can you try adding

printf(“loading %s\n”, uri.c_str() );

just after the comment //Load the image

and see what you get as a printout?

It does print correct uris out. So when i move my mouse, it prints alot of uris out (i got alot of images). And when i stop it shows that last uri it has printed. But all the others don’t get shown. But if i move my mouse very slowly it does load the images (and show them). So it must have something to do with not being finished loading before beginning on a new load.

perhaps ofImage needs 1- or 2- updates for the loaded data to propagate through to the actual drawn image?

to test this, try only calling loadImage every second or third time mouseMoved is called. if this solves it, try having two images and swapping between them (load from one but draw from the other, swap each frame), or three images and rotating between them.

there’s another (better) way to do this, though, which is to load in all the images at setup time, and only switch which one you’re drawing at runtime. loading at runtime is always going to lead to performance problems…

perhaps ofImage needs 1- or 2- updates for the loaded data to propagate through to the actual drawn image?

I really doubt this - (more accurate with quicktime, though, where it’s internally threaded). freeImage calls are blocking for sure. I wonder if there isn’t a problem with doing opengl operations in the mouse events (although I am 99.9% sure these are ok). I do think damian’s solution (better) is better :slight_smile:

Can you post a zip of the src/data folder that shows the problem?


I solved the problem. The problem is only present if i call loadImg directly from mouseMoved. When i debug, it does call the function (it prints in the console), but if i put a print it to the monitor with a font in the draw method, it does not write it out (which means that draw is’nt called). But by moving the loadImg to the update function it does draw every frame.

So by some way the order of function calling gets screwed up when i call my load image every frame from the mouseMoved!

but if i put a print it to the monitor with a font in the draw method, it does not write it out (which means that draw is’nt called).

note - the main app “draw” function clears the screen at the start, so if you do draw in the mouse function, it will get over written and you wont see anything… you should only draw in “draw” :slight_smile:

  • zach

I did not draw in mouseMoved. I just defined a string that was drawn in draw, and loading the new image. Actually very strange that it doesnt work.

edit : sorry! I misread that…

can you upload the code? sounds very curious to me…


I have made a simplified version of my program that only does the loading. And it writes it to the console when it loads a new image, and to the screen (in the draw method). When pressing i key it switches the mode of loading, so its pretty easy to see the difference (at least on my macbook with Mac OS X 10.5)

You can download it from here:

And by the way, i originaly made this in processing, but i had huge problems loading image fast enough (i think it took one second per image almost). And here in oF it can load the same images extremely fast! Thumbs up!