So, I realize you can’t run OpenGL inside of an ofxThread, but I’m having trouble just loading an image with webImageLoader in the threadedFunction. I’m not sure why this would be, but can you not use Poco commands in a thread (i’m using 057)? I’m setting “setUseTexture(false)”. Is there something else I need to do? I just need to load the pixels regularly from an IP camera, then use the pixel values in my main thread.
Thanks,
Matt
Here’s the thread I’m running:
#ifndef _THREADED_OBJECT
#define _THREADED_OBJECT
#include "ofMain.h"
#define OF_ADDON_USING_OFXTHREAD
#include "ofAddons.h"
#include "webImageLoader.h"
// this is not a very exciting example yet
// but ofThread provides the basis for ofNetwork and other
// operations that require threading.
//
// please be careful - threading problems are notoriously hard
// to debug and working with threads can be quite difficult
class threadedObject : public ofxThread{
public:
webImageLoader img;
int count; // threaded fucntions that share data need to use lock (mutex)
// and unlock in order to write to that data
// otherwise it's possible to get crashes.
//
// also no opengl specific stuff will work in a thread...
// threads can't create textures, or draw stuff on the screen
// since opengl is single thread safe
//--------------------------
threadedObject(){
count = 0;
}
void start(){
startThread(true, false); // blocking, verbose
img.setUseTexture(false);
}
void stop(){
stopThread();
}
//--------------------------
void threadedFunction(){
while( isThreadRunning() != 0 ){
if( lock() ){
img.loadFromUrl("[http://images.apple.com/startpage/images/2008/10/promo-macbookpro-20081014.jpg");](http://images.apple.com/startpage/images/2008/10/promo-macbookpro-20081014.jpg");)
count++;
if(count > 50000) count = 0;
unlock();
ofSleepMillis(1 * 1000);
}
}
}
//--------------------------
void draw(){
string str = "I am a slowly increasing thread. \nmy current count is: ";
if( lock() ){
str += ofToString(count);
unlock();
}else{
str = "can't lock!\neither an error\nor the thread has stopped";
}
ofDrawBitmapString(str, 50, 56);
}
};
#endif
And here’s the webImageLoader:
#include "webImageLoader.h"
void webImageLoader::loadFromUrl(string url){
//poco is not happy if we register the factory more than once
if(!factoryLoaded){
HTTPStreamFactory::registerFactory();
factoryLoaded = true;
}
//specify out url and open stream
URI uri(url);
std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri));
//copy to our string
string str;
StreamCopier::copyToString(*pStr.get(), str);
//figure out how many bytes the image is and allocate
int bytesToRead = str.size();
char buff[bytesToRead];
memset(buff, 0, bytesToRead);
//copy the bytes from the string to our buffer
for(int i = 0; i < bytesToRead; i++){
buff[i] = str[i];
}
printf("numBytes copied is %i \n", sizeof(buff));
//if we already have a loaded image clear it
// if(isValid()){
clear();
// }
//create a freeimage memory handle from the buffer address
FIMEMORY *hmem = NULL;
hmem = FreeImage_OpenMemory((BYTE *)buff, bytesToRead);
if (hmem == NULL){ printf("couldn't create memory handle! \n"); return; }
//get the file type!
FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(hmem);
//make the image!!
putBmpIntoPixels(FreeImage_LoadFromMemory(fif, hmem, 0), myPixels);
// bmp = FreeImage_LoadFromMemory(fif, hmem, 0);
//free our memory
FreeImage_CloseMemory(hmem);
if (getBmpFromPixels(myPixels) == NULL){ printf("couldn't create bmp! \n"); return; }
//flip it!
FreeImage_FlipVertical(getBmpFromPixels(myPixels));
if (myPixels.bAllocated == true && bUseTexture == true){
tex.allocate(myPixels.width, myPixels.height, myPixels.glDataType);
}
swapRgb(myPixels);
update();
}