ViSP
imageDiskRW.cpp
1 /****************************************************************************
2  *
3  * $Id: imageDiskRW.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  * 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  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
184  env_ipath = vpIoTools::getViSPImagesDataPath();
185 
186  // Set the default input path
187  if (! env_ipath.empty())
188  ipath = env_ipath;
189 
190  // Set the default output path
191 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
192  opt_opath = "/tmp";
193 #elif defined(_WIN32)
194  opt_opath = "C:\\temp";
195 #endif
196 
197  // Get the user login name
198  vpIoTools::getUserName(username);
199 
200  // Read the command line options
201  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
202  exit (-1);
203  }
204 
205  // Get the option values
206  if (!opt_ipath.empty())
207  ipath = opt_ipath;
208  if (!opt_opath.empty())
209  opath = opt_opath;
210 
211  // Append to the output path string, the login name of the user
212  std::string dirname = vpIoTools::createFilePath(opath, username);
213 
214  // Test if the output path exist. If no try to create it
215  if (vpIoTools::checkDirectory(dirname) == false) {
216  try {
217  // Create the dirname
218  vpIoTools::makeDirectory(dirname);
219  }
220  catch (...) {
221  usage(argv[0], NULL, ipath, opath, username);
222  std::cerr << std::endl
223  << "ERROR:" << std::endl;
224  std::cerr << " Cannot create " << dirname << std::endl;
225  std::cerr << " Check your -o " << opath << " option " << std::endl;
226  exit(-1);
227  }
228  }
229 
230  // Compare ipath and env_ipath. If they differ, we take into account
231  // the input path comming from the command line option
232  if (!opt_ipath.empty() && !env_ipath.empty()) {
233  if (ipath != env_ipath) {
234  std::cout << std::endl
235  << "WARNING: " << std::endl;
236  std::cout << " Since -i <visp image path=" << ipath << "> "
237  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
238  << " we skip the environment variable." << std::endl;
239  }
240  }
241 
242  // Test if an input path is set
243  if (opt_ipath.empty() && env_ipath.empty()){
244  usage(argv[0], NULL, ipath, opath, username);
245  std::cerr << std::endl
246  << "ERROR:" << std::endl;
247  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
248  << std::endl
249  << " environment variable to specify the location of the " << std::endl
250  << " image path where test images are located." << std::endl << std::endl;
251  exit(-1);
252  }
253 
255 
256  // First we wanted to have gray level image (8bits)
257  // vpImage is a template class you can declare vpImage of ... everything...
259 
260  // Although I is a gray level image you can read and write
261  // color image. Obviously the color will be translated as a gray level
262 
263  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
264  vpImageIo::read(I, filename);
265 
266  filename = vpIoTools::createFilePath(dirname, "IoPPM.Klimt_char.ppm");
267  vpImageIo::write(I, filename) ;
268 
269  // test io error
270  // if the image you want to read on the disk does not exist
271  // an exception is thrown
272  //Try to load a non existing image
273  try {
274  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
275  vpImageIo::read(I,filename) ;
276  }
277  catch(vpException e) {
278  std::cout << "Catch an exception: " << e << std::endl;
279  }
280 
281  // same thing if you to write in a directory that does not exist
282  // or where you are not allowd to write.
283  try {
284  filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
285  vpImageIo::write(I,filename) ;
286  }
287  catch(vpException e) {
288  std::cout << "Catch an exception: " << e << std::endl;
289  }
290 
291 
292  std::cout << "----------------------------------------------------" << std::endl ;
293 
294  // Let's consider that the image is now a color image (32 bits RGBa)
295  vpImage<vpRGBa> Irgba ;
296 
297  // read write unsigned char ppm image.
298 
299  // Load a color image from the disk
300  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
301  vpImageIo::read(Irgba, filename);
302 
303  // Write the content of the color image on the disk
304  filename = vpIoTools::createFilePath(dirname, "IoPGM.Klimt_rgba.ppm");
305  vpImageIo::write(Irgba, filename) ;
306 
307  // test io error
308  try {
309  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
310  vpImageIo::read(Irgba,filename) ;
311  }
312  catch(vpException e) {
313  std::cout << "Catch an exception: " << e << std::endl;
314  }
315 
316  // test io error
317  try {
318  filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
319  vpImageIo::write(Irgba,filename) ;
320  }
321 
322  catch(vpException e) {
323  std::cout << "Catch an exception: " << e << std::endl;
324  }
325  return 0;
326  }
327  catch(vpException e) {
328  std::cout << "Catch an exception: " << e << std::endl;
329  return 1;
330  }
331 }
332 
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
error that can be emited by ViSP classes.
Definition: vpException.h:76
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