forked from IENT/YUView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplaywidget.cpp
357 lines (298 loc) · 12 KB
/
displaywidget.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
/* YUView - YUV player with advanced analytics toolset
* Copyright (C) 2015 Institut für Nachrichtentechnik
* RWTH Aachen University, GERMANY
*
* YUView is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* YUView is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with YUView. If not, see <http://www.gnu.org/licenses/>.
*/
#include "displaywidget.h"
#include "frameobject.h"
#include <QPainter>
#include <QMessageBox>
#include <QSettings>
#include <QUrl>
#include <QTextDocument>
#ifndef _USE_MATH_DEFINES
#define _USE_MATH_DEFINES
#endif
#include <math.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
DisplayWidget::DisplayWidget(QWidget *parent) : QWidget(parent)
{
p_drawGrid = false;
p_gridSize = 64;
p_displayObject = NULL;
p_overlayStatisticsObject = NULL;
p_displayRect = QRect();
p_selectionRect = QRect();
p_zoomBoxPoint = QPoint();
QPalette Pal(palette());
// load color from preferences
QSettings settings;
QColor bgColor = settings.value("Background/Color").value<QColor>();
Pal.setColor(QPalette::Background, bgColor);
setAutoFillBackground(true);
setPalette(Pal);
}
DisplayWidget::~DisplayWidget() {
}
void DisplayWidget::drawFrame(int frameIdx)
{
// make sure that frame objects contain requested frame
if( p_displayObject != NULL )
p_displayObject->loadImage(frameIdx);
if( p_overlayStatisticsObject )
p_overlayStatisticsObject->loadImage(frameIdx);
// changed from repaint to update -> performance gain?!
update();
}
QPixmap DisplayWidget::captureScreenshot()
{
QPixmap pixmap;
QImage tmpImage(p_displayRect.size(), QImage::Format_ARGB32);
tmpImage.fill(qRgba(0, 0, 0, 0)); // clear with transparent color
pixmap.convertFromImage(tmpImage);
// TODO: check if it is possible to _not_ draw the widget's background
render(&pixmap, QPoint(), QRegion(p_displayRect), RenderFlags(DrawChildren));
return pixmap;
}
void DisplayWidget::resetView()
{
p_displayRect.setWidth(displayObject()->width());
p_displayRect.setHeight(displayObject()->height());
}
void DisplayWidget::drawFrame()
{
//draw Frame
QPainter painter(this);
QPixmap image = p_displayObject->displayImage();
painter.drawPixmap(p_displayRect, image, image.rect());
}
void DisplayWidget::drawRegularGrid()
{
// draw regular grid
QPainter painter(this);
float stepSize = (float)p_gridSize*zoomFactor();
for (float i=0; i<=p_displayRect.width(); i+=stepSize)
{
QPointF start = p_displayRect.topLeft() + QPointF(i,0);
QPointF end = p_displayRect.bottomLeft() + QPointF(i,0);
painter.drawLine(start, end);
}
for (float i=0; i<=p_displayRect.height(); i+=stepSize)
{
QPointF start = p_displayRect.topLeft() + QPointF(0,i);
QPointF end = p_displayRect.topRight() + QPointF(0,i);
painter.drawLine(start, end);
}
}
void DisplayWidget::drawStatisticsOverlay()
{
// Check if the size (resolution) of the statistics overlay and the display object match
if (!(p_displayObject->width() == p_overlayStatisticsObject->width() &&
p_displayObject->height() == p_overlayStatisticsObject->height())) {
// Error. Don't draw non matching statistics.
return;
}
//draw Frame
QPainter painter(this);
QPixmap overlayImage = p_overlayStatisticsObject->displayImage();
painter.drawPixmap(p_displayRect, overlayImage, overlayImage.rect());
}
void DisplayWidget::drawZoomFactor()
{
QString zoomString;
if( zoomFactor() >= 1.0 )
zoomString = QString::number((int)zoomFactor()) + " ×";
else
zoomString = "1/" + QString::number((int)floor(1.0/zoomFactor())) + " ×";
QFont font = QFont("Helvetica", 24);
QFontMetrics fm(font);
int height = fm.height()*(zoomString.count("\n")+1);
QPoint targetPoint( 10, 10+height );
QPainter painter(this);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen( QColor(Qt::black) );
painter.setFont( font );
painter.drawText(targetPoint, zoomString);
}
void DisplayWidget::clear()
{
QPalette Pal(palette());
// load color from preferences
QSettings settings;
QColor bgColor = settings.value("Background/Color").value<QColor>();
Pal.setColor(QPalette::Background, bgColor);
setAutoFillBackground(true);
setPalette(Pal);
// actually clear by drawing an invalid frame index
drawFrame(INT_INVALID);
}
void DisplayWidget::paintEvent(QPaintEvent*)
{
// check if we have at least one object to draw
if( p_displayObject != NULL )
{
p_displayObject->setInfo("");
drawFrame();
// if there is a zoom, show it
if( zoomFactor() != 1.0 )
{
drawZoomFactor();
}
// draw overlay, if requested
if(p_overlayStatisticsObject)
{
drawStatisticsOverlay();
}
if(p_drawGrid)
{
drawRegularGrid();
}
// draw rectangular selection area.
if (!p_selectionRect.isEmpty())
{
drawSelectionRectangle();
}
// draw Zoombox
if (!p_zoomBoxPoint.isNull())
{
drawZoomBox();
}
}
}
void DisplayWidget::drawSelectionRectangle()
{
QPainter painter(this);
painter.drawRect(p_selectionRect);
}
void DisplayWidget::drawZoomBox()
{
// position within image with range [0:width-1;0:height-1]
int srcX = ((double)p_zoomBoxPoint.x() - (double)p_displayRect.left() + 0.5)/zoomFactor();
int srcY = ((double)p_zoomBoxPoint.y() - (double)p_displayRect.top() + 0.5)/zoomFactor();
QPoint srcPoint = QPoint(srcX, srcY);
ValuePairList valuesAtPos = p_displayObject->getValuesAt( srcPoint.x(), srcPoint.y() );
// zoom in
const int zoomBoxFactor = 32;
const int srcSize = 5;
const int targetSize = srcSize*zoomBoxFactor;
const int margin = 11;
const int padding = 6;
QPainter painter(this);
QSettings settings;
QColor bgColor = settings.value("Background/Color").value<QColor>();
// mark pixel under cursor
int zoomFactorInt = (int)zoomFactor();
// restrict this marker to pixel grid
int xPixel = (srcX*zoomFactorInt) + p_displayRect.left();//((p_zoomBoxPoint.x()-(zoomFactorInt>>1))/zoomFactorInt)*zoomFactorInt;
int yPixel = (srcY*zoomFactorInt) + p_displayRect.top();//((p_zoomBoxPoint.y()-(zoomFactorInt>>1))/zoomFactorInt)*zoomFactorInt;
QRect pixelRect = QRect(xPixel, yPixel, zoomFactorInt, zoomFactorInt);
painter.drawRect(pixelRect);
// translate to lower right corner
painter.translate(width()-targetSize-margin, height()-targetSize-margin);
// fill zoomed image into rect
QPixmap image = p_displayObject->displayImage();
int internalScaleFactor = p_displayObject->internalScaleFactor();
QRect srcRect = QRect((srcPoint.x()-(srcSize>>1))*internalScaleFactor, (srcPoint.y()-(srcSize>>1))*internalScaleFactor, srcSize*internalScaleFactor, srcSize*internalScaleFactor);
QRect targetRect = QRect(0, 0, targetSize, targetSize);
painter.fillRect(targetRect, bgColor);
painter.drawPixmap(targetRect, image, srcRect);
// if we have an overlayed statistics image, draw it also and get pixel value from there...
if(p_overlayStatisticsObject)
{
QPixmap overlayImage = p_overlayStatisticsObject->displayImage();
int internalScaleFactorOverlay = p_overlayStatisticsObject->internalScaleFactor();
QRect srcRectOverlay = QRect((srcPoint.x()-(srcSize>>1))*internalScaleFactorOverlay, (srcPoint.y()-(srcSize>>1))*internalScaleFactorOverlay, srcSize*internalScaleFactorOverlay, srcSize*internalScaleFactorOverlay);
// draw overlay
painter.drawPixmap(targetRect, overlayImage, srcRectOverlay);
// use overlay raw value
valuesAtPos = p_overlayStatisticsObject->getValuesAt( srcPoint.x(), srcPoint.y() );
}
// draw border
painter.drawRect(targetRect);
// mark pixel under cursor in zoom box
const int srcPixelSize = 1;
const int targetPixelSize = srcPixelSize*zoomBoxFactor;
QRect pixelRectZoomed = QRect((targetSize-targetPixelSize)/2, (targetSize-targetPixelSize)/2, targetPixelSize, targetPixelSize);
painter.drawRect(pixelRectZoomed);
// draw pixel info
QString pixelInfoString = QString("<h4>Coordinates</h4>"
"<table width=\"100%\">"
"<tr><td>X:</td><td align=\"right\">%1</td></tr>"
"<tr><td>Y:</td><td align=\"right\">%2</td></tr>"
"</table>"
).arg(srcPoint.x()).arg(srcPoint.y());
// if we have some values, show them
if( valuesAtPos.size() > 0 )
{
pixelInfoString.append( "<h4>Values</h4>"
"<table width=\"100%\">" );
for (int i = 0; i < valuesAtPos.size(); ++i)
{
pixelInfoString.append( QString("<tr><td>%1:</td><td align=\"right\">%2</td></tr>").arg(valuesAtPos[i].first).arg(valuesAtPos[i].second) );
}
pixelInfoString.append( "</table>" );
}
QTextDocument textDocument;
textDocument.setDefaultStyleSheet("* { color: #FFFFFF }");
textDocument.setHtml(pixelInfoString);
textDocument.setTextWidth(textDocument.size().width());
QRect rect(QPoint(0, 0), textDocument.size().toSize() + QSize(2*padding, 2*padding));
painter.translate(-rect.width(), targetSize-rect.height());
painter.setBrush(QColor(0, 0, 0, 70));
painter.drawRect(rect);
painter.translate(padding, padding);
textDocument.drawContents(&painter);
painter.end();
}
void DisplayWidget::setRegularGridParameters(bool show, int size, QColor gridColor) {
p_drawGrid = show;
p_gridSize = size;
p_gridColor = gridColor;
update();
}
void DisplayWidget::setOverlayStatisticsObject(StatisticsObject* newStatisticsObject)
{
p_overlayStatisticsObject = newStatisticsObject;
if (p_displayObject != NULL && p_overlayStatisticsObject != NULL) {
// Check if the size (resolution) of the statistics overlay and the display object match
if (!(p_displayObject->width() == p_overlayStatisticsObject->width() &&
p_displayObject->height() == p_overlayStatisticsObject->height())) {
// Error. Don't draw non matching statistics.
p_overlayStatisticsObject->setInfo("Cannot display Statistics: Size does not match YUV size.");
p_displayObject->setInfo("Cannot display Statistics: Size does not match YUV size.");
return;
}
}
if (p_overlayStatisticsObject != NULL) p_overlayStatisticsObject->setInfo("");
if (p_displayObject != NULL) p_displayObject->setInfo("");
}
void DisplayWidget::setDisplayObject(DisplayObject* newDisplayObject)
{
p_displayObject = newDisplayObject;
if (p_displayObject != NULL && p_overlayStatisticsObject != NULL) {
// Check if the size (resolution) of the statistics overlay and the display object match
if (!(p_displayObject->width() == p_overlayStatisticsObject->width() &&
p_displayObject->height() == p_overlayStatisticsObject->height())) {
// Error. Don't draw non matching statistics.
p_overlayStatisticsObject->setInfo("Cannot display Statistics: Size does not match YUV size.");
p_displayObject->setInfo("Cannot display Statistics: Size does not match YUV size.");
return;
}
}
if (p_overlayStatisticsObject != NULL) p_overlayStatisticsObject->setInfo("");
if (p_displayObject != NULL) p_displayObject->setInfo("");
}