Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
GuiBase.hh
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 #ifndef __GuiBase_hh__
26 #define __GuiBase_hh__
27 
28 #include <bias_config.h>
29 #ifndef BIAS_HAVE_PTHREADS
30 # error BIAS_HAVE_PTHREADS not defined. But BIAS::GuiBase needs pthreads (on Unix and Win32). Please enable USE_PTHREADS in CMake configure step.
31 #endif
32 #include <pthread.h>
33 
34 #include <fstream>
35 #include <string>
36 
37 #include <Base/Debug/Debug.hh>
38 #include <Base/Debug/Error.hh>
39 #include <Base/Image/Image.hh>
40 #include <Image/PyramidImage.hh>
41 
42 /// SetDebugLevel uses the following flags
43 #define D_GUI_DESTRUCT 0x0001
44 #define D_GUI_MAIN 0x0002
45 #define D_GUI_SIGNAL 0x0004
46 #define D_GUI_KEY 0x0008
47 #define D_GUI_MUTEX 0x0010
48 #define D_GUI_MOUSE_BUTTON 0x0020
49 
50 //#define BIAS_MUTEX_DEBUG
51 #ifdef BIAS_MUTEX_DEBUG
52 #define BIAS_MUTEX_LOCK(arg)\
53  cerr <<__FILE__<<":"<<__LINE__<<" attempting to lock mutex .. "<<#arg;\
54  cerr.flush();\
55  pthread_mutex_lock(arg);\
56  cerr <<" .. got lock\n";
57 
58 #define BIAS_MUTEX_UNLOCK(arg)\
59  cerr <<__FILE__<<":"<<__LINE__<<" unlocking mutex .. "<<#arg;\
60  cerr.flush();\
61  pthread_mutex_unlock(arg);\
62  cerr <<" .. unlocked\n";
63 #else
64 # define BIAS_MUTEX_LOCK(arg) pthread_mutex_lock(arg);
65 # define BIAS_MUTEX_UNLOCK(arg) pthread_mutex_unlock(arg);
66 #endif
67 
68 namespace BIAS
69 {
70  /** @class GuiBase
71  @brief Gui is a simple windowing environment...
72 
73  This is a base class and should not be used directly.
74  */
75  class BIASGui_EXPORT GuiBase : public Debug
76  {
77  public:
78  GuiBase(bool ShowMousePos = true);
79 
80  /// as standard constructor, also sets Title_
81  GuiBase(std::string const& Title,bool ShowMousePos = true);
82 
83  GuiBase(const GuiBase& gui);
84 
85  virtual ~GuiBase();
86 
87  virtual void SetTitle(std::string const& Title);
88 
89  std::string GetTitle(){return Title_;};
90 
91  /// stops the main loop and closes the window
92  virtual int DestroyWindow();
93 
94  /** @brief shows/updates the image shown in window.
95 
96  This method accepts all different template instances and
97  converts them to unsigned char before calling
98  ShowConvertedImage_() */
99  virtual int ShowImage(ImageBase &image,float min=0.0,float max=255.0);
100 
101  virtual int ShowImage(PyramidImage<unsigned char> &pim);
102  virtual int ShowImage(PyramidImage<float> &pim);
103 
104 
105  /** @brief checks if any key is pressed and returns it
106  returns -1 if no key is pressed
107  @author Felix Woelk */
108  virtual char CheckForKeyEvent();
109 
110  /** waits until a key is pressed and returns it
111  @status tested
112  @author Felix Woelk */
113  virtual char WaitForKeyEvent();
114 
115  inline int GetMouseX(){ return MousePosX_;};
116  inline int GetMouseY(){ return MousePosY_;};
117 
118  /** @brief waits until any mouse button is pressed or released
119 
120  Xpos and Ypos are set to the coordinates within the drawing area
121  Pressed is set to true if the button was pressed and to false if the
122  button was released
123  Button is set to th mouse button no: 1 = left, 2 = middle, 3 = right */
124  virtual void WaitForMouseButtonEvent(bool& Pressed, int& Button,
125  int& Xpos, int& Ypos);
126 
127  /** return true if mouse button is pressed or released, false else
128  see WaitForMouseButtonEvent for detailed description */
129  virtual bool CheckForMouseButtonEvent(bool& Pressed, int& Button,
130  int& Xpos, int& Ypos);
131 
132  /** waits for either KeyEvent or MouseButtonEvent, KeyEvent is set to true
133  if key is pressed, the other variables are set as in
134  WaitForKeyEvent and WaitForMouseButtonEvent */
135  virtual void WaitForEvent(bool& KeyEvent, char& key, bool& Pressed,
136  int& Button, int& Xpos, int& Ypos);
137 
138  /** as above but does return immediate with true if event occured */
139  virtual bool CheckForEvent(bool& KeyEvent, char& key, bool& Pressed,
140  int& Button,int& Xpos, int& Ypos);
141 
142  /** as above but does return immediate with true if event occured
143  if key != -1 key event occured, if Xpos != -1 && YPos != -1
144  MouseButton Event occured, also both is possible */
145  virtual bool CheckForEvent(char& key, bool& Pressed, int& Button,
146  int& Xpos, int& Ypos);
147 
148 
149  /** sets offset and factor for mouse position
150  mouse position in (e.g. zoomed) window is calculated as follows:
151  x = real_xpos/factor + xoffset;
152  y = real_ypos/factor + yoffset;
153  real_x/ypos are in the window coordinate system */
154  virtual void SetFactorOffset(int xoffset, int yoffset, int factor);
155 
156  /** gets offset and factor for mouse position
157  mouse position in (e.g. zoomed) window is calculated as follows:
158  x = real_xpos/factor + xoffset;
159  y = real_ypos/factor + yoffset;
160  real_x/ypos are in the window coordinate system */
161  virtual void GetFactorOffset(int& xoffset, int& yoffset, int& factor);
162  /** returns true if window is closed by user via x button
163  @author woelk 05/2003 */
164  inline bool Closed()
165  { return !WindowCreated_; }
166 
167  protected:
168 
169  /** Set up thread and stuff. Must be overloaded
170  Derived class::Init_() should call GuiBase::Init_()
171  first.
172  */
173  virtual void Init_(bool ShowMousePos);
174 
175  /// Does the real update of the image shown in window. Must be overloaded
176  virtual int ShowConvertedImage_(Image<unsigned char> &image);
177 
178 
179  bool ShowMouse_; // shall mouse position be shown?
181  /// if the following 3 variables are set , Buffer_ is already allocated
182  unsigned int Width_;
183  unsigned int Height_;
184  unsigned int nChannels_;
185  unsigned int Pyramid0Width_;
186  unsigned int Pyramid0Height_;
187  unsigned int PyramidLevels_;
188 
197 
198  /// This is the original representation of the picture
200 
201  std::string Title_;
203  int Factor_; // zoom factor for mouse position
204  int XOffset_, YOffset_; // offset for mouse position
205  int IsPyramid_; // set to 1 if PryramidImage is displayed, else to zero
206 
207  static pthread_mutex_t MouseButtonMutex_;
208  static bool BaseInitialized_;
209 
210  template <class PixelType>
211  int ConvertToUC_(Image<PixelType> &image,float min, float max);
212 
213  /// test if internal variables Width_, Height_ and nChannels_
214  /// are set correct for image
215  /// these 3 variables are used as an indicator if Buffer_ is
216  /// allocated in right size
217  inline bool TestImage_(Image<unsigned char> &image)
218  { return ( ( Width_ == image.GetWidth() ) &&
219  ( Height_ == image.GetHeight() ) &&
220  ( nChannels_ == image.GetChannelCount() ) ); };
221 
222  };
223 
224 } // namepsace BIAS
225 #endif // __Gui_hh__
unsigned int Pyramid0Width_
Definition: GuiBase.hh:185
static pthread_mutex_t MouseButtonMutex_
Definition: GuiBase.hh:207
Gui is a simple windowing environment...
Definition: GuiBase.hh:75
int MouseButtonReleaseY_
Definition: GuiBase.hh:196
bool WindowCreated_
Definition: GuiBase.hh:180
unsigned int GetWidth() const
Definition: ImageBase.hh:312
char KeyBuffer_
Definition: GuiBase.hh:202
int GetMouseX()
Definition: GuiBase.hh:115
int MouseButtonReleaseX_
Definition: GuiBase.hh:195
bool TestImage_(Image< unsigned char > &image)
test if internal variables Width_, Height_ and nChannels_ are set correct for image these 3 variables...
Definition: GuiBase.hh:217
unsigned int Pyramid0Height_
Definition: GuiBase.hh:186
int MousePosX_
Definition: GuiBase.hh:189
std::string GetTitle()
Definition: GuiBase.hh:89
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
std::string Title_
Definition: GuiBase.hh:201
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
int MouseButtonPressed_
Definition: GuiBase.hh:191
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int IsPyramid_
Definition: GuiBase.hh:205
unsigned int Height_
Definition: GuiBase.hh:183
bool Closed()
returns true if window is closed by user via x button
Definition: GuiBase.hh:164
unsigned int nChannels_
Definition: GuiBase.hh:184
int GetMouseY()
Definition: GuiBase.hh:116
int MousePosY_
Definition: GuiBase.hh:190
int MouseButtonPressY_
Definition: GuiBase.hh:193
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
int MouseButtonReleased_
Definition: GuiBase.hh:194