ViSP  2.10.0
testIoPPM.cpp
1 /****************************************************************************
2  *
3  * $Id: testIoPPM.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 
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-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
163  env_ipath = vpIoTools::getViSPImagesDataPath();
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::createFilePath(opath, 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 
238  // Create a grey level image
240 
241  // Load a color image from the disk and convert it to a grey level one
242  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
243  std::cout << "Read image: " << filename << std::endl;
244  vpImageIo::read(I, filename);
245  // Write the content of the image on the disk
246  filename = vpIoTools::createFilePath(opath, "Klimt_grey.ppm");
247  std::cout << "Write image: " << filename << std::endl;
248  vpImageIo::write(I, filename) ;
249 
250  // Try to load a non existing image (test for exceptions)
251  try
252  {
253  // Load a non existing grey image
254  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
255  std::cout << "Read image: " << filename << std::endl;
256  vpImageIo::read(I, filename) ;
257  }
258  catch(vpImageException e)
259  {
260  vpERROR_TRACE("at main level");
261  std::cout << e << std::endl ;
262  }
263 
264  // Try to write an image to a non existing directory
265  try {
266  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
267  std::cout << "Write image: " << filename << std::endl;
268  vpImageIo::write(I, filename) ;
269  }
270  catch(vpException e) {
271  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
272  }
273 
275  // Create a color image
276  vpImage<vpRGBa> Irgba ;
277 
278  // Load a color image from the disk
279  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
280  std::cout << "Read image: " << filename << std::endl;
281  vpImageIo::read(Irgba, filename);
282  // Write the content of the color image on the disk
283  filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
284  std::cout << "Write image: " << filename << std::endl;
285  vpImageIo::write(Irgba, filename) ;
286 
287  try {
288  // Try to load a non existing image (test for exceptions)
289  // Load a non existing color image
290  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
291  std::cout << "Read image: " << filename << std::endl;
292  vpImageIo::read(Irgba, filename) ;
293  }
294  catch(vpException e) {
295  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
296  }
297 
298  try {
299  // Try to write a color image to a non existing directory
300  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
301  std::cout << "Write image: " << filename << std::endl;
302  vpImageIo::write(Irgba, filename) ;
303  }
304  catch(vpException e) {
305  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
306  }
307  return 0;
308  }
309  catch(vpException e) {
310  std::cout << "Catch an exception: " << e << std::endl;
311  return 1;
312  }
313 }
314 
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
#define vpERROR_TRACE
Definition: vpDebug.h:395
error that can be emited by ViSP classes.
Definition: vpException.h:76
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: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