Hello openFrameworks-Community.
I have a sketch which is supposed to sort pixels by brightness within an image, frame by frame.
This is my code:
#include "ofApp.h"
ofImage image;
int w;
int h;
//--------------------------------------------------------------
void ofApp::setup(){
w = ofGetWidth();
h = ofGetHeight();
unsigned char *pixels = new unsigned char[w * h * 3];
for(int y = 0;y<h;y++){
for(int x = 0;x<w;x++){
int red = ofRandom(255);
int green = 0;
int blue = ofRandom(255);
int index = 3 * (x + w * y);
pixels[index] = red;
pixels[index + 1] = green;
pixels[index + 2] = blue;
}
}
image.setFromPixels(pixels,w,h,OF_IMAGE_COLOR);
delete[] pixels;
}
//--------------------------------------------------------------
void ofApp::update(){
ofImage next;
next.clone(image);
for(int x = 1;x<w-1;x++){
for(int y = 1;y<h-1;y++){
ofColor c = next.getColor( x, y );
ofColor n = next.getColor( x+1, y );
int b1 = c.getBrightness();
int b2 = n.getBrightness();
if(b1<b2){
ofColor temp = c;
next.setColor(x,y,n);
next.setColor(x+1,y,c);
}
}
}
image.clone(next);
cout << ofGetFrameRate() << endl;
}
It seems to me that it could be much much faster due to the fact that I have a similar program in Processing, which runs at about 18 FPS. In openFrameworks I have about 9 FPS.
Could you give me any hints for improving the performance of such an algorithm? I am kind of new to openFrameworks and switched from Processing because of the fact that it should run faster. It seems I did not find the best way to reach my goal.
Thank you very much and sorry for my poor english, I hope you got my point.
Greets!