-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePlayer.cpp
More file actions
182 lines (151 loc) · 4.03 KB
/
Copy pathImagePlayer.cpp
File metadata and controls
182 lines (151 loc) · 4.03 KB
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
#include "ImagePlayer.h"
// Qt
#include <QApplication>
#include <QMutexLocker>
// oscv
#include "VideoDefs.h"
#include "FileUtils.h"
#include "StringUtils.h"
#include "TimeUtils.h"
#include "ImageUtils.h"
// cv
#ifdef OPENCV_3
#include <opencv2/highgui.hpp> //imread
#else
#include <opencv2/highgui/highgui.hpp>
#endif
using namespace oscv;
ImagePlayer::ImagePlayer(QObject *parent)
: QThread(parent)
, m_stop(true)
, m_name("")
, m_filepath("")
, m_prefix("")
, m_fileExt("")
, m_frameNumber(0)
, m_totalFrames(0)
, m_speed(Speed::Fast)
{
qRegisterMetaType<cv::Mat>("cv::Mat");
}
ImagePlayer::~ImagePlayer()
{
close();
}
void ImagePlayer::close()
{
m_mutex.lock();
m_stop = true;
m_frameNumber = 0;
init("");
m_mutex.unlock();
m_waitCondition.wakeOne();
//condition.wakeAll();
wait();
}
bool ImagePlayer::open(QString filename)
{
bool ok = init(filename);
ok = ok && readFrame();
if ( ok )
{
m_variant.setValue( m_frame );
emit newFrame( m_variant, m_frameNumber );
}
return ok;
}
void ImagePlayer::play()
{
if ( ! isRunning() )
{
{
QMutexLocker locker(&m_mutex);
if ( isStopped() ) {
m_stop = false;
}
}
start(LowPriority);
}
}
bool ImagePlayer::go(int relativeFrame)
{
m_mutex.lock();
int frameNumber = m_frameNumber +relativeFrame;
m_mutex.unlock();
bool ok = setCurrentFrame(frameNumber);
if ( ok )
{
m_mutex.lock();
ok = readFrame();
m_mutex.unlock();
if (ok )
{
m_mutex.lock();
m_variant.setValue( m_frame );
m_mutex.unlock();
emit newFrame( m_variant, m_frameNumber );
}
}
return ok;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Protected
///
void ImagePlayer::run()
{
int delay = static_cast<int>(m_speed)/getFrameRate();
bool ok = true;
while( !m_stop )
{
m_mutex.lock();
m_frameNumber++;
ok = readFrame( );
m_mutex.unlock();
if ( !ok ) {
m_mutex.lock();
m_stop = true;
m_mutex.unlock();
emit donePlay(m_stop);
}
if ( ! m_stop ) {
m_mutex.lock();
delay = static_cast<int>(m_speed)/getFrameRate();
m_variant.setValue( m_frame );
m_mutex.unlock();
emit newFrame( m_variant, m_frameNumber );
}
sleepMiliSecond(delay);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////PRIVATE/////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// filename must be exist before calling this function
bool ImagePlayer::init(QString filename)
{
m_name = filename;
m_filepath = FileUtils::getFilePathWithSeparator(filename);
QString frameNumber;
m_prefix = FileUtils::getNamePrefix(filename, frameNumber);
m_frameNumber = frameNumber.toInt();
m_totalFrames = FileUtils::getNumberOfSequences(filename);
m_fileExt = FileUtils::getExtension(filename);
return ( !m_name.compare("")? false: true );
}
bool ImagePlayer::readFrame()
{
if ( m_frameNumber > m_totalFrames ) {
return false;
}
QString num;
beautifyNumberToString(m_frameNumber, ImageDefs::NUMBER_OF_IMAGESEQ_DIGITS, num);
m_name = m_filepath;
m_name.append(m_prefix).append(num).append(".").append(m_fileExt);
m_frame = cv::imread(m_name.toStdString());
if ( m_frame.data == 0 || m_frame.data == nullptr ) {
return false;
}
return true;
}
///////////////////////////////////////////////////END OF FILE////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////