ViSP  2.9.0
testIoPGM.cpp
1 /****************************************************************************
2  *
3  * $Id: testIoPGM.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 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  * Read and write PGM images on the disk.
36  *
37  * Authors:
38  * Eric Marchand
39  * Fabien Spindler
40  *
41  *****************************************************************************/
42 
43 #include <visp/vpImage.h>
44 #include <visp/vpImageIo.h>
45 #include <visp/vpParseArgv.h>
46 #include <visp/vpIoTools.h>
47 #include <visp/vpDebug.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 
58 // List of allowed command line options
59 #define GETOPTARGS "i:o:h"
60 
61 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
62 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
63 
64 /*
65 
66  Print the program options.
67 
68  \param name : Program name.
69  \param badparam : Bad parameter name.
70  \param ipath: Input image path.
71  \param opath : Output image path.
72  \param user : Username.
73 
74  */
75 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
76 {
77  fprintf(stdout, "\n\
78 Read and write PGM images on the disk. Also test exceptions.\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-i <input image path>] [-o <output image path>]\n\
82  [-h]\n \
83 ", name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87  -i <input image path> %s\n\
88  Set image input path.\n\
89  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
90  image.\n\
91  Setting the VISP_INPUT_IMAGE_PATH environment\n\
92  variable produces the same behaviour than using\n\
93  this option.\n\
94 \n\
95  -o <output image path> %s\n\
96  Set image output path.\n\
97  From this directory, creates the \"%s\"\n\
98  subdirectory depending on the username, where \n\
99  Klimt_grey.pgm output image is written.\n\
100 \n\
101  -h\n\
102  Print the help.\n\n",
103  ipath.c_str(), opath.c_str(), user.c_str());
104 
105  if (badparam)
106  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
107 }
108 
121 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
122 {
123  const char *optarg_;
124  int c;
125  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
126 
127  switch (c) {
128  case 'i': ipath = optarg_; break;
129  case 'o': opath = optarg_; break;
130  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; 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_IMAGE_PATH environment variable value
161  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
162  if (ptenv != NULL)
163  env_ipath = ptenv;
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::path("/") + 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 
237  // Create a grey level image
239 
240  // Load a grey image from the disk
241  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
242  std::cout << "Read image: " << filename << std::endl;
243  vpImageIo::read(I, filename);
244  // Write the content of the image on the disk
245  filename = opath + vpIoTools::path("/Klimt_grey.pgm");
246  std::cout << "Write image: " << filename << std::endl;
247  vpImageIo::write(I, filename) ;
248 
249  try {
250  // Try to load a non existing image (test for exceptions)
251  // Load a non existing grey image
252  filename = ipath + vpIoTools::path("/ViSP-images/image-that-does-not-exist.pgm");
253  std::cout << "Read image: " << filename << std::endl;
254  vpImageIo::read(I, filename) ;
255  }
256  catch(vpException e) {
257  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
258  }
259 
260  try {
261  // Try to write an image to a non existing directory
262  filename = opath + vpIoTools::path("/directory-that-does-not-exist/Klimt.pgm");
263  std::cout << "Write image: " << filename << std::endl;
264  vpImageIo::write(I, filename) ;
265  }
266  catch(vpException e) {
267  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
268  }
269  return 0;
270  }
271  catch(vpException e) {
272  std::cout << "Catch an exception: " << e << std::endl;
273  return 1;
274  }
275 }
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:452
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
static std::string getUserName()
Definition: vpIoTools.cpp:140
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278