Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testIoPGM.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Read and write PGM images on the disk.
33 *
34*****************************************************************************/
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <visp3/core/vpDebug.h>
39#include <visp3/core/vpImage.h>
40#include <visp3/core/vpIoTools.h>
41#include <visp3/io/vpImageIo.h>
42#include <visp3/io/vpParseArgv.h>
43
51// List of allowed command line options
52#define GETOPTARGS "cdi:o:h"
53
54/*
55
56 Print the program options.
57
58 \param name : Program name.
59 \param badparam : Bad parameter name.
60 \param ipath: Input image path.
61 \param opath : Output image path.
62 \param user : Username.
63
64 */
65void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
66 const std::string &user)
67{
68 fprintf(stdout, "\n\
69Read and write PGM images on the disk. Also test exceptions.\n\
70\n\
71SYNOPSIS\n\
72 %s [-i <input image path>] [-o <output image path>]\n\
73 [-h]\n\
74",
75 name);
76
77 fprintf(stdout, "\n\
78OPTIONS: Default\n\
79 -i <input image path> %s\n\
80 Set image input path.\n\
81 From this path read \"Klimt/Klimt.pgm\"\n\
82 image.\n\
83 Setting the VISP_INPUT_IMAGE_PATH environment\n\
84 variable produces the same behaviour than using\n\
85 this option.\n\
86\n\
87 -o <output image path> %s\n\
88 Set image output path.\n\
89 From this directory, creates the \"%s\"\n\
90 subdirectory depending on the username, where \n\
91 Klimt_grey.pgm output image is written.\n\
92\n\
93 -h\n\
94 Print the help.\n\n",
95 ipath.c_str(), opath.c_str(), user.c_str());
96
97 if (badparam)
98 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99}
100
113bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user)
114{
115 const char *optarg_;
116 int c;
117 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
118
119 switch (c) {
120 case 'i':
121 ipath = optarg_;
122 break;
123 case 'o':
124 opath = optarg_;
125 break;
126 case 'h':
127 usage(argv[0], NULL, ipath, opath, user);
128 return false;
129 break;
130
131 case 'c':
132 case 'd':
133 break;
134
135 default:
136 usage(argv[0], optarg_, ipath, opath, user);
137 return false;
138 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
153int main(int argc, const char **argv)
154{
155 try {
156 std::string env_ipath;
157 std::string opt_ipath;
158 std::string opt_opath;
159 std::string ipath;
160 std::string opath;
161 std::string filename;
162 std::string username;
163
164 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
165 // environment variable value
167
168 // Set the default input path
169 if (!env_ipath.empty())
170 ipath = env_ipath;
171
172// Set the default output path
173#if defined(_WIN32)
174 opt_opath = "C:/temp";
175#else
176 opt_opath = "/tmp";
177#endif
178
179 // Get the user login name
180 vpIoTools::getUserName(username);
181
182 // Read the command line options
183 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
184 return EXIT_FAILURE;
185 }
186
187 // Get the option values
188 if (!opt_ipath.empty())
189 ipath = opt_ipath;
190 if (!opt_opath.empty())
191 opath = opt_opath;
192
193 // Append to the output path string, the login name of the user
194 opath = vpIoTools::createFilePath(opath, username);
195
196 // Test if the output path exist. If no try to create it
197 if (vpIoTools::checkDirectory(opath) == false) {
198 try {
199 // Create the dirname
201 } catch (...) {
202 usage(argv[0], NULL, ipath, opt_opath, username);
203 std::cerr << std::endl << "ERROR:" << std::endl;
204 std::cerr << " Cannot create " << opath << std::endl;
205 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
206 return EXIT_FAILURE;
207 }
208 }
209
210 // Compare ipath and env_ipath. If they differ, we take into account
211 // the input path comming from the command line option
212 if (!opt_ipath.empty() && !env_ipath.empty()) {
213 if (ipath != env_ipath) {
214 std::cout << std::endl << "WARNING: " << std::endl;
215 std::cout << " Since -i <visp image path=" << ipath << "> "
216 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
217 << " we skip the environment variable." << std::endl;
218 }
219 }
220
221 // Test if an input path is set
222 if (opt_ipath.empty() && env_ipath.empty()) {
223 usage(argv[0], NULL, ipath, opt_opath, username);
224 std::cerr << std::endl << "ERROR:" << std::endl;
225 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
226 << " environment variable to specify the location of the " << std::endl
227 << " image path where test images are located." << std::endl
228 << std::endl;
229 return EXIT_FAILURE;
230 }
231
232 //
233 // Here starts really the test
234 //
235
236 // Create a grey level image
238
239 // Load a grey image from the disk
240 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
241 std::cout << "Read image: " << filename << std::endl;
242 vpImageIo::read(I, filename);
243 // Write the content of the image on the disk
244 filename = vpIoTools::createFilePath(opath, "Klimt_grey.pgm");
245 std::cout << "Write image: " << filename << std::endl;
246 vpImageIo::write(I, filename);
247
248 try {
249 // Try to load a non existing image (test for exceptions)
250 // Load a non existing grey image
251 filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.pgm");
252 std::cout << "Read image: " << filename << std::endl;
253 vpImageIo::read(I, filename);
254 } catch (const vpException &e) {
255 std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
256 }
257
258 try {
259 // Try to write an image to a non existing directory
260 filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.pgm");
261 std::cout << "Write image: " << filename << std::endl;
262 vpImageIo::write(I, filename);
263 } catch (const vpException &e) {
264 std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
265 }
266 return EXIT_SUCCESS;
267 } catch (const vpException &e) {
268 std::cout << "Catch an exception: " << e << std::endl;
269 return EXIT_FAILURE;
270 }
271}
error that can be emitted by ViSP classes.
Definition vpException.h:59
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)
Definition of the vpImage class member functions.
Definition vpImage.h:135
static std::string getViSPImagesDataPath()
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static std::string createFilePath(const std::string &parent, const std::string &child)
static void makeDirectory(const std::string &dirname)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)