ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testReadImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testReadImage.cpp 4314 2013-07-16 17:41:21Z 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  try {
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(...) {
240  std::cout << "Unsupported image format" << std::endl;
241  }
242 
243  return 0;
244 }
#define vpTRACE
Definition: vpDebug.h:401
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:277