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