From d055a64c3fdb549713765601a8a4f8732d6d73dc Mon Sep 17 00:00:00 2001 From: hideyukisaito Date: Fri, 25 Oct 2013 05:56:29 +0900 Subject: [PATCH] init --- README.md | 35 ++++++++++++ src/ofxCompositeImgae.cpp | 76 +++++++++++++++++++++++++ src/ofxCompositeImgae.h | 115 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 226 insertions(+) create mode 100644 README.md create mode 100644 src/ofxCompositeImgae.cpp create mode 100644 src/ofxCompositeImgae.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..bed878b --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +ofxCompositeImage +=== + +openFrameworks addon for creating composite image. + +## Dependencies + + +This addon depends on the followings: + +- ofxOpenGLContextScope + + +## License +The MIT License (MIT) + +Copyright (c) 2013 hideyuki saito + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/src/ofxCompositeImgae.cpp b/src/ofxCompositeImgae.cpp new file mode 100644 index 0000000..b808cc3 --- /dev/null +++ b/src/ofxCompositeImgae.cpp @@ -0,0 +1,76 @@ +#include "ofxCompositeImgae.h" + +using namespace ofxCompositeImage; + +Composer::~Composer() +{ + if (isThreadRunning()) + { + stopThread(); + } +} + +void Composer::addLayers(vector layers, Setting setting) +{ + lock(); + tasks.push_back(layers); + settings.push_back(setting); + unlock(); + + if (!isThreadRunning()) startThread(); +} + +void Composer::threadedFunction() +{ + ofxOpenGLContextScope scope; + + while (isThreadRunning()) + { + ofLogNotice() << "is main thread? : " << isMainThread(); + if (0 < tasks.size()) + { + lock(); + composite(); + unlock(); + } + + ofSleepMillis(10); + } +} + +void Composer::composite() +{ + ofFbo fbo; + fbo.allocate((int)settings.at(0).getWidth(), (int)settings.at(0).getHeight()); + fbo.begin(); + { + ofClear(0, 0, 0); + ofBackground(settings.at(0).getBackgroundColor()); + + ofPushStyle(); + { + ofEnableAlphaBlending(); + for (int i = 0; i < tasks.at(0).size(); ++i) + { + Material m = tasks.at(0).at(i); + m.resize(); + m.draw(); + } + ofDisableAlphaBlending(); + } + ofPopStyle(); + } + fbo.end(); + + ofImage output; + fbo.readToPixels(output.getPixelsRef()); + output.update(); + output.resize(settings.at(0).getWidth(), settings.at(0).getHeight()); + + tasks.erase(tasks.begin()); + settings.erase(settings.begin()); + + if (tasks.empty()) stopThread(); + + ofNotifyEvent(compositionComplete, output, this); +} \ No newline at end of file diff --git a/src/ofxCompositeImgae.h b/src/ofxCompositeImgae.h new file mode 100644 index 0000000..fc965d3 --- /dev/null +++ b/src/ofxCompositeImgae.h @@ -0,0 +1,115 @@ +#pragma once + +#include "ofMain.h" +#include "ofxOpenGLContextScope.h" + +namespace ofxCompositeImage +{ + class Setting; + class Material; + class Composer; +}; + +#pragma mark - class ofxCompositeImageSetting + +class ofxCompositeImage::Setting +{ + +public: + + Setting(); + Setting(float width, float height, ofColor backgroundColor) + { + this->width = width; + this->height = height; + this->backgroundColor = backgroundColor; + } + + float getWidth() { return width; } + float getHeight() { return height; } + ofColor getBackgroundColor() { return backgroundColor; } + +private: + + float width; + float height; + ofColor backgroundColor; +}; + +class ofxCompositeImage::Material +{ + +public: + + Material() {}; + ~Material() {}; + + Material(ofImage img, float x, float y, float width, float height) + { + this->image = img; + this->x = x; + this->y = y; + this->width = width; + this->height = height; + } + + void setup(ofImage img, float x, float y, float width, float height) + { + this->image = img; + this->x = x; + this->y = y; + this->width = width; + this->height = height; + } + + void setImage(ofImage img) { this->image = img; } + + void setPosition(float x, float y) + { + this->x = x; + this->y = y; + }; + + void setWidth(float width) { this->width = width; } + void setHeight(float height) { this->height = height; } + + void resize() + { + image.resize(width, height); + } + + void draw() + { + image.draw(x, y); + } + +private: + + ofImage image; + float x, y, width, height; +}; + + +# pragma mark - class ofxCompositeImgae + +class ofxCompositeImage::Composer : public ofThread +{ + +public: + + Composer() { ofxOpenGLContextScope::setup(); } + ~Composer(); + + void addLayers(vector layers, Setting setting); + + void threadedFunction(); + + ofEvent compositionComplete; + +private: + + vector< vector > tasks; + vector settings; + + void composite(); +}; \ No newline at end of file