Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
perfImageLoadSave.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 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 * Benchmark color image conversion.
32 */
33
37
38#include <visp3/core/vpConfig.h>
39
40#if defined(VISP_HAVE_CATCH2) && defined(VISP_HAVE_THREADS)
41
42#include <catch_amalgamated.hpp>
43
44#include <thread>
45#include <visp3/core/vpIoTools.h>
46#include <visp3/io/vpImageIo.h>
47
48#ifdef ENABLE_VISP_NAMESPACE
49using namespace VISP_NAMESPACE_NAME;
50#endif
51
52VP_ATTRIBUTE_NO_DESTROY static std::string ipath = vpIoTools::getViSPImagesDataPath();
53VP_ATTRIBUTE_NO_DESTROY static std::vector<std::string> paths {
54 ipath + "/Solvay/Solvay_conference_1927_Version2_640x440",
55 ipath + "/Solvay/Solvay_conference_1927_Version2_1024x705",
56 ipath + "/Solvay/Solvay_conference_1927_Version2_1280x881",
57 ipath + "/Solvay/Solvay_conference_1927_Version2_2126x1463",
58};
59VP_ATTRIBUTE_NO_DESTROY static std::vector<std::string> names { "Solvay (640x440)", "Solvay (1024x705)", "Solvay (1280x881)",
60 "Solvay (2126x1463)" };
61VP_ATTRIBUTE_NO_DESTROY static std::vector<vpImageIo::vpImageIoBackendType> backends
62{
63#if defined(VISP_HAVE_JPEG) && defined(VISP_HAVE_PNG)
65#endif
66#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
68#endif
69#if defined(VISP_HAVE_SIMDLIB)
71#endif
73};
74VP_ATTRIBUTE_NO_DESTROY static std::vector<std::string> backendNamesJpeg
75{
76#if defined(VISP_HAVE_JPEG)
77 "libjpeg",
78#endif
79#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
80 "OpenCV",
81#endif
82 "simd", "stb"
83};
84VP_ATTRIBUTE_NO_DESTROY static std::vector<std::string> backendNamesPng
85{
86#if defined(VISP_HAVE_PNG)
87 "libpng",
88#endif
89#if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGCODECS)
90 "OpenCV",
91#endif
92 "simd", "stb"
93};
94static int nThreads = 0;
95
96TEST_CASE("Benchmark grayscale JPEG image loading", "[benchmark]")
97{
98 for (size_t i = 0; i < paths.size(); i++) {
99 SECTION(names[i])
100 {
101 for (size_t j = 0; j < backends.size(); j++) {
103
104 BENCHMARK(backendNamesJpeg[j] + " backend")
105 {
106 vpImageIo::read(I, paths[i] + ".jpg", backends[j]);
107 return I;
108 };
109 }
110 }
111 }
112}
113
114TEST_CASE("Benchmark RGBA JPEG image loading", "[benchmark]")
115{
116 for (size_t i = 0; i < paths.size(); i++) {
117 SECTION(names[i])
118 {
119 for (size_t j = 0; j < backends.size(); j++) {
121
122 BENCHMARK(backendNamesJpeg[j] + " backend")
123 {
124 vpImageIo::read(I, paths[i] + ".jpg", backends[j]);
125 return I;
126 };
127 }
128 }
129 }
130}
131
132TEST_CASE("Benchmark grayscale PNG image loading", "[benchmark]")
133{
134 for (size_t i = 0; i < paths.size(); i++) {
135 SECTION(names[i])
136 {
137 for (size_t j = 0; j < backends.size(); j++) {
139
140 BENCHMARK(backendNamesPng[j] + " backend")
141 {
142 vpImageIo::read(I, paths[i] + ".png", backends[j]);
143 return I;
144 };
145 }
146 }
147 }
148}
149
150TEST_CASE("Benchmark RGBA PNG image loading", "[benchmark]")
151{
152 for (size_t i = 0; i < paths.size(); i++) {
153 SECTION(names[i])
154 {
155 for (size_t j = 0; j < backends.size(); j++) {
157
158 BENCHMARK(backendNamesPng[j] + " backend")
159 {
160 vpImageIo::read(I, paths[i] + ".png", backends[j]);
161 return I;
162 };
163 }
164 }
165 }
166}
167
168TEST_CASE("Benchmark grayscale JPEG image saving", "[benchmark]")
169{
171 std::string directory_filename_tmp =
172 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
173 vpIoTools::makeDirectory(directory_filename_tmp);
174 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
175
176 for (size_t i = 0; i < paths.size(); i++) {
178 vpImageIo::read(I, paths[i] + ".png");
179
180 SECTION(names[i])
181 {
182 for (size_t j = 0; j < backends.size(); j++) {
183 BENCHMARK(backendNamesJpeg[j] + " backend")
184 {
185 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg", backends[j]);
186 return I;
187 };
188 }
189 }
190 }
191
192 REQUIRE(vpIoTools::remove(directory_filename_tmp));
193}
194
195TEST_CASE("Benchmark RGBA JPEG image saving", "[benchmark]")
196{
198 std::string directory_filename_tmp =
199 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
200 vpIoTools::makeDirectory(directory_filename_tmp);
201 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
202
203 for (size_t i = 0; i < paths.size(); i++) {
205 vpImageIo::read(I, paths[i] + ".png");
206
207 SECTION(names[i])
208 {
209 for (size_t j = 0; j < backends.size(); j++) {
210 BENCHMARK(backendNamesJpeg[j] + " backend")
211 {
212 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.jpg", backends[j]);
213 return I;
214 };
215 }
216 }
217 }
218
219 REQUIRE(vpIoTools::remove(directory_filename_tmp));
220}
221
222TEST_CASE("Benchmark grayscale PNG image saving", "[benchmark]")
223{
225 std::string directory_filename_tmp =
226 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
227 vpIoTools::makeDirectory(directory_filename_tmp);
228 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
229
230 for (size_t i = 0; i < paths.size(); i++) {
232 vpImageIo::read(I, paths[i] + ".png");
233
234 SECTION(names[i])
235 {
236 for (size_t j = 0; j < backends.size(); j++) {
237 BENCHMARK(backendNamesPng[j] + " backend")
238 {
239 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.png", backends[j]);
240 return I;
241 };
242 }
243 }
244 }
245
246 REQUIRE(vpIoTools::remove(directory_filename_tmp));
247}
248
249TEST_CASE("Benchmark RGBA PNG image saving", "[benchmark]")
250{
252 std::string directory_filename_tmp =
253 tmp_dir + "/vpIoTools_perfImageLoadSave_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
254 vpIoTools::makeDirectory(directory_filename_tmp);
255 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
256
257 for (size_t i = 0; i < paths.size(); i++) {
259 vpImageIo::read(I, paths[i] + ".png");
260
261 SECTION(names[i])
262 {
263 for (size_t j = 0; j < backends.size(); j++) {
264 BENCHMARK(backendNamesPng[j] + " backend")
265 {
266 vpImageIo::write(I, directory_filename_tmp + "/ViSP_tmp_perf_write.png", backends[j]);
267 return I;
268 };
269 }
270 }
271 }
272
273 REQUIRE(vpIoTools::remove(directory_filename_tmp));
274}
275
276int main(int argc, char *argv[])
277{
278 Catch::Session session; // There must be exactly one instance
279
280 bool runBenchmark = false;
281
282 auto cli = session.cli() // Get Catch's composite command line parser
283 | Catch::Clara::Opt(runBenchmark)["--benchmark"]("Run benchmark")
284 | Catch::Clara::Opt(nThreads, "nThreads")["--nThreads"]("Number of threads");
285
286 session.cli(cli);
287
288 session.applyCommandLine(argc, argv);
289
290 if (runBenchmark) {
291 std::cout << "nThreads: " << nThreads << " / available threads: " << std::thread::hardware_concurrency() << std::endl;
292
293 int numFailed = session.run();
294 return numFailed;
295 }
296
297 return EXIT_SUCCESS;
298}
299#else
300#include <iostream>
301
302int main() { return EXIT_SUCCESS; }
303#endif
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
Definition vpImageIo.h:134
@ IO_SIMDLIB_BACKEND
Use embedded simd library.
Definition vpImageIo.h:133
@ IO_SYSTEM_LIB_BACKEND
Use system libraries like libpng or libjpeg-turbo.
Definition vpImageIo.h:131
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
Definition vpImageIo.h:132
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:131
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")