ViSP  3.0.0
testReadImage.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Nicolas Melchior
35  *
36  *****************************************************************************/
37 
38 #include <visp3/core/vpImage.h>
39 #include <visp3/io/vpImageIo.h>
40 #include <visp3/core/vpIoTools.h>
41 #include <visp3/io/vpParseArgv.h>
42 #include <visp3/core/vpDebug.h>
43 
44 #include <stdlib.h>
45 
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, std::string ppath);
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  \param opath : Personal image path.
67  \param user : Username.
68 
69  */
70 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
71 {
72  fprintf(stdout, "\n\
73 Read images on the disk.\n\
74 \n\
75 SYNOPSIS\n\
76  %s [-i <input image path>] [-p <personal image path>]\n\
77  [-h]\n \
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 \"ViSP-images/Klimt/Klimt.pgm,\n\
85  .ppm, .jpeg and .png 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  -p <personal image path> %s\n\
91  Set personal image path.\n\
92  From this path read the image \"%s\".\n\
93 \n\
94  -h\n\
95  Print the help.\n\n",
96  ipath.c_str(), ppath.c_str(), ppath.c_str());
97 
98  if (badparam)
99  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
100 }
101 
113 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath)
114 {
115  const char *optarg_;
116  int c;
117  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118 
119  switch (c) {
120  case 'i': ipath = optarg_; break;
121  case 'p': ppath = optarg_; break;
122  case 'h': usage(argv[0], NULL, ipath, ppath); return false; break;
123 
124  case 'c':
125  case 'd':
126  break;
127 
128  default:
129  usage(argv[0], optarg_, ipath, ppath); return false; break;
130  }
131  }
132 
133  if ((c == 1) || (c == -1)) {
134  // standalone param or error
135  usage(argv[0], NULL, ipath, ppath);
136  std::cerr << "ERROR: " << std::endl;
137  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
138  return false;
139  }
140 
141  return true;
142 }
143 
144 int
145 main(int argc, const char ** argv)
146 {
147  try {
148  std::string env_ipath;
149  std::string opt_ipath;
150  std::string opt_ppath;
151  std::string ipath;
152  std::string ppath;
153  std::string filename;
154 
155  // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH environment variable value
156  env_ipath = vpIoTools::getViSPImagesDataPath();
157 
158  // Set the default input path
159  if (! env_ipath.empty())
160  ipath = env_ipath;
161 
162  // Read the command line options
163  if (getOptions(argc, argv, opt_ipath, opt_ppath) == false) {
164  exit (-1);
165  }
166 
167  // Get the option values
168  if (!opt_ipath.empty())
169  ipath = opt_ipath;
170  if (!opt_ppath.empty())
171  ppath = opt_ppath;
172 
173  // Compare ipath and env_ipath. If they differ, we take into account
174  // the input path comming from the command line option
175  if (!opt_ipath.empty() && !env_ipath.empty()) {
176  if (ipath != env_ipath) {
177  std::cout << std::endl
178  << "WARNING: " << std::endl;
179  std::cout << " Since -i <visp image path=" << ipath << "> "
180  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
181  << " we skip the environment variable." << std::endl;
182  }
183  }
184 
185 
186  //
187  // Here starts really the test
188  //
189 
191  // Create a grey level image
192  //vpImage<vpRGBa> I;
194  vpImage<vpRGBa> Irgb;
195 
196  if (opt_ppath.empty())
197  {
198  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
199  vpImageIo::read(I,filename);
200  printf("Read ppm ok\n");
201  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
202  vpImageIo::read(I,filename);
203  printf("Read pgm ok\n");
204  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.jpeg");
205  vpImageIo::read(I,filename);
206  printf("Read jpeg ok\n");
207  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.jpg");
208  vpImageIo::read(I,filename);
209  printf("Read jpg ok\n");
210  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.png");
211  vpImageIo::read(I,filename);
212  printf("Read png ok\n");
213 
214  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.ppm");
215  vpImageIo::read(Irgb,filename);
216  printf("Read ppm ok\n");
217  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.pgm");
218  vpImageIo::read(Irgb,filename);
219  printf("Read pgm ok\n");
220  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.jpeg");
221  vpImageIo::read(Irgb,filename);
222  printf("Read jpeg ok\n");
223  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.jpg");
224  vpImageIo::read(Irgb,filename);
225  printf("Read jpg ok\n");
226  filename = vpIoTools::createFilePath(ipath, "ViSP-images/Klimt/Klimt.png");
227  vpImageIo::read(Irgb,filename);
228  printf("Read png ok\n");
229  }
230  else
231  {
232  filename = opt_ppath;
233  vpImageIo::read(I,filename);
234  vpTRACE("image read without problem");
235  }
236  }
237  catch(vpException e) {
238  std::cout << "Catch an exception: " << e << std::endl;
239  }
240  return 0;
241 }
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1091
error that can be emited by ViSP classes.
Definition: vpException.h:73
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
#define vpTRACE
Definition: vpDebug.h:414
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1265
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:274