ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Modules Pages
testClick.cpp
1 /****************************************************************************
2  *
3  * $Id: testClick.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Test for mouse click manipulations.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpConfig.h>
43 #include <visp/vpDebug.h>
44 
45 #include <stdlib.h>
46 #include <iostream>
47 #include <string>
48 
49 #if (defined (VISP_HAVE_GTK) || defined(VISP_HAVE_X11) || defined(VISP_HAVE_GDI) || defined(VISP_HAVE_D3D9) || defined(VISP_HAVE_OPENCV))
50 
51 #include <visp/vpImage.h>
52 #include <visp/vpImageIo.h>
53 #include <visp/vpParseArgv.h>
54 #include <visp/vpIoTools.h>
55 
56 #include <visp/vpDisplayOpenCV.h>
57 #include <visp/vpDisplayGTK.h>
58 #include <visp/vpDisplayX.h>
59 #include <visp/vpDisplayGDI.h>
60 #include <visp/vpDisplayD3D.h>
61 
69 // List of allowed command line options
70 #define GETOPTARGS "i:hlt:dc"
71 
72 typedef enum {
73  vpX11,
74  vpGTK,
75  vpGDI,
76  vpD3D,
77  vpCV
78 } vpDisplayType;
79 
80 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype);
81 bool getOptions(int argc, const char **argv,
82  std::string &ipath, vpDisplayType &dtype, bool &list,
83  bool &click_allowed, bool &display );
84 
95 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype)
96 {
97  fprintf(stdout, "\n\
98 Test click functionnalities in video devices or display.\n\
99 \n\
100 SYNOPSIS\n\
101  %s [-i <input image path>] \n\
102  [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
103 ", name);
104 
105  std::string display;
106  switch(dtype) {
107  case vpX11: display = "X11"; break;
108  case vpGTK: display = "GTK"; break;
109  case vpGDI: display = "GDI"; break;
110  case vpD3D: display = "D3D"; break;
111  case vpCV: display = "CV"; break;
112  }
113 
114  fprintf(stdout, "\n\
115 OPTIONS: Default\n\
116  -i <input image path> %s\n\
117  Set image input path.\n\
118  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
119  and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
120  Setting the VISP_INPUT_IMAGE_PATH environment\n\
121  variable produces the same behaviour than using\n\
122  this option.\n\
123 \n\
124  -t <type of video device> \"%s\"\n\
125  String specifying the video device to use.\n\
126  Possible values:\n\
127  \"X11\": only on UNIX platforms,\n\
128  \"GTK\": on all plaforms,\n\
129  \"GDI\": only on Windows platform (Graphics Device Interface),\n\
130  \"D3D\": only on Windows platform (Direct3D).\n\
131  \"CV\" : (OpenCV).\n\
132 \n\
133  -l\n\
134  Print the list of video-devices available and exit.\n\
135 \n\
136  -c\n\
137  Disable the mouse click. Useful to automaze the \n\
138  execution of this program without humain intervention.\n\
139 \n\
140  -d \n\
141  Turn off the display.\n\
142 \n\
143  -h\n\
144  Print the help.\n\n",
145  ipath.c_str(), display.c_str());
146 
147  if (badparam)
148  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
149 }
150 
169 bool getOptions(int argc, const char **argv,
170  std::string &ipath, vpDisplayType &dtype, bool &list,
171  bool &click_allowed, bool &display)
172 {
173  const char *optarg_;
174  int c;
175  std::string sDisplayType;
176  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
177 
178  switch (c) {
179  case 'i': ipath = optarg_; break;
180  case 'l': list = true; break;
181  case 't': sDisplayType = optarg_;
182  // Parse the display type option
183  if (sDisplayType.compare("X11") == 0) {
184  dtype = vpX11;
185  }
186  else if (sDisplayType.compare("GTK") == 0) {
187  dtype = vpGTK;
188  }
189  else if (sDisplayType.compare("GDI") == 0) {
190  dtype = vpGDI;
191  }
192  else if (sDisplayType.compare("D3D") == 0) {
193  dtype = vpD3D;
194  }
195  else if (sDisplayType.compare("CV") == 0) {
196  dtype = vpCV;
197  }
198 
199  break;
200  case 'h': usage(argv[0], NULL, ipath, dtype); return false; break;
201  case 'c': click_allowed = false; break;
202  case 'd': display = false; break;
203 
204  default:
205  usage(argv[0], optarg_, ipath, dtype); return false; break;
206  }
207  }
208 
209 
210  if ((c == 1) || (c == -1)) {
211  // standalone param or error
212  usage(argv[0], NULL, ipath, dtype);
213  std::cerr << "ERROR: " << std::endl;
214  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
215  return false;
216  }
217 
218  return true;
219 }
220 
221 int
222 main(int argc, const char ** argv)
223 {
224  try {
225  std::string env_ipath;
226  std::string opt_ipath;
227  bool opt_list = false; // To print the list of video devices
228  vpDisplayType opt_dtype; // Type of display to use
229  std::string ipath;
230  std::string filename;
231  bool opt_click_allowed = true;
232  bool opt_display = true;
233 
234  // Default display is one available
235 #if defined VISP_HAVE_GTK
236  opt_dtype = vpGTK;
237 #elif defined VISP_HAVE_X11
238  opt_dtype = vpX11;
239 #elif defined VISP_HAVE_GDI
240  opt_dtype = vpGDI;
241 #elif defined VISP_HAVE_D3D9
242  opt_dtype = vpD3D;
243 #elif defined VISP_HAVE_OPENCV
244  opt_dtype = vpCV;
245 #endif
246 
247  // Get the VISP_IMAGE_PATH environment variable value
248  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
249  if (ptenv != NULL)
250  env_ipath = ptenv;
251 
252  // Set the default input path
253  if (! env_ipath.empty())
254  ipath = env_ipath;
255 
256  // Read the command line options
257  if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list,
258  opt_click_allowed, opt_display) == false) {
259  exit (-1);
260  }
261 
262  // Print the list of video-devices available
263  if (opt_list) {
264  unsigned nbDevices = 0;
265  std::cout << "List of video-devices available: \n";
266 #if defined VISP_HAVE_GTK
267  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
268  nbDevices ++;
269 #endif
270 #if defined VISP_HAVE_X11
271  std::cout << " X11 (use \"-t X11\" option to use it)\n";
272  nbDevices ++;
273 #endif
274 #if defined VISP_HAVE_GDI
275  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
276  nbDevices ++;
277 #endif
278 #if defined VISP_HAVE_D3D9
279  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
280  nbDevices ++;
281 #endif
282 #if defined VISP_HAVE_OPENCV
283  std::cout << " CV (use \"-t CV\" option to use it)\n";
284  nbDevices ++;
285 #endif
286  if (!nbDevices) {
287  std::cout << " No display is available\n";
288  }
289  return (0);
290  }
291 
292 
293  // Get the option values
294  if (!opt_ipath.empty())
295  ipath = opt_ipath;
296 
297  // Compare ipath and env_ipath. If they differ, we take into account
298  // the input path comming from the command line option
299  if (!opt_ipath.empty() && !env_ipath.empty()) {
300  if (ipath != env_ipath) {
301  std::cout << std::endl
302  << "WARNING: " << std::endl;
303  std::cout << " Since -i <visp image path=" << ipath << "> "
304  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
305  << " we skip the environment variable." << std::endl;
306  }
307  }
308 
309  // Test if an input path is set
310  if (opt_ipath.empty() && env_ipath.empty()){
311  usage(argv[0], NULL, ipath, opt_dtype);
312  std::cerr << std::endl
313  << "ERROR:" << std::endl;
314  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
315  << std::endl
316  << " environment variable to specify the location of the " << std::endl
317  << " image path where test images are located." << std::endl << std::endl;
318  exit(-1);
319  }
320 
321  // Create a grey level image
323 
324  // Load a grey image from the disk
325  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
326  vpCTRACE << "Load " << filename << std::endl;
327  vpImageIo::read(I, filename) ;
328 
329  // Create a display for the image
330  vpDisplay *display = NULL;
331 
332  switch(opt_dtype) {
333  case vpX11:
334  std::cout << "Requested X11 display functionnalities..." << std::endl;
335 #if defined VISP_HAVE_X11
336  display = new vpDisplayX;
337 #else
338  std::cout << " Sorry, X11 video device is not available.\n";
339  std::cout << "Use \"" << argv[0]
340  << " -l\" to print the list of available devices.\n";
341  return 0;
342 #endif
343  break;
344  case vpGTK:
345  std::cout << "Requested GTK display functionnalities..." << std::endl;
346 #if defined VISP_HAVE_GTK
347  display = new vpDisplayGTK;
348 #else
349  std::cout << " Sorry, GTK video device is not available.\n";
350  std::cout << "Use \"" << argv[0]
351  << " -l\" to print the list of available devices.\n";
352  return 0;
353 #endif
354  break;
355  case vpGDI:
356  std::cout << "Requested GDI display functionnalities..." << std::endl;
357 #if defined VISP_HAVE_GDI
358  display = new vpDisplayGDI;
359 #else
360  std::cout << " Sorry, GDI video device is not available.\n";
361  std::cout << "Use \"" << argv[0]
362  << " -l\" to print the list of available devices.\n";
363  return 0;
364 #endif
365  break;
366  case vpD3D:
367  std::cout << "Requested D3D display functionnalities..." << std::endl;
368 #if defined VISP_HAVE_D3D9
369  display = new vpDisplayD3D;
370 #else
371  std::cout << " Sorry, D3D video device is not available.\n";
372  std::cout << "Use \"" << argv[0]
373  << " -l\" to print the list of available devices.\n";
374  return 0;
375 #endif
376  break;
377  case vpCV:
378  std::cout << "Requested OpenCV display functionnalities..." << std::endl;
379 #if defined VISP_HAVE_OPENCV
380  display = new vpDisplayOpenCV;
381 #else
382  std::cout << " Sorry, OpenCV video device is not available.\n";
383  std::cout << "Use \"" << argv[0]
384  << " -l\" to print the list of available devices.\n";
385  return 0;
386 #endif
387  break;
388  }
389 
390  if (opt_display){
391 
392  // We open a window using either X11 or GTK or GDI.
393  // Its size is automatically defined by the image (I) size
394  display->init(I, 100, 100,"Display...") ;
395 
396  // Display the image
397  // The image class has a member that specify a pointer toward
398  // the display that has been initialized in the display declaration
399  // therefore is is no longuer necessary to make a reference to the
400  // display variable.
401  vpDisplay::display(I) ;
402  //Flush the display
403  vpDisplay::flush(I) ;
404  if ( opt_click_allowed ){
405  std::cout << "Click on a pixel to get his coordinates...\n";
406  vpImagePoint ip;
408  vpDisplay::getClick(I, ip, button);
409  std::cout << " You click down on pixel (" << ip <<") ";
410  switch(button) {
411  case vpMouseButton::button1: std::cout << "with left button.\n"; break;
412  case vpMouseButton::button2: std::cout << "with middle button.\n"; break;
413  case vpMouseButton::button3: std::cout << "with right button.\n"; break;
414  }
415  vpDisplay::getClickUp(I, ip, button);
416  std::cout << " You click up on pixel (" << ip <<") ";
417  switch(button) {
418  case vpMouseButton::button1: std::cout << "with left button.\n"; break;
419  case vpMouseButton::button2: std::cout << "with middle button.\n"; break;
420  case vpMouseButton::button3: std::cout << "with right button.\n"; break;
421  }
423  std::cout << " Pointer poisition : " << ip << std::endl;
424  std::cout << "A click to exit...\n";
426  }
427  }
428  delete display;
429  }
430  catch(...) {
431  vpERROR_TRACE("Error while displaying the image") ;
432  exit(-1);
433  }
434 }
435 
436 #else
437 int
438 main()
439 {
440  vpERROR_TRACE("You do not have display functionalities...");
441 }
442 
443 #endif
virtual void init(vpImage< unsigned char > &I, int x=-1, int y=-1, const char *title=NULL)=0
Class that defines generic functionnalities for display.
Definition: vpDisplay.h:176
#define vpERROR_TRACE
Definition: vpDebug.h:395
Display for windows using GDI (available on any windows 32 platform).
Definition: vpDisplayGDI.h:132
Define the X11 console to display images.
Definition: vpDisplayX.h:152
virtual bool getClickUp(vpImagePoint &ip, vpMouseButton::vpMouseButtonType &button, bool blocking=true)=0
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:1994
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
#define vpCTRACE
Definition: vpDebug.h:341
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
virtual bool getPointerPosition(vpImagePoint &ip)=0
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:206
The vpDisplayOpenCV allows to display image using the opencv library.
The vpDisplayGTK allows to display image using the GTK+ library version 1.2.
Definition: vpDisplayGTK.h:145
virtual bool getClick(bool blocking=true)=0
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:92
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278