ViSP
testReadImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testReadImage.cpp 4814 2014-07-31 11:38:39Z 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-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:1071
#define vpTRACE
Definition: vpDebug.h:418
error that can be emited by ViSP classes.
Definition: vpException.h:76
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80
static std::string createFilePath(const std::string &parent, const std::string child)
Definition: vpIoTools.cpp:1245
static void read(vpImage< unsigned char > &I, const char *filename)
Definition: vpImageIo.cpp:278