ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testCreateSubImage.cpp
1 /****************************************************************************
2  *
3  * $Id: testCreateSubImage.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  * Test for sub-image extraction.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
42 #include <visp/vpImage.h>
43 #include <visp/vpImageIo.h>
44 #include <visp/vpImageTools.h>
45 #include <visp/vpIoTools.h>
46 #include <visp/vpRect.h>
47 #include <visp/vpParseArgv.h>
48 #include <visp/vpDebug.h>
49 
50 #include <stdlib.h>
51 
62 // List of allowed command line options
63 #define GETOPTARGS "i:o:h"
64 
65 /*
66 
67  Print the program options.
68 
69  \param name : Program name.
70  \param badparam : Bad parameter name.
71  \param ipath: Input image path.
72  \param opath : Output image path.
73  \param user : Username.
74 
75  */
76 void usage(const char *name, const char *badparam, std::string ipath, std::string opath, std::string user)
77 {
78  fprintf(stdout, "\n\
79 Read an image from the disk (Klimt.pgm), crop a rectangular area\n\
80 and save the cropped image on the disk (Klimt_cropped.pgm).\n\
81 \n\
82 SYNOPSIS\n\
83  %s [-i <input image path>] [-o <output image path>]\n\
84  [-h]\n \
85 ", name);
86 
87  fprintf(stdout, "\n\
88 OPTIONS: Default\n\
89  -i <input image path> %s\n\
90  Set image input path.\n\
91  From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
92  image.\n\
93  Setting the VISP_INPUT_IMAGE_PATH environment\n\
94  variable produces the same behaviour than using\n\
95  this option.\n\
96 \n\
97  -o <output image path> %s\n\
98  Set image output path.\n\
99  From this directory, creates the \"%s\"\n\
100  subdirectory depending on the username, where \n\
101  Klimt_cropped.pgm output image is written.\n\
102 \n\
103  -h\n\
104  Print the help.\n\n",
105  ipath.c_str(), opath.c_str(), user.c_str());
106 
107  if (badparam)
108  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
109 }
110 
123 bool getOptions(int argc, const char **argv,
124  std::string &ipath, std::string &opath, std::string user)
125 {
126  const char *optarg;
127  int c;
128  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
129 
130  switch (c) {
131  case 'i': ipath = optarg; break;
132  case 'o': opath = optarg; break;
133  case 'h': usage(argv[0], NULL, ipath, opath, user); return false; break;
134 
135  default:
136  usage(argv[0], optarg, ipath, opath, user); return false; break;
137  }
138  }
139 
140  if ((c == 1) || (c == -1)) {
141  // standalone param or error
142  usage(argv[0], NULL, ipath, opath, user);
143  std::cerr << "ERROR: " << std::endl;
144  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
145  return false;
146  }
147 
148  return true;
149 }
150 
151 int
152 main(int argc, const char ** argv)
153 {
154  std::string env_ipath;
155  std::string opt_ipath;
156  std::string opt_opath;
157  std::string ipath;
158  std::string opath;
159  std::string filename;
160  std::string username;
161 
162  // Get the VISP_IMAGE_PATH environment variable value
163  char *ptenv = getenv("VISP_INPUT_IMAGE_PATH");
164  if (ptenv != NULL)
165  env_ipath = ptenv;
166 
167  // Set the default input path
168  if (! env_ipath.empty())
169  ipath = env_ipath;
170 
171  // Set the default output path
172 #ifdef WIN32
173  opt_opath = "C:/temp";
174 #else
175  opt_opath = "/tmp";
176 #endif
177 
178  // Get the user login name
179  vpIoTools::getUserName(username);
180 
181  // Read the command line options
182  if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
183  exit (-1);
184  }
185 
186  // Get the option values
187  if (!opt_ipath.empty())
188  ipath = opt_ipath;
189  if (!opt_opath.empty())
190  opath = opt_opath;
191 
192  // Append to the output path string, the login name of the user
193  opath += vpIoTools::path("/") + username;
194 
195  // Test if the output path exist. If no try to create it
196  if (vpIoTools::checkDirectory(opath) == false) {
197  try {
198  // Create the dirname
200  }
201  catch (...) {
202  usage(argv[0], NULL, ipath, opt_opath, username);
203  std::cerr << std::endl
204  << "ERROR:" << std::endl;
205  std::cerr << " Cannot create " << opath << std::endl;
206  std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
207  exit(-1);
208  }
209  }
210 
211  // Compare ipath and env_ipath. If they differ, we take into account
212  // the input path comming from the command line option
213  if (opt_ipath.empty()) {
214  if (ipath != env_ipath) {
215  std::cout << std::endl
216  << "WARNING: " << std::endl;
217  std::cout << " Since -i <visp image path=" << ipath << "> "
218  << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
219  << " we skip the environment variable." << std::endl;
220  }
221  }
222 
223  // Test if an input path is set
224  if (opt_ipath.empty() && env_ipath.empty()){
225  usage(argv[0], NULL, ipath, opt_opath, username);
226  std::cerr << std::endl
227  << "ERROR:" << std::endl;
228  std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
229  << std::endl
230  << " environment variable to specify the location of the " << std::endl
231  << " image path where test images are located." << std::endl << std::endl;
232  exit(-1);
233  }
234 
235  //
236  // Here starts really the test
237  //
238  vpImage<unsigned char> I; // Input image
239  vpImage<unsigned char> C; // Cropped output image
240 
241 
242  // Read the input grey image from the disk
243  filename = ipath + vpIoTools::path("/ViSP-images/Klimt/Klimt.pgm");
244  std::cout << "Read image: " << filename << std::endl;
245  vpImageIo::readPGM(I, filename) ;
246 
247  // Specify the cropping area
248  vpRect crop;
249  crop.setLeft(-10.2);
250  crop.setTop(10.0);
251  crop.setWidth(I.getWidth());
252  crop.setHeight(20.0);
253 
254  // Create the cropped image
255  vpImageTools::createSubImage(I, crop, C);
256 
257  // Write the cropped image on the disk
258  filename = opath + vpIoTools::path("/Klimt_crop.pgm");
259  std::cout << "Write cropped image: " << filename << std::endl;
260  vpImageIo::writePGM(C, filename) ;
261 
262 }