Visual Servoing Platform  version 3.1.0
displayX.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Read an image on the disk and display it using X11.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
48 #include <stdlib.h>
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpDebug.h>
51 #ifdef VISP_HAVE_X11
52 
53 #include <visp3/core/vpImage.h>
54 #include <visp3/core/vpIoTools.h>
55 #include <visp3/gui/vpDisplayX.h>
56 #include <visp3/io/vpImageIo.h>
57 #include <visp3/io/vpParseArgv.h>
58 
59 #include <visp3/core/vpTime.h>
60 
70 // List of allowed command line options
71 #define GETOPTARGS "cdi:o:p:h"
72 
84 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
85 {
86  fprintf(stdout, "\n\
87 Read an image on the disk, display it using X11, display some\n\
88 features (line, circle, caracters) in overlay and finaly write \n\
89 the image and the overlayed features in an image on the disk.\n\
90 \n\
91 SYNOPSIS\n\
92  %s [-i <input image path>] [-o <output image path>]\n\
93  [-c] [-d] [-h]\n \
94 ", name);
95 
96  fprintf(stdout, "\n\
97 OPTIONS: Default\n\
98  -i <input image path> %s\n\
99  Set image input path.\n\
100  From this path read \"Klimt/Klimt.pgm\"\n\
101  image.\n\
102  Setting the VISP_INPUT_IMAGE_PATH environment\n\
103  variable produces the same behaviour than using\n\
104  this option.\n\
105 \n\
106  -o <output image path> %s\n\
107  Set image output path.\n\
108  From this directory, creates the \"%s\"\n\
109  subdirectory depending on the username, where \n\
110  Klimt_grey.overlay.ppm output image is written.\n\
111 \n\
112  -c\n\
113  Disable the mouse click. Useful to automate the \n\
114  execution of this program without humain intervention.\n\
115 \n\
116  -d \n\
117  Disable the image display. This can be useful \n\
118  for automatic tests using crontab under Unix or \n\
119  using the task manager under Windows.\n\
120 \n\
121  -h\n\
122  Print the help.\n\n", ipath.c_str(), opath.c_str(), user.c_str());
123 
124  if (badparam) {
125  fprintf(stderr, "ERROR: \n");
126  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
127  }
128 }
129 
148 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, bool &click_allowed,
149  const std::string &user, bool &display)
150 {
151  const char *optarg_;
152  int c;
153  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
154 
155  switch (c) {
156  case 'c':
157  click_allowed = false;
158  break;
159  case 'd':
160  display = false;
161  break;
162  case 'i':
163  ipath = optarg_;
164  break;
165  case 'o':
166  opath = optarg_;
167  break;
168  case 'h':
169  usage(argv[0], NULL, ipath, opath, user);
170  return false;
171  break;
172 
173  default:
174  usage(argv[0], optarg_, ipath, opath, user);
175  return false;
176  break;
177  }
178  }
179 
180  if ((c == 1) || (c == -1)) {
181  // standalone param or error
182  usage(argv[0], NULL, ipath, opath, user);
183  std::cerr << "ERROR: " << std::endl;
184  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
185  return false;
186  }
187 
188  return true;
189 }
190 
191 int main(int argc, const char **argv)
192 {
193  try {
194  std::string env_ipath;
195  std::string opt_ipath;
196  std::string opt_opath;
197  std::string ipath;
198  std::string opath;
199  std::string filename;
200  std::string username;
201  bool opt_click_allowed = true;
202  bool opt_display = true;
203 
204  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
205  // environment variable value
206  env_ipath = vpIoTools::getViSPImagesDataPath();
207 
208  // Set the default input path
209  if (!env_ipath.empty())
210  ipath = env_ipath;
211 
212 // Set the default output path
213 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
214  opt_opath = "/tmp";
215 #elif defined(_WIN32)
216  opt_opath = "C:\\temp";
217 #endif
218 
219  // Get the user login name
220  vpIoTools::getUserName(username);
221 
222  // Read the command line options
223  if (getOptions(argc, argv, opt_ipath, opt_opath, opt_click_allowed, username, opt_display) == false) {
224  exit(-1);
225  }
226 
227  // Get the option values
228  if (!opt_ipath.empty())
229  ipath = opt_ipath;
230  if (!opt_opath.empty())
231  opath = opt_opath;
232 
233  // Append to the output path string, the login name of the user
234  std::string odirname = vpIoTools::createFilePath(opath, username);
235 
236  // Test if the output path exist. If no try to create it
237  if (vpIoTools::checkDirectory(odirname) == false) {
238  try {
239  // Create the dirname
240  vpIoTools::makeDirectory(odirname);
241  } catch (...) {
242  usage(argv[0], NULL, ipath, opath, username);
243  std::cerr << std::endl << "ERROR:" << std::endl;
244  std::cerr << " Cannot create " << odirname << std::endl;
245  std::cerr << " Check your -o " << opath << " option " << std::endl;
246  exit(-1);
247  }
248  }
249 
250  // Compare ipath and env_ipath. If they differ, we take into account
251  // the input path comming from the command line option
252  if (!opt_ipath.empty() && !env_ipath.empty()) {
253  if (ipath != env_ipath) {
254  std::cout << std::endl << "WARNING: " << std::endl;
255  std::cout << " Since -i <visp image path=" << ipath << "> "
256  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
257  << " we skip the environment variable." << std::endl;
258  }
259  }
260 
261  // Test if an input path is set
262  if (opt_ipath.empty() && env_ipath.empty()) {
263  usage(argv[0], NULL, ipath, opath, username);
264  std::cerr << std::endl << "ERROR:" << std::endl;
265  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
266  << " environment variable to specify the location of the " << std::endl
267  << " image path where test images are located." << std::endl
268  << std::endl;
269  exit(-1);
270  }
271 
272  // Create a grey level image
274  vpImagePoint ip, ip1, ip2;
275 
276  // Load a grey image from the disk
277  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
278  vpImageIo::read(I, filename);
279 
280  // Create a display using X11
281  vpDisplayX display;
282 
283  if (opt_display) {
284  // For this grey level image, open a X11 display at position 100,100
285  // in the screen, and with title "X11 display"
286  display.init(I, 100, 100, "X11 display");
287 
288  // Display the image
290 
291  // Display in overlay a red cross at position 10,10 in the
292  // image. The lines are 10 pixels long
293  ip.set_i(100);
294  ip.set_j(10);
295 
297 
298  // Display in overlay horizontal red lines
299  for (unsigned i = 0; i < I.getHeight(); i += 20) {
300  ip1.set_i(i);
301  ip1.set_j(0);
302  ip2.set_i(i);
303  ip2.set_j(I.getWidth());
304  vpDisplay::displayLine(I, ip1, ip2, vpColor::red);
305  }
306 
307  // Display a ligne in the diagonal
308  ip1.set_i(-10);
309  ip1.set_j(-10);
310  ip2.set_i(I.getHeight() + 10);
311  ip2.set_j(I.getWidth() + 10);
312 
313  vpDisplay::displayLine(I, ip1, ip2, vpColor::red);
314 
315  // Display in overlay vertical green dot lines
316  for (unsigned i = 0; i < I.getWidth(); i += 20) {
317  ip1.set_i(0);
318  ip1.set_j(i);
319  ip2.set_i(I.getWidth());
320  ip2.set_j(i);
322  }
323 
324  // Display a rectangle
325  ip.set_i(I.getHeight() - 45);
326  ip.set_j(-10);
328 
329  // Display in overlay a blue arrow
330  ip1.set_i(0);
331  ip1.set_j(0);
332  ip2.set_i(100);
333  ip2.set_j(100);
335 
336  // Display in overlay some circles. The position of the center is 200,
337  // 200 the radius is increased by 20 pixels for each circle
338 
339  for (unsigned int i = 0; i < 100; i += 20) {
340  ip.set_i(80);
341  ip.set_j(80);
343  }
344 
345  ip.set_i(-10);
346  ip.set_j(300);
348 
349  // Display in overlay a yellow string
350  ip.set_i(85);
351  ip.set_j(100);
352  vpDisplay::displayText(I, ip, "ViSP is a marvelous software", vpColor::yellow);
353  // Flush the display
354  vpDisplay::flush(I);
355 
356  // Create a color image
357  vpImage<vpRGBa> Ioverlay;
358  // Updates the color image with the original loaded image and the
359  // overlay
360  vpDisplay::getImage(I, Ioverlay);
361 
362  // Write the color image on the disk
363  filename = vpIoTools::createFilePath(odirname, "Klimt_grey.overlay.ppm");
364  vpImageIo::write(Ioverlay, filename);
365 
366  // If click is allowed, wait for a mouse click to close the display
367  if (opt_click_allowed) {
368  std::cout << "\nA click to close the windows..." << std::endl;
369  // Wait for a blocking mouse click
371  }
372 
373  // Close the display
374  vpDisplay::close(I);
375  }
376 
377  // Create a color image
378  vpImage<vpRGBa> Irgba;
379 
380  // Load a grey image from the disk and convert it to a color image
381  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
382  vpImageIo::read(Irgba, filename);
383 
384  // Create a new display
385  vpDisplayX displayRGBa;
386 
387  if (opt_display) {
388  // For this color image, open a X11 display at position 100,100
389  // in the screen, and with title "X11 color display"
390  displayRGBa.init(Irgba, 100, 100, "X11 color display");
391 
392  // Display the color image
393  vpDisplay::display(Irgba);
394  vpDisplay::flush(Irgba);
395 
396  // If click is allowed, wait for a blocking mouse click to display a
397  // cross at the clicked pixel position
398  if (opt_click_allowed) {
399  std::cout << "\nA click to display a cross..." << std::endl;
400  // Blocking wait for a click. Get the position of the selected pixel
401  // (i correspond to the row and j to the column coordinates in the
402  // image)
403  vpDisplay::getClick(Irgba, ip);
404  // Display a red cross on the click pixel position
405  std::cout << "Cross position: " << ip << std::endl;
406  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
407  } else {
408  ip.set_i(10);
409  ip.set_j(20);
410  // Display a red cross at position i, j (i correspond to the row
411  // and j to the column coordinates in the image)
412  std::cout << "Cross position: " << ip << std::endl;
413  vpDisplay::displayCross(Irgba, ip, 15, vpColor::red);
414  }
415  // Flush the display. Sometimes the display content is
416  // bufferized. Force to display the content that has been bufferized.
417  vpDisplay::flush(Irgba);
418 
419  // Create a color image
420  vpImage<vpRGBa> Ioverlay;
421  // Updates the color image with the original loaded image and the
422  // overlay
423  vpDisplay::getImage(Irgba, Ioverlay);
424 
425  // Write the color image on the disk
426  filename = vpIoTools::createFilePath(odirname, "Klimt_color.overlay.ppm");
427  vpImageIo::write(Ioverlay, filename);
428 
429  // If click is allowed, wait for a blocking mouse click to exit.
430  if (opt_click_allowed) {
431  std::cout << "\nA click to exit the program..." << std::endl;
432  vpDisplay::getClick(Irgba);
433  std::cout << "Bye" << std::endl;
434  }
435  }
436  return 0;
437  } catch (vpException &e) {
438  std::cout << "Catch an exception: " << e << std::endl;
439  return 1;
440  }
441 }
442 #else
443 int main() { vpERROR_TRACE("You do not have X11 functionalities to display images..."); }
444 
445 #endif
void init(vpImage< unsigned char > &I, int winx=-1, int winy=-1, const std::string &title="")
Definition: vpDisplayX.cpp:258
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:367
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1210
static void close(vpImage< unsigned char > &I)
#define vpERROR_TRACE
Definition: vpDebug.h:393
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
Definition: vpDisplayX.h:151
error that can be emited by ViSP classes.
Definition: vpException.h:71
static const vpColor green
Definition: vpColor.h:183
static void flush(const vpImage< unsigned char > &I)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
static const vpColor red
Definition: vpColor.h:180
static const vpColor orange
Definition: vpColor.h:190
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:375
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:495
void set_i(const double ii)
Definition: vpImagePoint.h:167
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1435
static void displayArrow(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color=vpColor::white, unsigned int w=4, unsigned int h=2, unsigned int thickness=1)
static void display(const vpImage< unsigned char > &I)
static std::string getUserName()
Definition: vpIoTools.cpp:198
static void displayRectangle(const vpImage< unsigned char > &I, const vpImagePoint &topLeft, unsigned int width, unsigned int height, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void getImage(const vpImage< unsigned char > &Is, vpImage< vpRGBa > &Id)
Definition: vpDisplay.cpp:144
void set_j(const double jj)
Definition: vpImagePoint.h:178
static void displayCircle(const vpImage< unsigned char > &I, const vpImagePoint &center, unsigned int radius, const vpColor &color, bool fill=false, unsigned int thickness=1)
static void displayCross(const vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, const vpColor &color, unsigned int thickness=1)
unsigned int getHeight() const
Definition: vpImage.h:178
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:88
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static const vpColor yellow
Definition: vpColor.h:188
unsigned int getWidth() const
Definition: vpImage.h:229
static void displayDotLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1)
static const vpColor blue
Definition: vpColor.h:186