-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglwidget.cpp
591 lines (509 loc) · 18 KB
/
glwidget.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#if defined(GLSL_YUV)
#include "sageShader.h"
#else
#include <QtOpenGL>
#endif
#include <QtGui>
#include <stdio.h>
#include <png.h>
#include "glwidget.h"
//////////////////////////////////////////////////////////////////////////////
#if defined(GLSL_YUV)
GLhandleARB GLWidget::programHandleYUV = 0;
#endif
////////////////////////////////////////////////////////////////////////////////
static const GLchar *yuv_fragment = " \
uniform sampler2D yuvtex; \
uniform float texel_width; \
uniform float texture_width; \
void main() \
{ \
float red, green, blue; \
vec4 luma_chroma; \
float luma, chroma_u, chroma_v; \
float pixelx, pixely; \
float xcoord, ycoord; \
pixelx = gl_TexCoord[0].x; \
pixely = gl_TexCoord[0].y; \
xcoord = floor (pixelx * texture_width); \
luma_chroma = texture2D(yuvtex, vec2(pixelx, pixely)); \
if (0.0 == mod(xcoord , 2.0)) \
{ \
chroma_u = luma_chroma.r; \
chroma_v = texture2D(yuvtex, vec2(pixelx + texel_width, pixely)).r; \
} \
else \
{ \
chroma_v = luma_chroma.r; \
chroma_u = texture2D(yuvtex, vec2(pixelx - texel_width, pixely)).r; \
} \
luma = 1.1640625 * (luma_chroma.a - 0.0625); \
chroma_u = chroma_u - 0.5; \
chroma_v = chroma_v - 0.5; \
red = luma + 1.59765625 * chroma_v; \
green = luma - 0.390625 * chroma_u - 0.8125 * chroma_v; \
blue = luma + 2.015625 * chroma_u; \
gl_FragColor = vec4(red, green, blue, 1.0); \
}";
#if !defined(GLSL_YUV)
inline unsigned char Clamp(int value)
{
if(value > 255) return 255;
if(value < 0) return 0;
return value;
}
void GLWidget::createTableLookup(){
int yy, uu, vv, ug_plus_vg, ub, vr, val;
// Red
for (int y = 0; y < 256; y++) {
for (int v = 0; v < 256; v++) {
yy = y << 8;
vv = v - 128;
vr = vv * 359;
val = (yy + vr) >> 8;
red[y][v] = Clamp(val);
}
}
// Blue
for (int y = 0; y < 256; y++) {
for (int u = 0; u < 256; u++) {
yy = y << 8;
uu = u - 128;
ub = uu * 454;
val = (yy + ub) >> 8;
blue[y][u] = Clamp(val);
}
}
// Green
for (int y = 0; y < 256; y++) {
for (int u = 0; u < 256; u++) {
for (int v = 0; v < 256; v++) {
yy = y << 8;
uu = u - 128;
vv = v - 128;
ug_plus_vg = uu * 88 + vv * 183;
val = (yy - ug_plus_vg) >> 8;
green[y][u][v] = Clamp(val);
}
}
}
}
#endif
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(QGLFormat(QGL::DoubleBuffer), parent) //SampleBuffers::DoubleBuffer
{
fprintf(stderr, "initing GLWIDGET\n");
#if !defined(GLSL_YUV)
this->createTableLookup();
#endif
this->buffer=NULL;
this->texture_buffer = NULL;
testFileWritten = false;
this->bpp = 2;
this->readyToReceiveNewFrame = false;
this->bufferReady = false;
}
#if defined(GLSL_YUV)
void GLWidget::initShaderProgram(){
GLenum err = glewInit();
if(GLEW_OK!=err){
fprintf(stderr, "Failed to init glew. exit\n");
exit(1);
}
if (programHandleYUV==0) {
//fprintf(stderr, "initing gl shader\n");
programHandleYUV = GLSLinstallFragmentShader(yuv_fragment);
glActiveTexture(GL_TEXTURE0);
int h=glGetUniformLocation(GLWidget::programHandleYUV,"yuvtex");
glUniform1i(h, 0);
int texture_width_location, texel_width_location;
//texture withd
texture_width_location = glGetUniformLocation(GLWidget::programHandleYUV, "texture_width");
//fprintf(stderr, "texture width location:%d\n", texture_width_location);
if (-1 == texture_width_location)
fprintf(stderr, "Warning: can't get texture_width location\n");
else
glUniform1f(texture_width_location, (float)this->getTextureWidth());
//texel width
texel_width_location = glGetUniformLocation(GLWidget::programHandleYUV, "texel_width");
//fprintf(stderr, "texel_width_location:%d\n", texel_width_location);
if (-1 == texel_width_location)
fprintf(stderr, "Warning: can't get texel_width location\n");
else
glUniform1f(texel_width_location, (1.0 / (float)this->getTextureWidth()));
//fprintf(stderr, "done initing gl shader %d\n", programHandleYUV);
}
}
#endif
GLWidget::~GLWidget()
{
if(this->buffer)
free(this->buffer);
this->deleteTexture();
}
QSize GLWidget::minimumSizeHint() const
{
return QSize(960, 540);
}
QSize GLWidget::sizeHint() const
{
return QSize(1920, 1080);
}
void GLWidget::updateGLSlot(){
//fprintf(stderr, "updateGLSlot\n");
if(this->buffer ==NULL)
return;
if(this->texture_buffer == NULL)
return;
#define clip(A) ( ((A)<0) ? 0 : ( ((A)>255) ? 255 : (A) ) )
#define YUV444toRGB888(Y,U,V,R,G,B) \
R = clip(( 298 * (Y-16) + 409 * (V-128) + 128) >> 8); \
G = clip(( 298 * (Y-16) - 100 * (U-128) - 208 * (V-128) + 128) >> 8); \
B = clip(( 298 * (Y-16) + 516 * (U-128) + 128) >> 8);
//while(this->bufferReady ==false);
if(this->bufferReady == false)
return;
this->mutex_lock.lock();
this->readyToReceiveNewFrame = false;
#if defined(GLSL_YUV)
glBindTexture(GL_TEXTURE_2D, this->texture);
glTexImage2D(GL_TEXTURE_2D , 0, GL_LUMINANCE_ALPHA, this->getTextureWidth(), this->getTextureHeight(),
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, this->buffer);
this->readyToReceiveNewFrame = true;
this->mutex_lock.unlock();
#else
/*
unsigned char *yuv = buffer;
unsigned char u,v,y1,y2;
unsigned char r1,r2,g1,g2,b1,b2;
int i, k;
k = 0;
for (i=0;i< (this->getTextureWidth()*this->getTextureHeight())/2;i++) {
u = yuv[4*i+0];
y1 = yuv[4*i+1];
v = yuv[4*i+2];
y2 = yuv[4*i+3];
YUV444toRGB888(y1, u, v, r1,g1,b1);
YUV444toRGB888(y2, u, v, r2,g2,b2);
texture_buffer[k + 0] = r1;
texture_buffer[k + 1] = g1;
texture_buffer[k + 2] = b1;
texture_buffer[k + 3] = r2;
texture_buffer[k + 4] = g2;
texture_buffer[k + 5] = b2;
k += 6;
}*/
//
unsigned int boundry = this->getTextureWidth()* this->getTextureHeight() * bpp;
unsigned char y, u, v;
unsigned char * yuv = this->buffer;
unsigned int j=0;
//#pragma omp for
for(unsigned int i=0; i<boundry; i+=4, j+=6){
y = yuv[i+1];
u = yuv[i];
v = yuv[i+2];
texture_buffer[j] = red[y][v];
texture_buffer[j+1] = green[y][u][v];
texture_buffer[j+2] = blue[y][u];
y = yuv[i+3];
texture_buffer[j+3] = red[y][v];
texture_buffer[j+4] = green[y][u][v];
texture_buffer[j+5] = blue[y][u];
}
this->readyToReceiveNewFrame = true;
this->mutex_lock.unlock();
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, this->getTextureWidth(), this->getTextureHeight(),
GL_RGB, GL_UNSIGNED_BYTE, texture_buffer);
#endif
//this->updateGL();
this->update();
}
void GLWidget::setBuffer(unsigned char * _buffer){
if(_buffer == NULL){
fprintf(stderr, "NULL return \n");
return;
}
this->mutex_lock.lock();
this->bufferReady = false;
memcpy(this->buffer, _buffer, this->getTextureWidth() * this->getTextureHeight() * bpp);
this->bufferReady = true;
this->mutex_lock.unlock();
}
void GLWidget::initializeGL()
{
qglClearColor(QColor(Qt::black));
//draw here
/*this->initDisplayList();*/
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
this->renewTexture();
#if defined(GLSL_YUV)
this->initShaderProgram();
#endif
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
//draw quad
//drawQuad(depth, alpha, tempAlpha, left, right, bottom, top);
glBegin(GL_QUADS);
//glColor3f(1, 1, 1);
glTexCoord2d(0, 1);
glVertex3i( 0, 0, 0);
glTexCoord2d(1 , 1);
glVertex3i(this->getTextureWidth(), 0, 0);
glTexCoord2d(1, 0);
glVertex3i(this->getTextureWidth(), this->getTextureHeight(), 0);
glTexCoord2d(0, 0);
glVertex3i(0, this->getTextureHeight(), 0);
glEnd();
this->swapBuffers();
glFlush();
}
void GLWidget::resizeGL(int w, int h)
{
int width = w;
int height = (h>0)?h:1;
glViewport(0,0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, this->getTextureWidth() , 0 , this->getTextureHeight(), 0.0 , 1.0);
glMatrixMode(GL_MODELVIEW);
}
void GLWidget::initDisplayList()
{
//std::cout<<"[start initDisplayList@GLWidget]" <<std::endl;
fprintf(stderr, "[GLWidget::initDisplayList] texture width: %d height:%d\n", this->getTextureWidth(), this->getTextureHeight());
/***creating texture **********/
glEnable(GL_TEXTURE_2D);
//get the texture
glGenTextures(1, &this->texture);
GLuint dList = glGenLists(1);
glNewList(dList, GL_COMPILE);
// glColor3f(1, 1, 1);
//bind the texture
glBindTexture(GL_TEXTURE_2D, this->texture);
/* Setup some parameters for texture filters */
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
// glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); // how to set texture coords
// glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
//init texture
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glTexCoord2d(0, 1);
glVertex3i( 0, 0, 0);
glTexCoord2d(1 , 1);
glVertex3i(this->getTextureWidth(), 0, 0);
glTexCoord2d(1, 0);
glVertex3i(this->getTextureWidth(), this->getTextureHeight(), 0);
glTexCoord2d(0, 0);
glVertex3i(0, this->getTextureHeight(), 0);
glEnd();
glEndList();
this->setDisplayList(dList);
}
/**
* get and set display list
*/
GLuint GLWidget::getDisplayList()
{
return this->displayList;
}
void GLWidget::setDisplayList(GLuint dList)
{
this->displayList = dList;
}
/**
* get and set texture
*/
GLuint GLWidget::getTexture()
{
return this->texture;
}
void GLWidget::setTexture(GLuint t)
{
this->texture = t;
}
void GLWidget::deleteTexture()
{
if (texture >= 0) {
glDeleteTextures(1, &texture);
if (this->texture_buffer)
free(this->texture_buffer);
}
}
void GLWidget::initBuffer(){
fprintf(stderr, "buffer initialized\n");
this->buffer = (unsigned char *) malloc(this->getTextureWidth() * this->getTextureHeight() * bpp);
this->readyToReceiveNewFrame = true;
}
/**
* renew texture
*/
void GLWidget::renewTexture(){
int newWidth, newHeight;
newWidth = this->getTextureWidth(); // getMax2n(texInfo.width);
newHeight = this->getTextureHeight(); // getMax2n(texInfo.height);
int maxt;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, (GLint*)&maxt);
if (newWidth > maxt) {
fprintf(stderr, "Warning: trying to create texture %d, bigger than maximun %d\n", newWidth, maxt);
newWidth = maxt;
}
if (newHeight > maxt) {
fprintf(stderr, "Warning: trying to create texture %d, bigger than maximun %d\n", newHeight, maxt);
newHeight = maxt;
}
/*if (newWidth <= this->getTextureWidth() && newHeight <= this->getTextureHeight()) {
fprintf(stderr,"exit here................\n");
return;
}*/
this->setTextureWidth(newWidth);
this->setTextureHeight(newHeight);
deleteTexture();
GLuint handle;
glGenTextures(1, &handle);
texture = handle;
// Create GL texture object
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
this->texture_buffer = (GLubyte *) malloc(this->getTextureWidth() * this->getTextureHeight() * bpp);
memset(this->texture_buffer, 0, this->getTextureWidth() * this->getTextureHeight() * bpp);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, handle);
#if defined(GLSL_YUV)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
glTexImage2D(GL_TEXTURE_2D , 0, GL_LUMINANCE_ALPHA, this->getTextureWidth(), this->getTextureHeight(),
0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, this->buffer);
#else
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, handle);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this->getTextureWidth(), this->getTextureHeight(), 0,
GL_RGB, GL_UNSIGNED_BYTE, this->texture_buffer);
#endif
}
int GLWidget::WritePNG(const char *fileName, int width, int height,
GLenum imageFormat, GLenum imageType,
GLvoid * imagePtr)
{
FILE *fp;
png_structp pngPtr;
png_infop infoPtr;
int colorType, bitDepth;
int k;
png_bytep rowPointers[4096];
png_bytep rowPtr;
int rowWidth;
/* open the file */
fp = fopen(fileName, "wb");
if (fp == NULL) {
printf("Viewperf: could not open image dump file: %s\n", fileName);
return 1;
}
/* Create and initialize the png_struct with the desired error handler
* functions. If you want to use the default stderr and longjump method,
* you can supply NULL for the last three parameters. We also check that
* the library version is compatible with the one used at compile time,
* in case we are using dynamically linked libraries. REQUIRED.
*/
pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
(png_voidp) NULL, (png_error_ptr)NULL, (png_error_ptr)NULL);
if (pngPtr == NULL)
{
fclose(fp);
printf("Viewperf: PNG image creation failed\n");
return 1;
}
/* Allocate/initialize the image information data. REQUIRED */
infoPtr = png_create_info_struct(pngPtr);
if (infoPtr == NULL)
{
fclose(fp);
png_destroy_write_struct(&pngPtr, (png_infopp)NULL);
printf("Viewperf: PNG image information data creation failed\n");
return 1;
}
// MWC: 2014-06-10: This section of code problematic on newer versions of Linux
// if (setjmp(pngPtr->jmpbuf)) {
// /* If we get here, we had a problem reading the file */
// fclose(fp);
// png_destroy_write_struct(&pngPtr, (png_infopp)NULL);
// printf("Viewperf: PNG write error occurred\n");
// return 1;
// }
/* set up the output control if you are using standard C streams */
png_init_io(pngPtr, fp);
/* Set the image information here. Width and height are up to 2^31,
* bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
* the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
* PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
* or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
* PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
* currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
*/
switch (imageFormat) {
case GL_COLOR_INDEX:
case GL_LUMINANCE:
colorType = PNG_COLOR_TYPE_GRAY;
rowWidth = 1;
break;
case GL_LUMINANCE_ALPHA:
colorType = PNG_COLOR_TYPE_GRAY_ALPHA;
rowWidth = 2;
break;
case GL_RGB:
colorType = PNG_COLOR_TYPE_RGB;
rowWidth = 3;
break;
case GL_RGBA:
colorType = PNG_COLOR_TYPE_RGB_ALPHA;
rowWidth = 4;
break;
default:
printf("Viewperf: internal error, unknown imageFormat found on dumping\n");
fflush(stdout);
exit(1);
}
if (imageType == GL_UNSIGNED_SHORT) {
bitDepth = 16;
rowWidth *= 2;
} else {
bitDepth = 8;
rowWidth *= 1;
}
png_set_IHDR(pngPtr, infoPtr, width, height, bitDepth, colorType,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
/* Optional gamma chunk is strongly suggested if you have any guess
* as to the correct gamma of the image.
*/
//png_set_gAMA(pngPtr, infoPtr, GAMMA);
/* Write the file header information. REQUIRED */
png_write_info(pngPtr, infoPtr);
/* set row pointers in reverse order */
rowWidth *= width;
for (k = 0; k < height; k++) {
rowPointers[k] = (png_bytep)imagePtr + (height-k-1)*rowWidth;
}
/* One of the following output methods is REQUIRED */
png_write_image(pngPtr, rowPointers);
/* It is REQUIRED to call this to finish writing the rest of the file */
png_write_end(pngPtr, infoPtr);
/* clean up after the write, and free any memory allocated */
png_destroy_write_struct(&pngPtr, (png_infopp)NULL);
/* close the file */
fclose(fp);
/* that's it */
return 0;
}