Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testIoPPM.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 <visp3/core/vpDebug.h>
37#include <visp3/core/vpImage.h>
38#include <visp3/core/vpIoTools.h>
39#include <visp3/io/vpImageIo.h>
40#include <visp3/io/vpParseArgv.h>
41
42#include <stdio.h>
43#include <stdlib.h>
44
52// List of allowed command line options
53#define GETOPTARGS "cdi:o:h"
54
55/*
56
57 Print the program options.
58
59 \param name : Program name.
60 \param badparam : Bad parameter name.
61 \param ipath: Input image path.
62 \param opath : Output image path.
63 \param user : Username.
64
65 */
66void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &opath,
67 const std::string &user)
68{
69 fprintf(stdout, "\n\
70Read and write PPM images on the disk. Also test exceptions.\n\
71\n\
72SYNOPSIS\n\
73 %s [-i <input image path>] [-o <output image path>]\n\
74 [-h]\n\
75",
76 name);
77
78 fprintf(stdout, "\n\
79OPTIONS: Default\n\
80 -i <input image path> %s\n\
81 Set image input path.\n\
82 From this path read \"Klimt/Klimt.pgm\"\n\
83 and \"Klimt/Klimt.ppm\" images.\n\
84 Setting the VISP_INPUT_IMAGE_PATH environment\n\
85 variable produces the same behaviour than using\n\
86 this option.\n\
87\n\
88 -o <output image path> %s\n\
89 Set image output path.\n\
90 From this directory, creates the \"%s\"\n\
91 subdirectory depending on the username, where \n\
92 Klimt_grey.ppm and Klimt_color.ppm output image\n\
93 are written.\n\
94\n\
95 -h\n\
96 Print the help.\n\n",
97 ipath.c_str(), opath.c_str(), user.c_str());
98
99 if (badparam)
100 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
101}
102
115bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const std::string &user)
116{
117 const char *optarg_;
118 int c;
119 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
120
121 switch (c) {
122 case 'i':
123 ipath = optarg_;
124 break;
125 case 'o':
126 opath = optarg_;
127 break;
128 case 'h':
129 usage(argv[0], NULL, ipath, opath, user);
130 return false;
131 break;
132
133 case 'c':
134 case 'd':
135 break;
136
137 default:
138 usage(argv[0], optarg_, ipath, opath, user);
139 return false;
140 break;
141 }
142 }
143
144 if ((c == 1) || (c == -1)) {
145 // standalone param or error
146 usage(argv[0], NULL, ipath, opath, user);
147 std::cerr << "ERROR: " << std::endl;
148 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
149 return false;
150 }
151
152 return true;
153}
154
155int main(int argc, const char **argv)
156{
157 try {
158 std::string env_ipath;
159 std::string opt_ipath;
160 std::string opt_opath;
161 std::string ipath;
162 std::string opath;
163 std::string filename;
164 std::string username;
165
166 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
167 // environment variable value
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 return EXIT_FAILURE;
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::createFilePath(opath, 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 } catch (...) {
204 usage(argv[0], NULL, ipath, opt_opath, username);
205 std::cerr << std::endl << "ERROR:" << std::endl;
206 std::cerr << " Cannot create " << opath << std::endl;
207 std::cerr << " Check your -o " << opt_opath << " option " << std::endl;
208 return EXIT_FAILURE;
209 }
210 }
211
212 // Compare ipath and env_ipath. If they differ, we take into account
213 // the input path comming from the command line option
214 if (!opt_ipath.empty() && !env_ipath.empty()) {
215 if (ipath != env_ipath) {
216 std::cout << std::endl << "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 << "ERROR:" << std::endl;
227 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
228 << " environment variable to specify the location of the " << std::endl
229 << " image path where test images are located." << std::endl
230 << std::endl;
231 return EXIT_FAILURE;
232 }
233
234 //
235 // Here starts really the test
236 //
237
239 // Create a grey level image
241
242 // Load a color image from the disk and convert it to a grey level one
243 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
244 std::cout << "Read image: " << filename << std::endl;
245 vpImageIo::read(I, filename);
246 // Write the content of the image on the disk
247 filename = vpIoTools::createFilePath(opath, "Klimt_grey.ppm");
248 std::cout << "Write image: " << filename << std::endl;
249 vpImageIo::write(I, filename);
250
251 // Try to load a non existing image (test for exceptions)
252 try {
253 // Load a non existing grey image
254 filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
255 std::cout << "Read image: " << filename << std::endl;
256 vpImageIo::read(I, filename);
257 } catch (vpImageException &e) {
258 vpERROR_TRACE("at main level");
259 std::cout << e << std::endl;
260 }
261
262 // Try to write an image to a non existing directory
263 try {
264 filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
265 std::cout << "Write image: " << filename << std::endl;
266 vpImageIo::write(I, filename);
267 } catch (const vpException &e) {
268 std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
269 }
270
272 // Create a color image
273 vpImage<vpRGBa> Irgba;
274
275 // Load a color image from the disk
276 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
277 std::cout << "Read image: " << filename << std::endl;
278 vpImageIo::read(Irgba, filename);
279 // Write the content of the color image on the disk
280 filename = vpIoTools::createFilePath(opath, "Klimt_color.ppm");
281 std::cout << "Write image: " << filename << std::endl;
282 vpImageIo::write(Irgba, filename);
283
284 try {
285 // Try to load a non existing image (test for exceptions)
286 // Load a non existing color image
287 filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
288 std::cout << "Read image: " << filename << std::endl;
289 vpImageIo::read(Irgba, filename);
290 } catch (const vpException &e) {
291 std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
292 }
293
294 try {
295 // Try to write a color image to a non existing directory
296 filename = vpIoTools::createFilePath(opath, "directory-that-does-not-exist/Klimt.ppm");
297 std::cout << "Write image: " << filename << std::endl;
298 vpImageIo::write(Irgba, filename);
299 } catch (const vpException &e) {
300 std::cout << "Catch an exception due to a non existing file: " << e << std::endl;
301 }
302 return EXIT_SUCCESS;
303 } catch (const vpException &e) {
304 std::cout << "Catch an exception: " << e << std::endl;
305 return EXIT_FAILURE;
306 }
307}
error that can be emitted by ViSP classes.
Definition vpException.h:59
Error that can be emitted by the vpImage class and its derivatives.
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)
#define vpERROR_TRACE
Definition vpDebug.h:388