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