Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
testReadImage.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2024 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 * Read images on the disk.
32 */
33
34#include <visp3/core/vpDebug.h>
35#include <visp3/core/vpImage.h>
36#include <visp3/core/vpIoTools.h>
37#include <visp3/io/vpImageIo.h>
38#include <visp3/io/vpParseArgv.h>
39
40#include <stdlib.h>
41
48
49#ifdef ENABLE_VISP_NAMESPACE
50using namespace VISP_NAMESPACE_NAME;
51#endif
52
53// List of allowed command line options
54#define GETOPTARGS "cdi:p:h"
55
56void usage(const char *name, const char *badparam, std::string ipath);
57bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath);
58
59/*
60
61 Print the program options.
62
63 \param name : Program name.
64 \param badparam : Bad parameter name.
65 \param ipath : Input image path.
66
67 */
68void usage(const char *name, const char *badparam, std::string ipath)
69{
70 fprintf(stdout, "\n\
71Read images on the disk.\n\
72\n\
73SYNOPSIS\n\
74 %s [-i <input image path>] [-p <personal image path>]\n\
75 [-h]\n\
76",
77name);
78
79 fprintf(stdout, "\n\
80OPTIONS: Default\n\
81 -i <input image path> %s\n\
82 Set image input path.\n\
83 From this path read \"Klimt/Klimt.pgm,\n\
84 .ppm, .jpeg and .png images.\n\
85 Setting the VISP_INPUT_IMAGE_PATH environment\n\
86 variable produces the same behaviour than using\n\
87 this option.\n\
88\n\
89 -p <personal image path> \n\
90 Path to an image used to test image reading function.\n\
91 Example: -p /my_path_to/image.png\n\
92\n\
93 -h\n\
94 Print the help.\n\n",
95 ipath.c_str());
96
97 if (badparam)
98 fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
99}
100
112bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath)
113{
114 const char *optarg_;
115 int c;
116 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
117
118 switch (c) {
119 case 'i':
120 ipath = optarg_;
121 break;
122 case 'p':
123 ppath = optarg_;
124 break;
125 case 'h':
126 usage(argv[0], nullptr, ipath);
127 return false;
128
129 case 'c':
130 case 'd':
131 break;
132
133 default:
134 usage(argv[0], optarg_, ipath);
135 return false;
136 }
137 }
138
139 if ((c == 1) || (c == -1)) {
140 // standalone param or error
141 usage(argv[0], nullptr, ipath);
142 std::cerr << "ERROR: " << std::endl;
143 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
144 return false;
145 }
146
147 return true;
148}
149
150int main(int argc, const char **argv)
151{
152 try {
153 std::string env_ipath;
154 std::string opt_ipath;
155 std::string opt_ppath;
156 std::string ipath;
157 std::string filename;
158
159 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
160 // environment variable value
162
163 // Set the default input path
164 if (!env_ipath.empty())
165 ipath = env_ipath;
166
167 // Read the command line options
168 if (getOptions(argc, argv, opt_ipath, opt_ppath) == false) {
169 return EXIT_FAILURE;
170 }
171
172 // Get the option values
173 if (!opt_ipath.empty())
174 ipath = opt_ipath;
175
176 // Compare ipath and env_ipath. If they differ, we take into account
177 // the input path coming from the command line option
178 if (!opt_ipath.empty() && !env_ipath.empty()) {
179 if (ipath != env_ipath) {
180 std::cout << std::endl << "WARNING: " << std::endl;
181 std::cout << " Since -i <visp image path=" << ipath << "> "
182 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
183 << " we skip the environment variable." << std::endl;
184 }
185 }
186
187 //
188 // Here starts really the test
189 //
190
192 // Create a grey level image
193 // vpImage<vpRGBa> I;
195 vpImage<vpRGBa> Irgb;
196
197 if (opt_ppath.empty()) {
198 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
199 vpImageIo::read(I, filename);
200 printf("Read ppm ok\n");
201 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
202 vpImageIo::read(I, filename);
203 printf("Read pgm ok\n");
204 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.jpeg");
205 vpImageIo::read(I, filename);
206 printf("Read jpeg ok\n");
207 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.png");
208 vpImageIo::read(I, filename);
209 printf("Read png ok\n");
210
211 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.ppm");
212 vpImageIo::read(Irgb, filename);
213 printf("Read ppm ok\n");
214 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.pgm");
215 vpImageIo::read(Irgb, filename);
216 printf("Read pgm ok\n");
217 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.jpeg");
218 vpImageIo::read(Irgb, filename);
219 printf("Read jpeg ok\n");
220 filename = vpIoTools::createFilePath(ipath, "Klimt/Klimt.png");
221 vpImageIo::read(Irgb, filename);
222 printf("Read png ok\n");
223 }
224 else {
225 filename = opt_ppath;
226 vpImageIo::read(I, filename);
227 printf("Image \"%s\" read successfully\n", filename.c_str());
228 }
229 }
230 catch (const vpException &e) {
231 std::cout << "Catch an exception: " << e.getMessage() << std::endl;
232 return EXIT_FAILURE;
233 }
234 return EXIT_SUCCESS;
235}
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)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)