-
Notifications
You must be signed in to change notification settings - Fork 1
/
sspDsp.cpp
448 lines (395 loc) · 16 KB
/
sspDsp.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include "sspDsp.h"
SspDsp::SspDsp(QString widgetName, QWidget *parent) :
YuvImgDsp(widgetName, parent)
{
imgShowY->installEventFilter(this);
tmpDataY = NULL;
tmpDataU = NULL;
tmpDataV = NULL;
sspData = NULL;
}
SspDsp::~SspDsp() {
if (sspData != NULL)
delete sspData;
}
void SspDsp::sspChangedByRgb(RawImg &rawImg)
{
crtWidth = rawImg.width;
crtHeight = rawImg.height;
// delete former QImage and data
delete imgY;
delete imgU;
delete imgV;
if (dataY != NULL)
delete dataY;
if (dataU != NULL)
delete dataU;
if (dataV != NULL)
delete dataV;
if (tmpDataY != NULL)
delete tmpDataY;
// update current width and height
crtWidth = rawImg.width;
crtHeight = rawImg.height;
// init sspData
if (sspData != NULL)
delete sspData;
sspData = new RawImg(rawImg);
// allocate data array
dataY = new unsigned char[rawImg.width * rawImg.height * 4];
dataU = new unsigned char[rawImg.width * rawImg.height * 4];
dataV = new unsigned char[rawImg.width * rawImg.height * 4];
tmpDataY = new unsigned char[crtWidth * crtHeight * 4];
for (int i = 0, j = 0; i < rawImg.width * rawImg.height; ++i, j += 4)
{
rgb2yuv(sspData->data + i * 3);
// Subsample on rows
int r = i/rawImg.width;
// U keeps even rows
// V keeps odd rows
if( r%2 == 1 )
{
sspData->data[i*3+1] = sspData->data[(i-rawImg.width)*3+1];
sspData->data[(i-rawImg.width)*3+1] = sspData->data[i*3+1];
}
}
for (int i = 0, j = 0; i < rawImg.width * rawImg.height; ++i, j += 4)
{
// Subsample on columns
int c = i%rawImg.width;
// U & V keeps even columns
if( c%2 == 1 )
{
sspData->data[i*3+1] = sspData->data[(i-1)*3+1];
sspData->data[i*3+2] = sspData->data[(i-1)*3+2];
}
}
for (int i = 0, j = 0; i < rawImg.width * rawImg.height; ++i, j += 4)
{
dataY[j] = dataY[j+1] = dataY[j+2] = sspData->data[i*3];
dataU[j] = dataU[j+1] = dataU[j+2] = sspData->data[i*3+1];
dataV[j] = dataV[j+1] = dataV[j+2] = sspData->data[i*3+2];
dataY[j + 3] = dataU[j + 3] = dataV[j + 3] = ~0; // Alpha
}
/*
// subsample
// U / Cb
for(int i=0, k=0; i<rawImg.height; i+=2)
{
for(int j=0; j<rawImg.width; j+=2)
{
dataU[k] = dataU[k+1] = dataU[k+2] = yuvData->data[(i*rawImg.width+j)*3+1];
dataU[k+3] = ~0; //Alpha
k+=4;
}
}
// V / Cr
for(int i=0, k=0; i<rawImg.height; i+=2)
{
for(int j=0; j<rawImg.width; j+=2)
{
if(i+1<rawImg.height)
dataV[k] = dataV[k+1] = dataV[k+2] = yuvData->data[((i+1)*rawImg.width+j)*3+2];
else
dataV[k] = dataV[k+1] = dataV[k+2] = yuvData->data[(i*rawImg.width+j)*3+2];
dataV[k+3] = ~0; //Alpha
k+=4;
}
}
//Y
for(int i=0; i < rawImg.width * rawImg.height; i++)
{
dataY[i*4] = dataY[i*4+1] = dataY[i*4+2] = yuvData->data[i * 3];
dataY[i*4+3] = ~0; //Alpha
}
for(int i=0; i < rawImg.width * rawImg.height; i++)
sspData->data[i]=dataY[i*4];
for(int i=0; i<UVwidth*UVheight; i++)
sspData->data[i+Ubase]=dataU[i*4];
for(int i=0; i<UVwidth*UVheight; i++)
sspData->data[i+Vbase]=dataV[i*4];
delete yuvData;
*/
// display YUV images
imgY = new QImage(dataY, rawImg.width, rawImg.height, QImage::Format_ARGB32);
imgShowY = new QLabel(this);
imgShowY->setPixmap(QPixmap::fromImage(imgY->scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowY->installEventFilter(this);
imgShowY->show();
mainLayout->addWidget(imgShowY, 0, 0, 10, 2);
imgU = new QImage(dataU, rawImg.width, rawImg.height, QImage::Format_ARGB32);
imgShowU = new QLabel(this);
imgShowU->setPixmap(QPixmap::fromImage(imgU->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowU->installEventFilter(this);
imgShowU->show();
mainLayout->addWidget(imgShowU, 0, 2, 5, 1);
imgV = new QImage(dataV, rawImg.width, rawImg.height, QImage::Format_ARGB32);
imgShowV = new QLabel(this);
imgShowV->setPixmap(QPixmap::fromImage(imgV->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowV->installEventFilter(this);
imgShowV->show();
mainLayout->addWidget(imgShowV, 5, 2, 5, 1);
emit sspChangingDct(*sspData);
}
void SspDsp::sspChangedByDct(RawImg &dctData)
{
// delete former QImage and data
delete imgY;
delete imgU;
delete imgV;
if (dataY != NULL)
delete dataY;
if (dataU != NULL)
delete dataU;
if (dataV != NULL)
delete dataV;
// update current width and height
crtWidth = dctData.width;
crtHeight = dctData.height;
// init dctData
if (sspData != NULL)
delete sspData;
sspData = new RawImg(dctData);
for (int i = 0, j = 0; i < dctData.width * dctData.height; ++i, j += 4)
{
sspData->data[i*3] *= QM[crtQM][(i/dctData.width)%8][(i%dctData.width)%8];
sspData->data[i*3+1] *= QM[crtQM][(i/dctData.width)%8][(i%dctData.width)%8];;
sspData->data[i*3+2] *= QM[crtQM][(i/dctData.width)%8][(i%dctData.width)%8];;
}
// idct
for(int i = 0; i<dctData.width; i += 8)
{
for(int j = 0; j<dctData.height; j += 8)
{
int w = 8, h = 8;
if( i+7 >= dctData.width)
w = dctData.width - i;
if( j+7 >= dctData.height)
h = dctData.height - j;
double **f;
f = new double*[8];
for(int u = 0; u < 8; u++)
f[u] = new double[8];
for(int inc = 0; inc<3; inc++)
{
for(int u = 0; u<w; u++)
for(int v = 0; v<h; v++)
f[u][v] = sspData->data[((j+v)*dctData.width+(i+u))*3+inc];
// fill in the rest
for(int u=w; u<8; u++)
for(int v = 0; v<h; v++)
f[u][v] = f[w-1][v];
for(int v=h; v<8; v++)
for(int u = 0; u<h; u++)
f[u][v] = f[u][h-1];
for(int u=w; u<8; u++)
for(int v=h; v<8; v++)
f[u][v] = f[w-1][h-1];
idct(f);
for(int u = 0; u<w; u++)
for(int v = 0; v<h; v++)
sspData->data[((j+v)*dctData.width+(i+u))*3+inc] = (int)f[u][v]+128;
}
for(int u = 0; u < 8; u++ )
delete[]f[u];
delete []f;
}
}
dataY = new unsigned char[dctData.width * dctData.height * 4];
dataU = new unsigned char[dctData.width * dctData.height * 4];
dataV = new unsigned char[dctData.width * dctData.height * 4];
// fill in data
for (int i = 0, j = 0; i < dctData.width * dctData.height; ++i, j += 4)
{
dataY[j] = dataY[j + 1] = dataY[j + 2] = sspData->data[i*3];
dataU[j] = dataU[j + 1] = dataU[j + 2] = sspData->data[i*3+1];
dataV[j] = dataV[j + 1] = dataV[j + 2] = sspData->data[i*3+2];
dataY[j + 3] = dataU[j+3] = dataV[j+3] = ~0; // Alpha
}
// display YUV images
imgY = new QImage(dataY, dctData.width, dctData.height, QImage::Format_ARGB32);
imgShowY = new QLabel(this);
imgShowY->setPixmap(QPixmap::fromImage(imgY->scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowY->installEventFilter(this);
imgShowY->show();
mainLayout->addWidget(imgShowY, 0, 0, 10, 2);
imgU = new QImage(dataU, dctData.width, dctData.height, QImage::Format_ARGB32);
imgShowU = new QLabel(this);
imgShowU->setPixmap(QPixmap::fromImage(imgU->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowU->installEventFilter(this);
imgShowU->show();
mainLayout->addWidget(imgShowU, 0, 2, 5, 1);
imgV = new QImage(dataV, dctData.width, dctData.height, QImage::Format_ARGB32);
imgShowV = new QLabel(this);
imgShowV->setPixmap(QPixmap::fromImage(imgV->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowV->installEventFilter(this);
imgShowV->show();
mainLayout->addWidget(imgShowV, 5, 2, 5, 1);
emit sspChangingRgb(*sspData);
}
bool SspDsp::eventFilter(QObject* obj, QEvent* event) {
int bX, bY;
if (event->type() == QEvent::MouseButtonPress) {
const QMouseEvent* const currentPos = static_cast<const QMouseEvent*>(event);
const QPoint p = currentPos->pos();
if (obj == imgShowY) {
// find yellow block
bX = (float)p.x() / imgShowY->width() * crtWidth / 8;
bY = (float)p.y() / imgShowY->height() * crtHeight / 8;
// draw a yellow square
if (tmpDataY == NULL)
tmpDataY = new unsigned char[crtWidth * crtHeight * 4];
for (int i = 0, j = 0; i < crtWidth * crtHeight; ++i, j += 4) {
if (isYellow(bX, bY, i)) {
tmpDataY[j] = 0;
tmpDataY[j + 1] = 255;
tmpDataY[j + 2] = 255;
tmpDataY[j + 3] = ~0;
}
else {
tmpDataY[j] = dataY[j];
tmpDataY[j + 1] = dataY[j + 1];
tmpDataY[j + 2] = dataY[j + 2];
tmpDataY[j + 3] = dataY[j + 3];
}
}
// display new image
if (imgY != NULL)
delete imgY;
imgY = new QImage(tmpDataY, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowY = new QLabel(this);
imgShowY->setPixmap(QPixmap::fromImage(imgY->scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowY->installEventFilter(this);
imgShowY->show();
mainLayout->addWidget(imgShowY, 0, 0, 10, 2);
if (imgU != NULL)
delete imgU;
imgU = new QImage(dataU, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowU = new QLabel(this);
imgShowU->setPixmap(QPixmap::fromImage(imgU->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowU->installEventFilter(this);
imgShowU->show();
mainLayout->addWidget(imgShowU, 0, 2, 5, 1);
if (imgV != NULL)
delete imgV;
imgV = new QImage(dataV, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowV = new QLabel(this);
imgShowV->setPixmap(QPixmap::fromImage(imgV->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowV->installEventFilter(this);
imgShowV->show();
mainLayout->addWidget(imgShowV, 5, 2, 5, 1);
// extract current block
emit sspChangingMatrix2(bX, bY, 0);
}
else if (obj == imgShowU) {
// find yellow block
bX = (float)p.x() / imgShowU->width() * crtWidth / 8;
bY = (float)p.y() / imgShowU->height() * crtHeight / 8;
// draw a yellow square
if (tmpDataU != NULL)
delete tmpDataU;
tmpDataU = new unsigned char[crtWidth * crtHeight * 4];
for (int i = 0, j = 0; i < crtWidth * crtHeight; ++i, j += 4) {
if (isYellow(bX, bY, i)) {
tmpDataU[j] = 0;
tmpDataU[j + 1] = 255;
tmpDataU[j + 2] = 255;
tmpDataU[j + 3] = ~0;
}
else {
tmpDataU[j] = dataU[j];
tmpDataU[j + 1] = dataU[j + 1];
tmpDataU[j + 2] = dataU[j + 2];
tmpDataU[j + 3] = dataU[j + 3];
}
}
// display new image
if (imgY != NULL)
delete imgY;
imgY = new QImage(dataY, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowY = new QLabel(this);
imgShowY->setPixmap(QPixmap::fromImage(imgY->scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowY->installEventFilter(this);
imgShowY->show();
mainLayout->addWidget(imgShowY, 0, 0, 10, 2);
if (imgU != NULL)
delete imgU;
imgU = new QImage(tmpDataU, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowU = new QLabel(this);
imgShowU->setPixmap(QPixmap::fromImage(imgU->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowU->installEventFilter(this);
imgShowU->show();
mainLayout->addWidget(imgShowU, 0, 2, 5, 1);
if (imgV != NULL)
delete imgV;
imgV = new QImage(dataV, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowV = new QLabel(this);
imgShowV->setPixmap(QPixmap::fromImage(imgV->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowV->installEventFilter(this);
imgShowV->show();
mainLayout->addWidget(imgShowV, 5, 2, 5, 1);
// extract current block
emit sspChangingMatrix2(bX, bY, 1);
}
else if (obj == imgShowV) {
// find yellow block
bX = (float)p.x() / imgShowV->width() * crtWidth / 8;
bY = (float)p.y() / imgShowV->height() * crtHeight / 8;
// draw a yellow square
if (tmpDataV != NULL)
delete tmpDataV;
tmpDataV = new unsigned char[crtWidth * crtHeight * 4];
for (int i = 0, j = 0; i < crtWidth * crtHeight; ++i, j += 4) {
if (isYellow(bX, bY, i)) {
tmpDataV[j] = 0;
tmpDataV[j + 1] = 255;
tmpDataV[j + 2] = 255;
tmpDataV[j + 3] = ~0;
}
else {
tmpDataV[j] = dataV[j];
tmpDataV[j + 1] = dataV[j + 1];
tmpDataV[j + 2] = dataV[j + 2];
tmpDataV[j + 3] = dataV[j + 3];
}
}
// display new image
if (imgY != NULL)
delete imgY;
imgY = new QImage(dataY, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowY = new QLabel(this);
imgShowY->setPixmap(QPixmap::fromImage(imgY->scaled(300, 300, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowY->installEventFilter(this);
imgShowY->show();
mainLayout->addWidget(imgShowY, 0, 0, 10, 2);
if (imgU != NULL)
delete imgU;
imgU = new QImage(dataU, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowU = new QLabel(this);
imgShowU->setPixmap(QPixmap::fromImage(imgU->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowU->installEventFilter(this);
imgShowU->show();
mainLayout->addWidget(imgShowU, 0, 2, 5, 1);
if (imgV != NULL)
delete imgV;
imgV = new QImage(tmpDataV, crtWidth, crtHeight, QImage::Format_ARGB32);
imgShowV = new QLabel(this);
imgShowV->setPixmap(QPixmap::fromImage(imgV->scaled(150, 150, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
imgShowV->installEventFilter(this);
imgShowV->show();
mainLayout->addWidget(imgShowV, 5, 2, 5, 1);
// extract current block
emit sspChangingMatrix2(bX, bY, 2);
}
}
return false;
}
bool SspDsp::isYellow(int bX, int bY, int p) {
if ((p / crtWidth + 1 < 8 * bY) || (p / crtWidth - 1 > 8 * bY + 7))
return false;
if ((p % crtWidth + 1 < 8 * bX) || (p % crtWidth - 1 > 8 * bX + 7))
return false;
if ((p / crtWidth + 1 != 8 * bY) && (p / crtWidth - 1 != 8 * bY + 7) && (p % crtWidth + 1 != 8 * bX) && (p % crtWidth - 1 != 8 * bX + 7))
return false;
return true;
}