-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVideoManagerSurface.cpp
129 lines (109 loc) · 4.21 KB
/
VideoManagerSurface.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
/* Camera Mouse Suite
* Copyright (C) 2015, Andrew Kurauchi
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
// This code is partially based on http://stackoverflow.com/questions/26229633/use-of-qabstractvideosurface
#include <QMouseEvent>
#include "VideoManagerSurface.h"
#include "asmOpenCV.h"
#include "Point.h"
#include "TemplateTrackingModule.h"
namespace CMS {
VideoManagerSurface::VideoManagerSurface(Settings &settings, CameraMouseController *controller, QLabel *imageLabel, QObject *parent) :
QAbstractVideoSurface(parent),
settings(settings),
controller(controller)
{
this->imageLabel = imageLabel;
supportedFormats = QList<QVideoFrame::PixelFormat>() << QVideoFrame::Format_RGB24
<< QVideoFrame::Format_RGB32;
connect(imageLabel, SIGNAL(mousePressed(QMouseEvent*)), this, SLOT(mousePressEvent(QMouseEvent*)));
}
VideoManagerSurface::~VideoManagerSurface()
{
// TODO Move to MainWindow if we decide to keep the pointer there
delete(controller);
}
QList<QVideoFrame::PixelFormat> VideoManagerSurface::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
if (handleType == QAbstractVideoBuffer::NoHandle)
{
return supportedFormats;
}
else
{
return QList<QVideoFrame::PixelFormat>();
}
}
bool VideoManagerSurface::present(const QVideoFrame &frame)
{
if (!supportedFormats.contains(frame.pixelFormat()))
{
setError(IncorrectFormatError);
return false;
}
else
{
QVideoFrame frameToProcess(frame);
if(!frameToProcess.map(QAbstractVideoBuffer::ReadWrite))
{
setError(ResourceError);
return false;
}
// This is a shallow operation. it just refer the frame buffer
QImage image(
frameToProcess.bits(),
frameToProcess.width(),
frameToProcess.height(),
frameToProcess.bytesPerLine(),
QVideoFrame::imageFormatFromPixelFormat(frameToProcess.pixelFormat()));
// The kind of mirroring needed depends on the OS
#ifdef Q_OS_LINUX
image = image.mirrored(true, false);
#elif defined Q_OS_WIN
image = image.mirrored(true, true);
#elif defined Q_OS_MAC
image = image.mirrored(true, false);
#endif
cv::Mat mat = ASM::QImageToCvMat(image);
controller->processFrame(mat);
image = ASM::cvMatToQImage(mat);
QImage scaledImage = image.scaled(imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
if (frameSize.isEmpty())
{
settings.setFrameSize(Point(image.size()));
frameSize = image.size();
scaledFrameSize = scaledImage.size();
frameOffset = Point(imageLabel->size().width() - scaledImage.width(), imageLabel->size().height() - scaledImage.height())/2;
}
// QPixmap::fromImage create a new buffer for the pixmap
imageLabel->setPixmap(QPixmap::fromImage(scaledImage));
// Release the data
frameToProcess.unmap();
imageLabel->update();
return true;
}
}
void VideoManagerSurface::mousePressEvent(QMouseEvent *event)
{
if (frameSize.isEmpty())
return;
double offX = frameOffset.X();
double offY = frameOffset.Y();
double x = (double) frameSize.width() * (event->x() - offX) / scaledFrameSize.width();
double y = (double) frameSize.height() * (event->y() - offY) / scaledFrameSize.height();
controller->processClick(Point(x, y));
}
} // namespace CMS