Visual Servoing Platform  version 3.6.1 under development (2024-10-18)
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 
44 #include "common.hpp"
45 #include <catch_amalgamated.hpp>
46 #include <visp3/core/vpImageTools.h>
47 #include <visp3/core/vpIoTools.h>
48 #include <visp3/io/vpImageIo.h>
49 
50 #ifdef ENABLE_VISP_NAMESPACE
51 using namespace VISP_NAMESPACE_NAME;
52 #endif
53 TEST_CASE("Benchmark vpImageTools::imageAdd()", "[benchmark]")
54 {
55  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.pgm");
57  vpImageIo::read(I, filepath);
58 
60  common_tools::fill(I2);
61 
63  SECTION("Without saturation")
64  {
65  const bool saturation = false;
66 
67  BENCHMARK("Benchmark naive imageAdd() code without saturation")
68  {
69  common_tools::imageAddRef(I, I2, Iadd, saturation);
70  return I;
71  };
72 
73  BENCHMARK("Benchmark ViSP imageAdd() code without saturation")
74  {
75  vpImageTools::imageAdd(I, I2, Iadd, saturation);
76  return I;
77  };
78  }
79  SECTION("With saturation")
80  {
81  const bool saturation = true;
82 
83  BENCHMARK("Benchmark naive imageAdd() code with saturation")
84  {
85  common_tools::imageAddRef(I, I2, Iadd, saturation);
86  return I;
87  };
88 
89  BENCHMARK("Benchmark ViSP imageAdd() code with saturation")
90  {
91  vpImageTools::imageAdd(I, I2, Iadd, saturation);
92  return I;
93  };
94  }
95 }
96 
97 TEST_CASE("Benchmark vpImageTools::imageSubtract()", "[benchmark]")
98 {
99  const std::string filepath = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "Klimt/Klimt.pgm");
101  vpImageIo::read(I, filepath);
102 
104  common_tools::fill(I2);
105 
107  SECTION("Without saturation")
108  {
109  const bool saturation = false;
110 
111  BENCHMARK("Benchmark naive imageSub() code without saturation")
112  {
113  common_tools::imageSubtractRef(I, I2, Isub, saturation);
114  return I;
115  };
116 
117  BENCHMARK("Benchmark ViSP imageSub() code without saturation")
118  {
119  vpImageTools::imageSubtract(I, I2, Isub, saturation);
120  return I;
121  };
122  }
123  SECTION("With saturation")
124  {
125  const bool saturation = true;
126 
127  BENCHMARK("Benchmark naive imageSub() code with saturation")
128  {
129  common_tools::imageSubtractRef(I, I2, Isub, saturation);
130  return I;
131  };
132 
133  BENCHMARK("Benchmark ViSP imageSub() code with saturation")
134  {
135  vpImageTools::imageSubtract(I, I2, Isub, saturation);
136  return I;
137  };
138  }
139 }
140 
141 int main(int argc, char *argv[])
142 {
143  Catch::Session session;
144 
145  bool runBenchmark = false;
146  auto cli = session.cli()
147  | Catch::Clara::Opt(runBenchmark)["--benchmark"]("run benchmark?");
148  session.cli(cli);
149 
150  session.applyCommandLine(argc, argv);
151 
152  if (runBenchmark) {
153  int numFailed = session.run();
154 
155  return numFailed;
156  }
157 
158  return EXIT_SUCCESS;
159 }
160 #else
161 int main() { return EXIT_SUCCESS; }
162 #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