昔に作ったoFでモザイク処理を作ってたのでアップしておきます。
ドラックした箇所はモザイクがかからないようにしています。バグっていたらすいません。
ソースはこんな感じです。
#include "testApp.h" #define WIDTH 960 // 出力サイズ(幅) #define HEIGHT 640 // 出力サイズ(縦) #define MOSAIC_SIZE 20 // 個々のモザイクのサイズ #define CLEAR_SIZE 40 // モザイクにならない箇所の範囲 //-------------------------------------------------------------- void testApp::setup(){ imageWidth = WIDTH; imageHeght = HEIGHT; mouseX = 20; mouseY = -40; inputImage.initGrabber(imageWidth, imageHeght); ofSetFrameRate(30); } //-------------------------------------------------------------- void testApp::update(){ inputImage.grabFrame(); } //-------------------------------------------------------------- void testApp::draw(){ inputImage.draw(0, 0); unsigned char *pixels = inputImage.getPixels(); for (int i = 0; i < imageWidth; i += MOSAIC_SIZE){ for (int j = 0; j < imageHeght; j += MOSAIC_SIZE){ // クリックしたところを中心にモザイクを外す if( ((j < (mouseY - CLEAR_SIZE)) || ((mouseY + CLEAR_SIZE < j)) || ((i < (mouseX - (CLEAR_SIZE))) || (mouseX + (CLEAR_SIZE) < i))) ){ int r = pixels[j * imageWidth * 3 + i * 3]; int g = pixels[j * imageWidth * 3 + i * 3 + 1]; int b = pixels[j * imageWidth * 3 + i * 3 + 2]; ofSetColor(r, g, b); ofFill(); ofRect(i, j, MOSAIC_SIZE, MOSAIC_SIZE); ofNoFill(); ofSetColor(255, 255, 255); } } } } //-------------------------------------------------------------- void testApp::keyPressed(int key){ } //-------------------------------------------------------------- void testApp::keyReleased(int key){ } //-------------------------------------------------------------- void testApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void testApp::mouseDragged(int x, int y, int button){ // クリックした座標の取得 mouseX = x; mouseY = y; } //-------------------------------------------------------------- void testApp::mousePressed(int x, int y, int button){ // ドラッグしている座標の取得 mouseX = x; mouseY = y; } //-------------------------------------------------------------- void testApp::mouseReleased(int x, int y, int button){ // マウスを放したタイミングで再度モザイク処理をするため初期化 mouseX = 20; mouseY = -40; }
これから昔作ったものをちょいちょい上げていきます。
おすすめ書籍