Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testIoPPM.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 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 and write PGM images on the disk.
33  *
34  * Authors:
35  * Eric Marchand
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
40 #include <visp3/core/vpDebug.h>
41 #include <visp3/core/vpImage.h>
42 #include <visp3/core/vpIoTools.h>
43 #include <visp3/io/vpImageIo.h>
44 #include <visp3/io/vpParseArgv.h>
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 
56 // List of allowed command line options
57 #define GETOPTARGS "cdi:o:h"
58 
59 /*
60 
61  Print the program options.
62 
63  \param name : Program name.
64  \param badparam : Bad parameter name.
65  \param ipath: Input image path.
66  \param opath : Output image path.
67  \param user : Username.
68 
69  */
70 void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
71  const std::string &user)
72 {
73  fprintf(stdout, "\n\
74 Read and write PPM 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 \"Klimt/Klimt.pgm\"\n\
86  and \"Klimt/Klimt.ppm\" images.\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.ppm and Klimt_color.ppm output image\n\
96  are written.\n\
97 \n\
98  -h\n\
99  Print the help.\n\n", 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, const 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':
125  ipath = optarg_;
126  break;
127  case 'o':
128  opath = optarg_;
129  break;
130  case 'h':
131  usage(argv[0], NULL, ipath, opath, user);
132  return false;
133  break;
134 
135  case 'c':
136  case 'd':
137  break;
138 
139  default:
140  usage(argv[0], optarg_, ipath, opath, user);
141  return false;
142  break;
143  }
144  }
145 
146  if ((c == 1) || (c == -1)) {
147  // standalone param or error
148  usage(argv[0], NULL, ipath, opath, user);
149  std::cerr << "ERROR: " << std::endl;
150  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
151  return false;
152  }
153 
154  return true;
155 }
156 
157 int main(int argc, const char **argv)
158 {
159  try {
160  std::string env_ipath;
161  std::string opt_ipath;
162  std::string opt_opath;
163  std::string ipath;
164  std::string opath;
165  std::string filename;
166  std::string username;
167 
168  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
169  // environment variable value
170  env_ipath = vpIoTools::getViSPImagesDataPath();
171 
172  // Set the default input path
173  if (!env_ipath.empty())
174  ipath = env_ipath;
175 
176 // Set the default output path
177 #if defined(_WIN32)
178  opt_opath = "C:/temp";
179 #else
180  opt_opath = "/tmp";
181 #endif
182 
183  // Get the user login name
184  vpIoTools::getUserName(username);
185 
186  // Read the command line options
187  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
188  exit(-1);
189  }
190 
191  // Get the option values
192  if (!opt_ipath.empty())
193  ipath = opt_ipath;
194  if (!opt_opath.empty())
195  opath = opt_opath;
196 
197  // Append to the output path string, the login name of the user
198  opath = vpIoTools::createFilePath(opath, username);
199 
200  // Test if the output path exist. If no try to create it
201  if (vpIoTools::checkDirectory(opath) == false) {
202  try {
203  // Create the dirname
205  } catch (...) {
206  usage(argv[0], NULL, ipath, opt_opath, username);
207  std::cerr << std::endl << "ERROR:" << std::endl;
208  std::cerr << " Cannot create " << opath << std::endl;
209  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
210  exit(-1);
211  }
212  }
213 
214  // Compare ipath and env_ipath. If they differ, we take into account
215  // the input path comming from the command line option
216  if (!opt_ipath.empty() && !env_ipath.empty()) {
217  if (ipath != env_ipath) {
218  std::cout << std::endl << "WARNING: " << std::endl;
219  std::cout << " Since -i <visp image path=" << ipath << "> "
220  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
221  << " we skip the environment variable." << std::endl;
222  }
223  }
224 
225  // Test if an input path is set
226  if (opt_ipath.empty() && env_ipath.empty()) {
227  usage(argv[0], NULL, ipath, opt_opath, username);
228  std::cerr << std::endl << "ERROR:" << std::endl;
229  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
230  << " environment variable to specify the location of the " << std::endl
231  << " image path where test images are located." << std::endl
232  << std::endl;
233  exit(-1);
234  }
235 
236  //
237  // Here starts really the test
238  //
239 
241  // Create a grey level image
243 
244  // Load a color image from the disk and convert it to a grey level one
245  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
246  std::cout << "Read image: " << filename << std::endl;
247  vpImageIo::read(I, filename);
248  // Write the content of the image on the disk
249  filename = vpIoTools::createFilePath(opath, "Klimt_grey.ppm");
250  std::cout << "Write image: " << filename << std::endl;
251  vpImageIo::write(I, filename);
252 
253  // Try to load a non existing image (test for exceptions)
254  try {
255  // Load a non existing grey image
256  filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
257  std::cout << "Read image: " << filename << std::endl;
258  vpImageIo::read(I, filename);
259  } catch (vpImageException &e) {
260  vpERROR_TRACE("at main level");
261  std::cout << e << std::endl;
262  }
263 
264  // Try to write an image to a non existing directory
265  try {
266  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
267  std::cout << "Write image: " << filename << std::endl;
268  vpImageIo::write(I, filename);
269  } catch (const vpException &e) {
270  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
271  }
272 
274  // Create a color image
275  vpImage<vpRGBa> Irgba;
276 
277  // Load a color image from the disk
278  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
279  std::cout << "Read image: " << filename << std::endl;
280  vpImageIo::read(Irgba, filename);
281  // Write the content of the color image on the disk
282  filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
283  std::cout << "Write image: " << filename << std::endl;
284  vpImageIo::write(Irgba, filename);
285 
286  try {
287  // Try to load a non existing image (test for exceptions)
288  // Load a non existing color image
289  filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
290  std::cout << "Read image: " << filename << std::endl;
291  vpImageIo::read(Irgba, filename);
292  } catch (const vpException &e) {
293  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
294  }
295 
296  try {
297  // Try to write a color image to a non existing directory
298  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
299  std::cout << "Write image: " << filename << std::endl;
300  vpImageIo::write(Irgba, filename);
301  } catch (const vpException &e) {
302  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
303  }
304  return 0;
305  } catch (const vpException &e) {
306  std::cout << "Catch an exception: " << e << std::endl;
307  return 1;
308  }
309 }
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:467
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1316
#define vpERROR_TRACE
Definition: vpDebug.h:393
error that can be emited by ViSP classes.
Definition: vpException.h:71
Error that can be emited by the vpImage class and its derivates.
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69
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:597
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1541
static std::string getUserName()
Definition: vpIoTools.cpp:298
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207