56 #include <visp3/core/vpDebug.h>
57 #include <visp3/core/vpImage.h>
58 #include <visp3/core/vpIoTools.h>
59 #include <visp3/io/vpImageIo.h>
60 #include <visp3/io/vpParseArgv.h>
62 #define GETOPTARGS "i:o:h"
75 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
78 Read and write PGM images on the disk. Also test exceptions.\n\
81 %s [-i <input image path>] [-o <output image path>]\n\
88 -i <input image path> %s\n\
89 Set image input path.\n\
90 From this path read \"Klimt/Klimt.pgm\"\n\
92 Setting the VISP_INPUT_IMAGE_PATH environment\n\
93 variable produces the same behaviour than using\n\
96 -o <output image path> %s\n\
97 Set image output path.\n\
98 From this directory, creates the \"%s\"\n\
99 subdirectory depending on the username, where \n\
100 Klimt_grey.pgm output image is written.\n\
103 Print the help.\n\n",
104 ipath.c_str(), opath.c_str(), user.c_str());
107 fprintf(stderr,
"ERROR: \n");
108 fprintf(stderr,
"\nBad parameter [%s]\n", badparam);
123 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user)
137 usage(argv[0],
nullptr, ipath, opath, user);
142 usage(argv[0], optarg_, ipath, opath, user);
148 if ((c == 1) || (c == -1)) {
150 usage(argv[0],
nullptr, ipath, opath, user);
151 std::cerr <<
"ERROR: " << std::endl;
152 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
159 int main(
int argc,
const char **argv)
162 std::string env_ipath;
163 std::string opt_ipath;
164 std::string opt_opath;
167 std::string filename;
168 std::string username;
170 std::cout <<
"-------------------------------------------------------" << std::endl;
171 std::cout <<
" imageDiskRW.cpp" << std::endl << std::endl;
173 std::cout <<
" reading and writting of PPM image" << std::endl;
174 std::cout <<
" read an image that does not exist" << std::endl;
175 std::cout <<
" write in a directory that does no exist" << std::endl;
176 std::cout <<
"-------------------------------------------------------" << std::endl;
177 std::cout << std::endl;
184 if (!env_ipath.empty())
188 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))
190 #elif defined(_WIN32)
191 opt_opath =
"C:\\temp";
198 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
203 if (!opt_ipath.empty())
205 if (!opt_opath.empty())
218 usage(argv[0],
nullptr, ipath, opath, username);
219 std::cerr << std::endl <<
"ERROR:" << std::endl;
220 std::cerr <<
" Cannot create " << dirname << std::endl;
221 std::cerr <<
" Check your -o " << opath <<
" option " << std::endl;
228 if (!opt_ipath.empty() && !env_ipath.empty()) {
229 if (ipath != env_ipath) {
230 std::cout << std::endl <<
"WARNING: " << std::endl;
231 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
232 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
233 <<
" we skip the environment variable." << std::endl;
238 if (opt_ipath.empty() && env_ipath.empty()) {
239 usage(argv[0],
nullptr, ipath, opath, username);
240 std::cerr << std::endl <<
"ERROR:" << std::endl;
241 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
242 <<
" environment variable to specify the location of the " << std::endl
243 <<
" image path where test images are located." << std::endl
273 std::cout <<
"Catch an expected exception: " << e << std::endl;
283 std::cout <<
"Catch an expected exception: " << e << std::endl;
286 std::cout <<
"----------------------------------------------------" << std::endl;
307 std::cout <<
"Catch an expected exception: " << e << std::endl;
316 std::cout <<
"Catch an expected exception: " << e << std::endl;
321 std::cout <<
"Catch an unexpected exception: " << e << std::endl;
error that can be emitted by ViSP classes.
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)