ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testIoPGM.cpp
1 /****************************************************************************
2  *
3  * $Id: testIoPGM.cpp 4323 2013-07-18 09:24:01Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
62 /*
63 
64  Print the program options.
65 
66  \param name : Program name.
67  \param badparam : Bad parameter name.
68  \param ipath: Input image path.
69  \param opath : Output image path.
70  \param user : Username.
71 
72  */
73 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
74 {
75  fprintf(stdout, "\n\
76 Read and write PGM images on the disk. Also test exceptions.\n\
77 \n\
78 SYNOPSIS\n\
79  %s [-i <input image path>] [-o <output image path>]\n\
80  [-h]\n \
81 ", name);
82 
83  fprintf(stdout, "\n\
84 OPTIONS: Default\n\
85  -i <input image path> %s\n\
86  Set image input path.\n\
87  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
88  image.\n\
89  Setting the VISP_INPUT_IMAGE_PATH environment\n\
90  variable produces the same behaviour than using\n\
91  this option.\n\
92 \n\
93  -o <output image path> %s\n\
94  Set image output path.\n\
95  From this directory, creates the \"%s\"\n\
96  subdirectory depending on the username, where \n\
97  Klimt_grey.pgm output image is 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,
120  std::string &ipath, std::string &opath, std::string user)
121 {
122  const char *optarg;
123  int c;
124  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
125 
126  switch (c) {
127  case 'i': ipath = optarg; break;
128  case 'o': opath = optarg; break;
129  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
130 
131  default:
132  usage(argv[0], optarg, ipath, opath, user); return false; break;
133  }
134  }
135 
136  if ((c == 1) || (c == -1)) {
137  // standalone param or error
138  usage(argv[0], NULL, ipath, opath, user);
139  std::cerr << "ERROR: " << std::endl;
140  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
141  return false;
142  }
143 
144  return true;
145 }
146 
147 int
148 main(int argc, const char ** argv)
149 {
150 
151  std::string env_ipath;
152  std::string opt_ipath;
153  std::string opt_opath;
154  std::string ipath;
155  std::string opath;
156  std::string filename;
157  std::string username;
158 
159  // Get the VISP_IMAGE_PATH environment variable value
160  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
161  if (ptenv != NULL)
162  env_ipath = ptenv;
163 
164  // Set the default input path
165  if (! env_ipath.empty())
166  ipath = env_ipath;
167 
168  // Set the default output path
169 #ifdef WIN32
170  opt_opath = "C:/temp";
171 #else
172  opt_opath = "/tmp";
173 #endif
174 
175  // Get the user login name
176  vpIoTools::getUserName(username);
177 
178  // Read the command line options
179  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
180  exit (-1);
181  }
182 
183  // Get the option values
184  if (!opt_ipath.empty())
185  ipath = opt_ipath;
186  if (!opt_opath.empty())
187  opath = opt_opath;
188 
189  // Append to the output path string, the login name of the user
190  opath += vpIoTools::path("/") + username;
191 
192  // Test if the output path exist. If no try to create it
193  if (vpIoTools::checkDirectory(opath) == false) {
194  try {
195  // Create the dirname
197  }
198  catch (...) {
199  usage(argv[0], NULL, ipath, opt_opath, username);
200  std::cerr << std::endl
201  << "ERROR:" << std::endl;
202  std::cerr << " Cannot create " << opath << std::endl;
203  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
204  exit(-1);
205  }
206  }
207 
208  // Compare ipath and env_ipath. If they differ, we take into account
209  // the input path comming from the command line option
210  if (!opt_ipath.empty() && !env_ipath.empty()) {
211  if (ipath != env_ipath) {
212  std::cout << std::endl
213  << "WARNING: " << std::endl;
214  std::cout << " Since -i <visp image path=" << ipath << "> "
215  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
216  << " we skip the environment variable." << std::endl;
217  }
218  }
219 
220  // Test if an input path is set
221  if (opt_ipath.empty() && env_ipath.empty()){
222  usage(argv[0], NULL, ipath, opt_opath, username);
223  std::cerr << std::endl
224  << "ERROR:" << std::endl;
225  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
226  << std::endl
227  << " environment variable to specify the location of the " << std::endl
228  << " image path where test images are located." << std::endl << std::endl;
229  exit(-1);
230  }
231 
232  //
233  // Here starts really the test
234  //
235 
236  // Create a grey level image
238 
239  // Load a grey image from the disk
240  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
241  std::cout << "Read image: " << filename << std::endl;
242  vpImageIo::read(I, filename);
243  // Write the content of the image on the disk
244  filename = opath + vpIoTools::path("/Klimt_grey.pgm");
245  std::cout << "Write image: " << filename << std::endl;
246  vpImageIo::write(I, filename) ;
247 
248  // Try to load a non existing image (test for exceptions)
249  try
250  {
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(vpImageException e)
257  {
258  vpERROR_TRACE("at main level");
259  std::cout << e << std::endl ;
260  }
261 
262  // Try to write an image to a non existing directory
263  try
264  {
265  filename = opath + vpIoTools::path("/directory-that-does-not-exist/Klimt.pgm");
266  std::cout << "Write image: " << filename << std::endl;
267  vpImageIo::write(I, filename) ;
268  }
269  catch(vpImageException e)
270  {
271  vpERROR_TRACE("at main level");
272  std::cout << e << std::endl ;
273  }
274 }