38 #include <visp3/core/vpImage.h>
39 #include <visp3/io/vpImageIo.h>
40 #include <visp3/io/vpParseArgv.h>
41 #include <visp3/core/vpIoTools.h>
42 #include <visp3/core/vpMath.h>
54 #define GETOPTARGS "cdi:o:h"
56 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user);
57 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user);
70 void usage(
const char *name,
const char *badparam, std::string ipath, std::string opath, std::string user)
73 Test performance between methods to iterate over pixel image.\n\
76 %s [-i <input image path>] [-o <output image path>]\n\
82 -i <input image path> %s\n\
83 Set image input path.\n\
84 From this path read \"ViSP-images/Klimt/Klimt.pgm\"\n\
86 Setting the VISP_INPUT_IMAGE_PATH environment\n\
87 variable produces the same behaviour than using\n\
90 -o <output image path> %s\n\
91 Set image output path.\n\
92 From this directory, creates the \"%s\"\n\
93 subdirectory depending on the username, where \n\
94 Klimt_grey.pgm output image is written.\n\
98 ipath.c_str(), opath.c_str(), user.c_str());
101 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
116 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath, std::string user)
123 case 'i': ipath = optarg_;
break;
124 case 'o': opath = optarg_;
break;
125 case 'h': usage(argv[0], NULL, ipath, opath, user);
return false;
break;
132 usage(argv[0], optarg_, ipath, opath, user);
return false;
break;
136 if ((c == 1) || (c == -1)) {
138 usage(argv[0], NULL, ipath, opath, user);
139 std::cerr <<
"ERROR: " << std::endl;
140 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
155 void iterate_method1(
vpImage<vpRGBa> &I,
const double alpha,
const double beta) {
157 unsigned char *ptrStart = (
unsigned char*) I.
bitmap;
158 unsigned char *ptrEnd = ptrStart + size*4;
159 unsigned char *ptrCurrent = ptrStart;
161 while(ptrCurrent != ptrEnd) {
162 *ptrCurrent = vpMath::saturate<unsigned char>((*ptrCurrent) * alpha + beta);
175 void iterate_method2(
vpImage<vpRGBa> &I,
const double alpha,
const double beta) {
176 for(
unsigned int i = 0; i < I.
getHeight(); i++) {
177 for(
unsigned int j = 0; j < I.
getWidth(); j++) {
178 I[i][j].R = vpMath::saturate<unsigned char>(I[i][j].R * alpha + beta);
179 I[i][j].G = vpMath::saturate<unsigned char>(I[i][j].G * alpha + beta);
180 I[i][j].B = vpMath::saturate<unsigned char>(I[i][j].B * alpha + beta);
181 I[i][j].A = vpMath::saturate<unsigned char>(I[i][j].A * alpha + beta);
197 main(
int argc,
const char ** argv)
200 std::string env_ipath;
201 std::string opt_ipath;
202 std::string opt_opath;
205 std::string filename;
206 std::string username;
212 if (! env_ipath.empty())
217 opt_opath =
"C:/temp";
226 if (getOptions(argc, argv, opt_ipath, opt_opath, username) ==
false) {
231 if (!opt_ipath.empty())
233 if (!opt_opath.empty())
246 usage(argv[0], NULL, ipath, opt_opath, username);
247 std::cerr << std::endl
248 <<
"ERROR:" << std::endl;
249 std::cerr <<
" Cannot create " << opath << std::endl;
250 std::cerr <<
" Check your -o " << opt_opath <<
" option " << std::endl;
257 if (!opt_ipath.empty() && !env_ipath.empty()) {
258 if (ipath != env_ipath) {
259 std::cout << std::endl
260 <<
"WARNING: " << std::endl;
261 std::cout <<
" Since -i <visp image path=" << ipath <<
"> "
262 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
263 <<
" we skip the environment variable." << std::endl;
268 if (opt_ipath.empty() && env_ipath.empty()){
269 usage(argv[0], NULL, ipath, opt_opath, username);
270 std::cerr << std::endl
271 <<
"ERROR:" << std::endl;
272 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH "
274 <<
" environment variable to specify the location of the " << std::endl
275 <<
" image path where test images are located." << std::endl << std::endl;
289 std::cout <<
"Read image: " << filename << std::endl;
294 std::cout <<
"I=" << I_iterate1.
getWidth() <<
"x" << I_iterate1.
getHeight() << std::endl;
296 double alpha = 1.5, beta = -30.0;
297 unsigned int nbIterations = 10;
301 for(
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
302 iterate_method1(I_iterate1, alpha, beta);
305 std::cout <<
"t_iterate1=" << t_iterate1 <<
" ms ; t_iterate1/" << nbIterations <<
"="
306 << (t_iterate1/nbIterations) <<
" ms" << std::endl;
314 for(
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
315 iterate_method2(I_iterate2, alpha, beta);
318 std::cout <<
"t_iterate2=" << t_iterate2 <<
" ms ; t_iterate2/" << nbIterations <<
"="
319 << (t_iterate2/nbIterations) <<
" ms" << std::endl;
327 for(
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
330 for(
unsigned int i = 0; i < 256; i++) {
331 lut[i].
R = vpMath::saturate<unsigned char>(alpha * i + beta);
332 lut[i].
G = vpMath::saturate<unsigned char>(alpha * i + beta);
333 lut[i].
B = vpMath::saturate<unsigned char>(alpha * i + beta);
334 lut[i].
A = vpMath::saturate<unsigned char>(alpha * i + beta);
337 lut_method(I_lut, lut);
340 std::cout <<
"t_lut=" << t_lut <<
" ms ; t_lut/" << nbIterations <<
"="
341 << (t_lut/nbIterations) <<
" ms" << std::endl;
348 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
static void write(const vpImage< unsigned char > &I, const char *filename)
unsigned int getWidth() const
unsigned char B
Blue component.
Type * bitmap
points toward the bitmap
error that can be emited by ViSP classes.
unsigned char G
Green component.
void performLut(const Type(&lut)[256])
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Class that defines a RGB 32 bits structure.
const char * what() const
unsigned char A
Additionnal component.
VISP_EXPORT double measureTimeMs()
unsigned char R
Red component.
unsigned int getHeight() const
static void read(vpImage< unsigned char > &I, const char *filename)