Cinder一巡り #3
画像処理の基礎
class BasicImageProcApp : public AppBasic { public: gl::Texture texture; void prepareSettings( Settings* settings ); void setup(); void mouseDown( MouseEvent event ); void update(); void draw(); }; void BasicImageProcApp::setup() { try { texture = gl::Texture( loadImage( loadResource("image.jpg") ) ); } catch (...) { console() << "unable to load the texture file..." << endl; } } void BasicImageProcApp::draw() { // clear out the window with black gl::clear( Color( 0, 0, 0 ) ); gl::draw( texture, getWindowBounds() ); }
gl::Textureで読み込んだ画像はGPUのメモリに読み込まれ、update()などで画像を処理したい場合にはGLSLを使います。CPUに読み込むようにするとC++のコードで画像を処理することができるようになります。この場合にはSurfaceクラスを使って画像を読込み、描画するときにTextureに変換します。
#include "cinder/app/AppBasic.h" #include "cinder/gl/gl.h" #include "cinder/ImageIo.h" #include "cinder/gl/Texture.h" #include "cinder/Surface.h" using namespace ci; using namespace ci::app; using namespace std; class BasicImageProcApp : public AppBasic { public: gl::Texture texture; Surface surface; void prepareSettings( Settings* settings ); void setup(); void mouseDown( MouseEvent event ); void update(); void draw(); }; void BasicImageProcApp::prepareSettings( Settings* settings ) { settings->setWindowSize( 600, 800 ); } void BasicImageProcApp::setup() { try { surface = Surface( loadImage( loadResource("image.jpg") ) ); } catch (...) { console() << "unable to load the surface file..." << endl; } } void BasicImageProcApp::mouseDown( MouseEvent event ) { } void BasicImageProcApp::update() { texture = gl::Texture( surface ); } void BasicImageProcApp::draw() { // clear out the window with black gl::clear( Color( 0, 0, 0 ) ); if( texture ) { gl::draw( texture, getWindowBounds() ); } } CINDER_APP_BASIC( BasicImageProcApp, RendererGl );
ピクセル操作
Surface::iterクラスを使うと簡単に画像の各ピクセルにアクセスすることができます。
#include "cinder/app/AppBasic.h" #include "cinder/gl/gl.h" #include "cinder/ImageIo.h" #include "cinder/gl/Texture.h" #include "cinder/Surface.h" #include "cinder/Channel.h" using namespace ci; using namespace ci::app; using namespace std; class BasicImageProcApp : public AppBasic { public: gl::Texture texture; Surface surface; void prepareSettings( Settings* settings ); void setup(); void mouseDown( MouseEvent event ); void update(); void draw(); }; void BasicImageProcApp::prepareSettings( Settings* settings ) { settings->setWindowSize( 600, 800 ); } void BasicImageProcApp::setup() { try { surface = Surface( loadImage( loadResource("image.jpg") ) ); } catch (...) { console() << "unable to load the texture file..." << endl; } } void BasicImageProcApp::mouseDown( MouseEvent event ) { } void BasicImageProcApp::update() { // 画像の大きさ Area area = surface.getBounds(); Surface::Iter iter = surface.getIter( area ); while( iter.line() ) { while( iter.pixel() ) { iter.r() += 1; iter.g() += 1; iter.b() += 1; } } texture = gl::Texture( surface ); } void BasicImageProcApp::draw() { // clear out the window with black gl::clear( Color( 0, 0, 0 ) ); if( texture ) { gl::draw( texture, getWindowBounds() ); } } CINDER_APP_BASIC( BasicImageProcApp, RendererGl );