Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
testIoPFM.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 I/O for PFM file format.
32  */
33 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_CATCH2) && (VISP_HAVE_DATASET_VERSION >= 0x030600)
43 #define CATCH_CONFIG_RUNNER
44 #include <catch.hpp>
45 
46 #include <iostream>
47 #include <limits>
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 
55 namespace
56 {
57 void checkColorImages(const vpImage<vpRGBf> &I1, const vpImage<vpRGBf> &I2)
58 {
59  for (unsigned int i = 0; i < I1.getHeight(); i++) {
60  for (unsigned int j = 0; j < I1.getWidth(); j++) {
61  REQUIRE(vpMath::equal(I1[i][j].R, I2[i][j].R, std::numeric_limits<float>::epsilon()));
62  REQUIRE(vpMath::equal(I1[i][j].G, I2[i][j].G, std::numeric_limits<float>::epsilon()));
63  REQUIRE(vpMath::equal(I1[i][j].B, I2[i][j].B, std::numeric_limits<float>::epsilon()));
64  }
65  }
66 }
67 
68 void checkGrayImages(const vpImage<float> &I1, const vpImage<float> &I2)
69 {
70  for (unsigned int i = 0; i < I1.getHeight(); i++) {
71  for (unsigned int j = 0; j < I1.getWidth(); j++) {
72  REQUIRE(vpMath::equal(I1[i][j], I2[i][j], std::numeric_limits<float>::epsilon()));
73  }
74  }
75 }
76 } // namespace
77 
78 TEST_CASE("HDR PFM image read", "[hdr_pfm_image_io]")
79 {
80  SECTION("Little-endian (LSB)")
81  {
82  SECTION("Color")
83  {
84  const std::string imgPath =
85  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_LSB.pfm");
86  REQUIRE(vpIoTools::checkFilename(imgPath));
87 
89  vpImageIo::readPFM_HDR(I, imgPath);
90  CHECK(I.getSize() > 0);
91  }
92  SECTION("Gray")
93  {
94  const std::string imgPath =
95  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_LSB.pfm");
96  REQUIRE(vpIoTools::checkFilename(imgPath));
97 
99  vpImageIo::readPFM_HDR(I, imgPath);
100  CHECK(I.getSize() > 0);
101  }
102  }
103 
104  SECTION("Big-endian (MSB)")
105  {
106  SECTION("Color")
107  {
108  const std::string imgPath =
109  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_MSB.pfm");
110  REQUIRE(vpIoTools::checkFilename(imgPath));
111 
112  vpImage<vpRGBf> I;
113  vpImageIo::readPFM_HDR(I, imgPath);
114  CHECK(I.getSize() > 0);
115  }
116  SECTION("Gray")
117  {
118  const std::string imgPath =
119  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_MSB.pfm");
120  REQUIRE(vpIoTools::checkFilename(imgPath));
121 
122  vpImage<float> I;
123  vpImageIo::readPFM_HDR(I, imgPath);
124  CHECK(I.getSize() > 0);
125  }
126  }
127 
128  SECTION("Endianness")
129  {
130  SECTION("Color")
131  {
132  const std::string imgPathLSB =
133  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_LSB.pfm");
134  const std::string imgPathMSB =
135  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_MSB.pfm");
136  REQUIRE(vpIoTools::checkFilename(imgPathLSB));
137  REQUIRE(vpIoTools::checkFilename(imgPathMSB));
138 
139  vpImage<vpRGBf> I_LSB;
140  vpImageIo::readPFM_HDR(I_LSB, imgPathLSB);
141  REQUIRE(I_LSB.getSize() > 0);
142 
143  vpImage<vpRGBf> I_MSB;
144  vpImageIo::readPFM_HDR(I_MSB, imgPathMSB);
145  REQUIRE(I_MSB.getSize() > 0);
146 
147  REQUIRE(I_LSB.getHeight() == I_MSB.getHeight());
148  REQUIRE(I_LSB.getWidth() == I_MSB.getWidth());
149 
150  checkColorImages(I_LSB, I_MSB);
151  }
152  SECTION("Gray")
153  {
154  const std::string imgPathLSB =
155  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_LSB.pfm");
156  const std::string imgPathMSB =
157  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_MSB.pfm");
158  REQUIRE(vpIoTools::checkFilename(imgPathLSB));
159  REQUIRE(vpIoTools::checkFilename(imgPathMSB));
160 
161  vpImage<float> I_LSB;
162  vpImageIo::readPFM_HDR(I_LSB, imgPathLSB);
163  REQUIRE(I_LSB.getSize() > 0);
164 
165  vpImage<float> I_MSB;
166  vpImageIo::readPFM_HDR(I_MSB, imgPathMSB);
167  REQUIRE(I_MSB.getSize() > 0);
168 
169  REQUIRE(I_LSB.getHeight() == I_MSB.getHeight());
170  REQUIRE(I_LSB.getWidth() == I_MSB.getWidth());
171 
172  checkGrayImages(I_LSB, I_MSB);
173  }
174  }
175 }
176 
177 TEST_CASE("HDR PFM image write", "[hdr_pfm_image_io]")
178 {
179  std::string tmp_dir = vpIoTools::makeTempDirectory(vpIoTools::getTempPath());
180  std::string directory_filename_tmp = tmp_dir + "/testIoPFM_" + vpTime::getDateTime("%Y-%m-%d_%H.%M.%S");
181  vpIoTools::makeDirectory(directory_filename_tmp);
182  REQUIRE(vpIoTools::checkDirectory(directory_filename_tmp));
183 
184  SECTION("Color")
185  {
186  const std::string imgPath =
187  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_color_LSB.pfm");
188  REQUIRE(vpIoTools::checkFilename(imgPath));
189 
190  vpImage<vpRGBf> I;
191  vpImageIo::readPFM_HDR(I, imgPath);
192  REQUIRE(I.getSize() > 0);
193 
194  vpImageIo::writePFM_HDR(I, vpIoTools::createFilePath(directory_filename_tmp, "write_color_pfm.pfm"));
195  vpImage<vpRGBf> I_write;
196  vpImageIo::readPFM_HDR(I_write, vpIoTools::createFilePath(directory_filename_tmp, "write_color_pfm.pfm"));
197 
198  REQUIRE(I.getHeight() == I_write.getHeight());
199  REQUIRE(I.getWidth() == I_write.getWidth());
200 
201  checkColorImages(I, I_write);
202  }
203  SECTION("Gray")
204  {
205  const std::string imgPath =
206  vpIoTools::createFilePath(vpIoTools::getViSPImagesDataPath(), "memorial/memorial_gray_LSB.pfm");
207  REQUIRE(vpIoTools::checkFilename(imgPath));
208 
209  vpImage<float> I;
210  vpImageIo::readPFM_HDR(I, imgPath);
211  REQUIRE(I.getSize() > 0);
212 
213  vpImageIo::writePFM_HDR(I, vpIoTools::createFilePath(directory_filename_tmp, "write_gray_pfm.pfm"));
214  vpImage<float> I_write;
215  vpImageIo::readPFM_HDR(I_write, vpIoTools::createFilePath(directory_filename_tmp, "write_gray_pfm.pfm"));
216 
217  REQUIRE(I.getHeight() == I_write.getHeight());
218  REQUIRE(I.getWidth() == I_write.getWidth());
219 
220  checkGrayImages(I, I_write);
221  }
222 
223  REQUIRE(vpIoTools::remove(directory_filename_tmp));
224  REQUIRE(vpIoTools::remove(tmp_dir));
225 }
226 
227 int main(int argc, char *argv[])
228 {
229  Catch::Session session; // There must be exactly one instance
230 
231  // Let Catch (using Clara) parse the command line
232  session.applyCommandLine(argc, argv);
233 
234  int numFailed = session.run();
235 
236  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
237  // This clamping has already been applied, so just return it here
238  // You can also do any post run clean-up here
239  return numFailed;
240 }
241 #else
242 int main() { return EXIT_SUCCESS; }
243 #endif
static void readPFM_HDR(vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1351
static void writePFM_HDR(const vpImage< float > &I, const std::string &filename)
Definition: vpImageIo.cpp:1308
Definition of the vpImage class member functions.
Definition: vpImage.h:131
unsigned int getWidth() const
Definition: vpImage.h:242
unsigned int getSize() const
Definition: vpImage.h:221
unsigned int getHeight() const
Definition: vpImage.h:181
static std::string getViSPImagesDataPath()
Definition: vpIoTools.cpp:1053
static bool checkFilename(const std::string &filename)
Definition: vpIoTools.cpp:786
static std::string getTempPath()
Definition: vpIoTools.cpp:189
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:396
static std::string createFilePath(const std::string &parent, const std::string &child)
Definition: vpIoTools.cpp:1427
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:550
static bool remove(const std::string &filename)
Definition: vpIoTools.cpp:921
static std::string makeTempDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:708
static bool equal(double x, double y, double threshold=0.001)
Definition: vpMath.h:458
VISP_EXPORT std::string getDateTime(const std::string &format="%Y/%m/%d %H:%M:%S")