Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
perfImageAddSub.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  * Test image addition / subtraction.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
42 #if defined(VISP_HAVE_CATCH2)
43 #define CATCH_CONFIG_ENABLE_BENCHMARKING
44 #define CATCH_CONFIG_RUNNER
45 #include "common.hpp"
46 #include <catch.hpp>
47 #include <visp3/core/vpImageTools.h>
48 #include <visp3/core/vpIoTools.h>
49 #include <visp3/io/vpImageIo.h>
50 
51 #ifdef ENABLE_VISP_NAMESPACE
52 using namespace VISP_NAMESPACE_NAME;
53 #endif
54 TEST_CASE("Benchmark vpImageTools::imageAdd()", "[benchmark]")
55 {
56  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.pgm");
58  vpImageIo::read(I, filepath);
59 
61  common_tools::fill(I2);
62 
64  SECTION("Without saturation")
65  {
66  const bool saturation = false;
67 
68  BENCHMARK("Benchmark naive imageAdd() code without saturation")
69  {
70  common_tools::imageAddRef(I, I2, Iadd, saturation);
71  return I;
72  };
73 
74  BENCHMARK("Benchmark ViSP imageAdd() code without saturation")
75  {
76  vpImageTools::imageAdd(I, I2, Iadd, saturation);
77  return I;
78  };
79  }
80  SECTION("With saturation")
81  {
82  const bool saturation = true;
83 
84  BENCHMARK("Benchmark naive imageAdd() code with saturation")
85  {
86  common_tools::imageAddRef(I, I2, Iadd, saturation);
87  return I;
88  };
89 
90  BENCHMARK("Benchmark ViSP imageAdd() code with saturation")
91  {
92  vpImageTools::imageAdd(I, I2, Iadd, saturation);
93  return I;
94  };
95  }
96 }
97 
98 TEST_CASE("Benchmark vpImageTools::imageSubtract()", "[benchmark]")
99 {
100  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.pgm");
102  vpImageIo::read(I, filepath);
103 
105  common_tools::fill(I2);
106 
108  SECTION("Without saturation")
109  {
110  const bool saturation = false;
111 
112  BENCHMARK("Benchmark naive imageSub() code without saturation")
113  {
114  common_tools::imageSubtractRef(I, I2, Isub, saturation);
115  return I;
116  };
117 
118  BENCHMARK("Benchmark ViSP imageSub() code without saturation")
119  {
120  vpImageTools::imageSubtract(I, I2, Isub, saturation);
121  return I;
122  };
123  }
124  SECTION("With saturation")
125  {
126  const bool saturation = true;
127 
128  BENCHMARK("Benchmark naive imageSub() code with saturation")
129  {
130  common_tools::imageSubtractRef(I, I2, Isub, saturation);
131  return I;
132  };
133 
134  BENCHMARK("Benchmark ViSP imageSub() code with saturation")
135  {
136  vpImageTools::imageSubtract(I, I2, Isub, saturation);
137  return I;
138  };
139  }
140 }
141 
142 int main(int argc, char *argv[])
143 {
144  Catch::Session session; // There must be exactly one instance
145 
146  bool runBenchmark = false;
147  // Build a new parser on top of Catch's
148  using namespace Catch::clara;
149  auto cli = session.cli() // Get Catch's composite command line parser
150  | Opt(runBenchmark) // bind variable to a new option, with a hint string
151  ["--benchmark"] // the option names it will respond to
152  ("run benchmark?"); // description string for the help output
153 
154 // Now pass the new composite back to Catch so it uses that
155  session.cli(cli);
156 
157  // Let Catch (using Clara) parse the command line
158  session.applyCommandLine(argc, argv);
159 
160  if (runBenchmark) {
161  int numFailed = session.run();
162 
163  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
164  // This clamping has already been applied, so just return it here
165  // You can also do any post run clean-up here
166  return numFailed;
167  }
168 
169  return EXIT_SUCCESS;
170 }
171 #else
172 int main() { return EXIT_SUCCESS; }
173 #endif
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:147
static void imageSubtract(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Ires, bool saturate=false)
static void imageAdd(const vpImage< unsigned char > &I1, const vpImage< unsigned char > &I2, vpImage< unsigned char > &Ires, bool saturate=false)
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427