ViSP  2.9.0
testReadImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testReadImage.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Read images on the disk.
36  *
37  * Authors:
38  * Nicolas Melchior
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpImage.h>
43 #include <visp/vpImageIo.h>
44 #include <visp/vpIoTools.h>
45 #include <visp/vpParseArgv.h>
46 #include <visp/vpDebug.h>
47 
48 #include <stdlib.h>
49 
57 // List of allowed command line options
58 #define GETOPTARGS "i:p:h"
59 
60 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath);
61 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath);
62 
63 /*
64 
65  Print the program options.
66 
67  \param name : Program name.
68  \param badparam : Bad parameter name.
69  \param ipath: Input image path.
70  \param opath : Personal image path.
71  \param user : Username.
72 
73  */
74 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
75 {
76  fprintf(stdout, "\n\
77 Read images on the disk.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-i <input image path>] [-p <personal image path>]\n\
81  [-h]\n \
82 ", name);
83 
84  fprintf(stdout, "\n\
85 OPTIONS: Default\n\
86  -i <input image path> %s\n\
87  Set image input path.\n\
88  From this path read \"ViSP-images/Klimt/Klimt.pgm,\n\
89  .ppm, .jpeg and .png images.\n\
90  Setting the VISP_INPUT_IMAGE_PATH environment\n\
91  variable produces the same behaviour than using\n\
92  this option.\n\
93 \n\
94  -p <personal image path> %s\n\
95  Set personal image path.\n\
96  From this path read the image \"%s\".\n\
97 \n\
98  -h\n\
99  Print the help.\n\n",
100  ipath.c_str(), ppath.c_str(), ppath.c_str());
101 
102  if (badparam)
103  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
104 }
105 
117 bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath)
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': ipath = optarg_; break;
125  case 'p': ppath = optarg_; break;
126  case 'h': usage(argv[0], NULL, ipath, ppath); return false; 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_IMAGE_PATH environment variable value
156  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
157  if (ptenv != NULL)
158  env_ipath = ptenv;
159 
160  // Set the default input path
161  if (! env_ipath.empty())
162  ipath = env_ipath;
163 
164  // Read the command line options
165  if (getOptions(argc, argv, opt_ipath, opt_ppath) == false) {
166  exit (-1);
167  }
168 
169  // Get the option values
170  if (!opt_ipath.empty())
171  ipath = opt_ipath;
172  if (!opt_ppath.empty())
173  ppath = opt_ppath;
174 
175  // Compare ipath and env_ipath. If they differ, we take into account
176  // the input path comming from the command line option
177  if (!opt_ipath.empty() && !env_ipath.empty()) {
178  if (ipath != env_ipath) {
179  std::cout << std::endl
180  << "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  //
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  {
200  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
201  vpImageIo::read(I,filename);
202  printf("Read ppm ok\n");
203  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
204  vpImageIo::read(I,filename);
205  printf("Read pgm ok\n");
206  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.jpeg");
207  vpImageIo::read(I,filename);
208  printf("Read jpeg ok\n");
209  filename = ipath + vpIoTools::path("/ViSP-images/mire/mire.jpg");
210  vpImageIo::read(I,filename);
211  printf("Read jpg ok\n");
212  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.png");
213  vpImageIo::read(I,filename);
214  printf("Read png ok\n");
215 
216  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
217  vpImageIo::read(Irgb,filename);
218  printf("Read ppm ok\n");
219  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
220  vpImageIo::read(Irgb,filename);
221  printf("Read pgm ok\n");
222  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.jpeg");
223  vpImageIo::read(Irgb,filename);
224  printf("Read jpeg ok\n");
225  filename = ipath + vpIoTools::path("/ViSP-images/mire/mire.jpg");
226  vpImageIo::read(Irgb,filename);
227  printf("Read jpg ok\n");
228  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.png");
229  vpImageIo::read(Irgb,filename);
230  printf("Read png ok\n");
231  }
232  else
233  {
234  filename = opt_ppath;
235  vpImageIo::read(I,filename);
236  vpTRACE("image read without problem");
237  }
238  }
239  catch(vpException e) {
240  std::cout << "Catch an exception: " << e << std::endl;
241  }
242  return 0;
243 }
#define vpTRACE
Definition: vpDebug.h:418
error that can be emited by ViSP classes.
Definition: vpException.h:76
static std::string path(const char *pathname)
Definition: vpIoTools.cpp:715
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:79
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278