Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testIoPGM.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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Read and write PGM images on the disk.
32  *
33  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpImage.h>
40 #include <visp3/io/vpImageIo.h>
41 #include <visp3/io/vpParseArgv.h>
42 #include <visp3/core/vpIoTools.h>
43 #include <visp3/core/vpDebug.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 
54 // List of allowed command line options
55 #define GETOPTARGS "cdi:o:h"
56 
57 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
58 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
59 
60 /*
61 
62  Print the program options.
63 
64  \param name : Program name.
65  \param badparam : Bad parameter name.
66  \param ipath: Input image path.
67  \param opath : Output image path.
68  \param user : Username.
69 
70  */
71 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
72 {
73  fprintf(stdout, "\n\
74 Read and write PGM images on the disk. Also test exceptions.\n\
75 \n\
76 SYNOPSIS\n\
77  %s [-i <input image path>] [-o <output image path>]\n\
78  [-h]\n \
79 ", name);
80 
81  fprintf(stdout, "\n\
82 OPTIONS: Default\n\
83  -i <input image path> %s\n\
84  Set image input path.\n\
85  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
86  image.\n\
87  Setting the VISP_INPUT_IMAGE_PATH environment\n\
88  variable produces the same behaviour than using\n\
89  this option.\n\
90 \n\
91  -o <output image path> %s\n\
92  Set image output path.\n\
93  From this directory, creates the \"%s\"\n\
94  subdirectory depending on the username, where \n\
95  Klimt_grey.pgm output image is written.\n\
96 \n\
97  -h\n\
98  Print the help.\n\n",
99  ipath.c_str(), opath.c_str(), user.c_str());
100 
101  if (badparam)
102  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
103 }
104 
117 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'i': ipath = optarg_; break;
125  case 'o': opath = optarg_; break;
126  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
127 
128  case 'c':
129  case 'd':
130  break;
131 
132  default:
133  usage(argv[0], optarg_, ipath, opath, user); return false; break;
134  }
135  }
136 
137  if ((c == 1) || (c == -1)) {
138  // standalone param or error
139  usage(argv[0], NULL, ipath, opath, user);
140  std::cerr << "ERROR: " << std::endl;
141  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
142  return false;
143  }
144 
145  return true;
146 }
147 
148 int
149 main(int argc, const char ** argv)
150 {
151  try {
152  std::string env_ipath;
153  std::string opt_ipath;
154  std::string opt_opath;
155  std::string ipath;
156  std::string opath;
157  std::string filename;
158  std::string username;
159 
160  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
161  env_ipath = vpIoTools::getViSPImagesDataPath();
162 
163  // Set the default input path
164  if (! env_ipath.empty())
165  ipath = env_ipath;
166 
167  // Set the default output path
168 #if defined(_WIN32)
169  opt_opath = "C:/temp";
170 #else
171  opt_opath = "/tmp";
172 #endif
173 
174  // Get the user login name
175  vpIoTools::getUserName(username);
176 
177  // Read the command line options
178  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
179  exit (-1);
180  }
181 
182  // Get the option values
183  if (!opt_ipath.empty())
184  ipath = opt_ipath;
185  if (!opt_opath.empty())
186  opath = opt_opath;
187 
188  // Append to the output path string, the login name of the user
189  opath = vpIoTools::createFilePath(opath, username);
190 
191  // Test if the output path exist. If no try to create it
192  if (vpIoTools::checkDirectory(opath) == false) {
193  try {
194  // Create the dirname
196  }
197  catch (...) {
198  usage(argv[0], NULL, ipath, opt_opath, username);
199  std::cerr << std::endl
200  << "ERROR:" << std::endl;
201  std::cerr << " Cannot create " << opath << std::endl;
202  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
203  exit(-1);
204  }
205  }
206 
207  // Compare ipath and env_ipath. If they differ, we take into account
208  // the input path comming from the command line option
209  if (!opt_ipath.empty() && !env_ipath.empty()) {
210  if (ipath != env_ipath) {
211  std::cout << std::endl
212  << "WARNING: " << std::endl;
213  std::cout << " Since -i <visp image path=" << ipath << "> "
214  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
215  << " we skip the environment variable." << std::endl;
216  }
217  }
218 
219  // Test if an input path is set
220  if (opt_ipath.empty() && env_ipath.empty()){
221  usage(argv[0], NULL, ipath, opt_opath, username);
222  std::cerr << std::endl
223  << "ERROR:" << std::endl;
224  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
225  << std::endl
226  << " environment variable to specify the location of the " << std::endl
227  << " image path where test images are located." << std::endl << std::endl;
228  exit(-1);
229  }
230 
231  //
232  // Here starts really the test
233  //
234 
235  // Create a grey level image
237 
238  // Load a grey image from the disk
239  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
240  std::cout << "Read image: " << filename << std::endl;
241  vpImageIo::read(I, filename);
242  // Write the content of the image on the disk
243  filename = vpIoTools::createFilePath(opath, "Klimt_grey.pgm");
244  std::cout << "Write image: " << filename << std::endl;
245  vpImageIo::write(I, filename) ;
246 
247  try {
248  // Try to load a non existing image (test for exceptions)
249  // Load a non existing grey image
250  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.pgm");
251  std::cout << "Read image: " << filename << std::endl;
252  vpImageIo::read(I, filename) ;
253  }
254  catch(vpException &e) {
255  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
256  }
257 
258  try {
259  // Try to write an image to a non existing directory
260  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.pgm");
261  std::cout << "Write image: " << filename << std::endl;
262  vpImageIo::write(I, filename) ;
263  }
264  catch(vpException &e) {
265  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
266  }
267  return 0;
268  }
269  catch(vpException &e) {
270  std::cout << "Catch an exception: " << e << std::endl;
271  return 1;
272  }
273 }
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static void write(const vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:368
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:427
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1366
static std::string getUserName()
Definition: vpIoTools.cpp:177
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:205