Created
March 26, 2017 19:58
-
-
Save Craigson/bf415b06b696561b514fdd0492516149 to your computer and use it in GitHub Desktop.
PixelMirror
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
#include "cinder/app/App.h" | |
#include "cinder/app/RendererGl.h" | |
#include "cinder/gl/gl.h" | |
#include "cinder/Surface.h" | |
#include "cinder/Capture.h" | |
#include "cinder/Camera.h" | |
#include "cinder/Text.h" | |
#include "cinder/Log.h" | |
using namespace ci; | |
using namespace ci::app; | |
#define TILE_WIDTH 4 | |
class PixelMirror : public App { | |
public: | |
void setup() override; | |
void resize() override; | |
void update() override; | |
void draw() override; | |
void keyDown (KeyEvent event) override; | |
CaptureRef mCapture; | |
Color8u getAverage(Surface* _surface, vec2 _ULvertex); | |
int getValue(Surface* _surface, vec2 _ULvertex); | |
int numX, numY; | |
//Surface8u mPixels; | |
std::vector<Color8u> mAveragedColors; | |
std::vector<int> mThresholdValues; | |
int threshold; | |
}; | |
void PixelMirror::keyDown(cinder::app::KeyEvent event) | |
{ | |
switch (event.getChar()) | |
{ | |
case 'i': | |
threshold++; | |
std::cout << threshold << std:: endl; | |
break; | |
case 'k': | |
threshold--; | |
std::cout << threshold << std:: endl; | |
break; | |
default: | |
break; | |
} | |
} | |
void PixelMirror::setup() | |
{ | |
threshold = 128; | |
setFrameRate(60); | |
try { | |
mCapture = Capture::create( 640, 480 ); | |
mCapture->start(); | |
} | |
catch( CaptureExc &exc ) { | |
CI_LOG_EXCEPTION( "failed to initialize the Capture: ", exc ); | |
} | |
numX = mCapture->getWidth() / TILE_WIDTH; | |
numY = mCapture->getHeight() / TILE_WIDTH; | |
// mPixels = Surface(mCapture->getWidth(),mCapture->getHeight(), false); | |
} | |
void PixelMirror::resize() | |
{ | |
} | |
void PixelMirror::update() | |
{ | |
// std::cout << "update" << std::endl; | |
if( mCapture && mCapture->checkNewFrame() ) | |
{ | |
mAveragedColors.clear(); | |
mThresholdValues.clear(); | |
auto mPixels = *mCapture->getSurface(); | |
// std::cout << "working " << std::endl; | |
for (int i = 0; i < mPixels.getWidth(); i += TILE_WIDTH) | |
{ | |
for (int j = 0; j < mPixels.getHeight(); j += TILE_WIDTH) | |
{ | |
mAveragedColors.push_back(getAverage(&mPixels, vec2(i,j))); | |
mThresholdValues.push_back(getValue(&mPixels, vec2(i,j))); | |
} | |
} | |
} | |
} | |
void PixelMirror::draw() | |
{ | |
gl::clear( Color::white() ); | |
if (mAveragedColors.size() != 0) | |
{ | |
int index = 0; | |
for (int i = 0; i < getWindowWidth(); i += TILE_WIDTH) | |
{ | |
for (int j = 0; j < getWindowHeight(); j += TILE_WIDTH) | |
{ | |
gl::color(Color8u(mAveragedColors[index])); | |
// std::cout << "i,j = " << i << "," << j << " - " << Color(mAveragedColors[index]) << std::endl; | |
Rectf rect = Rectf(vec2(i,j),vec2(i+TILE_WIDTH, j+TILE_WIDTH)); | |
// gl::drawSolidEllipse(vec2(i + TILE_WIDTH/2, j + TILE_WIDTH/2), 2,2) ; | |
gl::drawSolidRect(rect); | |
// gl::color(Color::black()); | |
// if (mThresholdValues[index] < threshold) gl::drawSolidEllipse(vec2(i + TILE_WIDTH/2, j + TILE_WIDTH/2), 1, 1); | |
index++; | |
} | |
} | |
} | |
} | |
Color8u PixelMirror::getAverage( Surface* _surface, vec2 _cornerPoint) | |
{ | |
Area area = Area(_cornerPoint.x, _cornerPoint.y, _cornerPoint.x + TILE_WIDTH, _cornerPoint.y + TILE_WIDTH); | |
int32_t sumR,sumG,sumB; | |
sumR = 0; | |
sumG = 0; | |
sumB = 0; | |
int count = 0; | |
Surface::Iter iter = _surface->getIter( area ); | |
while( iter.line() ) { | |
while( iter.pixel() ) { | |
sumR += (int)iter.r(); | |
sumG += (int)iter.g(); | |
sumB += (int)iter.b(); | |
count++; | |
} | |
} | |
return Color8u(sumR/count + 100, sumG/count, sumB/count); //should presumably return avg color!? | |
} | |
int PixelMirror::getValue( Surface* _surface, vec2 _cornerPoint) | |
{ | |
Area area = Area(_cornerPoint.x, _cornerPoint.y, _cornerPoint.x + TILE_WIDTH, _cornerPoint.y + TILE_WIDTH); | |
int32_t sumR,sumG,sumB; | |
sumR = 0; | |
sumG = 0; | |
sumB = 0; | |
int count = 0; | |
Surface::Iter iter = _surface->getIter( area ); | |
while( iter.line() ) { | |
while( iter.pixel() ) { | |
sumR += (int)iter.r(); | |
sumG += (int)iter.g(); | |
sumB += (int)iter.b(); | |
count++; | |
} | |
} | |
int totalR = sumR/count; | |
int totalG = sumG/count; | |
int totalB = sumB/count; | |
int threshold = (totalR + totalG + totalB ) / 3; | |
return threshold; | |
} | |
CINDER_APP( PixelMirror, RendererGl( RendererGl::Options().msaa( 4 ) ), | |
[&](App::Settings *settings){ | |
settings->setWindowSize(640, 480); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment