-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPaint.cpp
381 lines (325 loc) · 7.35 KB
/
Paint.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
/*
* Paint.cpp
*
* (c) 2013 Sofian Audry -- info(@)sofianaudry(.)com
* (c) 2013 Alexandre Quessy -- alexandre(@)quessy(.)net
*
* 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/>.
*/
#include "Paint.h"
#include "VideoImpl.h"
#include "VideoUriDecodeBinImpl.h"
#include "VideoV4l2SrcImpl.h"
#include "VideoShmSrcImpl.h"
#include <iostream>
namespace mmp {
UidAllocator Paint::allocator;
void Texture::update()
{
if (textureId == 0)
{
glGenTextures(1, &textureId);
}
}
void Texture::read(const QDomElement& obj)
{
Paint::read(obj);
// Set x and y.
QDomElement e;
e = obj.firstChildElement("x");
if (!e.isNull())
setX(e.text().toDouble());
e = obj.firstChildElement("y");
if (!e.isNull())
setY(e.text().toDouble());
}
void Texture::write(QDomElement& obj)
{
Paint::write(obj);
_writeNode(obj, "x", QString::number(getX()));
_writeNode(obj, "y", QString::number(getY()));
}
Paint::Paint(uid id)
: Element(id, &allocator),
_isPlaying(false)
{
}
Paint::~Paint()
{
allocator.free(getId());
}
Image::Image(int id)
: Texture(id),
_rate(0),
_currentFrame(0),
_currentFrameReal(0.0),
_prevTime(0),
_bits(0)
{
setRate(1.0);
}
Image::Image(const QString uri_, uid id)
: Texture(id),
_rate(0),
_currentFrame(-1),
_currentFrameReal(0.0),
_prevTime(0),
_bits(0)
{
setUri(uri_);
setRate(1.0);
}
bool Image::setUri(const QString &uri)
{
if (uri != _uri)
{
_uri = uri;
build();
_emitPropertyChanged("uri");
}
return !_images.isEmpty();
}
void Image::build()
{
// Read all images.
QImageReader reader(_uri);
_images.clear();
for (int i=0; i<reader.imageCount(); i++)
_images.push_back(
QGLWidget::convertToGLFormat(reader.read())
.mirrored(true, false)
.transformed(QTransform().rotate(180))
);
rewind();
}
void Image::update()
{
if (isAnimation() && isPlaying())
{
// Compute the interval of time since last call to update().
qreal currentTime = _elapsedTime();
qreal diffTime = currentTime - _prevTime;
// Update next frame.
_currentFrameReal += diffTime * _rate * MM::DEFAULT_FRAMES_PER_SECOND;
_currentFrameReal = wrapAround(_currentFrameReal, (qreal)_images.size());
uint nextFrame = (int)_currentFrameReal;
// If frame changed, update image bits pointer.
if (nextFrame != _currentFrame)
{
_currentFrame = nextFrame;
_bits = _images[_currentFrame].bits();
bitsChanged = true;
}
// Reset previous time.
_prevTime = currentTime;
}
}
void Image::rewind()
{
// Reset/restart everything.
if (isAnimation())
{
_currentFrame = 0;
_currentFrameReal = 0.0;
_prevTime = 0;
_timer.start();
}
_bits = _images.isEmpty() ? 0 : _images[0].bits();
bitsChanged = true;
}
const uchar* Image::getBits() {
return _bits;
}
void Image::setRate(double rate)
{
_rate = rate;
}
void Image::_doPlay()
{
_prevTime = _elapsedTime();
}
/* Implementation of the Video class */
Video::Video(int id) : Texture(id),
_uri(""),
_impl(NULL)
{
_impl = new VideoUriDecodeBinImpl();
setRate(1);
setVolume(1);
}
Video::Video(const QString uri_, VideoType type, double rate, uid id):
Texture(id),
_uri(""),
_impl(NULL)
{
switch (type) {
case VIDEO_URI:
_impl = new VideoUriDecodeBinImpl();
break;
case VIDEO_WEBCAM:
_impl = new VideoV4l2SrcImpl();
break;
case VIDEO_SHMSRC:
_impl = new VideoShmSrcImpl();
break;
default:
fprintf (stderr, "Could not determine type for video source\n ");
break;
}
//_impl = new VideoShmSrcImpl();//V4l2SrcImpl();//UriDecodeBinImpl();
setRate(rate);
setVolume(1);
setUri(uri_);
}
// vertigo
Video::~Video()
{
delete _impl;
}
void Video::build()
{
this->_impl->build();
}
int Video::getWidth() const
{
return this->_impl->getWidth();
}
int Video::getHeight() const
{
return this->_impl->getHeight();
}
void Video::update() {
_impl->update();
Texture::update();
}
void Video::rewind()
{
_impl->resetMovie();
}
void Video::lockMutex() {
_impl->lockMutex();
}
void Video::unlockMutex() {
_impl->unlockMutex();
}
const uchar* Video::getBits()
{
return this->_impl->getBits();
}
bool Video::bitsHaveChanged() const
{
return this->_impl->bitsHaveChanged();
}
void Video::setRate(double rate)
{
if (rate != _impl->getRate())
{
_impl->setRate(rate);
_emitPropertyChanged("rate");
}
}
double Video::getRate() const
{
return _impl->getRate();
}
void Video::setVolume(double volume)
{
if (volume != _impl->getVolume())
{
_impl->setVolume(volume);
_emitPropertyChanged("volume");
}
}
double Video::getVolume() const
{
return _impl->getVolume();
}
bool Video::hasVideoSupport()
{
return VideoImpl::hasVideoSupport();
}
bool Video::setUri(const QString &uri)
{
// Check if we're actually changing the uri.
if (uri != _uri)
{
// Try to load movie.
if (!_impl->loadMovie(uri))
{
qDebug() << "Cannot load movie " << uri << "." << endl;
return false;
}
// Set uri.
_uri = uri;
// Try to get thumbnail.
// Wait for the first samples to be available to make sure we are ready.
if (!_impl->waitForNextBits(1000))
{
qDebug() << "No bits coming" << endl;
return false;
}
if (!_generateThumbnail())
qDebug() << "Could not generate thumbnail for " << uri << ": using generic icon." << endl;
_emitPropertyChanged("uri");
}
// Return success.
return true;
}
void Video::_doPlay()
{
_impl->setPlayState(true);
}
void Video::_doPause()
{
_impl->setPlayState(false);
}
bool Video::_generateThumbnail()
{
static QFileIconProvider provider;
// Default (in case seeking and loading don't work).
_icon = provider.icon(QFileInfo(_uri));
// Try seeking to the middle of the movie.
if (!_impl->seekTo(0.5))
{
_impl->resetMovie();
return false;
}
// Try to get a sample from the current position.
// NOTE: There is no guarantee the sample has yet been acquired.
const uchar* bits;
if (!_impl->waitForNextBits(ICON_TIMEOUT, &bits))
{
qDebug() << "Second waiting wrong..." << endl;
return false;
}
// Copy bits into thumbnail QImage.
QImage thumbnail(getWidth(), getHeight(), QImage::Format_ARGB32);
for (int y=0; y<getHeight(); y++)
for (int x=0; x<getWidth(); x++)
{
// Transfer RGBA to ARGB.
uint r = *bits++;
uint b = *bits++;
uint g = *bits++;
bits++; // skip alpha
thumbnail.setPixel(x, y, qRgb(r, g, b));
}
// Generate icon.
_icon = QIcon(QPixmap::fromImage(thumbnail).scaled(MM::MAPPING_LIST_ICON_SIZE, MM::MAPPING_LIST_ICON_SIZE,
Qt::IgnoreAspectRatio));
// Reset movie.
_impl->resetMovie();
return true;
}
}