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