Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
imageDiskRW.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * Reading and writting images on the disk.
32 */
33
42
51
52#include <stdio.h>
53#include <stdlib.h>
54#include <visp3/core/vpConfig.h>
55#include <visp3/core/vpDebug.h>
56#include <visp3/core/vpImage.h>
57#include <visp3/core/vpIoTools.h>
58#include <visp3/io/vpImageIo.h>
59#include <visp3/io/vpParseArgv.h>
60 // List of allowed command line options
61#define GETOPTARGS "i:o:h"
62
63#ifdef ENABLE_VISP_NAMESPACE
64using namespace VISP_NAMESPACE_NAME;
65#endif
66
78void usage(const char *name, const char *badparam, const std::string &ipath, std::string opath, std::string user)
79{
80 fprintf(stdout, "\n\
81Read and write PGM images on the disk. Also test exceptions.\n\
82\n\
83SYNOPSIS\n\
84 %s [-i <input image path>] [-o <output image path>]\n\
85 [-h]\n\
86",
87name);
88
89 fprintf(stdout, "\n\
90OPTIONS: Default\n\
91 -i <input image path> %s\n\
92 Set image input path.\n\
93 From this path read \"Klimt/Klimt.pgm\"\n\
94 image.\n\
95 Setting the VISP_INPUT_IMAGE_PATH environment\n\
96 variable produces the same behaviour than using\n\
97 this option.\n\
98\n\
99 -o <output image path> %s\n\
100 Set image output path.\n\
101 From this directory, creates the \"%s\"\n\
102 subdirectory depending on the username, where \n\
103 Klimt_grey.pgm output image is written.\n\
104\n\
105 -h\n\
106 Print the help.\n\n",
107 ipath.c_str(), opath.c_str(), user.c_str());
108
109 if (badparam) {
110 fprintf(stderr, "ERROR: \n");
111 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
112 }
113}
126bool getOptions(int argc, const char **argv, std::string &ipath, std::string &opath, const 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':
134 ipath = optarg_;
135 break;
136 case 'o':
137 opath = optarg_;
138 break;
139 case 'h':
140 usage(argv[0], nullptr, ipath, opath, user);
141 return false;
142
143 default:
144 usage(argv[0], optarg_, ipath, opath, user);
145 return false;
146 }
147 }
148
149 if ((c == 1) || (c == -1)) {
150 // standalone param or error
151 usage(argv[0], nullptr, ipath, opath, user);
152 std::cerr << "ERROR: " << std::endl;
153 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
154 return false;
155 }
156
157 return true;
158}
159
160int main(int argc, const char **argv)
161{
162 try {
163 std::string env_ipath;
164 std::string opt_ipath;
165 std::string opt_opath;
166 std::string ipath;
167 std::string opath;
168 std::string filename;
169 std::string username;
170
171 std::cout << "-------------------------------------------------------" << std::endl;
172 std::cout << " imageDiskRW.cpp" << std::endl << std::endl;
173
174 std::cout << " reading and writting of PPM image" << std::endl;
175 std::cout << " read an image that does not exist" << std::endl;
176 std::cout << " write in a directory that does no exist" << std::endl;
177 std::cout << "-------------------------------------------------------" << std::endl;
178 std::cout << std::endl;
179
180 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
181 // environment variable value
183
184 // Set the default input path
185 if (!env_ipath.empty())
186 ipath = env_ipath;
187
188 // Set the default output path
189#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
190 opt_opath = "/tmp";
191#elif defined(_WIN32)
192 opt_opath = "C:\\temp";
193#endif
194
195 // Get the user login name
196 vpIoTools::getUserName(username);
197
198 // Read the command line options
199 if (getOptions(argc, argv, opt_ipath, opt_opath, username) == false) {
200 return EXIT_SUCCESS;
201 }
202
203 // Get the option values
204 if (!opt_ipath.empty())
205 ipath = opt_ipath;
206 if (!opt_opath.empty())
207 opath = opt_opath;
208
209 // Append to the output path string, the login name of the user
210 std::string dirname = vpIoTools::createFilePath(opath, username);
211
212 // Test if the output path exist. If no try to create it
213 if (vpIoTools::checkDirectory(dirname) == false) {
214 try {
215 // Create the dirname
217 }
218 catch (...) {
219 usage(argv[0], nullptr, ipath, opath, username);
220 std::cerr << std::endl << "ERROR:" << std::endl;
221 std::cerr << " Cannot create " << dirname << std::endl;
222 std::cerr << " Check your -o " << opath << " option " << std::endl;
223 return EXIT_FAILURE;
224 }
225 }
226
227 // Compare ipath and env_ipath. If they differ, we take into account
228 // the input path coming from the command line option
229 if (!opt_ipath.empty() && !env_ipath.empty()) {
230 if (ipath != env_ipath) {
231 std::cout << std::endl << "WARNING: " << std::endl;
232 std::cout << " Since -i <visp image path=" << ipath << "> "
233 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
234 << " we skip the environment variable." << std::endl;
235 }
236 }
237
238 // Test if an input path is set
239 if (opt_ipath.empty() && env_ipath.empty()) {
240 usage(argv[0], nullptr, ipath, opath, username);
241 std::cerr << std::endl << "ERROR:" << std::endl;
242 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
243 << " environment variable to specify the location of the " << std::endl
244 << " image path where test images are located." << std::endl
245 << std::endl;
246 return EXIT_SUCCESS;
247 }
248
250
251 // First we wanted to have gray level image (8bits)
252 // vpImage is a template class you can declare vpImage of ...
253 // everything...
255
256 // Although I is a gray level image you can read and write
257 // color image. Obviously the color will be translated as a gray level
258
259 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
260 vpImageIo::read(I, filename);
261
262 filename = vpIoTools::createFilePath(dirname, "IoPPM.Klimt_char.ppm");
263 vpImageIo::write(I, filename);
264
265 // test io error
266 // if the image you want to read on the disk does not exist
267 // an exception is thrown
268 // Try to load a non existing image
269 try {
270 filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
271 vpImageIo::read(I, filename);
272 }
273 catch (const vpException &e) {
274 std::cout << "Catch an expected exception: " << e << std::endl;
275 }
276
277 // same thing if you to write in a directory that does not exist
278 // or where you are not allowd to write.
279 try {
280 filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
281 vpImageIo::write(I, filename);
282 }
283 catch (const vpException &e) {
284 std::cout << "Catch an expected exception: " << e << std::endl;
285 }
286
287 std::cout << "----------------------------------------------------" << std::endl;
288
289 // Let's consider that the image is now a color image (32 bits RGBa)
290 vpImage<vpRGBa> Irgba;
291
292 // read write unsigned char ppm image.
293
294 // Load a color image from the disk
295 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
296 vpImageIo::read(Irgba, filename);
297
298 // Write the content of the color image on the disk
299 filename = vpIoTools::createFilePath(dirname, "IoPGM.Klimt_rgba.ppm");
300 vpImageIo::write(Irgba, filename);
301
302 // test io error
303 try {
304 filename = vpIoTools::createFilePath(ipath, "image-that-does-not-exist.ppm");
305 vpImageIo::read(Irgba, filename);
306 }
307 catch (const vpException &e) {
308 std::cout << "Catch an expected exception: " << e << std::endl;
309 }
310
311 // test io error
312 try {
313 filename = vpIoTools::createFilePath(dirname, "directory-that-does-not-exist/Klimt.ppm");
314 vpImageIo::write(Irgba, filename);
315 }
316 catch (const vpException &e) {
317 std::cout << "Catch an expected exception: " << e << std::endl;
318 }
319 return EXIT_SUCCESS;
320 }
321 catch (const vpException &e) {
322 std::cout << "Catch an unexpected exception: " << e << std::endl;
323 return EXIT_FAILURE;
324 }
325}
error that can be emitted by ViSP classes.
Definition vpException.h:60
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:131
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)