diff options
| -rw-r--r-- | offsetProject/addons.make | 1 | ||||
| -rw-r--r-- | offsetProject/src/imageStore.cpp | 8 | ||||
| -rw-r--r-- | offsetProject/src/imageStore.h | 142 | ||||
| -rw-r--r-- | offsetProject/src/main.cpp | 2 | ||||
| -rw-r--r-- | offsetProject/src/ofApp.cpp | 29 | ||||
| -rw-r--r-- | offsetProject/src/ofApp.h | 7 |
6 files changed, 169 insertions, 20 deletions
diff --git a/offsetProject/addons.make b/offsetProject/addons.make index ab4486b..05d5c04 100644 --- a/offsetProject/addons.make +++ b/offsetProject/addons.make @@ -1,2 +1,3 @@ ofxKinect ofxOpenCv +ofxJSON diff --git a/offsetProject/src/imageStore.cpp b/offsetProject/src/imageStore.cpp new file mode 100644 index 0000000..da6241d --- /dev/null +++ b/offsetProject/src/imageStore.cpp @@ -0,0 +1,8 @@ +#include "imageStore.h" + +long ofToLong(const string& intString) { + long x = 0; + istringstream cur(intString); + cur >> x; + return x; +}
\ No newline at end of file diff --git a/offsetProject/src/imageStore.h b/offsetProject/src/imageStore.h new file mode 100644 index 0000000..3cfb7eb --- /dev/null +++ b/offsetProject/src/imageStore.h @@ -0,0 +1,142 @@ +#pragma once + +#define IMAGE_STORE_SIZE 256 + +#define MIN_TILE_SIZE 8 +#define MAX_TILE_SIZE 32 + +#include "ofMain.h" +#include "ofxJSONElement.h" + +long ofToLong(const string& intString); + +class imageStore : public ofThread{ + + public: + + float interval; //time between refreshes in seconds + + std::string instagram_url; + ofxJSONElement response; + std::map<std::string,ofImage> images; + deque<std::string> to_update; + + imageStore(){ + instagram_url = "https://api.instagram.com/v1/tags/tycleeson/media/recent?client_id=c1d74d701fdf4ddd9f8d30ee9e8f944b"; + interval=5.00f; + } + + void set_interval(float _interval){ + interval=_interval; + } + + void start(){ + startThread(true, false); // blocking, verbose + } + + void stop(){ + stopThread(); + } + + //-------------------------- + void threadedFunction(){ + + //1st get the pre-existing images + + ofDirectory image_path(ofToString(IMAGE_STORE_SIZE)+"/"); + cerr<<"image path: "<<image_path.getAbsolutePath()<<endl; + if (image_path.exists()){ + image_path.listDir(); + cerr<<"image path found, "<<image_path.size()<<" images"<<endl; + for (int i=0;i<image_path.size();i++){ + ofImage img; + img.setUseTexture(false); + img.loadImage(ofToString(IMAGE_STORE_SIZE)+"/"+image_path.getFiles()[i].getFileName()); + img.resize(MAX_TILE_SIZE,MAX_TILE_SIZE); + if( lock() ){ + images[image_path.getFiles()[i].getBaseName()]=img; + to_update.push_back(image_path.getFiles()[i].getBaseName()); + unlock(); + } + } + } + else { + cerr<<"creating image path"<<endl; + image_path.create(); + } + + cout << "Api: " << instagram_url<<endl; + + while( isThreadRunning() != 0 ){ + + cout<<"."<<std::flush; + + if (!response.open(instagram_url)) { + cout << "Failed to parse JSON\n" << endl; + } + else { //int numImages = MIN(5,response["data"].size()); + + + for(int i=0; i< response["data"].size(); i++) { + //cout << "response " <<response["data"][i]["caption"]["id"].asString()<< endl; + + if (images.find(response["data"][i]["caption"]["id"].asString())==images.end()){ + std::string url = response["data"][i]["images"]["standard_resolution"]["url"].asString(); + std::string id = response["data"][i]["caption"]["id"].asString(); + cout<<"fetching "<<id<<":"<<instagram_url<<endl; + + ofImage img; + img.setUseTexture(false); + img.loadImage(url); + img.resize(IMAGE_STORE_SIZE,IMAGE_STORE_SIZE); + img.saveImage(ofToString(IMAGE_STORE_SIZE)+"/"+id+".png"); + img.resize(MAX_TILE_SIZE,MAX_TILE_SIZE); + if( lock() ){ + images[id]=img; + to_update.push_back(id); + unlock(); + } + } + } + } + ofSleepMillis(interval * 1000); + } + } + + //-------------------------- + void draw(){ + if( lock() ){ + if (to_update.size()){ + std::string im = to_update.front(); + + const ofPixels& pix = images[im].getPixelsRef(); + images[im].getTextureReference().allocate( + pix.getWidth() + ,pix.getHeight() + ,ofGetGlInternalFormat(pix) + ); + + images[im].setUseTexture(true); + images[im].update(); + + to_update.pop_front(); + + int drawcount=0; + for (map<string,ofImage>::iterator i=images.begin();i!=images.end();++i){ + if(i->second.isUsingTexture()){ + drawcount++; + } + } + //cout<<"loaded "<<im<<" "<<ofToLong(im)%(long)(ofGetWidth()-images[im].getWidth()+1)<<","<<ofToLong(im)%(long)(ofGetHeight()-images[im].getHeight()+1)<<endl; + } + for (map<string,ofImage>::iterator i=images.begin();i!=images.end();++i){ + if(i->second.isUsingTexture()){ + i->second.draw(ofToLong(i->first)%(long)(ofGetWidth()-i->second.getWidth()+1),ofToLong(i->first)%(long)(ofGetHeight()-i->second.getHeight()+1)); + } + } + unlock(); + } + } + + +};
\ No newline at end of file diff --git a/offsetProject/src/main.cpp b/offsetProject/src/main.cpp index 1fa9125..9013dff 100644 --- a/offsetProject/src/main.cpp +++ b/offsetProject/src/main.cpp @@ -1,6 +1,6 @@ #include "ofApp.h" int main() { - ofSetupOpenGL(1600, 900, OF_WINDOW); + ofSetupOpenGL(600, 400, OF_WINDOW); ofRunApp(new ofApp()); } diff --git a/offsetProject/src/ofApp.cpp b/offsetProject/src/ofApp.cpp index 00c7f57..f65746e 100644 --- a/offsetProject/src/ofApp.cpp +++ b/offsetProject/src/ofApp.cpp @@ -38,9 +38,20 @@ get list of images in defined folder load them and create mip maps store indexed by file stub +start thread when a new image comes in load and create mip maps -save to + +how to store pictures +threaded loader works away in the background +when a new picture comes in + +a data structure which is part of the threaded object +each image contains a mutex to enable the threaded object to be accessed +the instagram id is just used to match against incoming - there needs to be the pattern matching algorithm + +how to draw pictures +need to search the db for pictures of a certain colour */ //-------------------------------------------------------------- @@ -49,19 +60,7 @@ void ofApp::setup() { ofSetFrameRate(60); - ofDirectory image_path(ofToString(IMAGE_STORE_SIZE)+"/"); - cerr<<"image path: "<<image_path.getAbsolutePath()<<endl; - if (image_path.exists()){ - image_path.listDir(); - cerr<<"image path found, "<<image_path.size()<<" images"<<endl; - for (int i=0;i<image_path.size();i++){ - cerr<<image_path.getFiles()[i].getFileName()<<endl; - } - } - else { - cerr<<"creating image path"<<endl; - image_path.create(); - } + store.start(); mode=MODE_COLOURTILES; @@ -85,7 +84,7 @@ void ofApp::draw() { ofSetColor(255, 255, 255); - + store.draw(); } diff --git a/offsetProject/src/ofApp.h b/offsetProject/src/ofApp.h index 67e1b71..f1f8253 100644 --- a/offsetProject/src/ofApp.h +++ b/offsetProject/src/ofApp.h @@ -4,8 +4,7 @@ #include "ofxOpenCv.h" #include "ofxKinect.h" -#define MIN_TILE_SIZE 4 //has to be a divisor of 320 and 240 as the tiles are centred -#define MAX_TILE_SIZE 32 //has to be a power of 2 * MIN_TILE_SIZE +#include "imageStore.h" #define MODE_COLOURTILES 0 #define MODE_DEPTH 1 @@ -13,7 +12,7 @@ #define NUM_MODES 3 -#define IMAGE_STORE_SIZE 256 + class ofApp : public ofBaseApp { @@ -30,7 +29,7 @@ public: void mouseReleased(int x, int y, int button); void windowResized(int w, int h); - + imageStore store; int mode; bool fullscreen; |
