1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Image display.
33  *
34  * Authors:
35  * Christophe Collewet
36  * Eric Marchand
37  *
38  *****************************************************************************/
39 
40 #ifndef vpDisplayGTK_h
41 #define vpDisplayGTK_h
42 
43 #include <visp3/core/vpConfig.h>
44 #if (defined(VISP_HAVE_GTK))
45 
46 #include <visp3/core/vpDisplay.h>
47 #include <visp3/core/vpImage.h>
48 
49 /*!
50   \file vpDisplayGTK.h
51   \brief Define the GTK console to display images.
52 */
53 
54 /*!
55 
56   \class vpDisplayGTK
57 
58   \ingroup group_gui_display
59 
60   \brief The vpDisplayGTK allows to display image using the GTK 3rd party
61   library. Thus to enable this class GTK should be installed. Installation
62   instructions are provided here https://visp.inria.fr/3rd_gtk.
63 
64   The example below shows how to display an image with this video device.
65   \code
66 #include <visp3/core/vpImagePoint.h>
67 #include <visp3/gui/vpDisplayGTK.h>
68 #include <visp3/io/vpImageIo.h>
69 
70 int main()
71 {
72 #if defined(VISP_HAVE_GTK)
73   vpImage<unsigned char> I; // Grey level image
74 
75   // Read an image in PGM P5 format
76 #ifdef _WIN32
77   vpImageIo::read(I, "C:/temp/ViSP-images/Klimt/Klimt.pgm");
78 #else
79   vpImageIo::read(I, "/local/soft/ViSP/ViSP-images/Klimt/Klimt.pgm");
80 #endif
81 
82   vpDisplayGTK d;
83 
84   // Initialize the display with the image I. Display and image are
85   // now link together.
86   d.init(I);
87 
88   // Specify the window location
89   vpDisplay::setWindowPosition(I, 400, 100);
90 
91   // Set the display window title
92   vpDisplay::setTitle(I, "My GTK display");
93 
94   // Set the display background with image I content
95   vpDisplay::display(I);
96 
97   // Draw a red rectangle in the display overlay (foreground)
98   vpDisplay::displayRectangle(I, 10, 10, 100, 20, vpColor::red, true);
99 
100   // Draw a red rectangle in the display overlay (foreground)
101   vpImagePoint topLeftCorner;
102   topLeftCorner.set_i(50);
103   topLeftCorner.set_j(10);
104   vpDisplay::displayRectangle(I, topLeftCorner, 100, 20, vpColor::green, true);
105 
106   // Flush the foreground and background display
107   vpDisplay::flush(I);
108 
109   // Get non blocking keyboard events
110   std::cout << "Check keyboard events..." << std::endl;
111   char key[10];
112   bool ret;
113   for (int i=0; i< 200; i++) {
114     bool ret = vpDisplay::getKeyboardEvent(I, key, false);
115     if (ret)
116       std::cout << "keyboard event: key: " << "\"" << key << "\"" << std::endl;
117     vpTime::wait(40);
118   }
119 
120   // Get a blocking keyboard event
121   std::cout << "Wait for a keyboard event..." << std::endl;
122   ret = vpDisplay::getKeyboardEvent(I, key, true);
123   std::cout << "keyboard event: " << ret << std::endl;
124   if (ret)
125     std::cout << "key: " << "\"" << key << "\"" << std::endl;
126 
127   // Wait for a click in the display window
128   std::cout << "Wait for a button click..." << std::endl;
129   vpDisplay::getClick(I);
130 #endif
131 }
132   \endcode
133 */
134 class VISP_EXPORT vpDisplayGTK : public vpDisplay
135 {
136 private:
137   typedef enum {
138     id_black = 0,
139     id_white,
140     id_lightGray,
141     id_gray,
142     id_darkGray,
143     id_lightRed,
144     id_red,
145     id_darkRed,
146     id_lightGreen,
147     id_green,
148     id_darkGreen,
149     id_lightBlue,
150     id_blue,
151     id_darkBlue,
152     id_yellow,
153     id_cyan,
154     id_orange,
155     id_purple,
156     id_npredefined // Number of predefined colors
157   } vpColorIdentifier;
158 
159 public:
160   vpDisplayGTK();
161   vpDisplayGTK(int win_x, int win_y, const std::string &win_title = "");
162   vpDisplayGTK(vpImage<unsigned char> &I, vpScaleType type);
163   vpDisplayGTK(vpImage<unsigned char> &I, int win_x = -1, int win_y = -1, const std::string &win_title = "",
164                vpScaleType type = SCALE_DEFAULT);
165   vpDisplayGTK(vpImage<vpRGBa> &I, vpScaleType type);
166   vpDisplayGTK(vpImage<vpRGBa> &I, int win_x = -1, int win_y = -1, const std::string &win_title = "",
167                vpScaleType type = SCALE_DEFAULT);
168 
169   virtual ~vpDisplayGTK();
170 
171   void getImage(vpImage<vpRGBa> &I);
172   unsigned int getScreenDepth();
173   unsigned int getScreenHeight();
174   void getScreenSize(unsigned int &screen_width, unsigned int &screen_height);
175   unsigned int getScreenWidth();
176 
177   void init(vpImage<unsigned char> &I, int win_x = -1, int win_y = -1, const std::string &win_title = "");
178   void init(vpImage<vpRGBa> &I, int win_x = -1, int win_y = -1, const std::string &win_title = "");
179   void init(unsigned int win_width, unsigned int win_height, int win_x = -1, int win_y = -1, const std::string &win_title = "");
180 
181 protected:
182   void setFont(const std::string &fontname);
183   void setTitle(const std::string &win_title);
184   void setWindowPosition(int win_x, int win_y);
185 
186   void clearDisplay(const vpColor &color = vpColor::white);
187 
188   void closeDisplay();
189 
190   void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color = vpColor::white,
191                     unsigned int w = 4, unsigned int h = 2, unsigned int thickness = 1);
192   void displayCharString(const vpImagePoint &ip, const char *text, const vpColor &color = vpColor::green);
193 
194   void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill = false,
195                      unsigned int thickness = 1);
196   void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness = 1);
197   void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color,
198                       unsigned int thickness = 1);
199 
200   void displayImage(const vpImage<vpRGBa> &I);
201   void displayImage(const vpImage<unsigned char> &I);
202   void displayImage(const unsigned char *I);
203 
204   void displayImageROI(const vpImage<unsigned char> &I, const vpImagePoint &iP, unsigned int width,
205                        unsigned int height);
206   void displayImageROI(const vpImage<vpRGBa> &I, const vpImagePoint &iP, unsigned int width,
207                        unsigned int height);
208 
209   void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness = 1);
210 
211   void displayPoint(const vpImagePoint &ip, const vpColor &color, unsigned int thickness = 1);
212   void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color,
213                         bool fill = false, unsigned int thickness = 1);
214   void displayRectangle(const vpImagePoint &topLeft, const vpImagePoint &bottomRight, const vpColor &color,
215                         bool fill = false, unsigned int thickness = 1);
216   void displayRectangle(const vpRect &rectangle, const vpColor &color, bool fill = false, unsigned int thickness = 1);
217 
218   void flushDisplay();
219   void flushDisplayROI(const vpImagePoint &iP, unsigned int width, unsigned int height);
220 
221   bool getClick(bool blocking = true);
222   bool getClick(vpImagePoint &ip, bool blocking = true);
223   bool getClick(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking = true);
224   bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking = true);
225   bool getKeyboardEvent(bool blocking = true);
226   bool getKeyboardEvent(std::string &key, bool blocking = true);
227   bool getPointerMotionEvent(vpImagePoint &ip);
228   bool getPointerPosition(vpImagePoint &ip);
229 
230 private:
231   // Implementation
232   class Impl;
233   Impl *m_impl;
234 };
235 
236 #endif
237 #endif
238