Hello everyone.
I have been trying to solve this by myself and looking in further posts here, but couldn’t find a solution, and this makes me clear I need to learn further to better understand how this things work regarding memory in oF and in c++.
I had some important help from roymacdonald in a post where this started I must mention.
Well, let’s go to the code.
The following code its working, but running really slow, about 7 fps. how could I make it more efficient and thus running smoother?
here is the ofApp.h
#pragma once
#include "ofMain.h"
class ofApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
void windowResized(int w, int h);
ofVideoGrabber vidGrabber;
int camWidth;
int camHeight;
int nDelayFrames;
int delayedFrame;
int delayedFrame1;
int delayedFrame2;
ofImage videoFlipH;
ofImage videoFlipV;
ofImage videoRotate;
deque<ofPixels> frames0;
deque<ofPixels> frames1;
deque<ofPixels> frames2;
ofColor getPixelColor0( int x, int y );
ofColor getPixelColor1( int x, int y );
ofColor getPixelColor2( int x, int y );
ofColor color0;
ofColor color1;
ofColor color2;
ofPixels pixels0;
ofPixels pixels1;
ofPixels pixels2;
int w;
int h;
void setupSignedNoiseDemo();
void updateSignedNoiseDemo();
int *signedNoiseData;
int nSignedNoiseData;
float radialNoiseCursor;
};
and here the .cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
camWidth = 640; // try to grab at this size.
camHeight = 480;
vidGrabber.setDeviceID(0);
vidGrabber.setDesiredFrameRate(60);
vidGrabber.initGrabber(camWidth, camHeight);
ofSetVerticalSync(true);
//------------------------------------------------------ new
nDelayFrames = 100; //Set buffer size
delayedFrame = nDelayFrames-1;
setupSignedNoiseDemo();
w = vidGrabber.getWidth();
h = vidGrabber.getHeight();
}
//--------------------------------------------------------------
void ofApp::update(){
ofBackground(100, 100, 100);
vidGrabber.update();
if(vidGrabber.isFrameNew()){
pixels0 = vidGrabber.getPixels();
pixels1 = pixels0;
pixels2 = pixels1;
//Push the new frame to the beginning of the frame list
frames0.push_front(pixels0); // this will put the pixels in indices in this array (or should I called it list?)
frames1.push_front(pixels1);
frames2.push_front(pixels2);
// If number of buffered frames > nDelayFrames, pop the oldest frame
// this limits the size of the buffer. Without it the ofPixels would pile up forever
if ( frames0.size() > nDelayFrames ) {
frames0.pop_back();
};
if ( frames1.size() > nDelayFrames ) {
frames1.pop_back();
};
if ( frames2.size() > nDelayFrames ) {
frames2.pop_back();
};
if ( !pixels0.isAllocated() ) {
pixels0 = frames0[0]; // index 0 is the oldest
}
if ( !pixels1.isAllocated() ) {
pixels1 = frames1[0];
}
if ( !pixels2.isAllocated() ) {
pixels2 = frames2[0];
}
//Getting video frame size for formulas simplification
//Scan all the pixels
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
//Get pixel color
color0 = getPixelColor0( x, y );
// ofColor *color0 = new ofColor(getPixelColor0( x, y ));
color1 = getPixelColor1( x, y );
color2 = getPixelColor2( x, y );
//Set pixel to image pixels
pixels0.setColor( x, y, color0 );
pixels1.setColor( x, y, color1 );
pixels2.setColor( x, y, color2 );
}
}
//videoFlipH.setImageType(OF_IMAGE_COLOR_ALPHA);
videoFlipH.setFromPixels( pixels0 );
videoRotate.setFromPixels( pixels1 );
videoFlipV.setFromPixels( pixels2 );
delayedFrame = signedNoiseData[0] + (nSignedNoiseData/4) % nSignedNoiseData;
delayedFrame1 = delayedFrame + (nSignedNoiseData/3) % nSignedNoiseData;
delayedFrame2 = delayedFrame1 + (nSignedNoiseData/2 ) % nSignedNoiseData;
}
updateSignedNoiseDemo();
// check the fps in the window
std::stringstream strm;
strm << "fps: " << ofGetFrameRate();
ofSetWindowTitle(strm.str());
}
//--------------------------------------------------------------
void ofApp::setupSignedNoiseDemo(){
// from the noise example
// Setup and allocate resources used in the signed noise demo.
nSignedNoiseData = 100; // we'll store a history of 400 numbers
signedNoiseData = new int[nSignedNoiseData];
for (int i=0; i<nSignedNoiseData; i++){
signedNoiseData[i] = 0;
}
radialNoiseCursor = 0.0;
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(255);
vidGrabber.draw(20, 20);
ofEnableAlphaBlending();
ofEnableBlendMode(OF_BLENDMODE_SCREEN);
ofSetColor(255,255,255,191);
// draw the raw video frame with the alpha value generated above
videoFlipH.draw(20 + camWidth, 20, - 1* camWidth, camHeight);
ofDisableAlphaBlending();
ofEnableBlendMode(OF_BLENDMODE_MULTIPLY);
ofSetColor(255,255,255,127);
videoRotate.draw(20 + camWidth, 20 + camHeight, -1 * camWidth, -1 * camHeight);
ofSetColor(255,255,255,64);
videoFlipV.draw(20, 20 + camHeight, camWidth, - 1* camHeight);
ofDisableBlendMode();
}
//--------------------------------------------------------------
void ofApp::updateSignedNoiseDemo(){
// Shift all of the old data forward through the array
for (int i=(nSignedNoiseData-1); i>0; i--){
signedNoiseData[i] = signedNoiseData[i-1];
}
// Compute the latest data, and insert it at the head of the array.
// Here is where ofSignedNoise is requested.
float noiseStep = 0.2;
float noiseAmount = 50;
signedNoiseData[0] = noiseAmount * ofSignedNoise( radialNoiseCursor );
radialNoiseCursor += noiseStep;
}
//--------------------------------------------------------------
ofColor ofApp::getPixelColor0( int x, int y ){
int n = frames0.size() - 1;
int i0 = ofClamp( delayedFrame, 0, n );
//Getting the frame colors
color0 = frames0[ i0 ].getColor( x, y );
// this here instead of in update makes a cool (d)effect:
// delayedFrame = ( delayedFrame - 1 + nDelayFrames ) % nDelayFrames;
}
ofColor ofApp::getPixelColor1( int x, int y ){
int n = frames1.size() - 1;
int i0 = ofClamp( delayedFrame1, 0, n );
color1 = frames1[ i0 ].getColor( x, y );
}
ofColor ofApp::getPixelColor2( int x, int y ){
int n = frames2.size() - 1;
int i0 = ofClamp( delayedFrame2, 0, n );
color2 = frames2[ i0 ].getColor( x, y );
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
I appreciate any help on this, In the meanwhile I’ll take a look at the ofBook: Memory in C++.
best regards,
Gil