Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testReadImage.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 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 <stdlib.h>
41 
49 #ifdef ENABLE_VISP_NAMESPACE
50 using namespace VISP_NAMESPACE_NAME;
51 #endif
52 
53 // List of allowed command line options
54 #define GETOPTARGS "cdi:p:h"
55 
56 void usage(const char *name, const char *badparam, std::string ipath);
57 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath);
58 
59 /*
60 
61  Print the program options.
62 
63  \param name : Program name.
64  \param badparam : Bad parameter name.
65  \param ipath: Input image path.
66 
67  */
68 void usage(const char *name, const char *badparam, std::string ipath)
69 {
70  fprintf(stdout, "\n\
71 Read images on the disk.\n\
72 \n\
73 SYNOPSIS\n\
74  %s [-i <input image path>] [-p <personal image path>]\n\
75  [-h]\n\
76 ",
77 name);
78 
79  fprintf(stdout, "\n\
80 OPTIONS: Default\n\
81  -i <input image path> %s\n\
82  Set image input path.\n\
83  From this path read \"Klimt/Klimt.pgm,\n\
84  .ppm, .jpeg and .png images.\n\
85  Setting the VISP_INPUT_IMAGE_PATH environment\n\
86  variable produces the same behaviour than using\n\
87  this option.\n\
88 \n\
89  -p <personal image path> \n\
90  Path to an image used to test image reading function.\n\
91  Example: -p /my_path_to/image.png\n\
92 \n\
93  -h\n\
94  Print the help.\n\n",
95  ipath.c_str());
96 
97  if (badparam)
98  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99 }
100 
112 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath)
113 {
114  const char *optarg_;
115  int c;
116  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117 
118  switch (c) {
119  case 'i':
120  ipath = optarg_;
121  break;
122  case 'p':
123  ppath = optarg_;
124  break;
125  case 'h':
126  usage(argv[0], nullptr, ipath);
127  return false;
128  break;
129 
130  case 'c':
131  case 'd':
132  break;
133 
134  default:
135  usage(argv[0], optarg_, ipath);
136  return false;
137  break;
138  }
139  }
140 
141  if ((c == 1) || (c == -1)) {
142  // standalone param or error
143  usage(argv[0], nullptr, ipath);
144  std::cerr << "ERROR: " << std::endl;
145  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
146  return false;
147  }
148 
149  return true;
150 }
151 
152 int main(int argc, const char **argv)
153 {
154  try {
155  std::string env_ipath;
156  std::string opt_ipath;
157  std::string opt_ppath;
158  std::string ipath;
159  std::string filename;
160 
161  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
162  // environment variable value
163  env_ipath = vpIoTools::getViSPImagesDataPath();
164 
165  // Set the default input path
166  if (!env_ipath.empty())
167  ipath = env_ipath;
168 
169  // Read the command line options
170  if (getOptions(argc, argv, opt_ipath, opt_ppath) == false) {
171  return EXIT_FAILURE;
172  }
173 
174  // Get the option values
175  if (!opt_ipath.empty())
176  ipath = opt_ipath;
177 
178  // Compare ipath and env_ipath. If they differ, we take into account
179  // the input path coming from the command line option
180  if (!opt_ipath.empty() && !env_ipath.empty()) {
181  if (ipath != env_ipath) {
182  std::cout << std::endl << "WARNING: " << std::endl;
183  std::cout << " Since -i <visp image path=" << ipath << "> "
184  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
185  << " we skip the environment variable." << std::endl;
186  }
187  }
188 
189  //
190  // Here starts really the test
191  //
192 
194  // Create a grey level image
195  // vpImage<vpRGBa> I;
197  vpImage<vpRGBa> Irgb;
198 
199  if (opt_ppath.empty()) {
200  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
201  vpImageIo::read(I, filename);
202  printf("Read ppm ok\n");
203  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
204  vpImageIo::read(I, filename);
205  printf("Read pgm ok\n");
206  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.jpeg");
207  vpImageIo::read(I, filename);
208  printf("Read jpeg ok\n");
209  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.png");
210  vpImageIo::read(I, filename);
211  printf("Read png ok\n");
212 
213  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
214  vpImageIo::read(Irgb, filename);
215  printf("Read ppm ok\n");
216  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
217  vpImageIo::read(Irgb, filename);
218  printf("Read pgm ok\n");
219  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.jpeg");
220  vpImageIo::read(Irgb, filename);
221  printf("Read jpeg ok\n");
222  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.png");
223  vpImageIo::read(Irgb, filename);
224  printf("Read png ok\n");
225  }
226  else {
227  filename = opt_ppath;
228  vpImageIo::read(I, filename);
229  printf("Image \"%s\" read successfully\n", filename.c_str());
230  }
231  }
232  catch (const vpException &e) {
233  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
234  return EXIT_FAILURE;
235  }
236  return EXIT_SUCCESS;
237 }
error that can be emitted by ViSP classes.
Definition: vpException.h:60
const char * getMessage() const
Definition: vpException.cpp:65
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70