41 #include <visp3/core/vpImage.h> 42 #include <visp3/core/vpIoTools.h> 43 #include <visp3/core/vpMath.h> 44 #include <visp3/io/vpImageIo.h> 45 #include <visp3/io/vpParseArgv.h> 55 #define GETOPTARGS "cdi:o:t:h" 67 void usage(
const char *name,
const char *badparam,
const std::string &ipath,
const std::string &opath,
68 const std::string &user)
71 Test performance between methods to iterate over pixel image.\n\ 74 %s [-i <input image path>] [-o <output image path>] [-t <nb threads>]\n\ 80 -i <input image path> %s\n\ 81 Set image input path.\n\ 82 From this path read \"Klimt/Klimt.pgm\"\n\ 84 Setting the VISP_INPUT_IMAGE_PATH environment\n\ 85 variable produces the same behaviour than using\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.pgm output image is written.\n\ 95 Set the number of threads to use for the computation.\n\ 98 Print the help.\n\n", ipath.c_str(), opath.c_str(), user.c_str());
101 fprintf(stdout,
"\nERROR: Bad parameter [%s]\n", badparam);
117 bool getOptions(
int argc,
const char **argv, std::string &ipath, std::string &opath,
const std::string &user,
118 unsigned int &nbThreads)
132 nbThreads = (
unsigned int)atoi(optarg_);
135 usage(argv[0], NULL, ipath, opath, user);
144 usage(argv[0], optarg_, ipath, opath, user);
150 if ((c == 1) || (c == -1)) {
152 usage(argv[0], NULL, ipath, opath, user);
153 std::cerr <<
"ERROR: " << std::endl;
154 std::cerr <<
" Bad argument " << optarg_ << std::endl << std::endl;
169 void iterate_method1(
vpImage<vpRGBa> &I,
const double alpha,
const double beta)
172 unsigned char *ptrStart = (
unsigned char *)I.
bitmap;
173 unsigned char *ptrEnd = ptrStart + size * 4;
174 unsigned char *ptrCurrent = ptrStart;
176 while (ptrCurrent != ptrEnd) {
177 *ptrCurrent = vpMath::saturate<unsigned char>((*ptrCurrent) * alpha + beta);
193 unsigned char *ptrStart = (
unsigned char *)I.
bitmap;
194 unsigned char *ptrEnd = ptrStart + size;
195 unsigned char *ptrCurrent = ptrStart;
197 while (ptrCurrent != ptrEnd) {
198 *ptrCurrent = vpMath::saturate<unsigned char>((*ptrCurrent) * alpha + beta);
211 void iterate_method2(
vpImage<vpRGBa> &I,
const double alpha,
const double beta)
213 for (
unsigned int i = 0; i < I.
getHeight(); i++) {
214 for (
unsigned int j = 0; j < I.
getWidth(); j++) {
215 I[i][j].R = vpMath::saturate<unsigned char>(I[i][j].R * alpha + beta);
216 I[i][j].G = vpMath::saturate<unsigned char>(I[i][j].G * alpha + beta);
217 I[i][j].B = vpMath::saturate<unsigned char>(I[i][j].B * alpha + beta);
218 I[i][j].A = vpMath::saturate<unsigned char>(I[i][j].A * alpha + beta);
223 int main(
int argc,
const char **argv)
226 std::string env_ipath;
227 std::string opt_ipath;
228 std::string opt_opath;
231 std::string filename;
232 std::string username;
233 unsigned int nbThreads = 4;
240 if (!env_ipath.empty())
245 opt_opath =
"C:/temp";
254 if (getOptions(argc, argv, opt_ipath, opt_opath, username, nbThreads) ==
false) {
259 if (!opt_ipath.empty())
261 if (!opt_opath.empty())
273 usage(argv[0], NULL, ipath, opt_opath, username);
274 std::cerr << std::endl <<
"ERROR:" << std::endl;
275 std::cerr <<
" Cannot create " << opath << std::endl;
276 std::cerr <<
" Check your -o " << opt_opath <<
" option " << std::endl;
283 if (!opt_ipath.empty() && !env_ipath.empty()) {
284 if (ipath != env_ipath) {
285 std::cout << std::endl <<
"WARNING: " << std::endl;
286 std::cout <<
" Since -i <visp image path=" << ipath <<
"> " 287 <<
" is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
288 <<
" we skip the environment variable." << std::endl;
293 if (opt_ipath.empty() && env_ipath.empty()) {
294 usage(argv[0], NULL, ipath, opt_opath, username);
295 std::cerr << std::endl <<
"ERROR:" << std::endl;
296 std::cerr <<
" Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
297 <<
" environment variable to specify the location of the " << std::endl
298 <<
" image path where test images are located." << std::endl
312 std::cout <<
"\nRead image: " << filename << std::endl;
317 std::cout <<
"I=" << I_iterate1.
getWidth() <<
"x" << I_iterate1.
getHeight() << std::endl;
319 double alpha = 1.5, beta = -30.0;
320 unsigned int nbIterations = 10;
324 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
325 iterate_method1(I_iterate1, alpha, beta);
328 std::cout <<
"t_iterate1=" << t_iterate1 <<
" ms ; t_iterate1/" << nbIterations <<
"=" 329 << (t_iterate1 / nbIterations) <<
" ms" << std::endl;
336 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
337 iterate_method2(I_iterate2, alpha, beta);
340 std::cout <<
"t_iterate2=" << t_iterate2 <<
" ms ; t_iterate2/" << nbIterations <<
"=" 341 << (t_iterate2 / nbIterations) <<
" ms" << std::endl;
348 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
351 for (
unsigned int i = 0; i < 256; i++) {
352 lut[i].
R = vpMath::saturate<unsigned char>(alpha * i + beta);
353 lut[i].
G = vpMath::saturate<unsigned char>(alpha * i + beta);
354 lut[i].
B = vpMath::saturate<unsigned char>(alpha * i + beta);
355 lut[i].
A = vpMath::saturate<unsigned char>(alpha * i + beta);
361 std::cout <<
"t_lut=" << t_lut <<
" ms ; t_lut/" << nbIterations <<
"=" << (t_lut / nbIterations) <<
" ms" 369 for (
unsigned int i = 0; i < I_iterate1.
getHeight() && same; i++) {
370 for (
unsigned int j = 0; j < I_iterate1.
getWidth() && same; j++) {
371 if (I_iterate1[i][j] != I_iterate2[i][j] || I_iterate1[i][j] != I_lut[i][j]) {
378 std::cerr <<
"Color images are different!" << std::endl;
387 std::cout <<
"\nRead image: " << filename << std::endl;
391 std::cout <<
"I_grayscale=" << I_lut_grayscale.
getWidth() <<
"x" << I_lut_grayscale.
getHeight() << std::endl;
395 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
396 iterate_method1(I_iterate_grayscale1, alpha, beta);
399 std::cout <<
"t_iterate_grayscale1=" << t_iterate_grayscale1 <<
" ms ; t_iterate1/" << nbIterations <<
"=" 400 << (t_iterate_grayscale1 / nbIterations) <<
" ms" << std::endl;
407 for (
unsigned int cpt = 0; cpt < nbIterations; cpt++) {
409 unsigned char lut[256];
410 for (
unsigned int i = 0; i < 256; i++) {
411 lut[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
417 std::cout <<
"t_lut_grayscale=" << t_lut_grayscale <<
" ms ; t_lut_grayscale/" << nbIterations <<
"=" 418 << (t_lut_grayscale / nbIterations) <<
" ms" << std::endl;
425 for (
unsigned int i = 0; i < I_lut_grayscale.
getHeight() && same; i++) {
426 for (
unsigned int j = 0; j < I_lut_grayscale.
getWidth() && same; j++) {
427 if (I_lut_grayscale[i][j] != I_iterate_grayscale1[i][j]) {
434 std::cerr <<
"Grayscale images are different!" << std::endl;
442 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
445 for (
unsigned int i = 0; i < 256; i++) {
446 lut[i].
R = vpMath::saturate<unsigned char>(alpha * i + beta);
447 lut[i].
G = vpMath::saturate<unsigned char>(alpha * i + beta);
448 lut[i].
B = vpMath::saturate<unsigned char>(alpha * i + beta);
449 lut[i].
A = vpMath::saturate<unsigned char>(alpha * i + beta);
459 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
462 for (
unsigned int i = 0; i < 256; i++) {
463 lut[i].
R = vpMath::saturate<unsigned char>(alpha * i + beta);
464 lut[i].
G = vpMath::saturate<unsigned char>(alpha * i + beta);
465 lut[i].
B = vpMath::saturate<unsigned char>(alpha * i + beta);
466 lut[i].
A = vpMath::saturate<unsigned char>(alpha * i + beta);
473 std::cout <<
"\nt_lut_singlethread/t_lut_multithread (color)=" << t_lut_singlethread / t_lut_multithread <<
"X" 480 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
482 unsigned char lut[256];
483 for (
unsigned int i = 0; i < 256; i++) {
484 lut[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
494 for (
unsigned int cpt = 0; cpt < nbIterations * 10; cpt++) {
496 unsigned char lut[256];
497 for (
unsigned int i = 0; i < 256; i++) {
498 lut[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
505 std::cout <<
"\nt_lut_singlethread/t_lut_multithread (grayscale)=" << t_lut_singlethread / t_lut_multithread <<
"X" 511 unsigned char lut_grayscale[256];
512 for (
unsigned int i = 0; i < 256; i++) {
513 lut_grayscale[i] = vpMath::saturate<unsigned char>(alpha * i + beta);
515 I_test_grayscale.performLut(lut_grayscale, nbThreads);
520 for (
unsigned int i = 0; i < 256; i++) {
521 lut_color[i].
R = vpMath::saturate<unsigned char>(alpha * i + beta);
522 lut_color[i].
G = vpMath::saturate<unsigned char>(alpha * i + beta);
523 lut_color[i].
B = vpMath::saturate<unsigned char>(alpha * i + beta);
524 lut_color[i].
A = vpMath::saturate<unsigned char>(alpha * i + beta);
526 I_test_color.performLut(lut_color, nbThreads);
530 std::cerr <<
"Catch an exception: " << e.
what() << std::endl;
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.
VISP_EXPORT double measureTimeMs()
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
static void write(const vpImage< unsigned char > &I, const std::string &filename)
const char * what() const
unsigned char A
Additionnal component.
static void read(vpImage< unsigned char > &I, const std::string &filename)
unsigned char R
Red component.
unsigned int getHeight() const
void performLut(const Type(&lut)[256], const unsigned int nbThreads=1)