Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testIoEXR.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 * Test image I/O for EXR file format.
33 *
34*****************************************************************************/
35
42#include <visp3/core/vpConfig.h>
43
44#if defined(VISP_HAVE_CATCH2) && (VISP_HAVE_DATASET_VERSION >= 0x030600)
45#define CATCH_CONFIG_RUNNER
46#include <catch.hpp>
47
48#include <iostream>
49#include <limits>
50#include <visp3/core/vpEndian.h>
51#include <visp3/core/vpIoTools.h>
52#include <visp3/io/vpImageIo.h>
53
54namespace
55{
56#ifdef VISP_LITTLE_ENDIAN
57void checkColorImages(const vpImage<vpRGBf> &I1, const vpImage<vpRGBf> &I2,
58 float epsilon = std::numeric_limits<float>::epsilon())
59{
60 for (unsigned int i = 0; i < I1.getHeight(); i++) {
61 for (unsigned int j = 0; j < I1.getWidth(); j++) {
62 REQUIRE(vpMath::equal(I1[i][j].R, I2[i][j].R, epsilon));
63 REQUIRE(vpMath::equal(I1[i][j].G, I2[i][j].G, epsilon));
64 REQUIRE(vpMath::equal(I1[i][j].B, I2[i][j].B, epsilon));
65 }
66 }
67}
68
69void checkGrayImages(const vpImage<float> &I1, const vpImage<float> &I2,
70 float epsilon = std::numeric_limits<float>::epsilon())
71{
72 for (unsigned int i = 0; i < I1.getHeight(); i++) {
73 for (unsigned int j = 0; j < I1.getWidth(); j++) {
74 REQUIRE(vpMath::equal(I1[i][j], I2[i][j], epsilon));
75 }
76 }
77}
78#endif
79} // namespace
80
81TEST_CASE("EXR image read", "[exr_image_io]")
82{
83// Disable the tests if big endian for now.
84// See: https://github.com/syoyo/tinyexr/issues/189#issuecomment-1465174904
85#ifdef VISP_LITTLE_ENDIAN
86 SECTION("Color")
87 {
88 const std::string imgPathRef = vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_LSB.pfm");
89 REQUIRE(vpIoTools::checkFilename(imgPathRef));
90
91 vpImage<vpRGBf> I_ref;
92 vpImageIo::readPFM_HDR(I_ref, imgPathRef);
93 CHECK(I_ref.getSize() > 0);
94
95 SECTION("32-bits")
96 {
97 const std::string imgPath =
98 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_32bits.exr");
99 REQUIRE(vpIoTools::checkFilename(imgPath));
100
102 vpImageIo::readEXR(I, imgPath);
103 CHECK(I.getSize() > 0);
104 checkColorImages(I_ref, I);
105 }
106
107 SECTION("16-bits")
108 {
109 const std::string imgPath =
110 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_16bits.exr");
111 REQUIRE(vpIoTools::checkFilename(imgPath));
112
114 vpImageIo::readEXR(I, imgPath);
115 CHECK(I.getSize() > 0);
116 checkColorImages(I_ref, I, 0.00097656f);
117 }
118 }
119
120 SECTION("Gray")
121 {
122 const std::string imgPathRef =
123 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_LSB.pfm");
124 REQUIRE(vpIoTools::checkFilename(imgPathRef));
125
126 vpImage<float> I_ref;
127 vpImageIo::readPFM_HDR(I_ref, imgPathRef);
128 CHECK(I_ref.getSize() > 0);
129
130 SECTION("32-bits")
131 {
132 const std::string imgPath =
133 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_32bits.exr");
134 REQUIRE(vpIoTools::checkFilename(imgPath));
135
137 vpImageIo::readEXR(I, imgPath);
138 CHECK(I.getSize() > 0);
139 checkGrayImages(I_ref, I);
140 }
141
142 SECTION("16-bits")
143 {
144 const std::string imgPath =
145 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_16bits.exr");
146 REQUIRE(vpIoTools::checkFilename(imgPath));
147
149 vpImageIo::readEXR(I, imgPath);
150 CHECK(I.getSize() > 0);
151 checkGrayImages(I_ref, I, 0.00097656f);
152 }
153 }
154#endif
155}
156
157TEST_CASE("EXR image write", "[exr_image_io]")
158{
159#ifdef VISP_LITTLE_ENDIAN
161 std::string directory_filename_tmp = tmp_dir + "/testIoEXR_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
162 vpIoTools::makeDirectory(directory_filename_tmp);
163 REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
164
165 SECTION("Color")
166 {
167 const std::string imgPath =
168 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_32bits.exr");
169 REQUIRE(vpIoTools::checkFilename(imgPath));
170
172 vpImageIo::readEXR(I, imgPath);
173 REQUIRE(I.getSize() > 0);
174
175 vpImageIo::writeEXR(I, vpIoTools::createFilePath(directory_filename_tmp, "write_color_exr.exr"));
176 vpImage<vpRGBf> I_write;
177 vpImageIo::readEXR(I_write, vpIoTools::createFilePath(directory_filename_tmp, "write_color_exr.exr"));
178
179 REQUIRE(I.getHeight() == I_write.getHeight());
180 REQUIRE(I.getWidth() == I_write.getWidth());
181
182 checkColorImages(I, I_write);
183 }
184
185 SECTION("Gray")
186 {
187 const std::string imgPath =
188 vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_32bits.exr");
189 REQUIRE(vpIoTools::checkFilename(imgPath));
190
192 vpImageIo::readEXR(I, imgPath);
193 REQUIRE(I.getSize() > 0);
194
195 vpImageIo::writeEXR(I, vpIoTools::createFilePath(directory_filename_tmp, "write_gray_exr.exr"));
196 vpImage<float> I_write;
197 vpImageIo::readEXR(I_write, vpIoTools::createFilePath(directory_filename_tmp, "write_gray_exr.exr"));
198
199 REQUIRE(I.getHeight() == I_write.getHeight());
200 REQUIRE(I.getWidth() == I_write.getWidth());
201
202 checkGrayImages(I, I_write);
203 }
204
205 CHECK(vpIoTools::remove(directory_filename_tmp));
206 CHECK(vpIoTools::remove(tmp_dir));
207#endif
208}
209
210int main(int argc, char *argv[])
211{
212 Catch::Session session; // There must be exactly one instance
213
214 // Let Catch (using Clara) parse the command line
215 session.applyCommandLine(argc, argv);
216
217 int numFailed = session.run();
218
219 // numFailed is clamped to 255 as some unices only use the lower 8 bits.
220 // This clamping has already been applied, so just return it here
221 // You can also do any post run clean-up here
222 return numFailed;
223}
224#else
225int main() { return EXIT_SUCCESS; }
226#endif
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
static void readEXR(vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
static void writeEXR(const vpImage< float > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:135
unsigned int getWidth() const
Definition vpImage.h:242
unsigned int getSize() const
Definition vpImage.h:223
unsigned int getHeight() const
Definition vpImage.h:184
static std::string getViSPImagesDataPath()
static bool checkFilename(const std::string &filename)
static std::string getTempPath()
static bool checkDirectory(const std::string &dirname)
static std::string createFilePath(const std::string &parent, const std::string &child)
static void makeDirectory(const std::string &dirname)
static bool remove(const std::string &filename)
static std::string makeTempDirectory(const std::string &dirname)
static bool equal(double x, double y, double threshold=0.001)
Definition vpMath.h:369
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")