ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testReadImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testReadImage.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 
61 /*
62 
63  Print the program options.
64 
65  \param name : Program name.
66  \param badparam : Bad parameter name.
67  \param ipath: Input image path.
68  \param opath : Personal image path.
69  \param user : Username.
70 
71  */
72 void usage(const char *name, const char *badparam, std::string ipath, std::string ppath)
73 {
74  fprintf(stdout, "\n\
75 Read images on the disk.\n\
76 \n\
77 SYNOPSIS\n\
78  %s [-i <input image path>] [-p <personal image path>]\n\
79  [-h]\n \
80 ", name);
81 
82  fprintf(stdout, "\n\
83 OPTIONS: Default\n\
84  -i <input image path> %s\n\
85  Set image input path.\n\
86  From this path read \"ViSP-images/Klimt/Klimt.pgm,\n\
87  .ppm, .jpeg and .png images.\n\
88  Setting the VISP_INPUT_IMAGE_PATH environment\n\
89  variable produces the same behaviour than using\n\
90  this option.\n\
91 \n\
92  -p <personal image path> %s\n\
93  Set personal image path.\n\
94  From this path read the image \"%s\".\n\
95 \n\
96  -h\n\
97  Print the help.\n\n",
98  ipath.c_str(), ppath.c_str(), ppath.c_str());
99 
100  if (badparam)
101  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
102 }
103 
115 bool getOptions(int argc, const char **argv,
116  std::string &ipath, std::string &ppath)
117 {
118  const char *optarg;
119  int c;
120  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
121 
122  switch (c) {
123  case 'i': ipath = optarg; break;
124  case 'p': ppath = optarg; break;
125  case 'h': usage(argv[0], NULL, ipath, ppath); return false; break;
126 
127  default:
128  usage(argv[0], optarg, ipath, ppath); return false; break;
129  }
130  }
131 
132  if ((c == 1) || (c == -1)) {
133  // standalone param or error
134  usage(argv[0], NULL, ipath, ppath);
135  std::cerr << "ERROR: " << std::endl;
136  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
137  return false;
138  }
139 
140  return true;
141 }
142 
143 int
144 main(int argc, const char ** argv)
145 {
146 
147  std::string env_ipath;
148  std::string opt_ipath;
149  std::string opt_ppath;
150  std::string ipath;
151  std::string ppath;
152  std::string filename;
153 
154  // Get the VISP_IMAGE_PATH environment variable value
155  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
156  if (ptenv != NULL)
157  env_ipath = ptenv;
158 
159  // Set the default input path
160  if (! env_ipath.empty())
161  ipath = env_ipath;
162 
163  // Read the command line options
164  if (getOptions(argc, argv, opt_ipath, opt_ppath) == false) {
165  exit (-1);
166  }
167 
168  // Get the option values
169  if (!opt_ipath.empty())
170  ipath = opt_ipath;
171  if (!opt_ppath.empty())
172  ppath = opt_ppath;
173 
174  // Compare ipath and env_ipath. If they differ, we take into account
175  // the input path comming from the command line option
176  if (!opt_ipath.empty() && !env_ipath.empty()) {
177  if (ipath != env_ipath) {
178  std::cout << std::endl
179  << "WARNING: " << std::endl;
180  std::cout << " Since -i <visp image path=" << ipath << "> "
181  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
182  << " we skip the environment variable." << std::endl;
183  }
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  {
199  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
200  vpImageIo::readPPM(I,filename);
201  vpTRACE("Read ppm ok");
202  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
203  vpImageIo::readPGM(I,filename);
204  vpTRACE("Read pgm ok");
205 #if defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV)
206  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.jpeg");
207  vpImageIo::readJPEG(I,filename);
208  vpTRACE("Read jpeg ok");
209  filename = ipath + vpIoTools::path("/ViSP-images/mire/mire.jpg");
210  vpImageIo::readJPEG(I,filename);
211  vpTRACE("Read jpeg ok");
212 #else
213  vpTRACE("To read jpeg you must have the libjpeg or OpenCV library");
214 #endif
215 
216 #if defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV)
217  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.png");
218  vpImageIo::readPNG(I,filename);
219  vpTRACE("Read png ok");
220 #else
221  vpTRACE("To read png you must have the libpng or OpenCV library");
222 #endif
223 
224  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.ppm");
225  vpImageIo::readPPM(Irgb,filename);
226  vpTRACE("Read ppm ok");
227  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
228  vpImageIo::readPGM(Irgb,filename);
229  vpTRACE("Read pgm ok");
230 #if defined(VISP_HAVE_LIBJPEG) || defined(VISP_HAVE_OPENCV)
231  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.jpeg");
232  vpImageIo::readJPEG(Irgb,filename);
233  vpTRACE("Read jpeg ok");
234  //vpImageIo::writePPM(Irgb,"jpeg-rgb.ppm");
235  filename = ipath + vpIoTools::path("/ViSP-images/mire/mire.jpg");
236  vpImageIo::readJPEG(Irgb,filename);
237  vpTRACE("Read jpeg ok");
238  //vpImageIo::writePPM(Irgb,"jpeg-uchar.ppm");
239 #else
240  vpTRACE("To read jpeg you must have the libjpeg or OpenCV library");
241 #endif
242 
243 #if defined(VISP_HAVE_LIBPNG) || defined(VISP_HAVE_OPENCV)
244  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.png");
245  vpImageIo::readPNG(Irgb,filename);
246  vpTRACE("Read png ok");
247 #else
248  vpTRACE("To read png you must have the libpng or OpenCV library");
249 #endif
250 
251  }
252 
253  else
254  {
255  filename = opt_ppath;
256  vpImageIo::read(I,filename);
257  vpTRACE("image read without problem");
258  }
259 
260  return 0;
261 }