Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hideyukisaito
committed
Oct 24, 2013
0 parents
commit d055a64
Showing
3 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "ofxCompositeImgae.h" | ||
|
||
using namespace ofxCompositeImage; | ||
|
||
Composer::~Composer() | ||
{ | ||
if (isThreadRunning()) | ||
{ | ||
stopThread(); | ||
} | ||
} | ||
|
||
void Composer::addLayers(vector<Material> 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Material> layers, Setting setting); | ||
|
||
void threadedFunction(); | ||
|
||
ofEvent<ofImage> compositionComplete; | ||
|
||
private: | ||
|
||
vector< vector<Material> > tasks; | ||
vector<Setting> settings; | ||
|
||
void composite(); | ||
}; |