-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcqtopencvviewergl.cxx
168 lines (125 loc) · 3.65 KB
/
cqtopencvviewergl.cxx
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
/*=========================================================================
Program: OpenCVDisparityTuner
Copyright (c) Julien Jomier
All rights reserved.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "cqtopencvviewergl.h"
#include <QOpenGLFunctions>
CQtOpenCVViewerGl::CQtOpenCVViewerGl(QWidget *parent) : QOpenGLWidget(parent)
{
mSceneChanged = false;
mBgColor = QColor::fromRgb(150, 150, 150);
mOutH = 0;
mOutW = 0;
mImgRatio = 4.0f/3.0f;
mPosX = 0;
mPosY = 0;
}
void CQtOpenCVViewerGl::initializeGL()
{
makeCurrent();
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
float r = ((float)mBgColor.darker().red())/255.0f;
float g = ((float)mBgColor.darker().green())/255.0f;
float b = ((float)mBgColor.darker().blue())/255.0f;
f->glClearColor(r,g,b,1.0f);
}
void CQtOpenCVViewerGl::resizeGL(int width, int height)
{
makeCurrent();
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, 0, 1); // To Draw image in the center of the area
glMatrixMode(GL_MODELVIEW);
// ---> Scaled Image Sizes
mOutH = width/mImgRatio;
mOutW = width;
if(mOutH>height)
{
mOutW = height*mImgRatio;
mOutH = height;
}
emit imageSizeChanged( mOutW, mOutH );
mPosX = (width-mOutW)/2;
mPosY = (height-mOutH)/2;
mSceneChanged = true;
updateScene();
}
void CQtOpenCVViewerGl::updateScene()
{
if( mSceneChanged && this->isVisible() )
{
update();
}
}
void CQtOpenCVViewerGl::paintGL()
{
makeCurrent();
if( !mSceneChanged )
{
return;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderImage();
mSceneChanged = false;
}
void CQtOpenCVViewerGl::renderImage()
{
makeCurrent();
glClear(GL_COLOR_BUFFER_BIT);
if (!mRenderQtImg.isNull())
{
glLoadIdentity();
QImage image; // the image rendered
glPushMatrix();
{
int imW = mRenderQtImg.width();
int imH = mRenderQtImg.height();
// The image is to be resized to fit the widget?
if( imW != this->size().width() &&
imH != this->size().height() )
{
image = mRenderQtImg.scaled( //this->size(),
QSize(mOutW,mOutH),
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation
);
//qDebug( QString( "Image size: (%1x%2)").arg(imW).arg(imH).toAscii() );
}
else
image = mRenderQtImg;
// ---> Centering image in draw area
glRasterPos2i( mPosX, mPosY );
// <--- Centering image in draw area
imW = image.width();
imH = image.height();
glDrawPixels( imW, imH, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
}
glPopMatrix();
// end
glFlush();
}
}
bool CQtOpenCVViewerGl::showImage(cv::Mat image)
{
image.copyTo(mOrigImage);
mImgRatio = (float)image.cols/(float)image.rows;
if( mOrigImage.channels() == 3)
mRenderQtImg = QImage((const unsigned char*)(mOrigImage.data),
mOrigImage.cols, mOrigImage.rows,
mOrigImage.step, QImage::Format_RGB888)/*.rgbSwapped()*/;
else if( mOrigImage.channels() == 1)
mRenderQtImg = QImage((const unsigned char*)(mOrigImage.data),
mOrigImage.cols, mOrigImage.rows,
mOrigImage.step, QImage::Format_Indexed8);
else
return false;
this->resizeGL(this->width(),this->height());
mSceneChanged = true;
updateScene();
return true;
}