Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testIoPPM.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  * Read and write PGM images on the disk.
32  */
33 
34 #include <visp3/core/vpDebug.h>
35 #include <visp3/core/vpImage.h>
36 #include <visp3/core/vpIoTools.h>
37 #include <visp3/io/vpImageIo.h>
38 #include <visp3/io/vpParseArgv.h>
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 
50 // List of allowed command line options
51 #define GETOPTARGS "cdi:o:h"
52 
53 #ifdef ENABLE_VISP_NAMESPACE
54 using namespace VISP_NAMESPACE_NAME;
55 #endif
56 
57 /*
58 
59  Print the program options.
60 
61  \param name : Program name.
62  \param badparam : Bad parameter name.
63  \param ipath: Input image path.
64  \param opath : Output image path.
65  \param user : Username.
66 
67  */
68 void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
69  const std::string &user)
70 {
71  fprintf(stdout, "\n\
72 Read and write PPM images on the disk. Also test exceptions.\n\
73 \n\
74 SYNOPSIS\n\
75  %s [-i <input image path>] [-o <output image path>]\n\
76  [-h]\n\
77 ",
78 name);
79 
80  fprintf(stdout, "\n\
81 OPTIONS: Default\n\
82  -i <input image path> %s\n\
83  Set image input path.\n\
84  From this path read \"Klimt/Klimt.pgm\"\n\
85  and \"Klimt/Klimt.ppm\" images.\n\
86  Setting the VISP_INPUT_IMAGE_PATH environment\n\
87  variable produces the same behaviour than using\n\
88  this option.\n\
89 \n\
90  -o <output image path> %s\n\
91  Set image output path.\n\
92  From this directory, creates the \"%s\"\n\
93  subdirectory depending on the username, where \n\
94  Klimt_grey.ppm and Klimt_color.ppm output image\n\
95  are written.\n\
96 \n\
97  -h\n\
98  Print the help.\n\n",
99  ipath.c_str(), opath.c_str(), user.c_str());
100 
101  if (badparam)
102  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
103 }
104 
117 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user)
118 {
119  const char *optarg_;
120  int c;
121  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
122 
123  switch (c) {
124  case 'i':
125  ipath = optarg_;
126  break;
127  case 'o':
128  opath = optarg_;
129  break;
130  case 'h':
131  usage(argv[0], nullptr, ipath, opath, user);
132  return false;
133  break;
134 
135  case 'c':
136  case 'd':
137  break;
138 
139  default:
140  usage(argv[0], optarg_, ipath, opath, user);
141  return false;
142  break;
143  }
144  }
145 
146  if ((c == 1) || (c == -1)) {
147  // standalone param or error
148  usage(argv[0], nullptr, ipath, opath, user);
149  std::cerr << "ERROR: " << std::endl;
150  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
151  return false;
152  }
153 
154  return true;
155 }
156 
157 int main(int argc, const char **argv)
158 {
159  try {
160  std::string env_ipath;
161  std::string opt_ipath;
162  std::string opt_opath;
163  std::string ipath;
164  std::string opath;
165  std::string filename;
166  std::string username;
167 
168  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
169  // environment variable value
170  env_ipath = vpIoTools::getViSPImagesDataPath();
171 
172  // Set the default input path
173  if (!env_ipath.empty())
174  ipath = env_ipath;
175 
176 // Set the default output path
177 #if defined(_WIN32)
178  opt_opath = "C:/temp";
179 #else
180  opt_opath = "/tmp";
181 #endif
182 
183  // Get the user login name
184  vpIoTools::getUserName(username);
185 
186  // Read the command line options
187  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
188  return EXIT_FAILURE;
189  }
190 
191  // Get the option values
192  if (!opt_ipath.empty())
193  ipath = opt_ipath;
194  if (!opt_opath.empty())
195  opath = opt_opath;
196 
197  // Append to the output path string, the login name of the user
198  opath = vpIoTools::createFilePath(opath, username);
199 
200  // Test if the output path exist. If no try to create it
201  if (vpIoTools::checkDirectory(opath) == false) {
202  try {
203  // Create the dirname
205  }
206  catch (...) {
207  usage(argv[0], nullptr, ipath, opt_opath, username);
208  std::cerr << std::endl << "ERROR:" << std::endl;
209  std::cerr << " Cannot create " << opath << std::endl;
210  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
211  return EXIT_FAILURE;
212  }
213  }
214 
215  // Compare ipath and env_ipath. If they differ, we take into account
216  // the input path coming from the command line option
217  if (!opt_ipath.empty() && !env_ipath.empty()) {
218  if (ipath != env_ipath) {
219  std::cout << std::endl << "WARNING: " << std::endl;
220  std::cout << " Since -i <visp image path=" << ipath << "> "
221  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
222  << " we skip the environment variable." << std::endl;
223  }
224  }
225 
226  // Test if an input path is set
227  if (opt_ipath.empty() && env_ipath.empty()) {
228  usage(argv[0], nullptr, ipath, opt_opath, username);
229  std::cerr << std::endl << "ERROR:" << std::endl;
230  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
231  << " environment variable to specify the location of the " << std::endl
232  << " image path where test images are located." << std::endl
233  << std::endl;
234  return EXIT_FAILURE;
235  }
236 
237  //
238  // Here starts really the test
239  //
240 
242  // Create a grey level image
244 
245  // Load a color image from the disk and convert it to a grey level one
246  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
247  std::cout << "Read image: " << filename << std::endl;
248  vpImageIo::read(I, filename);
249  // Write the content of the image on the disk
250  filename = vpIoTools::createFilePath(opath, "Klimt_grey.ppm");
251  std::cout << "Write image: " << filename << std::endl;
252  vpImageIo::write(I, filename);
253 
254  // Try to load a non existing image (test for exceptions)
255  try {
256  // Load a non existing grey image
257  filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
258  std::cout << "Read image: " << filename << std::endl;
259  vpImageIo::read(I, filename);
260  }
261  catch (vpImageException &e) {
262  vpERROR_TRACE("at main level");
263  std::cout << e << std::endl;
264  }
265 
266  // Try to write an image to a non existing directory
267  try {
268  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
269  std::cout << "Write image: " << filename << std::endl;
270  vpImageIo::write(I, filename);
271  }
272  catch (const vpException &e) {
273  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
274  }
275 
277  // Create a color image
278  vpImage<vpRGBa> Irgba;
279 
280  // Load a color image from the disk
281  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
282  std::cout << "Read image: " << filename << std::endl;
283  vpImageIo::read(Irgba, filename);
284  // Write the content of the color image on the disk
285  filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
286  std::cout << "Write image: " << filename << std::endl;
287  vpImageIo::write(Irgba, filename);
288 
289  try {
290  // Try to load a non existing image (test for exceptions)
291  // Load a non existing color image
292  filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
293  std::cout << "Read image: " << filename << std::endl;
294  vpImageIo::read(Irgba, filename);
295  }
296  catch (const vpException &e) {
297  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
298  }
299 
300  try {
301  // Try to write a color image to a non existing directory
302  filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
303  std::cout << "Write image: " << filename << std::endl;
304  vpImageIo::write(Irgba, filename);
305  }
306  catch (const vpException &e) {
307  std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
308  }
309  std::cout << "Test succeed" << std::endl;
310  return EXIT_SUCCESS;
311  }
312  catch (const vpException &e) {
313  std::cout << "Catch an exception: " << e << std::endl;
314  return EXIT_FAILURE;
315  }
316 }
error that can be emitted by ViSP classes.
Definition: vpException.h:60
Error that can be emitted by the vpImage class and its derivatives.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string getUserName()
Definition: vpIoTools.cpp:285
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
#define vpERROR_TRACE
Definition: vpDebug.h:409