Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testIoPPM.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 
45 #include <stdlib.h>
46 #include <stdio.h>
47 
55 // List of allowed command line options
56 #define GETOPTARGS "cdi:o:h"
57 
58 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
59 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
60 
61 /*
62 
63  Print the program options.
64 
65  \param name : Program name.
66  \param badparam : Bad parameter name.
67  \param ipath: Input image path.
68  \param opath : Output image path.
69  \param user : Username.
70 
71  */
72 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
73 {
74  fprintf(stdout, "\n\
75 Read and write PPM images on the disk. Also test exceptions.\n\
76 \n\
77 SYNOPSIS\n\
78  %s [-i <input image path>] [-o <output image path>]\n\
79  [-h]\n \
80 ", name);
81 
82  fprintf(stdout, "\n\
83 OPTIONS: Default\n\
84  -i <input image path> %s\n\
85  Set image input path.\n\
86  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
87  and \"ViSP-images/Klimt/Klimt.ppm\" images.\n\
88  Setting the VISP_INPUT_IMAGE_PATH environment\n\
89  variable produces the same behaviour than using\n\
90  this option.\n\
91 \n\
92  -o <output image path> %s\n\
93  Set image output path.\n\
94  From this directory, creates the \"%s\"\n\
95  subdirectory depending on the username, where \n\
96  Klimt_grey.ppm and Klimt_color.ppm output image\n\
97  are written.\n\
98 \n\
99  -h\n\
100  Print the help.\n\n",
101  ipath.c_str(), opath.c_str(), user.c_str());
102 
103  if (badparam)
104  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
105 }
106 
119 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
120 {
121  const char *optarg_;
122  int c;
123  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
124 
125  switch (c) {
126  case 'i': ipath = optarg_; break;
127  case 'o': opath = optarg_; break;
128  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
129 
130  case 'c':
131  case 'd':
132  break;
133 
134  default:
135  usage(argv[0], optarg_, ipath, opath, user); return false; break;
136  }
137  }
138 
139  if ((c == 1) || (c == -1)) {
140  // standalone param or error
141  usage(argv[0], NULL, ipath, opath, user);
142  std::cerr << "ERROR: " << std::endl;
143  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
144  return false;
145  }
146 
147  return true;
148 }
149 
150 int
151 main(int argc, const char ** argv)
152 {
153  try {
154  std::string env_ipath;
155  std::string opt_ipath;
156  std::string opt_opath;
157  std::string ipath;
158  std::string opath;
159  std::string filename;
160  std::string username;
161 
162  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
163  env_ipath = vpIoTools::getViSPImagesDataPath();
164 
165  // Set the default input path
166  if (! env_ipath.empty())
167  ipath = env_ipath;
168 
169  // Set the default output path
170 #if defined(_WIN32)
171  opt_opath = "C:/temp";
172 #else
173  opt_opath = "/tmp";
174 #endif
175 
176  // Get the user login name
177  vpIoTools::getUserName(username);
178 
179  // Read the command line options
180  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
181  exit (-1);
182  }
183 
184  // Get the option values
185  if (!opt_ipath.empty())
186  ipath = opt_ipath;
187  if (!opt_opath.empty())
188  opath = opt_opath;
189 
190  // Append to the output path string, the login name of the user
191  opath = vpIoTools::createFilePath(opath, username);
192 
193  // Test if the output path exist. If no try to create it
194  if (vpIoTools::checkDirectory(opath) == false) {
195  try {
196  // Create the dirname
198  }
199  catch (...) {
200  usage(argv[0], NULL, ipath, opt_opath, username);
201  std::cerr << std::endl
202  << "ERROR:" << std::endl;
203  std::cerr << " Cannot create " << opath << std::endl;
204  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
205  exit(-1);
206  }
207  }
208 
209  // Compare ipath and env_ipath. If they differ, we take into account
210  // the input path comming from the command line option
211  if (!opt_ipath.empty() && !env_ipath.empty()) {
212  if (ipath != env_ipath) {
213  std::cout << std::endl
214  << "WARNING: " << std::endl;
215  std::cout << " Since -i <visp image path=" << ipath << "> "
216  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
217  << " we skip the environment variable." << std::endl;
218  }
219  }
220 
221  // Test if an input path is set
222  if (opt_ipath.empty() && env_ipath.empty()){
223  usage(argv[0], NULL, ipath, opt_opath, username);
224  std::cerr << std::endl
225  << "ERROR:" << std::endl;
226  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
227  << std::endl
228  << " environment variable to specify the location of the " << std::endl
229  << " image path where test images are located." << std::endl << std::endl;
230  exit(-1);
231  }
232 
233  //
234  // Here starts really the test
235  //
236 
238  // Create a grey level image
240 
241  // Load a color image from the disk and convert it to a grey level one
242  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
243  std::cout << "Read image: " << filename << std::endl;
244  vpImageIo::read(I, filename);
245  // Write the content of the image on the disk
246  filename = vpIoTools::createFilePath(opath, "Klimt_grey.ppm");
247  std::cout << "Write image: " << filename << std::endl;
248  vpImageIo::write(I, filename) ;
249 
250  // Try to load a non existing image (test for exceptions)
251  try
252  {
253  // Load a non existing grey image
254  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
255  std::cout << "Read image: " << filename << std::endl;
256  vpImageIo::read(I, filename) ;
257  }
258  catch(vpImageException &e)
259  {
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  }
270  catch(vpException &e) {
271  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
272  }
273 
275  // Create a color image
276  vpImage<vpRGBa> Irgba ;
277 
278  // Load a color image from the disk
279  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
280  std::cout << "Read image: " << filename << std::endl;
281  vpImageIo::read(Irgba, filename);
282  // Write the content of the color image on the disk
283  filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
284  std::cout << "Write image: " << filename << std::endl;
285  vpImageIo::write(Irgba, filename) ;
286 
287  try {
288  // Try to load a non existing image (test for exceptions)
289  // Load a non existing color image
290  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
291  std::cout << "Read image: " << filename << std::endl;
292  vpImageIo::read(Irgba, filename) ;
293  }
294  catch(vpException &e) {
295  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
296  }
297 
298  try {
299  // Try to write a color image to a non existing directory
300  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
301  std::cout << "Write image: " << filename << std::endl;
302  vpImageIo::write(Irgba, filename) ;
303  }
304  catch(vpException &e) {
305  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
306  }
307  return 0;
308  }
309  catch(vpException &e) {
310  std::cout << "Catch an exception: " << e << std::endl;
311  return 1;
312  }
313 }
314 
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:358
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1157
#define vpERROR_TRACE
Definition: vpDebug.h:391
error that can be emited by ViSP classes.
Definition: vpException.h:73
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: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