-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderthread.cpp
148 lines (132 loc) · 5.63 KB
/
renderthread.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
/**
* The MIT License (MIT)
* Copyright (c) 2017-2018 Kirill Lebedev
**/
#include "renderthread.h"
#include <QtGui>
#include <array>
#include <cmath>
#define StepX 20
#define StepY 10
class mapperC {
public:
constexpr mapperC(int centerTo, qreal centerFrom, qreal scale) : _centerTo(centerTo), _centerFrom(centerFrom), _scale(scale)
{ }
constexpr qreal mapTo(int v) const {
return _centerTo + (v - _centerFrom) / _scale;
}
private:
int _centerTo;
qreal _centerFrom, _scale;
};
class mapperPt {
public:
constexpr mapperPt(const QPoint ¢erTo, const QPointF ¢erFrom, const QPointF &scale) : _centerTo(centerTo), _centerFrom(centerFrom), _scale(scale)
{ }
QPoint mapTo(const QPointF &v) const {
return QPoint(std::round(_centerTo.x() + (v.x() - _centerFrom.x()) / _scale.x()), std::round(_centerTo.y() + (v.y() - _centerFrom.y()) / _scale.y()));
}
QRectF mapTo(const QRect &v) const {
return QRectF(mapTo(v.topLeft()), mapTo(v.bottomRight()));
}
private:
QPoint _centerTo;
QPointF _centerFrom, _scale;
};
RenderThread::RenderThread(QObject *parent)
: QThread(parent), restart(ATOMIC_VAR_INIT(false)), abort(false)
{
}
RenderThread::~RenderThread()
{
mutex.lock();
abort = true;
condition.wakeOne();
mutex.unlock();
wait();
}
void RenderThread::render(QPointF scrCenter, qreal scaleFactor,
QSize resultSize, const QImage &image, const std::vector<QRect> &frects, const std::vector<QPointF> &pts)
{
QMutexLocker locker(&mutex);
//std::cout << "render: " << std::internal <<std::hex << std::setw(16) << std::setfill('0') << image.constBits() << std::endl;
this->_scrCenter = scrCenter;
this->_scaleFactor = scaleFactor;
this->_resultSize = resultSize;
this->_image = image;
this->_frects = frects;
this->_pts = pts;
if (!isRunning()) {
start(LowPriority);
} else {
restart.store(true, std::memory_order_relaxed);
condition.wakeOne();
}
}
void RenderThread::run()
{
forever {
mutex.lock();
QSize resultSize = this->_resultSize;
qreal scaleFactor = this->_scaleFactor;
QPointF scrCenter = this->_scrCenter;
qreal centerX = this->_scrCenter.x();
qreal centerY = this->_scrCenter.y();
QImage image = this->_image;
std::vector<QRect> frects = this->_frects;
std::vector<QPointF> pts = this->_pts;
mutex.unlock();
int halfWidth = resultSize.width() / 2;
int halfHeight = resultSize.height() / 2;
QImage imageRes(resultSize, QImage::Format_RGB32);
if (abort)
return;
QPainter painter(&imageRes);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.fillRect(0, 0, resultSize.width(), resultSize.height(), Qt::white);
painter.setPen(Qt::black);
const QRectF rectImg(halfWidth - centerX / scaleFactor, halfHeight - centerY / scaleFactor, image.width() / scaleFactor, image.height() / scaleFactor);
const QRectF rectDst(QRectF(imageRes.rect()).intersected(rectImg));
//QRectF rectDst(halfWidth - dstW / 2.f, halfHeight - dstH / 2.f, dstW, dstH);
if (!rectDst.isEmpty())
{
const QRectF rectSrc((rectDst.left() - rectImg.left()) * scaleFactor, (rectDst.top() - rectImg.top()) * scaleFactor, rectDst.width() * scaleFactor, rectDst.height() * scaleFactor);
painter.drawImage(rectDst, image, rectSrc);
}
if (!frects.empty()) {
const mapperPt mapper(QPoint(halfWidth, halfHeight), scrCenter, QPointF(scaleFactor, scaleFactor));
QPen oldPen = painter.pen();
painter.setPen(QPen(Qt::red, 2));
std::for_each(frects.cbegin(), frects.cend(), [&mapper, &painter](const auto &e){ painter.drawRect(mapper.mapTo(e)); });
/*const size_t count(frects.size());
std::array<QPointF, 5> points;
for (size_t i(0); i < count; ++i) {
points[0] = mapper.mapTo(frects[i].topLeft()); // QPoint(halfWidth + (-centerX + frects[i].left()) / scaleFactor, halfHeight + (-centerY + frects[i].top()) / scaleFactor);
points[1] = mapper.mapTo(frects[i].topRight()); // QPoint(halfWidth + (-centerX + frects[i].right()) / scaleFactor, halfHeight + (-centerY + frects[i].top()) / scaleFactor);
points[2] = mapper.mapTo(frects[i].bottomRight()); // QPoint(halfWidth + (-centerX + frects[i].right()) / scaleFactor, halfHeight + (-centerY + frects[i].bottom()) / scaleFactor);
points[3] = mapper.mapTo(frects[i].bottomLeft()); // QPoint(halfWidth + (-centerX + frects[i].left()) / scaleFactor, halfHeight + (-centerY + frects[i].bottom()) / scaleFactor);
points[4] = points[0];
painter.drawPolyline(points.data(), points.size());
}*/
painter.setPen(oldPen);
}
if (!pts.empty()) {
const mapperPt mapper(QPoint(halfWidth, halfHeight), scrCenter, QPointF(scaleFactor, scaleFactor));
for (size_t i(0); i < pts.size(); ++i) {
const auto ptDraw = mapper.mapTo(QPointF(pts[i].x(), pts[i].y()));
painter.drawLine(ptDraw - QPoint(2, 2), ptDraw + QPoint(1, 1));
painter.drawLine(ptDraw + QPoint(-2, 2), ptDraw + QPoint(1, -1));
}
}
if (!restart.load(std::memory_order_relaxed)) {
emit renderedImage(imageRes, scrCenter, scaleFactor);
}
if (abort)
return;
mutex.lock();
if (!restart.load(std::memory_order_relaxed))
condition.wait(&mutex);
restart.store(false, std::memory_order_relaxed);
mutex.unlock();
}
}