Repository navigation
Expand file tree
/
Copy pathFilterThread.cpp
More file actions
198 lines (178 loc) · 5.88 KB
/
Copy pathFilterThread.cpp
File metadata and controls
198 lines (178 loc) · 5.88 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "FilterThread.h"
#include <limits>
#include <QPixmap>
#include <QPen>
#include <QBrush>
#include <QGraphicsPixmapItem>
#include <QGraphicsItemGroup>
#include <QElapsedTimer>
#include <QDebug>
#include "io/multiresolutionimageinterface/MultiResolutionImage.h"
#include "interfaces/interfaces.h"
#include "core/PathologyEnums.h"
#include "core/Patch.h"
#include "core/Point.h"
#include "UtilityFunctions.h"
using namespace pathology;
Q_DECLARE_METATYPE(Patch<double>*)
Q_DECLARE_METATYPE(std::vector<Point>)
Q_DECLARE_METATYPE(std::vector<std::vector<Point> >)
FilterThread::FilterThread(QObject *parent) :
QThread(parent),
_restart(false),
_abort(false),
_FOV(QRectF()),
_level(0),
_channel(0),
_img(NULL),
_filterPlugin(NULL)
{
}
FilterThread::~FilterThread()
{
stopFilter();
_mutex.lock();
_abort = true;
_mutex.unlock();
while (isRunning()) {
_condition.wakeOne();
}
wait();
}
void FilterThread::updateFilterResult() {
stopFilter();
_restart = true;
_condition.wakeOne();
}
void FilterThread::updateFilterResult(const QRectF& FOV, MultiResolutionImage* img, const unsigned int level, int channel)
{
stopFilter();
_img = img;
_level = level;
_FOV = FOV;
if (channel >= 0) {
_channel = channel;
}
if (!isRunning()) {
start(HighPriority);
}
else {
_restart = true;
_condition.wakeOne();
}
}
void FilterThread::setFilter(const std::shared_ptr<ImageFilterPluginInterface>& filterPlugin) {
QMutexLocker locker(&_mutex);
_filterPlugin.reset();
_filterPlugin = filterPlugin;
}
void FilterThread::removeFilter() {
stopFilter();
QMutexLocker locker(&_mutex);
_filterPlugin.reset();
}
void FilterThread::stopFilter() {
if (_filterPlugin) {
_filterPlugin->cancel();
}
}
void FilterThread::run()
{
forever{
_mutex.lock();
QRectF FOV = _FOV;
int level = _level;
MultiResolutionImage* img = _img;
int channel = _channel;
if (img) {
if (_abort) {
return;
}
if (_filterPlugin)
{
float downsample = img->getLevelDownsample(level);
unsigned int width = FOV.width() / downsample;
unsigned int height = FOV.height() / downsample;
Patch<double>* input = img->getPatch<double>(FOV.left(), FOV.top(), width, height, level);
QVariant variant;
if (!_restart) {
_filterPlugin->filter(*input, variant);
}
delete input;
input = NULL;
if (variant.isValid() && !variant.isNull()) {
Patch<double>* output = variant.value<Patch<double>*>();
// Image
if (output && !output->empty()) {
std::vector<unsigned long long> dims = output->getDimensions();
unsigned char* imgBuf = new unsigned char[dims[0] * dims[1] * dims[2]];
double *typedOutput = output->getPointer();
QImage renderedImg;
if (output->getColorType() == pathology::ColorType::Monochrome || output->getColorType() == pathology::ColorType::Indexed) {
renderedImg = convertMonochromeToRGB(typedOutput, dims[0], dims[1], 0, dims[2], output->getMinValue(), output->getMaxValue());
}
else {
std::copy(typedOutput, typedOutput + dims[0] * dims[1] * dims[2], imgBuf);
if (output->getColorType() == pathology::ColorType::RGB) {
renderedImg = QImage(imgBuf, dims[0], dims[1], dims[0] * dims[2], QImage::Format_RGB888);
}
else {
renderedImg = QImage(imgBuf, dims[0], dims[1], dims[0] * dims[2], QImage::Format_ARGB32);
}
}
QPixmap pixmap = QPixmap::fromImage(renderedImg);
QGraphicsPixmapItem* result = new QGraphicsPixmapItem(pixmap);
QRectF size(0, 0, width, height);
emit filterResult(result, size);
if (imgBuf) {
delete[] imgBuf;
imgBuf = NULL;
}
}
else {
// PointSet
std::vector<Point> pts = variant.value<std::vector<Point> >();
if (!pts.empty()) {
QGraphicsItemGroup* result = new QGraphicsItemGroup();
for (std::vector<Point>::const_iterator it = pts.begin(); it != pts.end(); ++it) {
QGraphicsEllipseItem *itm = new QGraphicsEllipseItem(it->getX() - 2.5, it->getY() - 2.5, 5, 5);
itm->setPen(QPen(QBrush("yellow"), 0));
itm->setBrush(QBrush("yellow"));
result->addToGroup(itm);
}
QRectF size(0, 0, width, height);
emit filterResult(result, size);
}
else {
// Polygons
std::vector<std::vector<Point> > polys = variant.value<std::vector<std::vector<Point> > >();
QGraphicsItemGroup* result = new QGraphicsItemGroup();
for (std::vector<std::vector<Point> >::const_iterator it = polys.begin(); it != polys.end(); ++it) {
QPolygonF poly;
for (std::vector<Point>::const_iterator it2 = it->begin(); it2 != it->end(); ++it2) {
poly << QPointF(it2->getX(), it2->getY());
}
QGraphicsPolygonItem *itm = new QGraphicsPolygonItem(poly);
itm->setPen(QPen(QBrush("yellow"), 2.));
result->addToGroup(itm);
}
QRectF size(0, 0, width, height);
emit filterResult(result, size);
}
}
if (output) {
delete output;
}
}
}
if (!_restart || !_filterPlugin) {
_condition.wait(&_mutex);
}
_restart = false;
_mutex.unlock();
if (_abort) {
return;
}
}
}
}