ViSP  2.10.0
testIoPGM.cpp
1 /****************************************************************************
2  *
3  * $Id: testIoPGM.cpp 4814 2014-07-31 11:38:39Z 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-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 void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:476
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:315
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1071
error that can be emited by ViSP classes.
Definition: vpException.h:76
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:384
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static std::string getUserName()
Definition: vpIoTools.cpp:141
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278