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