62 #include <visp/vpImage.h>
63 #include <visp/vpImageIo.h>
64 #include <visp/vpParseArgv.h>
65 #include <visp/vpIoTools.h>
66 #include <visp/vpDebug.h>
70 #define GETOPTARGS "i:o:h"
72 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user);
73 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user);
86 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
89 Read and write PGM images on the disk. Also test exceptions.\n\
92 %s [-i <input image path>] [-o <output image path>]\n\
98 -i <input image path> %s\n\
99 Set image input path.\n\
100 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
102 Setting the VISP_INPUT_IMAGE_PATH environment\n\
103 variable produces the same behaviour than using\n\
106 -o <output image path> %s\n\
107 Set image output path.\n\
108 From this directory, creates the \"%s\"\n\
109 subdirectory depending on the username, where \n\
110 Klimt_grey.pgm output image is written.\n\
113 Print the help.\n\n",
114 ipath.c_str(), opath.c_str(), user.c_str());
117 fprintf(stderr,
"ERROR: \n" );
118 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
133 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user)
140 case 'i': ipath = optarg_;
break;
141 case 'o': opath = optarg_;
break;
142 case 'h': usage(argv[0], NULL, ipath, opath, user);
return false;
break;
145 usage(argv[0], optarg_, ipath, opath, user);
return false;
break;
149 if ((c == 1) || (c == -1)) {
151 usage(argv[0], NULL, ipath, opath, user);
152 std::cerr <<
"ERROR: " << std::endl;
153 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
163 main(
int argc,
const char ** argv)
166 std::string env_ipath;
167 std::string opt_ipath;
168 std::string opt_opath;
171 std::string filename;
172 std::string username;
174 std::cout <<
"-------------------------------------------------------" << std::endl ;
175 std::cout <<
" imageDiskRW.cpp" <<std::endl << std::endl ;
177 std::cout <<
" reading and writting of PPM image" << std::endl ;
178 std::cout <<
" read an image that does not exist" << std::endl ;
179 std::cout <<
" write in a directory that does no exist" << std::endl ;
180 std::cout <<
"-------------------------------------------------------" << std::endl ;
181 std::cout << std::endl ;
185 char *ptenv = getenv(
"VISP_INPUT_IMAGE_PATH");
190 if (! env_ipath.empty())
194 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
196 #elif defined(_WIN32)
197 opt_opath =
"C:\\temp";
204 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
209 if (!opt_ipath.empty())
211 if (!opt_opath.empty())
224 usage(argv[0], NULL, ipath, opath, username);
225 std::cerr << std::endl
226 <<
"ERROR:" << std::endl;
227 std::cerr <<
" Cannot create " << dirname << std::endl;
228 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
235 if (!opt_ipath.empty() && !env_ipath.empty()) {
236 if (ipath != env_ipath) {
237 std::cout << std::endl
238 <<
"WARNING: " << std::endl;
239 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
240 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
241 <<
" we skip the environment variable." << std::endl;
246 if (opt_ipath.empty() && env_ipath.empty()){
247 usage(argv[0], NULL, ipath, opath, username);
248 std::cerr << std::endl
249 <<
"ERROR:" << std::endl;
250 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
252 <<
" environment variable to specify the location of the " << std::endl
253 <<
" image path where test images are located." << std::endl << std::endl;
281 filename = ipath +
vpIoTools::path(
"/ViSP-images/image-that-does-not-exist.ppm");
285 std::cout <<
"Catch an exception: " << e << std::endl;
292 filename = dirname +
vpIoTools::path(
"/directory-that-does-not-exist/Klimt.ppm");
296 std::cout <<
"Catch an exception: " << e << std::endl;
300 std::cout <<
"----------------------------------------------------" << std::endl ;
317 filename = ipath +
vpIoTools::path(
"/ViSP-images/image-that-does-not-exist.ppm");
321 std::cout <<
"Catch an exception: " << e << std::endl;
326 filename = dirname +
vpIoTools::path(
"/directory-that-does-not-exist/Klimt.ppm");
331 std::cout <<
"Catch an exception: " << e << std::endl;
336 std::cout <<
"Catch an exception: " << e << std::endl;
static void write(const vpImage< unsigned char > &I, const char *filename)
error that can be emited by ViSP classes.
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void read(vpImage< unsigned char > &I, const char *filename)