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