ofImage won't load an image unless inside testApp

I can’t seem to get an image to load using loadImage unless it’s in testApp. For example, I have a class ‘brain’ with a single instance in testApp and try to load an image bain.cpp:

  
  
bool bLoaded = brainImage.loadImage("circle.png");  
cout << bLoaded << endl; //false if not in testApp.cpp  
  

It seems to be finding the image since ofImage.cpp in the loadImageIntoPixels() function is able to find .png for fif (file type), however FreeImage_Load(fif, fileName.c_str(), 0); fails to load and create the bmp.

Has anyone else seen this? The imageload example works and I can load an image into my own project if I put it in testApp. Any other class and it fails to load.

I’ve tried manually putting in the file location directly into FreeImage_Load, but I can’t seem to get it working. I don’t know if its a bug or an error on my part.

Hi,

it should definitely work in any class, not just testApp. Just to check the obvious: you’re declaring

  
ofImage brainImage;  

in the header file for your specific class each time, right?

And your other classes are in the same src folder as the testApp - just to eliminate any (relative) path differences…?

Yup, I define the ofImage in the header and then loadImage() in the constructor just like the loadImage example. I’m pretty clueless on this one since everything seems to be correct and I find it odd that it’ll load in testapp but nothing else and that it’s clearly finding the image since it knows it’s a png, but freeimage won’t load it.

I’ve tried in 3 different classes and even using the example png(s). None of them are loading it though. All the classes are in the src folder and the images in the data folder. I’m trying to add the images to an already working application which is running fine and compiling correctly already.

Ok after further inspection, it seems the loadImage didn’t like being called in the constructor. So I moved it to a setup() function called after it’s constructed. That seems to work.

Good to hear it’s working - odd reason though, I only know constructors don’t have a return type, didn’t know of other restrictions - maybe it was the sequence of constructing/initialising that caused it…

are you sure FreeImage_Load is failing? i guess the problem should be that when the constructor is called the opengl context is not created yet. if you have your brain class as a variable in your testApp the constructor will be called at the very beginning of the application when you create the testApp in main. in the desktop versions it should work, but it seems in iphone the opengl context is not initialized at that point so the texture cannot be created yet

Yeah, I think that was exactly the issue. I moved things to a setup() instead which is probably better practice anyway for what I was trying to do.

Well, after 3 years I’m having the same issue.

I tried to load images after constructor, inside constructor… but nothing changes.

[ error ] ofImage: loadImage(): couldn't load image from "of_data/canvas1/canvas1.png"

this file are located at:

bin/data/of_data/canvas1/canvas1.png 

my folder is organized like this:
project folder

-bin
--data
---www
---of_data
----canvas1
-----canvas1.png
-src
-testApp.cpp //here, everything is fine
--canvas1
---canvas1.cpp //that i'm trying to load an image
--canvas2

those folders inside of src are group related.

Any ideas?

EDIT

this was working perfectly using OF for mac. I’m just porting the code from mac to ios.

you shouldn’t be loading images in the constructor, in some platforms it might work but in mobile mostly the GL context is not guaranteed to be created until setup is called

1 Like