forked from funkey/gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImagePainter.h
453 lines (327 loc) · 10.4 KB
/
ImagePainter.h
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#ifndef IMAGE_PAINTER_H__
#define IMAGE_PAINTER_H__
#include <boost/concept_check.hpp>
#include <boost/lexical_cast.hpp>
#include <gui/ContextSettings.h>
#include <gui/GlContext.h>
#include <gui/OpenGl.h>
#include <gui/IsImage.h>
#include <gui/Painter.h>
#include <gui/TextPainter.h>
#include <gui/Texture.h>
#include <util/Logger.h>
#include <util/point.hpp>
#include <util/rect.hpp>
static logger::LogChannel imagepainterlog("imagepainterlog", "[ImagePainter] ");
namespace gui {
template <typename Pointer>
struct init_ptr {
// default for smart pointers: do nothing
void operator()(Pointer& p) {}
};
template <typename Pointer>
struct init_ptr<Pointer*> {
// real pointers will be set to 0
void operator()(Pointer*& p) {
p = 0;
}
};
/**
* Class ImagePainter.
*
* Provides a painter for a generic Image. The Image has to provide:
*
* • Image::value_type (the pixel type)
* • const value_type& operator()(x, y) (to access the pixels)
* • unsigned int width() and height()
*/
template <typename Image, typename Pointer = boost::shared_ptr<Image> >
class ImagePainter :
public gui::Painter {
BOOST_CONCEPT_ASSERT((IsImage<Image>));
typedef typename Image::value_type value_type;
typedef Pointer pointer_type;
public:
/**
* Constructs a new image painter with the given name.
*
* @param reloadThread true, if this ImagePainter should use a separate
* texture reload thread.
*/
ImagePainter(bool reloadThread = false);
/**
* Destroys texture and closes reload thread.
*/
~ImagePainter();
/**
* Set the image to show.
*
* @param A pointer to the image to show.
* @param An optional mutex to guard data read operations to the image.
*/
void setImage(pointer_type image, boost::shared_mutex* imageMutex = 0);
/**
* Enable image normalization. Currently, this only works for intensity
* images.
*/
void setNormalize(bool normalize = true) { _normalize = normalize; }
/**
* Get a pointer to the image this painter is showing.
*
* @return Pointer to the image that this painter is showing.
*/
const pointer_type& getImage() { return _image; }
/**
* Overwritten from painter.
*/
virtual void draw(
const util::rect<double>& roi,
const util::point<double>& resolution);
/**
* Reloading of the image.
*/
virtual void update();
private:
/**
* Normalize and re-load image for intensity images.
*/
void loadNormalized(const boost::true_type& arithmetic);
/**
* Normalize and re-load image for multi-channel images.
*/
void loadNormalized(const boost::false_type& arithmetic);
/**
* Entry point for the texture reload thread.
*/
void reloadThread();
/**
* Reloads a texture by creating a temporary texture and replacing it with
* the current one.
*/
void reloadTexture();
// the image to observe (shared ownership)
pointer_type _image;
// normalize the image?
bool _normalize;
// a type to initialize the pointer
init_ptr<pointer_type> _init_ptr;
// the texture of the image to show (exclusivey owned)
Texture* _imageTexture;
// set to true if this image painter has a worker thread
bool _hasReloadThread;
// a worker thread to update the texture in the background
boost::thread _reloadThread;
// mutex to guard the image (not ours, has to be guaranteed to survive this
// object)
boost::shared_mutex* _imageMutex;
// indicate the need to reload the texture to the texture thread
bool _needReload;
};
/******************
* IMPLEMENTATION *
******************/
template <typename Image, typename Pointer>
ImagePainter<Image, Pointer>::ImagePainter(bool reloadThread) :
_normalize(false),
_imageTexture(0),
_hasReloadThread(reloadThread),
_needReload(true) {
LOG_ALL(imagepainterlog) << "initializing..." << std::endl;
_init_ptr(_image);
// launch a new worker thread
if (_hasReloadThread) {
LOG_ALL(imagepainterlog) << "launching texture worker thread..." << std::endl;
_reloadThread = boost::thread(&ImagePainter<Image, Pointer>::reloadThread, this);
}
LOG_ALL(imagepainterlog) << "done initializing" << std::endl;
}
template <typename Image, typename Pointer>
ImagePainter<Image, Pointer>::~ImagePainter() {
LOG_ALL(imagepainterlog) << "destroying..." << std::endl;
if (_hasReloadThread) {
_hasReloadThread = false;
LOG_ALL(imagepainterlog) << "waiting for reload thread to finish..." << std::endl;
_reloadThread.join();
}
LOG_ALL(imagepainterlog) << "deleting texture" << std::endl;
if (_imageTexture != 0)
delete _imageTexture;
LOG_ALL(imagepainterlog) << "destroyed" << std::endl;
}
template <typename Image, typename Pointer>
void
ImagePainter<Image, Pointer>::setImage(pointer_type image, boost::shared_mutex* imageMutex) {
_image = image;
_imageMutex = imageMutex;
LOG_ALL(imagepainterlog) << "size of image is "
<< _image->width() << "x"
<< _image->height() << std::endl;
setSize(0.0, 0.0, _image->width(), _image->height());
}
template <typename Image, typename Pointer>
void
ImagePainter<Image, Pointer>::draw(
const util::rect<double>& roi,
const util::point<double>& resolution) {
// wait for image
if (!_image) {
LOG_ALL(imagepainterlog) << "have no image, yet" << std::endl;
return;
}
// wait for content
if (_image->width() == 0 || _image->height() == 0) {
LOG_ALL(imagepainterlog) << "image has zero size, yet" << std::endl;
return;
}
if (_hasReloadThread) {
// come back later if the reload thread is done
if (!_imageTexture) {
// ensure that OpenGl operations are save
OpenGl::Guard guard;
// draw a dummy rectangle
glColor3f(0.5, 0.7, 1.0);
glBegin(GL_QUADS);
glVertex2d(getSize().minX, getSize().minY);
glVertex2d(getSize().maxX, getSize().minY);
glVertex2d(getSize().maxX, getSize().maxY);
glVertex2d(getSize().minX, getSize().maxY);
glEnd();
return;
}
}
LOG_ALL(imagepainterlog) << "drawing..." << std::endl;
// ensure that OpenGl operations are save
OpenGl::Guard guard;
// draw a rectangle
GLdouble width = _imageTexture->width();
GLdouble height = _imageTexture->height();
glEnable(GL_TEXTURE_2D);
_imageTexture->bind();
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glTexCoord2d(0.0, 0.0); glVertex2d(0.0, 0.0);
glTexCoord2d(1.0, 0.0); glVertex2d(width, 0.0);
glTexCoord2d(1.0, 1.0); glVertex2d(width, height);
glTexCoord2d(0.0, 1.0); glVertex2d(0.0, height);
glEnd();
// a pixel is large enough to be written in
if (resolution.x > 30) {
// for every pixel
for (int x = 0; x < _image->width(); x++) {
for (int y = 0; y < _image->height(); y++) {
// if visible
if (util::rect<double>(x, y, x+1, y+1).intersects(roi)) {
TextPainter valuePainter(boost::lexical_cast<std::string>((*_image)(x, y)));
valuePainter.setTextSize(0.1);
valuePainter.setTextColor((*_image)(x, y), 0.5 - (*_image)(x, y)/2.0, 0.5 + (*_image)(x, y)/2.0, (resolution.x - 30)/100);
LOG_ALL(imagepainterlog) << "drawing text with roi "
<< (roi - util::point<double>(x, y)) << " and resolution "
<< resolution << std::endl;
glTranslatef( x, y, 0.0f);
valuePainter.draw(roi - util::point<double>(x, y), resolution);
glTranslatef(-x, -y, 0.0f);
}
}
}
}
}
template <typename Image, typename Pointer>
void
ImagePainter<Image, Pointer>::update() {
if (!_hasReloadThread)
reloadTexture();
else
_needReload = true;
}
template <typename Image, typename Pointer>
void
ImagePainter<Image, Pointer>::reloadThread() {
LOG_ALL(imagepainterlog) << "reload thread started" << std::endl;
try {
while (_hasReloadThread) {
while (_needReload) {
// set to false to see whether another thread changed it
_needReload= false;
LOG_ALL(imagepainterlog) << "texture needs to be reloaded" << std::endl;
if (_imageMutex) {
// lock access to the image
boost::shared_lock<boost::shared_mutex> lockImage(*_imageMutex);
reloadTexture();
} else {
reloadTexture();
}
// if _needReload is still false that's nice -- otherwise some other
// thread changed it and we repeat the update...
}
// TODO: use barriers for that...
usleep(1000);
}
} catch (boost::exception& e) {
std::cerr << "details: " << std::endl
<< boost::diagnostic_information(e)
<< std::endl;
throw &e;
}
}
template <typename Image, typename Pointer>
void
ImagePainter<Image, Pointer>::reloadTexture() {
if (!_image) {
LOG_ALL(imagepainterlog) << "image was not initialised..." << std::endl;
return;
}
// ensure that OpenGl operations are save
OpenGl::Guard guard;
if (!_imageTexture) {
LOG_ALL(imagepainterlog) << "creating new texture "
<< _image->width() << "x" << _image->height() << std::endl;
// crate new texture
_imageTexture = new Texture(_image->width(), _image->height(), detail::pixel_format_traits<value_type>::gl_format);
} else {
if (_image->width() != _imageTexture->width() ||
_image->height() != _imageTexture->height()) {
LOG_ALL(imagepainterlog) << "resizing texture to "
<< _image->width() << "x" << _image->height() << std::endl;
// resize texture
_imageTexture->resize(_image->width(), _image->height());
}
}
LOG_ALL(imagepainterlog) << "loading data" << std::endl;
// load pixel data
if (_normalize) {
loadNormalized(boost::is_arithmetic<value_type>());
} else {
_imageTexture->loadData(_image->begin());
}
// set reported size
setSize(0.0, 0.0, _image->width(), _image->height());
LOG_ALL(imagepainterlog) << "done (re)loading texture" << std::endl;
}
template <typename Image, typename Pointer>
void
ImagePainter<Image, Pointer>::loadNormalized(const boost::true_type& arithmetic) {
bool valid = false;
value_type min = 0.0f;
value_type max = 1.0f;
// find min and max of image
for (typename Image::iterator i = _image->begin(); i != _image->end(); i++) {
if (valid) {
min = std::min(min, *i);
max = std::max(max, *i);
} else {
min = *i;
max = *i;
valid = true;
}
}
// scale and shift to [0..1]
_imageTexture->loadData(_image->begin(), 1.0f/(max - min), -min/(max - min));
}
template <typename Image, typename Pointer>
void
ImagePainter<Image, Pointer>::loadNormalized(const boost::false_type& arithmetic) {
// for non-intensity images we leave it like that for the moment...
_imageTexture->loadData(_image->begin());
}
} // namespace gui
#endif // IMAGE_PAINTER_H__