ViSP  2.9.0
imageDiskRW.cpp
1 /****************************************************************************
2  *
3  * $Id: imageDiskRW.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  * Reading and writting images on the disk.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
62 #include <visp/vpImage.h>
63 #include <visp/vpImageIo.h>
64 #include <visp/vpParseArgv.h>
65 #include <visp/vpIoTools.h>
66 #include <visp/vpDebug.h>
67 #include <stdlib.h>
68 #include <stdio.h>
69 // List of allowed command line options
70 #define GETOPTARGS "i:o:h"
71 
72 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
73 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
74 
86 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
87 {
88  fprintf(stdout, "\n\
89 Read and write PGM images on the disk. Also test exceptions.\n\
90 \n\
91 SYNOPSIS\n\
92  %s [-i <input image path>] [-o <output image path>]\n\
93  [-h]\n \
94 ", name);
95 
96  fprintf(stdout, "\n\
97 OPTIONS: Default\n\
98  -i <input image path> %s\n\
99  Set image input path.\n\
100  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
101  image.\n\
102  Setting the VISP_INPUT_IMAGE_PATH environment\n\
103  variable produces the same behaviour than using\n\
104  this option.\n\
105 \n\
106  -o <output image path> %s\n\
107  Set image output path.\n\
108  From this directory, creates the \"%s\"\n\
109  subdirectory depending on the username, where \n\
110  Klimt_grey.pgm output image is written.\n\
111 \n\
112  -h\n\
113  Print the help.\n\n",
114  ipath.c_str(), opath.c_str(), user.c_str());
115 
116  if (badparam) {
117  fprintf(stderr, "ERROR: \n" );
118  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
119  }
120 }
133 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
134 {
135  const char *optarg_;
136  int c;
137  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
138 
139  switch (c) {
140  case 'i': ipath = optarg_; break;
141  case 'o': opath = optarg_; break;
142  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
143 
144  default:
145  usage(argv[0], optarg_, ipath, opath, user); return false; break;
146  }
147  }
148 
149  if ((c == 1) || (c == -1)) {
150  // standalone param or error
151  usage(argv[0], NULL, ipath, opath, user);
152  std::cerr << "ERROR: " << std::endl;
153  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
154  return false;
155  }
156 
157  return true;
158 }
159 
160 
161 
162 int
163 main(int argc, const char ** argv)
164 {
165  try {
166  std::string env_ipath;
167  std::string opt_ipath;
168  std::string opt_opath;
169  std::string ipath;
170  std::string opath;
171  std::string filename;
172  std::string username;
173 
174  std::cout << "-------------------------------------------------------" << std::endl ;
175  std::cout << " imageDiskRW.cpp" <<std::endl << std::endl ;
176 
177  std::cout << " reading and writting of PPM image" << std::endl ;
178  std::cout << " read an image that does not exist" << std::endl ;
179  std::cout << " write in a directory that does no exist" << std::endl ;
180  std::cout << "-------------------------------------------------------" << std::endl ;
181  std::cout << std::endl ;
182 
183 
184  // Get the VISP_IMAGE_PATH environment variable value
185  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
186  if (ptenv != NULL)
187  env_ipath = ptenv;
188 
189  // Set the default input path
190  if (! env_ipath.empty())
191  ipath = env_ipath;
192 
193  // Set the default output path
194 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
195  opt_opath = "/tmp";
196 #elif defined(_WIN32)
197  opt_opath = "C:\\temp";
198 #endif
199 
200  // Get the user login name
201  vpIoTools::getUserName(username);
202 
203  // Read the command line options
204  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
205  exit (-1);
206  }
207 
208  // Get the option values
209  if (!opt_ipath.empty())
210  ipath = opt_ipath;
211  if (!opt_opath.empty())
212  opath = opt_opath;
213 
214  // Append to the output path string, the login name of the user
215  std::string dirname = opath + vpIoTools::path("/") + username;
216 
217  // Test if the output path exist. If no try to create it
218  if (vpIoTools::checkDirectory(dirname) == false) {
219  try {
220  // Create the dirname
221  vpIoTools::makeDirectory(dirname);
222  }
223  catch (...) {
224  usage(argv[0], NULL, ipath, opath, username);
225  std::cerr << std::endl
226  << "ERROR:" << std::endl;
227  std::cerr << " Cannot create " << dirname << std::endl;
228  std::cerr << " Check your -o " << opath << " option " << std::endl;
229  exit(-1);
230  }
231  }
232 
233  // Compare ipath and env_ipath. If they differ, we take into account
234  // the input path comming from the command line option
235  if (!opt_ipath.empty() && !env_ipath.empty()) {
236  if (ipath != env_ipath) {
237  std::cout << std::endl
238  << "WARNING: " << std::endl;
239  std::cout << " Since -i <visp image path=" << ipath << "> "
240  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
241  << " we skip the environment variable." << std::endl;
242  }
243  }
244 
245  // Test if an input path is set
246  if (opt_ipath.empty() && env_ipath.empty()){
247  usage(argv[0], NULL, ipath, opath, username);
248  std::cerr << std::endl
249  << "ERROR:" << std::endl;
250  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
251  << std::endl
252  << " environment variable to specify the location of the " << std::endl
253  << " image path where test images are located." << std::endl << std::endl;
254  exit(-1);
255  }
256 
257 
259 
260 
261 
262  // First we wanted to have gray level image (8bits)
263  // vpImage is a template class you can declare vpImage of ... everything...
265 
266  // Although I is a gray level image you can read and write
267  // color image. Obviously the color will be translated as a gray level
268 
269  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
270  vpImageIo::read(I, filename);
271 
272  filename = dirname + vpIoTools::path("/IoPPM.Klimt_char.ppm");
273  vpImageIo::write(I, filename) ;
274 
275 
276  // test io error
277  // if the image you want to read on the disk does not exist
278  // an exception is thrown
279  //Try to load a non existing image
280  try {
281  filename = ipath + vpIoTools::path("/ViSP-images/image-that-does-not-exist.ppm");
282  vpImageIo::read(I,filename) ;
283  }
284  catch(vpException e) {
285  std::cout << "Catch an exception: " << e << std::endl;
286  }
287 
288 
289  // same thing if you to write in a directory that does not exist
290  // or where you are not allowd to write.
291  try {
292  filename = dirname + vpIoTools::path("/directory-that-does-not-exist/Klimt.ppm");
293  vpImageIo::write(I,filename) ;
294  }
295  catch(vpException e) {
296  std::cout << "Catch an exception: " << e << std::endl;
297  }
298 
299 
300  std::cout << "----------------------------------------------------" << std::endl ;
301 
302  // Let's consider that the image is now a color image (32 bits RGBa)
303  vpImage<vpRGBa> Irgba ;
304 
305  // read write unsigned char ppm image.
306 
307  // Load a color image from the disk
308  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
309  vpImageIo::read(Irgba, filename);
310 
311  // Write the content of the color image on the disk
312  filename = dirname + vpIoTools::path("/IoPGM.Klimt_rgba.ppm");
313  vpImageIo::write(Irgba, filename) ;
314 
315  // test io error
316  try {
317  filename = ipath + vpIoTools::path("/ViSP-images/image-that-does-not-exist.ppm");
318  vpImageIo::read(Irgba,filename) ;
319  }
320  catch(vpException e) {
321  std::cout << "Catch an exception: " << e << std::endl;
322  }
323 
324  // test io error
325  try {
326  filename = dirname + vpIoTools::path("/directory-that-does-not-exist/Klimt.ppm");
327  vpImageIo::write(Irgba,filename) ;
328  }
329 
330  catch(vpException e) {
331  std::cout << "Catch an exception: " << e << std::endl;
332  }
333  return 0;
334  }
335  catch(vpException e) {
336  std::cout << "Catch an exception: " << e << std::endl;
337  return 1;
338  }
339 }
340 
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
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
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