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