ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testClick.cpp
1 /****************************************************************************
2  *
3  * $Id: testClick.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
90 void usage(const char *name, const char *badparam, std::string ipath, vpDisplayType &dtype)
91 {
92  fprintf(stdout, "\n\
93 Test click functionnalities in video devices or display.\n\
94 \n\
95 SYNOPSIS\n\
96  %s [-i <input image path>] \n\
97  [-t <type of video device>] [-l] [-c] [-d] [-h]\n\
98 ", name);
99 
100  std::string display;
101  switch(dtype) {
102  case vpX11: display = "X11"; break;
103  case vpGTK: display = "GTK"; break;
104  case vpGDI: display = "GDI"; break;
105  case vpD3D: display = "D3D"; break;
106  case vpCV: display = "CV"; break;
107  }
108 
109  fprintf(stdout, "\n\
110 OPTIONS: Default\n\
111  -i <input image path> %s\n\
112  Set image input path.\n\
113  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
114  and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
115  Setting the VISP_INPUT_IMAGE_PATH environment\n\
116  variable produces the same behaviour than using\n\
117  this option.\n\
118 \n\
119  -t <type of video device> \"%s\"\n\
120  String specifying the video device to use.\n\
121  Possible values:\n\
122  \"X11\": only on UNIX platforms,\n\
123  \"GTK\": on all plaforms,\n\
124  \"GDI\": only on Windows platform (Graphics Device Interface),\n\
125  \"D3D\": only on Windows platform (Direct3D).\n\
126  \"CV\" : (OpenCV).\n\
127 \n\
128  -l\n\
129  Print the list of video-devices available and exit.\n\
130 \n\
131  -c\n\
132  Disable the mouse click. Useful to automaze the \n\
133  execution of this program without humain intervention.\n\
134 \n\
135  -d \n\
136  Turn off the display.\n\
137 \n\
138  -h\n\
139  Print the help.\n\n",
140  ipath.c_str(), display.c_str());
141 
142  if (badparam)
143  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
144 }
145 
164 bool getOptions(int argc, const char **argv,
165  std::string &ipath, vpDisplayType &dtype, bool &list,
166  bool &click_allowed, bool &display )
167 {
168  const char *optarg;
169  int c;
170  std::string sDisplayType;
171  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
172 
173  switch (c) {
174  case 'i': ipath = optarg; break;
175  case 'l': list = true; break;
176  case 't': sDisplayType = optarg;
177  // Parse the display type option
178  if (sDisplayType.compare("X11") == 0) {
179  dtype = vpX11;
180  }
181  else if (sDisplayType.compare("GTK") == 0) {
182  dtype = vpGTK;
183  }
184  else if (sDisplayType.compare("GDI") == 0) {
185  dtype = vpGDI;
186  }
187  else if (sDisplayType.compare("D3D") == 0) {
188  dtype = vpD3D;
189  }
190  else if (sDisplayType.compare("CV") == 0) {
191  dtype = vpCV;
192  }
193 
194  break;
195  case 'h': usage(argv[0], NULL, ipath, dtype); return false; break;
196  case 'c': click_allowed = false; break;
197  case 'd': display = false; break;
198 
199  default:
200  usage(argv[0], optarg, ipath, dtype); return false; break;
201  }
202  }
203 
204 
205  if ((c == 1) || (c == -1)) {
206  // standalone param or error
207  usage(argv[0], NULL, ipath, dtype);
208  std::cerr << "ERROR: " << std::endl;
209  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
210  return false;
211  }
212 
213  return true;
214 }
215 
216 int
217 main(int argc, const char ** argv)
218 {
219  try {
220  std::string env_ipath;
221  std::string opt_ipath;
222  bool opt_list = false; // To print the list of video devices
223  vpDisplayType opt_dtype; // Type of display to use
224  std::string ipath;
225  std::string filename;
226  bool opt_click_allowed = true;
227  bool opt_display = true;
228 
229  // Default display is one available
230 #if defined VISP_HAVE_GTK
231  opt_dtype = vpGTK;
232 #elif defined VISP_HAVE_X11
233  opt_dtype = vpX11;
234 #elif defined VISP_HAVE_GDI
235  opt_dtype = vpGDI;
236 #elif defined VISP_HAVE_D3D9
237  opt_dtype = vpD3D;
238 #elif defined VISP_HAVE_OPENCV
239  opt_dtype = vpCV;
240 #endif
241 
242  // Get the VISP_IMAGE_PATH environment variable value
243  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
244  if (ptenv != NULL)
245  env_ipath = ptenv;
246 
247  // Set the default input path
248  if (! env_ipath.empty())
249  ipath = env_ipath;
250 
251  // Read the command line options
252  if (getOptions(argc, argv, opt_ipath, opt_dtype, opt_list,
253  opt_click_allowed, opt_display) == false) {
254  exit (-1);
255  }
256 
257  // Print the list of video-devices available
258  if (opt_list) {
259  unsigned nbDevices = 0;
260  std::cout << "List of video-devices available: \n";
261 #if defined VISP_HAVE_GTK
262  std::cout << " GTK (use \"-t GTK\" option to use it)\n";
263  nbDevices ++;
264 #endif
265 #if defined VISP_HAVE_X11
266  std::cout << " X11 (use \"-t X11\" option to use it)\n";
267  nbDevices ++;
268 #endif
269 #if defined VISP_HAVE_GDI
270  std::cout << " GDI (use \"-t GDI\" option to use it)\n";
271  nbDevices ++;
272 #endif
273 #if defined VISP_HAVE_D3D9
274  std::cout << " D3D (use \"-t D3D\" option to use it)\n";
275  nbDevices ++;
276 #endif
277 #if defined VISP_HAVE_OPENCV
278  std::cout << " CV (use \"-t CV\" option to use it)\n";
279  nbDevices ++;
280 #endif
281  if (!nbDevices) {
282  std::cout << " No display is available\n";
283  }
284  return (0);
285  }
286 
287 
288  // Get the option values
289  if (!opt_ipath.empty())
290  ipath = opt_ipath;
291 
292  // Compare ipath and env_ipath. If they differ, we take into account
293  // the input path comming from the command line option
294  if (!opt_ipath.empty() && !env_ipath.empty()) {
295  if (ipath != env_ipath) {
296  std::cout << std::endl
297  << "WARNING: " << std::endl;
298  std::cout << " Since -i <visp image path=" << ipath << "> "
299  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
300  << " we skip the environment variable." << std::endl;
301  }
302  }
303 
304  // Test if an input path is set
305  if (opt_ipath.empty() && env_ipath.empty()){
306  usage(argv[0], NULL, ipath, opt_dtype);
307  std::cerr << std::endl
308  << "ERROR:" << std::endl;
309  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
310  << std::endl
311  << " environment variable to specify the location of the " << std::endl
312  << " image path where test images are located." << std::endl << std::endl;
313  exit(-1);
314  }
315 
316  // Create a grey level image
318 
319  // Load a grey image from the disk
320  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
321  vpCTRACE << "Load " << filename << std::endl;
322  vpImageIo::readPGM(I, filename) ;
323 
324  // Create a display for the image
325  vpDisplay *display = NULL;
326 
327  switch(opt_dtype) {
328  case vpX11:
329  std::cout << "Requested X11 display functionnalities..." << std::endl;
330 #if defined VISP_HAVE_X11
331  display = new vpDisplayX;
332 #else
333  std::cout << " Sorry, X11 video device is not available.\n";
334  std::cout << "Use \"" << argv[0]
335  << " -l\" to print the list of available devices.\n";
336  return 0;
337 #endif
338  break;
339  case vpGTK:
340  std::cout << "Requested GTK display functionnalities..." << std::endl;
341 #if defined VISP_HAVE_GTK
342  display = new vpDisplayGTK;
343 #else
344  std::cout << " Sorry, GTK video device is not available.\n";
345  std::cout << "Use \"" << argv[0]
346  << " -l\" to print the list of available devices.\n";
347  return 0;
348 #endif
349  break;
350  case vpGDI:
351  std::cout << "Requested GDI display functionnalities..." << std::endl;
352 #if defined VISP_HAVE_GDI
353  display = new vpDisplayGDI;
354 #else
355  std::cout << " Sorry, GDI video device is not available.\n";
356  std::cout << "Use \"" << argv[0]
357  << " -l\" to print the list of available devices.\n";
358  return 0;
359 #endif
360  break;
361  case vpD3D:
362  std::cout << "Requested D3D display functionnalities..." << std::endl;
363 #if defined VISP_HAVE_D3D9
364  display = new vpDisplayD3D;
365 #else
366  std::cout << " Sorry, D3D video device is not available.\n";
367  std::cout << "Use \"" << argv[0]
368  << " -l\" to print the list of available devices.\n";
369  return 0;
370 #endif
371  break;
372  case vpCV:
373  std::cout << "Requested OpenCV display functionnalities..." << std::endl;
374 #if defined VISP_HAVE_OPENCV
375  display = new vpDisplayOpenCV;
376 #else
377  std::cout << " Sorry, OpenCV video device is not available.\n";
378  std::cout << "Use \"" << argv[0]
379  << " -l\" to print the list of available devices.\n";
380  return 0;
381 #endif
382  break;
383  }
384 
385  if (opt_display){
386 
387  // We open a window using either X11 or GTK or GDI.
388  // Its size is automatically defined by the image (I) size
389  display->init(I, 100, 100,"Display...") ;
390 
391  // Display the image
392  // The image class has a member that specify a pointer toward
393  // the display that has been initialized in the display declaration
394  // therefore is is no longuer necessary to make a reference to the
395  // display variable.
396  vpDisplay::display(I) ;
397  //Flush the display
398  vpDisplay::flush(I) ;
399  if ( opt_click_allowed ){
400  std::cout << "Click on a pixel to get his coordinates...\n";
401  vpImagePoint ip;
403  vpDisplay::getClick(I, ip, button);
404  std::cout << " You click down on pixel (" << ip <<") ";
405  switch(button) {
406  case vpMouseButton::button1: std::cout << "with left button.\n"; break;
407  case vpMouseButton::button2: std::cout << "with middle button.\n"; break;
408  case vpMouseButton::button3: std::cout << "with right button.\n"; break;
409  }
410  vpDisplay::getClickUp(I, ip, button);
411  std::cout << " You click up on pixel (" << ip <<") ";
412  switch(button) {
413  case vpMouseButton::button1: std::cout << "with left button.\n"; break;
414  case vpMouseButton::button2: std::cout << "with middle button.\n"; break;
415  case vpMouseButton::button3: std::cout << "with right button.\n"; break;
416  }
418  std::cout << " Pointer poisition : " << ip << std::endl;
419  std::cout << "A click to exit...\n";
421  }
422  }
423  delete display;
424  }
425  catch(...) {
426  vpERROR_TRACE("Error while displaying the image") ;
427  exit(-1);
428  }
429 
430 }
431 
432 #else
433 int
434 main()
435 {
436  vpERROR_TRACE("You do not have display functionalities...");
437 }
438 
439 #endif