Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
testReadImage.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  * Read images on the disk.
33  *
34  * Authors:
35  * Nicolas Melchior
36  *
37  *****************************************************************************/
38 
39 #include <visp3/core/vpDebug.h>
40 #include <visp3/core/vpImage.h>
41 #include <visp3/core/vpIoTools.h>
42 #include <visp3/io/vpImageIo.h>
43 #include <visp3/io/vpParseArgv.h>
44 
45 #include <stdlib.h>
46 
54 // List of allowed command line options
55 #define GETOPTARGS "cdi:p:h"
56 
57 void usage(const char *name, const char *badparam, std::string ipath);
58 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath);
59 
60 /*
61 
62  Print the program options.
63 
64  \param name : Program name.
65  \param badparam : Bad parameter name.
66  \param ipath: Input image path.
67 
68  */
69 void usage(const char *name, const char *badparam, std::string ipath)
70 {
71  fprintf(stdout, "\n\
72 Read images on the disk.\n\
73 \n\
74 SYNOPSIS\n\
75  %s [-i <input image path>] [-p <personal image path>]\n\
76  [-h]\n \
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", ipath.c_str());
95 
96  if (badparam)
97  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
98 }
99 
111 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath)
112 {
113  const char *optarg_;
114  int c;
115  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
116 
117  switch (c) {
118  case 'i':
119  ipath = optarg_;
120  break;
121  case 'p':
122  ppath = optarg_;
123  break;
124  case 'h':
125  usage(argv[0], NULL, ipath);
126  return false;
127  break;
128 
129  case 'c':
130  case 'd':
131  break;
132 
133  default:
134  usage(argv[0], optarg_, ipath);
135  return false;
136  break;
137  }
138  }
139 
140  if ((c == 1) || (c == -1)) {
141  // standalone param or error
142  usage(argv[0], NULL, ipath);
143  std::cerr << "ERROR: " << std::endl;
144  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
145  return false;
146  }
147 
148  return true;
149 }
150 
151 int main(int argc, const char **argv)
152 {
153  try {
154  std::string env_ipath;
155  std::string opt_ipath;
156  std::string opt_ppath;
157  std::string ipath;
158  std::string filename;
159 
160  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
161  // environment variable value
162  env_ipath = vpIoTools::getViSPImagesDataPath();
163 
164  // Set the default input path
165  if (!env_ipath.empty())
166  ipath = env_ipath;
167 
168  // Read the command line options
169  if (getOptions(argc, argv, opt_ipath, opt_ppath) == false) {
170  exit(-1);
171  }
172 
173  // Get the option values
174  if (!opt_ipath.empty())
175  ipath = opt_ipath;
176 
177  // Compare ipath and env_ipath. If they differ, we take into account
178  // the input path comming from the command line option
179  if (!opt_ipath.empty() && !env_ipath.empty()) {
180  if (ipath != env_ipath) {
181  std::cout << std::endl << "WARNING: " << std::endl;
182  std::cout << " Since -i <visp image path=" << ipath << "> "
183  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
184  << " we skip the environment variable." << std::endl;
185  }
186  }
187 
188  //
189  // Here starts really the test
190  //
191 
193  // Create a grey level image
194  // vpImage<vpRGBa> I;
196  vpImage<vpRGBa> Irgb;
197 
198  if (opt_ppath.empty()) {
199  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
200  vpImageIo::read(I, filename);
201  printf("Read ppm ok\n");
202  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
203  vpImageIo::read(I, filename);
204  printf("Read pgm ok\n");
205  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.jpeg");
206  vpImageIo::read(I, filename);
207  printf("Read jpeg ok\n");
208  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.png");
209  vpImageIo::read(I, filename);
210  printf("Read png ok\n");
211 
212  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
213  vpImageIo::read(Irgb, filename);
214  printf("Read ppm ok\n");
215  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
216  vpImageIo::read(Irgb, filename);
217  printf("Read pgm ok\n");
218  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.jpeg");
219  vpImageIo::read(Irgb, filename);
220  printf("Read jpeg ok\n");
221  filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.png");
222  vpImageIo::read(Irgb, filename);
223  printf("Read png ok\n");
224  } else {
225  filename = opt_ppath;
226  vpImageIo::read(I, filename);
227  printf("Image \"%s\" read successfully\n", filename.c_str());
228  }
229  } catch (const vpException &e) {
230  std::cout << "Catch an exception: " << e.getMessage() << std::endl;
231  }
232  return 0;
233 }
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 std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1541
const char * getMessage(void) const
Definition: vpException.cpp:90
static void read(vpImage< unsigned char > &I, const std::string &filename)
Definition: vpImageIo.cpp:207