1 // This is brl/bbas/bgui/bgui_image_tableau.h
2 #ifndef bgui_image_tableau_h_
3 #define bgui_image_tableau_h_
4 //:
5 // \file
6 // \author  Marko Bacic (u97mb@robots.ox.ac.uk)
7 // \brief An image tableau with an associated region of interest.
8 //
9 // \verbatim
10 //  Modifications
11 //   Marko Bacic    AUG-2000  Initial version.
12 //   K.Y.McGaul  05-AUG-2002  Print mouse position on status bar.
13 //   K.Y.McGaul  06-AUG-2002  Print RGB value on status bar.
14 //   Joseph Mundy   DEC-2002  Copied to brl to serve as a basis for extended functions
15 //   Peter Vanroose AUG-2004  Moved bgui_roi_tableau_make_roi to separate file
16 //   Gamze Tunali   SEP-2007  Added class bgui_image_tableau_vp_sc_snapshot
17 // \endverbatim
18 
19 #include "bgui_image_tableau_sptr.h"
20 #include <vgui/vgui_image_tableau.h>
21 #include <vgui/vgui_range_map_params_sptr.h>
22 #include <vil/vil_image_resource.h>
23 #include <vil/vil_image_view_base.h>
24 #include <vgui/vgui_text_tableau.h>
25 #include <vil1/vil1_image.h>
26 #include <vgui/vgui_gl.h>
27 #include "vgui/vgui_utils.h"
28 
29 class bgui_image_tableau_vp_sc_snapshot
30 {
31  public:
32   GLint vp[4];
33   GLint sc[4];
34   bool sc_was_enabled;
35 
bgui_image_tableau_vp_sc_snapshot()36   bgui_image_tableau_vp_sc_snapshot() {
37     vgui_utils::get_glViewport(vp);
38 
39     //std::cout << "Saving    [" << vp[0] << ' ' << vp[1] << ' ' << vp[2] << ' ' << vp[3] << std::endl;
40     glGetIntegerv(GL_SCISSOR_BOX, sc);
41     sc_was_enabled = glIsEnabled(GL_SCISSOR_TEST) == GL_TRUE;
42   }
43 
~bgui_image_tableau_vp_sc_snapshot()44   ~bgui_image_tableau_vp_sc_snapshot() {
45     // restore viewport :
46     vgui_utils::set_glViewport(vp[0], vp[1], vp[2], vp[3]);
47     //std::cout << "restoring [" << vp[0] << ' ' << vp[1] << ' ' << vp[2] << ' ' << vp[3] << std::endl;
48     // turn off the scissor test, if it wasn't already on, and
49     // restore old scissor settings :
50     if (sc_was_enabled)
51       glEnable(GL_SCISSOR_TEST);
52     else
53       glDisable(GL_SCISSOR_TEST);
54     vgui_utils::set_glScissor(sc[0], sc[1], sc[2], sc[3]);
55   }
56 };
57 
58 class bgui_image_tableau : public vgui_image_tableau
59 {
60  public:
61   typedef vgui_image_tableau base;
62 
63   bgui_image_tableau();
64 
65   bgui_image_tableau(vil_image_resource_sptr const & img,
66                      vgui_range_map_params_sptr const & rmp = nullptr);
67 
68   bgui_image_tableau( vil_image_view_base const & img,
69                       vgui_range_map_params_sptr const & rmp = nullptr);
70 
71   bgui_image_tableau(vil1_image const & img,
72                      vgui_range_map_params_sptr const & rmp = nullptr);
73 
74   bgui_image_tableau(char const* filename,
75                      vgui_range_map_params_sptr const & rmp = nullptr);
76 
77 
type_name()78   std::string type_name() const { return "vgui_image_tableau"; }
79 
80   //: Return the vil1_image
81   vil1_image get_image() const;
82 
83   //: Return the vil_image_resource
84   vil_image_resource_sptr get_image_resource() const;
85 
86   //: Extract a line of pixel values (force to grey scale)
87   void image_line(const float col_start,
88                   const float row_start,
89                   const float col_end,
90                   const float row_end,
91                   std::vector<double>& line_pos,
92                   std::vector<double>& vals);
93 
94   //: Extract a line of pixel values return color if available
95   void image_line(const float col_start,
96                   const float row_start,
97                   const float col_end,
98                   const float row_end,
99                   std::vector<double>& line_pos,
100                   std::vector<std::vector<double> >& vals);
101 
102   //: set handle_motion true
set_handle_motion()103   void set_handle_motion() { handle_motion_ = true; }
unset_handle_motion()104   void unset_handle_motion() { handle_motion_ = false; }
handle_motion()105   bool handle_motion() const { return handle_motion_; }
106 
107   //: display a message at the mouse pointer
set_capture_mouse()108   void set_capture_mouse(){capture_mouse_ = true;}
unset_capture_mouse()109   void unset_capture_mouse(){capture_mouse_ = false;}
set_mouse_message()110   void set_mouse_message() {mouse_message_ = true; capture_mouse_ = true;}
111   void unset_mouse_message();
mouse_message()112   bool mouse_message() const {return mouse_message_;}
set_mouse_message_text(std::string const & msg)113   void set_mouse_message_text(std::string const& msg){mouse_message_text_ = msg;}
mouse_message_text()114   std::string mouse_message_text() const{ return mouse_message_text_;}
mouse_pos(float & mouse_u,float & mouse_v)115   bool mouse_pos(float& mouse_u, float& mouse_v){mouse_u = mouse_pos_[0],
116       mouse_v = mouse_pos_[1]; return mouse_message_;}
117 
118   // true means locked, false unlocked
lock_linenum(bool b)119   void lock_linenum(bool b) { locked_ = b; handle_motion_=!b; }
show_image_path(bool show)120   void show_image_path(bool show){show_path_ = show;}
text_tab()121   vgui_text_tableau_sptr text_tab(){return tt_;}
122  protected:
123   //: Handle all events for this tableau.
124   bool handle(vgui_event const &e);
125 
126   //: get the pixel value as a double. RGB converted to grey.
127   double get_pixel_value(const unsigned c, const unsigned r);
128 
129 //: get the pixel value as color
130   std::vector<double> get_color_pixel_value(const unsigned c, const unsigned r);
131 
132   //: Get pixel info from the frame buffer)
133   void get_pixel_info_from_frame_buffer(const int x, const int y,
134                                         vgui_event const &e, char* msg);
135 
136   //: Get pixel info from the image (rather than frame buffer)
137   void get_pixel_info_from_image(const int x, const int y,
138                                  vgui_event const &e, char* msg);
139 
140   //: If false this tableau stops handling motion
141   bool handle_motion_;
142 
143   bool locked_;
144   bool show_path_;
145   bool mouse_message_;
146   bool capture_mouse_;
147   std::string mouse_message_text_;
148   float mouse_pos_[2];
149   vgui_text_tableau_sptr tt_;
150 };
151 
152 //: Creates a smart-pointer to a bgui_image_tableau.
153 struct bgui_image_tableau_new : public bgui_image_tableau_sptr {
154 
155   //: Constructor - creates an empty image tableau.
bgui_image_tableau_newbgui_image_tableau_new156   bgui_image_tableau_new()
157     : bgui_image_tableau_sptr( new bgui_image_tableau() )
158   { }
159 
160   //:  Constructors - create a tableau displaying the given image.
161 
162   bgui_image_tableau_new(vil_image_resource_sptr const & img,
163                          vgui_range_map_params_sptr const & rmp = nullptr)
bgui_image_tableau_sptrbgui_image_tableau_new164     : bgui_image_tableau_sptr( new bgui_image_tableau(img, rmp) )
165   { }
166 
167   bgui_image_tableau_new(vil_image_view_base const & img,
168                          vgui_range_map_params_sptr const & rmp = nullptr)
bgui_image_tableau_sptrbgui_image_tableau_new169     : bgui_image_tableau_sptr( new bgui_image_tableau(img, rmp) )
170   { }
171 
172   bgui_image_tableau_new(vil1_image const & img,
173                          vgui_range_map_params_sptr const & rmp = nullptr)
bgui_image_tableau_sptrbgui_image_tableau_new174     : bgui_image_tableau_sptr( new bgui_image_tableau(img, rmp) )
175   { }
176 
177   bgui_image_tableau_new(char const* filename,
178                          vgui_range_map_params_sptr const & rmp = nullptr)
bgui_image_tableau_sptrbgui_image_tableau_new179     : bgui_image_tableau_sptr( new bgui_image_tableau(filename, rmp) )
180   { }
181 
182   //:to cast up to a vgui_image_tableau
vgui_image_tableau_sptrbgui_image_tableau_new183   operator vgui_image_tableau_sptr () const
184   { vgui_image_tableau_sptr tt; tt.vertical_cast(*this); return tt; }
185 };
186 
187 #endif // bgui_image_tableau_h_
188