Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
hideyukisaito committed Oct 24, 2013
0 parents commit d055a64
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
35 changes: 35 additions & 0 deletions 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.
76 changes: 76 additions & 0 deletions src/ofxCompositeImgae.cpp
@@ -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);
}
115 changes: 115 additions & 0 deletions 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<Material> layers, Setting setting);

void threadedFunction();

ofEvent<ofImage> compositionComplete;

private:

vector< vector<Material> > tasks;
vector<Setting> settings;

void composite();
};

0 comments on commit d055a64

Please sign in to comment.