ViSP  2.10.0
displayD3D.cpp
1 /****************************************************************************
2  *
3  * $Id: displayD3D.cpp 5004 2014-11-24 08:24:18Z 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  * 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  try {
184  std::string env_ipath;
185  std::string opt_ipath;
186  std::string opt_opath;
187  std::string ipath;
188  std::string opath;
189  std::string filename;
190  std::string username;
191  bool opt_click_allowed = true;
192  bool opt_display = true;
193 
194  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
195  env_ipath = vpIoTools::getViSPImagesDataPath();
196 
197  // Set the default input path
198  if (! env_ipath.empty())
199  ipath = env_ipath;
200 
201  // Set the default output path
202  opt_opath = "C:\\temp";
203 
204 
205  // Get the user login name
206  vpIoTools::getUserName(username);
207 
208  // Read the command line options
209  if (getOptions(argc, argv, opt_ipath, opt_opath,
210  opt_click_allowed, username, opt_display) == false) {
211  exit (-1);
212  }
213 
214  // Get the option values
215  if (!opt_ipath.empty())
216  ipath = opt_ipath;
217  if (!opt_opath.empty())
218  opath = opt_opath;
219 
220  // Append to the output path string, the login name of the user
221  std::string odirname = vpIoTools::createFilePath(opath, username);
222 
223  // Test if the output path exist. If no try to create it
224  if (vpIoTools::checkDirectory(odirname) == false) {
225  try {
226  // Create the dirname
227  vpIoTools::makeDirectory(odirname);
228  }
229  catch (...) {
230  usage(argv[0], NULL, ipath, opath, username);
231  std::cerr << std::endl
232  << "ERROR:" << std::endl;
233  std::cerr << " Cannot create " << odirname << std::endl;
234  std::cerr << " Check your -o " << opath << " option " << std::endl;
235  exit(-1);
236  }
237  }
238 
239  // Compare ipath and env_ipath. If they differ, we take into account
240  // the input path comming from the command line option
241  if (!opt_ipath.empty() && !env_ipath.empty()) {
242  if (ipath != env_ipath) {
243  std::cout << std::endl
244  << "WARNING: " << std::endl;
245  std::cout << " Since -i <visp image path=" << ipath << "> "
246  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
247  << " we skip the environment variable." << std::endl;
248  }
249  }
250 
251  // Test if an input path is set
252  if (opt_ipath.empty() && env_ipath.empty()){
253  usage(argv[0], NULL, ipath, opath, username);
254  std::cerr << std::endl
255  << "ERROR:" << std::endl;
256  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
257  << std::endl
258  << " environment variable to specify the location of the " << std::endl
259  << " image path where test images are located." << std::endl << std::endl;
260  exit(-1);
261  }
262 
263  // Create a grey level image
265  vpImagePoint ip, ip1, ip2;
266 
267  // Load a grey image from the disk
268  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
269  vpImageIo::read(I, filename) ;
270 
271  // Create a display using X11
272  vpDisplayD3D display;
273 
274  if (opt_display) {
275  // For this grey level image, open a X11 display at position 100,100
276  // in the screen, and with title "X11 display"
277  display.init(I, 100, 100, "X11 display") ;
278 
279  // Display the image
280  vpDisplay::display(I) ;
281 
282  // Display in overlay a red cross at position 10,10 in the
283  // image. The lines are 10 pixels long
284  ip.set_i( 100 );
285  ip.set_j( 10 );
286 
288 
289  // Display in overlay horizontal red lines
290  for (unsigned i=0 ; i < I.getHeight() ; i+=20) {
291  ip1.set_i( i );
292  ip1.set_j( 0 );
293  ip2.set_i( i );
294  ip2.set_j( I.getWidth() );
295  vpDisplay::displayLine(I, ip1, ip2, vpColor::red) ;
296  }
297 
298  // Display a ligne in the diagonal
299  ip1.set_i( -10 );
300  ip1.set_j( -10 );
301  ip2.set_i( I.getHeight() + 10 );
302  ip2.set_j( I.getWidth() + 10 );
303 
304  vpDisplay::displayLine(I, ip1, ip2, vpColor::red) ;
305 
306  // Display in overlay vertical green dot lines
307  for (unsigned i=0 ; i < I.getWidth() ; i+=20) {
308  ip1.set_i( 0 );
309  ip1.set_j( i );
310  ip2.set_i( I.getWidth() );
311  ip2.set_j( i );
313  }
314 
315  // Display a rectangle
316  ip.set_i( I.getHeight() - 45 );
317  ip.set_j( -10 );
319 
320  // Display in overlay a blue arrow
321  ip1.set_i( 0 );
322  ip1.set_j( 0 );
323  ip2.set_i( 100 );
324  ip2.set_j( 100 );
325  vpDisplay::displayArrow(I, ip1, ip2, vpColor::blue) ;
326 
327  // Display in overlay some circles. The position of the center is 200, 200
328  // the radius is increased by 20 pixels for each circle
329 
330  for (unsigned int i=0 ; i < 100 ; i+=20) {
331  ip.set_i( 80 );
332  ip.set_j( 80 );
334  }
335 
336  ip.set_i( -10 );
337  ip.set_j( 300 );
339 
340  // Display in overlay a yellow string
341  ip.set_i( 85 );
342  ip.set_j( 100 );
344  "ViSP is a marvelous software",
345  vpColor::yellow) ;
346  //Flush the display
347  vpDisplay::flush(I);
348 
349  // Create a color image
350  vpImage<vpRGBa> Ioverlay ;
351  // Updates the color image with the original loaded image and the overlay
352  vpDisplay::getImage(I, Ioverlay) ;
353 
354  // Write the color image on the disk
355  filename = vpIoTools::createFilePath(odirname, "Klimt_grey.overlay.ppm");
356  vpImageIo::write(Ioverlay, filename) ;
357 
358  // If click is allowed, wait for a mouse click to close the display
359  if (opt_click_allowed) {
360  std::cout << "\nA click to close the windows..." << std::endl;
361  // Wait for a blocking mouse click
363  }
364 
365  // Close the display
366  vpDisplay::close(I);
367  }
368 
369  // Create a color image
370  vpImage<vpRGBa> Irgba ;
371 
372  // Load a grey image from the disk and convert it to a color image
373  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
374  vpImageIo::read(Irgba, filename) ;
375 
376  // Create a new display
377  vpDisplayD3D displayRGBa;
378 
379  if (opt_display) {
380  // For this color image, open a X11 display at position 100,100
381  // in the screen, and with title "X11 color display"
382  displayRGBa.init(Irgba, 100, 100, "X11 color display");
383 
384  // Display the color image
385  vpDisplay::display(Irgba) ;
386  vpDisplay::flush(Irgba) ;
387 
388  // If click is allowed, wait for a blocking mouse click to display a cross
389  // at the clicked pixel position
390  if (opt_click_allowed) {
391  std::cout << "\nA click to display a cross..." << std::endl;
392  // Blocking wait for a click. Get the position of the selected pixel
393  // (i correspond to the row and j to the column coordinates in the image)
394  vpDisplay::getClick(Irgba, ip);
395  // Display a red cross on the click pixel position
396  std::cout << "Cross position: " << ip << std::endl;
397  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
398  }
399  else {
400  ip.set_i( 10 );
401  ip.set_j( 20 );
402  // Display a red cross at position i, j (i correspond to the row
403  // and j to the column coordinates in the image)
404  std::cout << "Cross position: " << ip << std::endl;
405  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
406 
407  }
408  // Flush the display. Sometimes the display content is
409  // bufferized. Force to display the content that has been bufferized.
410  vpDisplay::flush(Irgba);
411 
412  // Create a color image
413  vpImage<vpRGBa> Ioverlay ;
414  // Updates the color image with the original loaded image and the overlay
415  vpDisplay::getImage(Irgba, Ioverlay) ;
416 
417  // Write the color image on the disk
418  filename = vpIoTools::createFilePath(odirname, "Klimt_color.overlay.ppm");
419  vpImageIo::write(Ioverlay, filename) ;
420 
421  // If click is allowed, wait for a blocking mouse click to exit.
422  if (opt_click_allowed) {
423  std::cout << "\nA click to exit the program..." << std::endl;
424  vpDisplay::getClick(Irgba) ;
425  std::cout << "Bye" << std::endl;
426  }
427  }
428  return 0;
429  }
430  catch(vpException e) {
431  std::cout << "Catch an exception: " << e << std::endl;
432  return 1;
433  }
434 }
435 #else
436 int
437 main()
438 {
439  vpERROR_TRACE("Direct 3D library is not available...");
440 }
441 #endif
virtual void displayCircle(const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
virtual void displayArrow(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)=0
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:315
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
static void close(vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2269
unsigned int getWidth() const
Definition: vpImage.h:161
#define vpERROR_TRACE
Definition: vpDebug.h:395
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Definition: vpDisplay.cpp:887
error that can be emited by ViSP classes.
Definition: vpException.h:76
static const vpColor green
Definition: vpColor.h:170
static void flush(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:2232
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
static const vpColor red
Definition: vpColor.h:167
static const vpColor orange
Definition: vpColor.h:177
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:384
Display for windows using Direct3D.
Definition: vpDisplayD3D.h:109
void set_i(const double ii)
Definition: vpImagePoint.h:159
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static void display(const vpImage< unsigned char > &I)
Definition: vpDisplay.cpp:210
virtual void displayCross(const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)=0
static std::string getUserName()
Definition: vpIoTools.cpp:141
virtual void displayRectangle(const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)=0
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const char *title=NULL)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:328
virtual void displayDotLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
void set_j(const double jj)
Definition: vpImagePoint.h:170
unsigned int getHeight() const
Definition: vpImage.h:152
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:93
virtual void displayLine(const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)=0
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278
static const vpColor yellow
Definition: vpColor.h:175
static const vpColor blue
Definition: vpColor.h:173