Visual Servoing Platform  version 3.0.0
imageDiskRW.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Reading and writting images on the disk.
32  *
33  * Authors:
34  * Eric Marchand
35  *
36  *****************************************************************************/
37 
58 #include <visp3/core/vpImage.h>
59 #include <visp3/io/vpImageIo.h>
60 #include <visp3/io/vpParseArgv.h>
61 #include <visp3/core/vpIoTools.h>
62 #include <visp3/core/vpDebug.h>
63 #include <stdlib.h>
64 #include <stdio.h>
65 // List of allowed command line options
66 #define GETOPTARGS "i:o:h"
67 
68 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user);
69 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user);
70 
82 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
83 {
84  fprintf(stdout, "\n\
85 Read and write PGM images on the disk. Also test exceptions.\n\
86 \n\
87 SYNOPSIS\n\
88  %s [-i <input image path>] [-o <output image path>]\n\
89  [-h]\n \
90 ", name);
91 
92  fprintf(stdout, "\n\
93 OPTIONS: Default\n\
94  -i <input image path> %s\n\
95  Set image input path.\n\
96  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
97  image.\n\
98  Setting the VISP_INPUT_IMAGE_PATH environment\n\
99  variable produces the same behaviour than using\n\
100  this option.\n\
101 \n\
102  -o <output image path> %s\n\
103  Set image output path.\n\
104  From this directory, creates the \"%s\"\n\
105  subdirectory depending on the username, where \n\
106  Klimt_grey.pgm output image is written.\n\
107 \n\
108  -h\n\
109  Print the help.\n\n",
110  ipath.c_str(), opath.c_str(), user.c_str());
111 
112  if (badparam) {
113  fprintf(stderr, "ERROR: \n" );
114  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
115  }
116 }
129 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, std::string user)
130 {
131  const char *optarg_;
132  int c;
133  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
134 
135  switch (c) {
136  case 'i': ipath = optarg_; break;
137  case 'o': opath = optarg_; break;
138  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
139 
140  default:
141  usage(argv[0], optarg_, ipath, opath, user); return false; break;
142  }
143  }
144 
145  if ((c == 1) || (c == -1)) {
146  // standalone param or error
147  usage(argv[0], NULL, ipath, opath, user);
148  std::cerr << "ERROR: " << std::endl;
149  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
150  return false;
151  }
152 
153  return true;
154 }
155 
156 
157 
158 int
159 main(int argc, const char ** argv)
160 {
161  try {
162  std::string env_ipath;
163  std::string opt_ipath;
164  std::string opt_opath;
165  std::string ipath;
166  std::string opath;
167  std::string filename;
168  std::string username;
169 
170  std::cout << "-------------------------------------------------------" << std::endl ;
171  std::cout << " imageDiskRW.cpp" <<std::endl << std::endl ;
172 
173  std::cout << " reading and writting of PPM image" << std::endl ;
174  std::cout << " read an image that does not exist" << std::endl ;
175  std::cout << " write in a directory that does no exist" << std::endl ;
176  std::cout << "-------------------------------------------------------" << std::endl ;
177  std::cout << std::endl ;
178 
179  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
180  env_ipath = vpIoTools::getViSPImagesDataPath();
181 
182  // Set the default input path
183  if (! env_ipath.empty())
184  ipath = env_ipath;
185 
186  // Set the default output path
187 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
188  opt_opath = "/tmp";
189 #elif defined(_WIN32)
190  opt_opath = "C:\\temp";
191 #endif
192 
193  // Get the user login name
194  vpIoTools::getUserName(username);
195 
196  // Read the command line options
197  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
198  exit (-1);
199  }
200 
201  // Get the option values
202  if (!opt_ipath.empty())
203  ipath = opt_ipath;
204  if (!opt_opath.empty())
205  opath = opt_opath;
206 
207  // Append to the output path string, the login name of the user
208  std::string dirname = vpIoTools::createFilePath(opath, username);
209 
210  // Test if the output path exist. If no try to create it
211  if (vpIoTools::checkDirectory(dirname) == false) {
212  try {
213  // Create the dirname
214  vpIoTools::makeDirectory(dirname);
215  }
216  catch (...) {
217  usage(argv[0], NULL, ipath, opath, username);
218  std::cerr << std::endl
219  << "ERROR:" << std::endl;
220  std::cerr << " Cannot create " << dirname << std::endl;
221  std::cerr << " Check your -o " << opath << " option " << std::endl;
222  exit(-1);
223  }
224  }
225 
226  // Compare ipath and env_ipath. If they differ, we take into account
227  // the input path comming from the command line option
228  if (!opt_ipath.empty() && !env_ipath.empty()) {
229  if (ipath != env_ipath) {
230  std::cout << std::endl
231  << "WARNING: " << std::endl;
232  std::cout << " Since -i <visp image path=" << ipath << "> "
233  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
234  << " we skip the environment variable." << std::endl;
235  }
236  }
237 
238  // Test if an input path is set
239  if (opt_ipath.empty() && env_ipath.empty()){
240  usage(argv[0], NULL, ipath, opath, username);
241  std::cerr << std::endl
242  << "ERROR:" << std::endl;
243  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
244  << std::endl
245  << " environment variable to specify the location of the " << std::endl
246  << " image path where test images are located." << std::endl << std::endl;
247  exit(-1);
248  }
249 
251 
252  // First we wanted to have gray level image (8bits)
253  // vpImage is a template class you can declare vpImage of ... everything...
255 
256  // Although I is a gray level image you can read and write
257  // color image. Obviously the color will be translated as a gray level
258 
259  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
260  vpImageIo::read(I, filename);
261 
262  filename = vpIoTools::createFilePath(dirname, "IoPPM.Klimt_char.ppm");
263  vpImageIo::write(I, filename) ;
264 
265  // test io error
266  // if the image you want to read on the disk does not exist
267  // an exception is thrown
268  //Try to load a non existing image
269  try {
270  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
271  vpImageIo::read(I,filename) ;
272  }
273  catch(vpException e) {
274  std::cout << "Catch an exception: " << e << std::endl;
275  }
276 
277  // same thing if you to write in a directory that does not exist
278  // or where you are not allowd to write.
279  try {
280  filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
281  vpImageIo::write(I,filename) ;
282  }
283  catch(vpException e) {
284  std::cout << "Catch an exception: " << e << std::endl;
285  }
286 
287 
288  std::cout << "----------------------------------------------------" << std::endl ;
289 
290  // Let's consider that the image is now a color image (32 bits RGBa)
291  vpImage<vpRGBa> Irgba ;
292 
293  // read write unsigned char ppm image.
294 
295  // Load a color image from the disk
296  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
297  vpImageIo::read(Irgba, filename);
298 
299  // Write the content of the color image on the disk
300  filename = vpIoTools::createFilePath(dirname, "IoPGM.Klimt_rgba.ppm");
301  vpImageIo::write(Irgba, filename) ;
302 
303  // test io error
304  try {
305  filename = vpIoTools::createFilePath(ipath, "ViSP-images/image-that-does-not-exist.ppm");
306  vpImageIo::read(Irgba,filename) ;
307  }
308  catch(vpException e) {
309  std::cout << "Catch an exception: " << e << std::endl;
310  }
311 
312  // test io error
313  try {
314  filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
315  vpImageIo::write(Irgba,filename) ;
316  }
317 
318  catch(vpException e) {
319  std::cout << "Catch an exception: " << e << std::endl;
320  }
321  return 0;
322  }
323  catch(vpException e) {
324  std::cout << "Catch an exception: " << e << std::endl;
325  return 1;
326  }
327 }
328 
static void write(const vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:472
static bool checkDirectory(const char *dirname)
Definition: vpIoTools.cpp:335
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1091
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
static void makeDirectory(const char *dirname)
Definition: vpIoTools.cpp:404
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1265
static std::string getUserName()
Definition: vpIoTools.cpp:161
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:274