1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#pragma once
#include "ofMain.h"
#include "ofxOpenCv.h"
class audioGlitcher {
public:
ofxCvColorImage buffer;
ofFbo renderFBO;
IplImage tmp;
vector<float> *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<float> *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.getPixelsRef());
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<srcX.cols;i++){
for (int j=0;j<srcX.rows;j++){
//srcX.at<float>(j,i)=i*xFactor;
//srcY.at<float>(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<srcX.cols;i++){
for (int j=0;j<srcX.rows;j++){
//dx[i,j]=i*xFactor;
//dy[i,j]=j*yFactor;
}
}
for (int i=0;i<scaledstX.cols;i++) {
for (int j=0;j<scaledstX.rows;j++) {
//scaledstX.at<float>(j,i)=i;
//scaledstY.at<float>(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> "<<sx[0]<<","<<sy[0]<<endl;
cerr<<"src: <7,5> "<<sx[5*16+7]<<","<<sy[5*16+7]<<endl;
cerr<<"src: <8,6> "<<sx[6*16+8]<<","<<sy[6*16+8]<<endl;
cerr<<"src: <15,11> "<<sx[11*16+15]<<","<<sy[11*16+15]<<endl;
cerr<<"dest: <7,5> "<<dx[5*16+7]<<","<<dy[5*16+7]<<endl;
cerr<<"dest: <8,6> "<<dx[6*16+8]<<","<<dy[6*16+8]<<endl;
cerr<<"Map at <0,0> "<<psx[0]<<","<<psy[0]<<endl;
cerr<<"Map at <256,192> "<<psx[192*512+256]<<","<<psy[192*512+256]<<endl;
cerr<<"Map at <511,383> "<<psx[383*512+511]<<","<<psy[383*512+511]<<endl;
cv::Mat buf = buffer.getCvImage();
cv::Mat dstbuf;
cv::remap(buf,dstbuf,scaledstX,scaledstY, cv::INTER_NEAREST, cv::BORDER_REPLICATE); //, cv::Scalar(0,0, 0) );
tmp = IplImage(dstbuf);
//buffer=tmp;
//buffer.remap(scaledstX,scaledstY);
buffer=&tmp;
renderFBO.begin();
//
ofSetColor(255,255,255);
buffer-=1;
buffer.draw(0,0);
//
//ofEnableAlphaBlending();
//ofSetColor(0,0,0,128);
//ofRect(0,0,ofGetWidth(),ofGetHeight());
//ofDisableAlphaBlending();
bool useAudio=false;
if (useAudio) {
ofPushMatrix();
ofTranslate(0,renderFBO.getHeight()/2);
ofSetColor(0,0,0);
ofFill();
ofBeginShape();
for (int i=0;i<samples->size();i++){
ofVertex(i,(*samples)[i]*renderFBO.getHeight());
}
ofEndShape();
ofSetColor(255,255,255);
ofNoFill();
ofBeginShape();
for (int i=0;i<samples->size();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();
}
};
|