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