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