ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
displayD3D.cpp
1 /****************************************************************************
2  *
3  * $Id: displayD3D.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  * Windows' D3D Display Test
36  *
37  * Authors:
38  * Bruno Renier
39  * Anthony Saunier
40  *
41  *****************************************************************************/
51 #include <visp/vpDebug.h>
52 #include <visp/vpConfig.h>
53 
54 #if ( defined(VISP_HAVE_D3D9) )
55 
56 #include <visp/vpDisplayD3D.h>
57 
58 #include <visp/vpImage.h>
59 #include <visp/vpImageIo.h>
60 #include <visp/vpParseArgv.h>
61 #include <visp/vpIoTools.h>
62 
72 // List of allowed command line options
73 #define GETOPTARGS "cdi:o:h"
74 
88 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
89 {
90  fprintf(stdout, "\n\
91 Read an image on the disk, display it using D3D, display some\n\
92 features (line, circle, caracters) in overlay and finaly write \n\
93 the image and the overlayed features in an image on the disk\n\
94 \n\
95 SYNOPSIS\n\
96  %s [-i <input image path>] [-o <output image path>]\n\
97  [-c] [-d] [-h]\n \
98 ", name);
99 
100  fprintf(stdout, "\n\
101 OPTIONS: Default\n\
102  -i <input image path> %s\n\
103  Set image input path.\n\
104  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
105  image.\n\
106  Setting the VISP_INPUT_IMAGE_PATH environment\n\
107  variable produces the same behaviour than using\n\
108  this option.\n\
109 \n\
110  -o <output image path> %s\n\
111  Set image output path.\n\
112  From this directory, creates the \"%s\"\n\
113  subdirectory depending on the username, where \n\
114  Klimt_grey.overlay.ppm output image is written.\n\
115 \n\
116  -c\n\
117  Disable the mouse click. Useful to automate the \n\
118  execution of this program without humain intervention.\n\
119 \n\
120  -d \n\
121  Disable the image display. This can be useful \n\
122  for automatic tests using the task manager under \n\
123  Windows.\n\
124 \n\
125  -h\n\
126  Print the help.\n\n",
127  ipath.c_str(), opath.c_str(), user.c_str());
128  if (badparam) {
129  fprintf(stderr, "ERROR: \n" );
130  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
131  }
132 }
133 
149 bool getOptions(int argc, const char **argv,
150  std::string &ipath, std::string &opath, bool &click_allowed,
151  std::string user, bool &display)
152 {
153  const char *optarg;
154  int c;
155  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
156 
157  switch (c) {
158  case 'c': click_allowed = false; break;
159  case 'd': display = false; break;
160  case 'i': ipath = optarg; break;
161  case 'o': opath = optarg; break;
162  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
163 
164  default:
165  usage(argv[0], optarg, ipath, opath, user); return false; break;
166  }
167  }
168 
169  if ((c == 1) || (c == -1)) {
170  // standalone param or error
171  usage(argv[0], NULL, ipath, opath, user);
172  std::cerr << "ERROR: " << std::endl;
173  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
174  return false;
175  }
176 
177  return true;
178 }
179 
180 int
181 main(int argc, const char ** argv)
182 {
183  std::string env_ipath;
184  std::string opt_ipath;
185  std::string opt_opath;
186  std::string ipath;
187  std::string opath;
188  std::string filename;
189  std::string username;
190  bool opt_click_allowed = true;
191  bool opt_display = true;
192 
193  // Get the VISP_IMAGE_PATH environment variable value
194  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
195  if (ptenv != NULL)
196  env_ipath = ptenv;
197 
198  // Set the default input path
199  if (! env_ipath.empty())
200  ipath = env_ipath;
201 
202  // Set the default output path
203  opt_opath = "C:\\temp";
204 
205 
206  // Get the user login name
207  vpIoTools::getUserName(username);
208 
209  // Read the command line options
210  if (getOptions(argc, argv, opt_ipath, opt_opath,
211  opt_click_allowed, username, opt_display) == false) {
212  exit (-1);
213  }
214 
215  // Get the option values
216  if (!opt_ipath.empty())
217  ipath = opt_ipath;
218  if (!opt_opath.empty())
219  opath = opt_opath;
220 
221  // Append to the output path string, the login name of the user
222  std::string odirname = opath + vpIoTools::path("/") + username;
223 
224  // Test if the output path exist. If no try to create it
225  if (vpIoTools::checkDirectory(odirname) == false) {
226  try {
227  // Create the dirname
228  vpIoTools::makeDirectory(odirname);
229  }
230  catch (...) {
231  usage(argv[0], NULL, ipath, opath, username);
232  std::cerr << std::endl
233  << "ERROR:" << std::endl;
234  std::cerr << " Cannot create " << odirname << std::endl;
235  std::cerr << " Check your -o " << opath << " option " << std::endl;
236  exit(-1);
237  }
238  }
239 
240  // Compare ipath and env_ipath. If they differ, we take into account
241  // the input path comming from the command line option
242  if (!opt_ipath.empty() && !env_ipath.empty()) {
243  if (ipath != env_ipath) {
244  std::cout << std::endl
245  << "WARNING: " << std::endl;
246  std::cout << " Since -i <visp image path=" << ipath << "> "
247  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
248  << " we skip the environment variable." << std::endl;
249  }
250  }
251 
252  // Test if an input path is set
253  if (opt_ipath.empty() && env_ipath.empty()){
254  usage(argv[0], NULL, ipath, opath, username);
255  std::cerr << std::endl
256  << "ERROR:" << std::endl;
257  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
258  << std::endl
259  << " environment variable to specify the location of the " << std::endl
260  << " image path where test images are located." << std::endl << std::endl;
261  exit(-1);
262  }
263 
264  // Create a grey level image
266  vpImagePoint ip, ip1, ip2;
267 
268  // Load a grey image from the disk
269  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
270  vpImageIo::readPGM(I, filename) ;
271 
272  // For this grey level image, open a D3D display at position 100,100
273  // in the screen, and with title "D3D display"
274  vpDisplayD3D display;
275 
276  if (opt_display) {
277  // We open a window using D3D.
278  // Its size is automatically defined by the image (I) size
279  display.init(I, 100, 100, "D3D display") ;
280 
281  // Display the image
282  vpDisplay::display(I) ;
283 
284  // Display in overlay a red cross at position 10,10 in the
285  // image. The lines are 10 pixels long
286  ip.set_i( 100 );
287  ip.set_j( 10 );
288 
290 
291  // Display in overlay horizontal red lines
292  for (unsigned i=0 ; i < I.getHeight() ; i+=20) {
293  ip1.set_i( i );
294  ip1.set_j( 0 );
295  ip2.set_i( i );
296  ip2.set_j( I.getWidth() );
297  vpDisplay::displayLine(I, ip1, ip2, vpColor::red) ;
298  }
299 
300  // Display a ligne in the diagonal
301  ip1.set_i( -10 );
302  ip1.set_j( -10 );
303  ip2.set_i( I.getHeight() + 10 );
304  ip2.set_j( I.getWidth() + 10 );
305 
306  vpDisplay::displayLine(I, ip1, ip2, vpColor::red) ;
307 
308  // Display in overlay vertical green dot lines
309  for (unsigned i=0 ; i < I.getWidth() ; i+=20) {
310  ip1.set_i( 0 );
311  ip1.set_j( i );
312  ip2.set_i( I.getWidth() );
313  ip2.set_j( i );
315  }
316 
317  // Display a rectangle
318  ip.set_i( I.getHeight() - 45 );
319  ip.set_j( -10 );
321 
322  // Display in overlay a blue arrow
323  ip1.set_i( 0 );
324  ip1.set_j( 0 );
325  ip2.set_i( 100 );
326  ip2.set_j( 100 );
327  vpDisplay::displayArrow(I, ip1, ip2, vpColor::blue) ;
328 
329  // Display in overlay some circles. The position of the center is 200, 200
330  // the radius is increased by 20 pixels for each circle
331 
332  for (unsigned int i=0 ; i < 100 ; i+=20) {
333  ip.set_i( 80 );
334  ip.set_j( 80 );
336  }
337 
338  ip.set_i( -10 );
339  ip.set_j( 300 );
341 
342  // Display in overlay a yellow string
343  ip.set_i( 85 );
344  ip.set_j( 100 );
346  "ViSP is a marvelous software",
347  vpColor::yellow) ;
348  //Flush the display
349  vpDisplay::flush(I);
350 
351  // Create a color image
352  vpImage<vpRGBa> Ioverlay ;
353  // Updates the color image with the original loaded image and the overlay
354  vpDisplay::getImage(I, Ioverlay) ;
355 
356  // Write the color image on the disk
357  filename = odirname + vpIoTools::path("/Klimt_grey.overlay.ppm");
358  vpImageIo::writePPM(Ioverlay, filename) ;
359 
360  // If click is allowed, wait for a mouse click to close the display
361  if (opt_click_allowed) {
362  std::cout << "\nA click to close the windows..." << std::endl;
363  // Wait for a blocking mouse click
365  }
366 
367  // Close the display
368  vpDisplay::close(I);
369  }
370 
371  // Create a color image
372  vpImage<vpRGBa> Irgba ;
373 
374  // Load a grey image from the disk and convert it to a color image
375  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
376  vpImageIo::readPGM(Irgba, filename) ;
377 
378  // For this color image, open a D3D display at position 100,100
379  // in the screen, and with title "D3D color display"
380  vpDisplayD3D displayRGBa;
381 
382  if (opt_display) {
383  // We open a window using D3D.
384  // Its size is automatically defined by the image (Irgba) size
385  displayRGBa.init(Irgba, 100, 100, "D3D color display");
386 
387  // Display the color image
388  vpDisplay::display(Irgba) ;
389  vpDisplay::flush(Irgba) ;
390 
391  // If click is allowed, wait for a blocking mouse click to display a cross
392  // at the clicked pixel position
393  if (opt_click_allowed) {
394  std::cout << "\nA click to display a cross..." << std::endl;
395  // Blocking wait for a click. Get the position of the selected pixel
396  // (i correspond to the row and j to the column coordinates in the image)
397  vpDisplay::getClick(Irgba, ip);
398  // Display a red cross on the click pixel position
399  std::cout << "Cross position: " << ip << std::endl;
400  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
401  }
402  else {
403  ip.set_i( 10 );
404  ip.set_j( 20 );
405  // Display a red cross at position i, j (i correspond to the row
406  // and j to the column coordinates in the image)
407  std::cout << "Cross position: " << ip << std::endl;
408  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
409 
410  }
411  // Flush the display. Sometimes the display content is
412  // bufferized. Force to display the content that has been bufferized.
413  vpDisplay::flush(Irgba);
414 
415  // If click is allowed, wait for a blocking mouse click to exit.
416  if (opt_click_allowed) {
417  std::cout << "\nA click to exit the program..." << std::endl;
418  vpDisplay::getClick(Irgba) ;
419  std::cout << "Bye" << std::endl;
420  }
421  }
422 }
423 #else
424 int
425 main()
426 {
427  vpERROR_TRACE("Direct 3D library is not available...");
428 }
429 #endif