#pragma once #include "ofMain.h" #include "ofxOpenCv.h" class audioGlitcher { public: ofxCvColorImage buffer; ofFbo renderFBO; IplImage tmp; vector *samples; int interp_x,interp_y; float trans_x,trans_y; float scale; float rotation; float origin_x,origin_y; void setup(int w,int h,vector *s){ buffer.allocate(w,h); renderFBO.allocate(w,h,GL_RGB); samples=s; origin_x=origin_y=0.5f; trans_x=trans_y=0.0f; scale=1.0f; rotation=0.0f; interp_x=16; interp_y=12; } void set_interp(int ix,int iy){ interp_x=ix; interp_y=iy; } void set_trans(float x,float y){ trans_x=x; trans_y=y; } void set_scale(float s){ scale=s; } void set_origin(float x,float y){ origin_x=x; origin_y=y; } void draw(int x,int y){ renderFBO.draw(x,y); } void update(){ renderFBO.readToPixels(buffer.getPixels()); buffer.flagImageChanged(); //create low res remap target cv::Mat dstX(interp_x,interp_y,CV_32FC1); cv::Mat dstY(interp_x,interp_y,CV_32FC1); cv::Mat srcX(interp_x,interp_y,CV_32FC1); cv::Mat srcY(interp_x,interp_y,CV_32FC1); //for a sanity check //512-1 / 16-1 = 34.066666667 //* 15= float* sx = (float*)srcX.data; float* sy = (float*)srcY.data; float* dx = (float*)dstX.data; float* dy = (float*)dstY.data; float xFactor=(renderFBO.getWidth()-1)/(srcX.cols-1); float yFactor=(renderFBO.getHeight()-1)/(srcX.rows-1); for (int i=0;i(j,i)=i*xFactor; //srcY.at(j,i)=j*yFactor; sx[j*srcX.cols+i]=i*xFactor; sy[j*srcX.cols+i]=j*yFactor; } } //render =512x384 //interp = 16x12 //xFactor=34.133333 yFactor=34.90909 // srcX.at (11,15)=480 // srcY.at (11,15)=352 // ==it is an effect caused by the pixels being computed at the centres // but finally being interpolated to the edges //transform the low res matrix float tX=trans_x; //-.05; //fraction of image float tY=trans_y; //-.08; //fraction of image float oX=origin_x; //fraction of image float oY=origin_y; //fraction of image float s=scale; float r=rotation; cv::Point2f srcTri[3], dstTri[3]; // Compute matrix by creating triangle and transforming srcTri[0].x=0; srcTri[0].y=0; srcTri[1].x=dstX.rows-1; srcTri[1].y=0; srcTri[2].x=0; srcTri[2].y=dstX.cols-1; for (int i=0;i<3;i++){ dstTri[i].x=srcTri[i].x+(tX*dstX.cols); dstTri[i].y=srcTri[i].y+(tY*dstX.cols); //use cols for equiv coords //rotate and scale around centre //transform to centre dstTri[i].x-=(oX*(dstX.cols)); dstTri[i].y-=(oY*(dstX.cols)); dstTri[i].x*=s; dstTri[i].y*=s; double dx=(dstTri[i].x*cos(r))-(dstTri[i].y*sin(r)); double dy=(dstTri[i].x*sin(r))+(dstTri[i].y*cos(r)); dstTri[i].x=dx; dstTri[i].y=dy; //transform back dstTri[i].x+=(oX*(dstX.cols)); dstTri[i].y+=(oY*(dstX.cols)); } cv::Mat trans_mat=getAffineTransform(srcTri,dstTri); warpAffine(srcX,dstX,trans_mat, srcX.size(), cv::INTER_LINEAR, cv::BORDER_WRAP); warpAffine(srcY,dstY,trans_mat, srcY.size(), cv::INTER_LINEAR, cv::BORDER_WRAP); cv::Mat scaledstX; cv::Mat scaledstY; cv::resize(dstX,scaledstX, cv::Size(renderFBO.getWidth(),renderFBO.getHeight()), 0, 0, cv::INTER_LINEAR); cv::resize(dstY,scaledstY, cv::Size(renderFBO.getWidth(),renderFBO.getHeight()), 0, 0, cv::INTER_LINEAR); for (int i=0;i(j,i)=i; //scaledstY.at(j,i)=j; } } float* psx = (float*)scaledstX.data; float* psy = (float*)scaledstY.data; //at the moment the midway between <7,5> and <8,6> // is coming out at 255.5,191.5 in the source - //think that should b2 255,192 cerr<<"src: <0,0> "< "< "< "< "< "< "< "< "<size();i++){ ofVertex(i,(*samples)[i]*renderFBO.getHeight()); } ofEndShape(); ofSetColor(255,255,255); ofNoFill(); ofBeginShape(); for (int i=0;isize();i++){ ofVertex(i,(*samples)[i]*renderFBO.getHeight()); } ofEndShape(); ofPopMatrix(); } else { ofNoFill(); ofPushMatrix(); ofTranslate(renderFBO.getWidth()/2,renderFBO.getHeight()/2); ofRect(-20,-20,40,40); ofPopMatrix(); } renderFBO.end(); //renderFBO.flagImageChanged(); } };