Hey All,
I have been watching this thread with interest:
http://forum.openframeworks.cc/t/loading-ofimage-from-url-using-poco/562/0
I grabbed madparker’s latest code and it worked fine for me too, with OF057 upwards.
What I want to do now is create a threaded version that can load from a URL and not slow the rest of the program.
My attempt is below and builds with the:
00573-Xcode-Fat-YCAM-WithGui
From:
http://www.openframeworks.cc/files/
webImageLoader.h:
/*
* webImageLoader.h
* openFrameworks
*
* Created by Joel Gethin Lewis on 31/10/2008.
* Totally based on:
* [http://forum.openframeworks.cc/t/loading-ofimage-from-url-using-poco/562/0](http://forum.openframeworks.cc/t/loading-ofimage-from-url-using-poco/562/0)
webImageLoader.cpp:
/*
* webImageLoader.cpp
* openFrameworks
*
* Created by Joel Gethin Lewis on 31/10/2008.
* Totally based on:
* [http://forum.openframeworks.cc/t/loading-ofimage-from-url-using-poco/562/0](http://forum.openframeworks.cc/t/loading-ofimage-from-url-using-poco/562/0)
testApp.h:
#ifndef _TEST_APP
#define _TEST_APP
#include "ofMain.h"
// this h file has the definition for an object that uses the threaded addon:
#include "threadedObject.h"
#include "Poco/URIStreamOpener.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/XML/XMLString.h"
#include "Poco/DOM/DOMParser.h"
#include "Poco/DOM/Document.h"
#include "Poco/DOM/NodeIterator.h"
#include "Poco/DOM/NodeFilter.h"
#include "Poco/DOM/NamedNodeMap.h"
#include <memory>
#include <iostream>
#include <fstream>
#include <vector>
using Poco::URIStreamOpener;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;
using Poco::Net::HTTPStreamFactory;
static bool factoryLoaded = false;
class testApp : public ofSimpleApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased();
// callback events ----------------------------------------------------------
void onThreadedStreamReceived(const void* pSender, string& response);
private:
threadedObject TO;
int mainAppsCount;
};
#endif
testApp.cpp:
#include "testApp.h"
#include "stdio.h"
bool locked = false;
//--------------------------------------------------------------
void testApp::setup(){
TO.threadedStreamReady += Delegate<testApp, string>(this, &testApp::onThreadedStreamReceived);
//poco is not happy if we register the factory more than once
if(!factoryLoaded){
HTTPStreamFactory::registerFactory();
factoryLoaded = true;
}
mainAppsCount = 0;
TO.start();
}
//--------------------------------------------------------------
void testApp::update(){
ofBackground(0,0,0); // black because threads are EVIL ;)
mainAppsCount++;
}
//--------------------------------------------------------------
void testApp::draw(){
ofSetColor(0xffffff);
//TO.draw();
string str = "I am a the main opengl thread.\nmy current count is: ";
str += ofToString(mainAppsCount);
ofDrawBitmapString(str, 350, 56);
//ofSetColor(0xff0033);
if(TO.webImageLoaded)
{
cout << "Image loaded!!!!" << endl;
TO.theWebImage.draw(0,0);
}
else
{
cout << "Image not loaded... yet" << endl;
}
}
//--------------------------------------------------------------
void testApp::keyPressed (int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(){
}
void testApp::onThreadedStreamReceived(const void* pSender, string& response) {
cout << "Image loaded" << endl;
string resp = response;
cout << resp << endl;
}
threadedObject.h:
#ifndef _THREADED_OBJECT
#define _THREADED_OBJECT
#include "ofMain.h"
#define OF_ADDON_USING_OFXTHREAD
#include "ofAddons.h"
#include "webImageLoader.h"
// event stuff -------------------------------------
#include "Poco/BasicEvent.h"
#include "Poco/Delegate.h"
using Poco::BasicEvent;
using Poco::Delegate;
// 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:
threadedObject();
void start();
void stop();
void threadedFunction();
//void draw();
int count;
BasicEvent<string> threadedStreamReady;
bool webImageLoaded;
webImageLoader theWebImage;
};
#endif
threadedObject.cpp:
/*
* threadedObject.cpp
* openFrameworks
*
* Created by Joel Gethin Lewis on 02/11/2008.
* Based on the ofxThread example
*
*/
#include "threadedObject.h"
//--------------------------
threadedObject::threadedObject(){
count = 0;
webImageLoaded = false;
}
void threadedObject::start(){
if (isThreadRunning() == false){
printf("thread started\n");
startThread(true,false);
}
}
void threadedObject::stop(){
stopThread();
}
//--------------------------
void threadedObject::threadedFunction(){
while(isThreadRunning())
{
webImageLoaded = false;
theWebImage.loadFromUrl("[http://cache.gawker.com/assets/images/io9/2008/11/ironman1.jpg");](http://cache.gawker.com/assets/images/io9/2008/11/ironman1.jpg");)
string response = "Image loaded";
threadedStreamReady.notify(this, response);
webImageLoaded = true;
stop();
}
}
Ideally, I’d like to be able to display progress of the load, or at least monitor it - but a system that notifies when the load is complete and the image is available to view for now is fine.
I have been reading some other posts about threading:
“threaded image loader”
http://forum.openframeworks.cc/t/threaded-image-loader/816/0
Do I have the classic “OpenGL is not thread safe” problem because I am trying to save to a texture in the threaded part of the code?
Jesus, did you get any closer? It seems we have very similar aims in mind.