Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
perfImageLoadSave.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5 *
6 * This software is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * See the file LICENSE.txt at the root directory of this source
11 * distribution for additional information about the GNU GPL.
12 *
13 * For using ViSP with software that can not be combined with the GNU
14 * GPL, please contact Inria about acquiring a ViSP Professional
15 * Edition License.
16 *
17 * See https://visp.inria.fr for more information.
18 *
19 * This software was developed at:
20 * Inria Rennes - Bretagne Atlantique
21 * Campus Universitaire de Beaulieu
22 * 35042 Rennes Cedex
23 * France
24 *
25 * If you have questions regarding the use of this file, please contact
26 * Inria at visp@inria.fr
27 *
28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30 *
31 * Description:
32 * Benchmark color image conversion.
33 *
34*****************************************************************************/
35
36#include <visp3/core/vpConfig.h>
37
38#ifdef VISP_HAVE_CATCH2
39#define CATCH_CONFIG_ENABLE_BENCHMARKING
40#define CATCH_CONFIG_RUNNER
41#include <catch.hpp>
42
43#include <thread>
44#include <visp3/core/vpIoTools.h>
45#include <visp3/io/vpImageIo.h>
46
47static std::string ipath = vpIoTools::getViSPImagesDataPath();
48static std::vector<std::string> paths{
49 ipath + "/Solvay/Solvay_conference_1927_Version2_640x440",
50 ipath + "/Solvay/Solvay_conference_1927_Version2_1024x705",
51 ipath + "/Solvay/Solvay_conference_1927_Version2_1280x881",
52 ipath + "/Solvay/Solvay_conference_1927_Version2_2126x1463",
53};
54static std::vector<std::string> names{"Solvay (640x440)", "Solvay (1024x705)", "Solvay (1280x881)",
55 "Solvay (2126x1463)"};
56static std::vector<vpImageIo::vpImageIoBackendType> backends
57{
58#if defined(VISP_HAVE_JPEG) && defined(VISP_HAVE_PNG)
60#endif
61#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
63#endif
65};
66static std::vector<std::string> backendNamesJpeg
67{
68#if defined(VISP_HAVE_JPEG)
69 "libjpeg",
70#endif
71#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
72 "OpenCV",
73#endif
74 "simd", "stb"
75};
76static std::vector<std::string> backendNamesPng
77{
78#if defined(VISP_HAVE_PNG)
79 "libpng",
80#endif
81#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
82 "OpenCV",
83#endif
84 "simd", "stb"
85};
86static int nThreads = 0;
87
88TEST_CASE("Benchmark grayscale JPEG image loading", "[benchmark]")
89{
90 for (size_t i = 0; i < paths.size(); i++) {
91 SECTION(names[i])
92 {
93 for (size_t j = 0; j < backends.size(); j++) {
95
96 BENCHMARK(backendNamesJpeg[j] + " backend")
97 {
98 vpImageIo::read(I, paths[i] + ".jpg", backends[j]);
99 return I;
100 };
101 }
102 }
103 }
104}
105
106TEST_CASE("Benchmark RGBA JPEG image loading", "[benchmark]")
107{
108 for (size_t i = 0; i < paths.size(); i++) {
109 SECTION(names[i])
110 {
111 for (size_t j = 0; j < backends.size(); j++) {
113
114 BENCHMARK(backendNamesJpeg[j] + " backend")
115 {
116 vpImageIo::read(I, paths[i] + ".jpg", backends[j]);
117 return I;
118 };
119 }
120 }
121 }
122}
123
124TEST_CASE("Benchmark grayscale PNG image loading", "[benchmark]")
125{
126 for (size_t i = 0; i < paths.size(); i++) {
127 SECTION(names[i])
128 {
129 for (size_t j = 0; j < backends.size(); j++) {
131
132 BENCHMARK(backendNamesPng[j] + " backend")
133 {
134 vpImageIo::read(I, paths[i] + ".png", backends[j]);
135 return I;
136 };
137 }
138 }
139 }
140}
141
142TEST_CASE("Benchmark RGBA PNG image loading", "[benchmark]")
143{
144 for (size_t i = 0; i < paths.size(); i++) {
145 SECTION(names[i])
146 {
147 for (size_t j = 0; j < backends.size(); j++) {
149
150 BENCHMARK(backendNamesPng[j] + " backend")
151 {
152 vpImageIo::read(I, paths[i] + ".png", backends[j]);
153 return I;
154 };
155 }
156 }
157 }
158}
159
160TEST_CASE("Benchmark grayscale JPEG image saving", "[benchmark]")
161{
163 std::string directory_filename_tmp =
164 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
165 vpIoTools::makeDirectory(directory_filename_tmp);
166 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
167
168 for (size_t i = 0; i < paths.size(); i++) {
170 vpImageIo::read(I, paths[i] + ".png");
171
172 SECTION(names[i])
173 {
174 for (size_t j = 0; j < backends.size(); j++) {
175 BENCHMARK(backendNamesJpeg[j] + " backend")
176 {
177 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg", backends[j]);
178 return I;
179 };
180 }
181 }
182 }
183
184 REQUIRE(vpIoTools::remove(directory_filename_tmp));
185}
186
187TEST_CASE("Benchmark RGBA JPEG image saving", "[benchmark]")
188{
190 std::string directory_filename_tmp =
191 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
192 vpIoTools::makeDirectory(directory_filename_tmp);
193 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
194
195 for (size_t i = 0; i < paths.size(); i++) {
197 vpImageIo::read(I, paths[i] + ".png");
198
199 SECTION(names[i])
200 {
201 for (size_t j = 0; j < backends.size(); j++) {
202 BENCHMARK(backendNamesJpeg[j] + " backend")
203 {
204 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg", backends[j]);
205 return I;
206 };
207 }
208 }
209 }
210
211 REQUIRE(vpIoTools::remove(directory_filename_tmp));
212}
213
214TEST_CASE("Benchmark grayscale PNG image saving", "[benchmark]")
215{
217 std::string directory_filename_tmp =
218 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
219 vpIoTools::makeDirectory(directory_filename_tmp);
220 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
221
222 for (size_t i = 0; i < paths.size(); i++) {
224 vpImageIo::read(I, paths[i] + ".png");
225
226 SECTION(names[i])
227 {
228 for (size_t j = 0; j < backends.size(); j++) {
229 BENCHMARK(backendNamesPng[j] + " backend")
230 {
231 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.png", backends[j]);
232 return I;
233 };
234 }
235 }
236 }
237
238 REQUIRE(vpIoTools::remove(directory_filename_tmp));
239}
240
241TEST_CASE("Benchmark RGBA PNG image saving", "[benchmark]")
242{
244 std::string directory_filename_tmp =
245 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
246 vpIoTools::makeDirectory(directory_filename_tmp);
247 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
248
249 for (size_t i = 0; i < paths.size(); i++) {
251 vpImageIo::read(I, paths[i] + ".png");
252
253 SECTION(names[i])
254 {
255 for (size_t j = 0; j < backends.size(); j++) {
256 BENCHMARK(backendNamesPng[j] + " backend")
257 {
258 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.png", backends[j]);
259 return I;
260 };
261 }
262 }
263 }
264
265 REQUIRE(vpIoTools::remove(directory_filename_tmp));
266}
267
268int main(int argc, char *argv[])
269{
270 Catch::Session session; // There must be exactly one instance
271
272 bool runBenchmark = false;
273 // Build a new parser on top of Catch's
274 using namespace Catch::clara;
275 auto cli = session.cli() // Get Catch's composite command line parser
276 | Opt(runBenchmark) // bind variable to a new option, with a hint string
277 ["--benchmark"] // the option names it will respond to
278 ("Run benchmark") |
279 Opt(nThreads, "nThreads")["--nThreads"]("Number of threads");
280
281 // Now pass the new composite back to Catch so it uses that
282 session.cli(cli);
283
284 // Let Catch (using Clara) parse the command line
285 session.applyCommandLine(argc, argv);
286
287 if (runBenchmark) {
288 std::cout << "nThreads: " << nThreads << " / available threads: " << std::thread::hardware_concurrency()
289 << std::endl;
290
291 int numFailed = session.run();
292
293 // numFailed is clamped to 255 as some unices only use the lower 8 bits.
294 // This clamping has already been applied, so just return it here
295 // You can also do any post run clean-up here
296 return numFailed;
297 }
298
299 return EXIT_SUCCESS;
300}
301#else
302#include <iostream>
303
304int main() { return EXIT_SUCCESS; }
305#endif
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
Definition vpImageIo.h:126
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
Definition vpImageIo.h:125
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg.
Definition vpImageIo.h:123
@ IO_OPENCV_BACKEND
Use OpenCV.
Definition vpImageIo.h:124
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:135
static std::string getViSPImagesDataPath()
static std::string getTempPath()
static bool checkDirectory(const std::string &dirname)
static void makeDirectory(const std::string &dirname)
static bool remove(const std::string &filename)
static std::string makeTempDirectory(const std::string &dirname)
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")