Visual Servoing Platform  version 3.6.1 under development (2024-05-09)
imageDiskRW.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <visp3/core/vpDebug.h>
57 #include <visp3/core/vpImage.h>
58 #include <visp3/core/vpIoTools.h>
59 #include <visp3/io/vpImageIo.h>
60 #include <visp3/io/vpParseArgv.h>
61  // List of allowed command line options
62 #define GETOPTARGS "i:o:h"
63 
75 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
76 {
77  fprintf(stdout, "\n\
78 Read and write PGM images on the disk. Also test exceptions.\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-i <input image path>] [-o <output image path>]\n\
82  [-h]\n\
83 ",
84 name);
85 
86  fprintf(stdout, "\n\
87 OPTIONS: Default\n\
88  -i <input image path> %s\n\
89  Set image input path.\n\
90  From this path read \"Klimt/Klimt.pgm\"\n\
91  image.\n\
92  Setting the VISP_INPUT_IMAGE_PATH environment\n\
93  variable produces the same behaviour than using\n\
94  this option.\n\
95 \n\
96  -o <output image path> %s\n\
97  Set image output path.\n\
98  From this directory, creates the \"%s\"\n\
99  subdirectory depending on the username, where \n\
100  Klimt_grey.pgm output image is written.\n\
101 \n\
102  -h\n\
103  Print the help.\n\n",
104  ipath.c_str(), opath.c_str(), user.c_str());
105 
106  if (badparam) {
107  fprintf(stderr, "ERROR: \n");
108  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
109  }
110 }
123 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user)
124 {
125  const char *optarg_;
126  int c;
127  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
128 
129  switch (c) {
130  case 'i':
131  ipath = optarg_;
132  break;
133  case 'o':
134  opath = optarg_;
135  break;
136  case 'h':
137  usage(argv[0], nullptr, ipath, opath, user);
138  return false;
139  break;
140 
141  default:
142  usage(argv[0], optarg_, ipath, opath, user);
143  return false;
144  break;
145  }
146  }
147 
148  if ((c == 1) || (c == -1)) {
149  // standalone param or error
150  usage(argv[0], nullptr, ipath, opath, user);
151  std::cerr << "ERROR: " << std::endl;
152  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
153  return false;
154  }
155 
156  return true;
157 }
158 
159 int 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
180  // environment variable value
181  env_ipath = vpIoTools::getViSPImagesDataPath();
182 
183  // Set the default input path
184  if (!env_ipath.empty())
185  ipath = env_ipath;
186 
187  // Set the default output path
188 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
189  opt_opath = "/tmp";
190 #elif defined(_WIN32)
191  opt_opath = "C:\\temp";
192 #endif
193 
194  // Get the user login name
195  vpIoTools::getUserName(username);
196 
197  // Read the command line options
198  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
199  return EXIT_SUCCESS;
200  }
201 
202  // Get the option values
203  if (!opt_ipath.empty())
204  ipath = opt_ipath;
205  if (!opt_opath.empty())
206  opath = opt_opath;
207 
208  // Append to the output path string, the login name of the user
209  std::string dirname = vpIoTools::createFilePath(opath, username);
210 
211  // Test if the output path exist. If no try to create it
212  if (vpIoTools::checkDirectory(dirname) == false) {
213  try {
214  // Create the dirname
215  vpIoTools::makeDirectory(dirname);
216  }
217  catch (...) {
218  usage(argv[0], nullptr, 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 coming 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], nullptr, 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  }
272  catch (const vpException &e) {
273  std::cout << "Catch an expected exception: " << e << std::endl;
274  }
275 
276  // same thing if you to write in a directory that does not exist
277  // or where you are not allowd to write.
278  try {
279  filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
280  vpImageIo::write(I, filename);
281  }
282  catch (const vpException &e) {
283  std::cout << "Catch an expected exception: " << e << std::endl;
284  }
285 
286  std::cout << "----------------------------------------------------" << std::endl;
287 
288  // Let's consider that the image is now a color image (32 bits RGBa)
289  vpImage<vpRGBa> Irgba;
290 
291  // read write unsigned char ppm image.
292 
293  // Load a color image from the disk
294  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
295  vpImageIo::read(Irgba, filename);
296 
297  // Write the content of the color image on the disk
298  filename = vpIoTools::createFilePath(dirname, "IoPGM.Klimt_rgba.ppm");
299  vpImageIo::write(Irgba, filename);
300 
301  // test io error
302  try {
303  filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
304  vpImageIo::read(Irgba, filename);
305  }
306  catch (const vpException &e) {
307  std::cout << "Catch an expected exception: " << e << std::endl;
308  }
309 
310  // test io error
311  try {
312  filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
313  vpImageIo::write(Irgba, filename);
314  }
315  catch (const vpException &e) {
316  std::cout << "Catch an expected exception: " << e << std::endl;
317  }
318  return EXIT_SUCCESS;
319  }
320  catch (const vpException &e) {
321  std::cout << "Catch an unexpected exception: " << e << std::endl;
322  return EXIT_FAILURE;
323  }
324 }
error that can be emitted by ViSP classes.
Definition: vpException.h:59
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:143
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:287
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1832
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:832
static std::string getUserName()
Definition: vpIoTools.cpp:725
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:2195
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:981
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:69