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
|
#include "video-widget.hpp"
#include "compat/check-visible.hpp"
#include "compat/math.hpp"
#include <cstring>
#include <QPainter>
#include <QtAlgorithms>
#include <QDebug>
void video_widget::init_image_nolock()
{
double dpi = devicePixelRatioF();
size_.store({ iround(width() * dpi), iround(height() * dpi) }, std::memory_order_release);
}
video_widget::video_widget(QWidget* parent) : QWidget(parent)
{
if (parent)
setFixedSize(parent->size());
else
setFixedSize(320, 240);
init_image_nolock();
connect(&timer, &QTimer::timeout, this, &video_widget::draw_image, Qt::DirectConnection);
timer.start(15);
}
void video_widget::update_image(const QImage& img)
{
if (fresh())
return;
set_image(img.constBits(), img.width(), img.height(),
img.bytesPerLine(), img.format());
set_fresh(true);
}
void video_widget::set_image(const unsigned char* src, int width, int height, int stride, QImage::Format fmt)
{
QMutexLocker l(&mtx);
texture = QImage();
unsigned nbytes = (unsigned)(stride * height);
vec.resize(nbytes); vec.shrink_to_fit();
std::memcpy(vec.data(), src, nbytes);
texture = QImage((const unsigned char*)vec.data(), width, height, stride, fmt);
}
void video_widget::paintEvent(QPaintEvent*)
{
QPainter painter(this);
QMutexLocker l(&mtx);
painter.drawImage(rect(), texture);
}
void video_widget::draw_image()
{
if (!fresh())
return;
if (!check_is_visible())
return;
repaint();
set_fresh(false);
}
void video_widget::resizeEvent(QResizeEvent*)
{
QMutexLocker l(&mtx);
init_image_nolock();
}
std::tuple<int, int> video_widget::preview_size() const
{
QSize sz = size_.load(std::memory_order_acquire);
return { sz.width(), sz.height() };
}
bool video_widget::fresh() const
{
return fresh_.load(std::memory_order_acquire);
}
void video_widget::set_fresh(bool x)
{
fresh_.store(x, std::memory_order_release);
}
|