Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GuiBase.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include "GuiBase.hh"
26 
27 #ifndef WIN32
28 //no unistd in WIN32 by default
29 # include <unistd.h>
30 //# include <pthread.h>
31 #endif //WIN32
32 
33 #include <Base/Image/ImageConvert.hh>
34 
35 #include <Base/Common/BIASpragma.hh>
36 
37 using namespace BIAS;
38 using namespace std;
39 
40 
41 
42 // instantiations for ConvertToUC_
43 template int GuiBase::ConvertToUC_<float>(Image<float>& image, float, float);
44 #ifdef BUILD_IMAGE_CHAR
45 template int GuiBase::ConvertToUC_<char>(Image<char>& image, float, float);
46 #endif
47 
48 #ifdef BUILD_IMAGE_DOUBLE
49 template int GuiBase::ConvertToUC_<double>(Image<double>& image, float, float);
50 #endif
51 
52 #ifdef BUILD_IMAGE_UINT
53 template int GuiBase::ConvertToUC_<unsigned int>(Image<unsigned int>& image,
54  float, float);
55 #endif
56 
57 #ifdef BUILD_IMAGE_INT
58 template int GuiBase::ConvertToUC_<int>(Image<int>& image, float, float);
59 #endif
60 
61 #ifdef BUILD_IMAGE_SHORT
62 template int GuiBase::ConvertToUC_<short int>(Image<short int>& image,
63  float, float);
64 #endif
65 
66 #ifdef BUILD_IMAGE_USHORT
67 template int GuiBase::ConvertToUC_<unsigned short>(Image<unsigned short>& image
68  , float, float);
69 #endif
70 
71 
72 
73 // instantiate static stuff
74  pthread_mutex_t GuiBase::MouseButtonMutex_;
75 
76 bool GuiBase::BaseInitialized_=false;
77 
78 GuiBase::GuiBase(bool ShowMousePos) : Debug()
79 {
80  Init_(ShowMousePos);
81 }
82 
83 GuiBase::GuiBase(const string& Title,bool ShowMousePos) : Debug()
84 {
85  Init_(ShowMousePos);
86  Title_ = Title;
87 }
88 
90 {
91  Init_(gui.ShowMouse_);
92  Title_ = gui.Title_;
93 }
94 
95 
97 {}
98 
99 
100 
101 void GuiBase::Init_(bool ShowMousePos)
102 {
103  WindowCreated_=false;
104  Width_=0;
105  Height_=0;
106  nChannels_=0;
107  Title_="Window";
108  KeyBuffer_=-1;
109  ShowMouse_ = ShowMousePos;
113  Factor_ = 1;
114  XOffset_ = YOffset_ = 0;
115  if (!BaseInitialized_){
116  pthread_mutex_init(&MouseButtonMutex_, NULL);
117  BaseInitialized_ = true;
118  }
119  IsPyramid_=false;
121 }
122 
123 void GuiBase::SetFactorOffset(int xoffset, int yoffset, int factor)
124 {
125  XOffset_ = xoffset;
126  YOffset_ = yoffset;
127  Factor_ = factor;
128 }
129 
130 void GuiBase::GetFactorOffset(int& xoffset, int& yoffset, int& factor)
131 {
132  xoffset = XOffset_;
133  yoffset = YOffset_;
134  factor = Factor_;
135 }
136 
137 int GuiBase::ShowImage(ImageBase& image, float min, float max)
138 {
139 
140  if (IsPyramid_>0) IsPyramid_--;
141  //cout <<"GuiBase::ShowImage() "<< IsPyramid_<<endl;
142  OrigPicture_ = image;
143  int result=0;
144  switch(image.GetStorageType()){
146  {
148  img.StealImage(image);
149 
150  Image<unsigned char> rgbimage;
152  result=ShowConvertedImage_(rgbimage);
153 
154  image.StealImage(img);
155  }
156  break;
157  case ImageBase::ST_float:
158  {
159  Image<float> img;
160  img.StealImage(image);
161  result=ConvertToUC_(img,min,max);
162  image.StealImage(img);
163  }
164  break;
165 #ifdef BUILD_IMAGE_USHORT
167  {
169  img.StealImage(image);
170  result=ConvertToUC_(img, min, max);
171  image.StealImage(img);
172  }
173  break;
174 #endif
175 #ifdef BUILD_IMAGE_CHAR
176  case ImageBase::ST_char:
177  {
178  Image<char> img;
179  img.StealImage(image);
180  result=ConvertToUC_(img,min,max);
181  image.StealImage(img);
182  }
183  break;
184 #endif
185 #ifdef BUILD_IMAGE_SHORT
187  {
188  Image<short int> img;
189  img.StealImage(image);
190  result=ConvertToUC_(img,min,max);
191  image.StealImage(img);
192  }
193  break;
194 #endif
195 #ifdef BUILD_IMAGE_INT
196  case ImageBase::ST_int:
197  {
198  Image<int> img;
199  img.StealImage(image);
200  result=ConvertToUC_(img,min,max);
201  image.StealImage(img);
202  }
203  break;
204 #endif
205 #ifdef BUILD_IMAGE_UINT
207  {
209  img.StealImage(image);
210  result=ConvertToUC_(img,min,max);
211  image.StealImage(img);
212  }
213  break;
214  break;
215 #endif
216 #ifdef BUILD_IMAGE_DOUBLE
218  {
219  Image<double> img;
220  img.StealImage(image);
221  result=ConvertToUC_(img,min,max);
222  image.StealImage(img);
223  }
224  break;
225 #endif
226  default:
227 
228  BIASERR("invalid storage type: "<<int(image.GetStorageType()) );
229 
230  return -1;
231  break;
232  }
233  return result;
234 }
235 
236 template <class PixelType>
237 int GuiBase::ConvertToUC_(Image<PixelType> &image, float min, float max)
238 {
239 // cout <<"GuiBase::ConvertToUC_()"<<endl;
240  Image<PixelType> tmpimage;
241  tmpimage = image;
242  tmpimage.ScaleShiftBetween(min, max);
243  Image<unsigned char> ucimage, rgbimage;
245  ImageConvert::Convert(ucimage, rgbimage, ImageBase::CM_RGB);
246  return ShowConvertedImage_(rgbimage);
247 }
248 
250 {
252  tmp=pim;
254  im.Init(pim[0]->GetWidth(), pim[0]->GetHeight() , pim[0]->GetChannelCount(),
255  pim.size());
256  for (unsigned p=0; p<pim.size(); p++){
257  tmp[p]->ScaleShiftBetween(0, 255);
258  if (ImageConvert::ConvertST(*tmp[p], *im[p],
260  BIASERR("error converting image");
261  }
262  }
263  return ShowImage(im);
264 }
265 
267 {
268  Pyramid0Width_=pim[0]->GetWidth();
269  Pyramid0Height_=pim[0]->GetHeight();
270  PyramidLevels_ = pim.size();
271  IsPyramid_=2;
273  pim.GetSingleImage(im);
274  return ShowImage(im);
275 }
276 
278 {
279  BIASERR("GuiBase::ShowConvertedImage_() must be overloaded!!");
280  return -1;
281 }
282 
284 {
285  BIASERR("GuiBase::DestroyWindow() must be overloaded!!");
286  return -1;
287 }
288 
289 void GuiBase::SetTitle(string const& Title)
290 {
291  BIASERR("GuiBase::SetTitle() must be overloaded!!");
292  return ;
293 }
294 
295 
296 bool GuiBase::CheckForEvent(bool& KeyEvent, char& key, bool& Pressed,
297  int& Button, int& Xpos, int& Ypos)
298 {
299  char tmp;
300  if (CheckForMouseButtonEvent(Pressed, Button, Xpos, Ypos)){
301  KeyEvent=false;
302  return true;
303  } else if ((tmp=CheckForKeyEvent()) != -1){
304  key=tmp;
305  KeyEvent=true;
306  return true;
307  }
308  return false;
309 }
310 
311 void GuiBase::WaitForEvent(bool& KeyEvent, char& key, bool& Pressed,
312  int& Button, int& Xpos, int& Ypos)
313 {
314  key = -1;
315  Button = -1;
316  Xpos = Ypos = -1;
317  while (WindowCreated_ && (key = CheckForKeyEvent()) == -1 &&
318  !CheckForMouseButtonEvent(Pressed, Button, Xpos, Ypos)){
319  #ifdef WIN32
320  Sleep(1000);
321  #else //not WIN32
322  usleep(10000);
323  #endif //WIN32
324  }
325  if (key == -1)
326  KeyEvent = false;
327  else
328  KeyEvent = true;
329 }
330 
331 bool GuiBase::CheckForEvent(char& key, bool& Pressed, int& Button,
332  int& Xpos, int& Ypos)
333 {
334  key = CheckForKeyEvent();
335  Button = -1;
336  Xpos = Ypos = -1;
337  return (CheckForMouseButtonEvent(Pressed, Button, Xpos, Ypos) || (key!=-1));
338 }
339 
341 {
342  char Key;
343  if (KeyBuffer_ != -1){
344  Key=KeyBuffer_;
345  KeyBuffer_=-1;
346  return Key;
347  }
348  return KeyBuffer_;
349 }
350 
351 
353 {
354  BIASDOUT(D_GUI_KEY,"WaitForKeyEvent()");
355  char Key;
356  while (WindowCreated_ && KeyBuffer_==-1){
357  #ifdef WIN32
358  Sleep(1000);
359  #else //not WIN32
360  usleep(10000);
361  #endif //WIN32
362  }
363  Key=KeyBuffer_;
364  KeyBuffer_=-1;
365  BIASDOUT(D_GUI_KEY,"WaitForKeyEvent() : key pressed : "<<Key);
366  return Key;
367 }
368 
369 bool GuiBase::CheckForMouseButtonEvent(bool& Pressed, int& Button,
370  int& Xpos, int& Ypos)
371 {
372  bool result=false;
373  BIAS_MUTEX_LOCK(&MouseButtonMutex_);
374  if (MouseButtonPressed_ == -1 && MouseButtonReleased_ == -1){
375  result = false;
376  } else {
377  result = true;
378  if (MouseButtonPressed_ != -1){
379  Pressed = true;
380  Button = MouseButtonPressed_;
381  Xpos = MouseButtonPressX_;
382  Ypos = MouseButtonPressY_;
384  MouseButtonPressed_ = -1;
385  } else {
386  Pressed = false;
387  Button = MouseButtonReleased_;
388  Xpos = MouseButtonReleaseX_;
389  Ypos = MouseButtonReleaseY_;
392  }
393  }
394  BIAS_MUTEX_UNLOCK(&MouseButtonMutex_);
395  return result;
396 }
397 
398 void GuiBase::WaitForMouseButtonEvent(bool& Pressed, int& Button,
399  int& Xpos, int& Ypos)
400 {
401  BIAS_MUTEX_LOCK(&MouseButtonMutex_);
402  while (WindowCreated_ && MouseButtonPressed_ == -1 &&
403  MouseButtonReleased_ == -1){
404  BIAS_MUTEX_UNLOCK(&MouseButtonMutex_);
405  #ifdef WIN32
406  Sleep(1000);
407  #else //not WIN32
408  usleep(10000);
409  #endif //WIN32
410  BIAS_MUTEX_LOCK(&MouseButtonMutex_);
411  }
412  if (MouseButtonPressed_ != -1){
413  Pressed = true;
414  Button = MouseButtonPressed_;
415  Xpos = MouseButtonPressX_;
416  Ypos = MouseButtonPressY_;
418  MouseButtonPressed_ = -1;
419  BIASCDOUT(D_GUI_MOUSE_BUTTON, "pressed at ("<<Xpos<<", "<<Ypos<<")"<<endl);
420  } else {
421  Pressed = false;
422  Button = MouseButtonReleased_;
423  Xpos = MouseButtonReleaseX_;
424  Ypos = MouseButtonReleaseY_;
427  BIASCDOUT(D_GUI_MOUSE_BUTTON, "released at ("<<Xpos<<", "<<Ypos<<")"<<endl);
428  }
429  BIAS_MUTEX_UNLOCK(&MouseButtonMutex_);
430 }
virtual void SetTitle(std::string const &Title)
Definition: GuiBase.cpp:289
virtual char WaitForKeyEvent()
waits until a key is pressed and returns it tested
Definition: GuiBase.cpp:352
unsigned int Pyramid0Width_
Definition: GuiBase.hh:185
(16bit) unsigned integer image storage type
Definition: ImageBase.hh:114
static pthread_mutex_t MouseButtonMutex_
Definition: GuiBase.hh:207
virtual void GetFactorOffset(int &xoffset, int &yoffset, int &factor)
gets offset and factor for mouse position mouse position in (e.g.
Definition: GuiBase.cpp:130
virtual char CheckForKeyEvent()
checks if any key is pressed and returns it returns -1 if no key is pressed
Definition: GuiBase.cpp:340
unsigned size() const
deprecated interface
virtual bool CheckForMouseButtonEvent(bool &Pressed, int &Button, int &Xpos, int &Ypos)
return true if mouse button is pressed or released, false else see WaitForMouseButtonEvent for detail...
Definition: GuiBase.cpp:369
Gui is a simple windowing environment...
Definition: GuiBase.hh:75
(8bit) signed char image storage type
Definition: ImageBase.hh:113
int ScaleShiftBetween(double Min, double Max)
scales and shifts image so afterwards every pixel has a value between Min and Max ...
Definition: Image.cpp:1118
void GetSingleImage(Image< StorageType > &im) const
returns a single image containing all pyramid images
float image storage type
Definition: ImageBase.hh:118
double image storage type
Definition: ImageBase.hh:119
int MouseButtonReleaseY_
Definition: GuiBase.hh:196
GuiBase(bool ShowMousePos=true)
Definition: GuiBase.cpp:78
bool WindowCreated_
Definition: GuiBase.hh:180
virtual void SetFactorOffset(int xoffset, int yoffset, int factor)
sets offset and factor for mouse position mouse position in (e.g.
Definition: GuiBase.cpp:123
char KeyBuffer_
Definition: GuiBase.hh:202
int MouseButtonReleaseX_
Definition: GuiBase.hh:195
unsigned int Pyramid0Height_
Definition: GuiBase.hh:186
(16bit) signed integer image storage type
Definition: ImageBase.hh:115
ImageBase OrigPicture_
This is the original representation of the picture.
Definition: GuiBase.hh:199
int MouseButtonPressX_
Definition: GuiBase.hh:192
bool ShowMouse_
Definition: GuiBase.hh:179
static bool BaseInitialized_
Definition: GuiBase.hh:208
int StealImage(ImageBase &source)
steals the image data array from source, after releasing the actual image data and sets source image ...
Definition: ImageBase.cpp:395
std::string Title_
Definition: GuiBase.hh:201
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
color values, 3 channels, order: red,green,blue
Definition: ImageBase.hh:131
void Init(const Image< StorageType > &image, const unsigned py_size=0)
copy image into level 0 and create other levels according to parameters set so far (pyramidsize...
int MouseButtonPressed_
Definition: GuiBase.hh:191
virtual bool CheckForEvent(bool &KeyEvent, char &key, bool &Pressed, int &Button, int &Xpos, int &Ypos)
as above but does return immediate with true if event occured
Definition: GuiBase.cpp:296
int IsPyramid_
Definition: GuiBase.hh:205
virtual void WaitForMouseButtonEvent(bool &Pressed, int &Button, int &Xpos, int &Ypos)
waits until any mouse button is pressed or released
Definition: GuiBase.cpp:398
virtual ~GuiBase()
Definition: GuiBase.cpp:96
unsigned int Height_
Definition: GuiBase.hh:183
unsigned int nChannels_
Definition: GuiBase.hh:184
int ConvertToUC_(Image< PixelType > &image, float min, float max)
Definition: GuiBase.cpp:237
(32bit) signed integer image storage type
Definition: ImageBase.hh:117
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
int MouseButtonPressY_
Definition: GuiBase.hh:193
virtual void Init_(bool ShowMousePos)
Set up thread and stuff.
Definition: GuiBase.cpp:101
virtual int DestroyWindow()
stops the main loop and closes the window
Definition: GuiBase.cpp:283
unsigned int Width_
if the following 3 variables are set , Buffer_ is already allocated
Definition: GuiBase.hh:182
unsigned int PyramidLevels_
Definition: GuiBase.hh:187
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
virtual int ShowConvertedImage_(Image< unsigned char > &image)
Does the real update of the image shown in window. Must be overloaded.
Definition: GuiBase.cpp:277
virtual int ShowImage(ImageBase &image, float min=0.0, float max=255.0)
shows/updates the image shown in window.
Definition: GuiBase.cpp:137
int MouseButtonReleased_
Definition: GuiBase.hh:194
virtual void WaitForEvent(bool &KeyEvent, char &key, bool &Pressed, int &Button, int &Xpos, int &Ypos)
waits for either KeyEvent or MouseButtonEvent, KeyEvent is set to true if key is pressed, the other variables are set as in WaitForKeyEvent and WaitForMouseButtonEvent
Definition: GuiBase.cpp:311
(32bit) unsigned integer image storage type
Definition: ImageBase.hh:116