DGtal  1.5.beta
testRawReader.cpp
Go to the documentation of this file.
1 
35 #include "DGtalCatch.h"
36 #include <DGtal/base/Common.h>
37 #include <DGtal/kernel/SpaceND.h>
38 #include <DGtal/kernel/domains/HyperRectDomain.h>
39 #include <DGtal/images/ImageSelector.h>
40 #include <DGtal/io/readers/RawReader.h>
41 #include <DGtal/io/writers/RawWriter.h>
42 #include <DGtal/kernel/domains/Linearizer.h>
43 
44 #include <ConfigTest.h>
45 
46 #include <string>
47 
49 
50 using namespace std;
51 using namespace DGtal;
52 
54 
59 template <
60  typename Image
61 >
62 void testImageOnRef( Image const& anImage )
63 {
64  typedef typename Image::Domain Domain;
65  typedef typename Domain::Point Point;
66 
67  // Checking domain
68  INFO( "Checking domain" )
69  REQUIRE( anImage.domain().lowerBound() == Point::diagonal(0) );
70  REQUIRE( anImage.domain().upperBound() == Point::diagonal(4) );
71 
72  // Checking values
73  INFO( "Checking values" )
74  const Domain domain = anImage.domain();
75  for ( typename Domain::ConstIterator it = domain.begin(), itEnd = domain.end(); it != itEnd; ++it )
76  {
77  const Point pt = *it;
78  const unsigned int refValue = anImage.dimension == 2 ?
79  1000000 * pt[0] * pt[1]
80  : 250000 * pt[0] * pt[1] * pt[2];
81 
82  INFO( "At point " << pt );
83  REQUIRE( anImage( pt ) == refValue );
84  }
85 }
86 
90 template <
92 >
94 {
95  REQUIRE( N >= 2 );
96  REQUIRE( N <= 3 );
97 
98  typedef SpaceND<N> Space;
101  typedef typename Domain::Vector Vector;
102 
103  // Generating file name
104  std::string fileName = testPath + "samples/";
105  if ( N == 2)
106  fileName += "raw32bits5x5.raw";
107  else
108  fileName += "raw32bits5x5x5.raw";
109 
110  // Reading file
111  const Vector extent = Vector::diagonal(5);
112 
113  INFO( "Reading file with importRaw32" << fileName );
114  Image imageRaw32 = RawReader<Image>::importRaw32( fileName, extent );
115  testImageOnRef( imageRaw32 );
116 
117  INFO( "Reading file with importRaw" << fileName );
118  Image imageRaw = RawReader<Image>::template importRaw< unsigned int >( fileName, extent );
119  testImageOnRef( imageRaw );
120 }
121 
127 template <
128  typename Image
129 >
130 void generateRefImage( Image & anImage, typename Image::Value aSeed )
131 {
132  typedef typename Image::Domain Domain;
133  typedef typename Domain::Point Point;
134 
135  const Domain domain = anImage.domain();
136 
137  for ( typename Domain::ConstIterator it = domain.begin(), itEnd = domain.end(); it != itEnd; ++it )
138  {
139  const Point pt = *it;
140  const typename Image::Value refValue = aSeed * Linearizer<Domain, RowMajorStorage>::getIndex( pt, domain );
141  anImage.setValue( pt, refValue );
142  }
143 }
144 
151 template <
153  typename T,
154  template<typename> class RawIO
155 >
156 void testWriteAndRead( T aSeed )
157 {
158  typedef SpaceND<N> Space;
160  typedef typename ImageSelector<Domain, T>::Type Image;
161  typedef typename Domain::Point Point;
162 
163  Point upperPt;
164  upperPt[ 0 ] = 8;
165  for ( Dimension i = 1; i < N; ++i )
166  upperPt[ i ] = upperPt[ i-1 ] * ( i == 1 ? 4 : 2 );
167 
168  const Domain domain( Point::diagonal(0), upperPt );
169  Image refImage( domain );
170  generateRefImage( refImage, aSeed );
171 
172  INFO( "Writing image" );
173  RawIO<Image>::write( "export-raw-writer.raw", refImage );
174 
175  INFO( "Reading image" );
176  Image image = RawIO<Image>::read( "export-raw-writer.raw", upperPt + Point::diagonal(1) );
177 
178  INFO( "Comparing image values" );
179  for ( typename Domain::ConstIterator it = domain.begin(), itEnd = domain.end(); it != itEnd; ++it )
180  {
181  const Point pt = *it;
182  INFO( "At point " << pt );
183  REQUIRE( image( pt ) == refImage( pt ) );
184  }
185 }
186 
187 // Some RawIO classes
188 template < typename Image >
189 struct RawIO8
190 {
191  static inline Image read( std::string const& filename, typename Image::Domain::Vector const& extent )
192  {
193  return DGtal::RawReader<Image>::importRaw8( filename, extent );
194  }
195 
196  static inline bool write( std::string const& filename, Image const& anImage )
197  {
198  return DGtal::RawWriter<Image>::exportRaw8( filename, anImage );
199  }
200 };
201 
202 template < typename Image >
203 struct RawIO16
204 {
205  static inline Image read( std::string const& filename, typename Image::Domain::Vector const& extent )
206  {
207  return DGtal::RawReader<Image>::importRaw16( filename, extent );
208  }
209 
210  static inline bool write( std::string const& filename, Image const& anImage )
211  {
212  return DGtal::RawWriter<Image>::exportRaw16( filename, anImage );
213  }
214 };
215 
216 template < typename Image >
217 struct RawIO32
218 {
219  static inline Image read( std::string const& filename, typename Image::Domain::Vector const& extent )
220  {
221  return DGtal::RawReader<Image>::importRaw32( filename, extent );
222  }
223 
224  static inline bool write( std::string const& filename, Image const& anImage )
225  {
226  return DGtal::RawWriter<Image>::exportRaw32( filename, anImage );
227  }
228 };
229 
230 template < typename Image >
231 struct RawIO
232 {
233  static inline Image read( std::string const& filename, typename Image::Domain::Vector const& extent )
234  {
235  return DGtal::RawReader<Image>::template importRaw< typename Image::Value>( filename, extent );
236  }
237 
238  static inline bool write( std::string const& filename, Image const& anImage )
239  {
240  return DGtal::RawWriter<Image>::template exportRaw< typename Image::Value>( filename, anImage );
241  }
242 };
243 
245 
246 TEST_CASE( "Checking RawReader with reference files in 2D", "[reader][2D][raw][raw32][uint32]" )
247 {
248  testRawReaderOnRef<2>();
249 }
250 
251 TEST_CASE( "Checking RawReader with reference files in 3D", "[reader][3D][raw][raw32][uint32]" )
252 {
253  testRawReaderOnRef<3>();
254 }
255 
256 // Signed and unsigned char
257 TEST_CASE( "Checking writing & reading uint8 in 2D with generic IO", "[reader][writer][2D][raw][uint8]" )
258 {
259  testWriteAndRead<2, DGtal::uint8_t, RawIO>( 1 );
260 }
261 
262 TEST_CASE( "Checking writing & reading uint8 in 2D with 8bits IO", "[reader][writer][2D][raw8][uint8]" )
263 {
264  testWriteAndRead<2, DGtal::uint8_t, RawIO>( 1 );
265 }
266 
267 TEST_CASE( "Checking writing & reading int8 in 2D with generic IO", "[reader][writer][2D][raw][int8]" )
268 {
269  testWriteAndRead<2, DGtal::int8_t, RawIO>( 1 );
270 }
271 
272 // Signed and unsigned short
273 TEST_CASE( "Checking writing & reading uint16 in 2D with generic IO", "[reader][writer][2D][raw][uint16]" )
274 {
275  testWriteAndRead<2, DGtal::uint16_t, RawIO>( 1 );
276 }
277 
278 TEST_CASE( "Checking writing & reading uint16 in 2D with 16bits IO", "[reader][writer][2D][raw16][uint16]" )
279 {
280  testWriteAndRead<2, DGtal::uint16_t, RawIO>( 1 );
281 }
282 
283 TEST_CASE( "Checking writing & reading int16 in 2D with generic IO", "[reader][writer][2D][raw][int16]" )
284 {
285  testWriteAndRead<2, DGtal::int16_t, RawIO>( 1 );
286 }
287 
288 // Signed and unsigned int
289 TEST_CASE( "Checking writing & reading uint32 in 2D with generic IO", "[reader][writer][2D][raw][uint32]" )
290 {
291  testWriteAndRead<2, DGtal::uint32_t, RawIO>( 1 );
292 }
293 
294 TEST_CASE( "Checking writing & reading uint32 in 2D with 32bits IO", "[reader][writer][2D][raw32][uint32]" )
295 {
296  testWriteAndRead<2, DGtal::uint32_t, RawIO>( 1 );
297 }
298 
299 TEST_CASE( "Checking writing & reading int32 in 2D with generic IO", "[reader][writer][2D][raw][int32]" )
300 {
301  testWriteAndRead<2, DGtal::int32_t, RawIO>( 1 );
302 }
303 
304 // Signed and unsigned long int
305 TEST_CASE( "Checking writing & reading uint64 in 2D with generic IO", "[reader][writer][2D][raw][uint64]" )
306 {
307  testWriteAndRead<2, DGtal::uint64_t, RawIO>( 1 );
308 }
309 
310 TEST_CASE( "Checking writing & reading int64 in 2D with generic IO", "[reader][writer][2D][raw][int64]" )
311 {
312  testWriteAndRead<2, DGtal::int64_t, RawIO>( 1 );
313 }
314 
315 // Double
316 TEST_CASE( "Checking writing & reading double in 1D with generic IO", "[reader][writer][1D][raw][double]" )
317 {
318  testWriteAndRead<1, double, RawIO>( 1.23456789 );
319 }
320 
321 TEST_CASE( "Checking writing & reading double in 2D with generic IO", "[reader][writer][2D][raw][double]" )
322 {
323  testWriteAndRead<2, double, RawIO>( 1.23456789 );
324 }
325 
326 TEST_CASE( "Checking writing & reading double in 3D with generic IO", "[reader][writer][3D][raw][double]" )
327 {
328  testWriteAndRead<3, double, RawIO>( 1.23456789 );
329 }
330 
Iterator for HyperRectDomain.
const ConstIterator & end() const
const ConstIterator & begin() const
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
const Domain & domain() const
Definition: Image.h:192
void setValue(const Point &aPoint, const Value &aValue)
Definition: Image.h:247
DigitalPlane::Point Vector
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::uint32_t Dimension
Definition: Common.h:136
Aim: Linearization and de-linearization interface for domains.
Definition: Linearizer.h:78
Aim: Raw binary import of an Image.
Definition: RawReader.h:94
static ImageContainer importRaw8(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
static ImageContainer importRaw16(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
static ImageContainer importRaw32(const std::string &filename, const Vector &extent, const Functor &aFunctor=Functor())
Aim: Raw binary export of an Image.
Definition: RawWriter.h:99
static bool exportRaw32(const std::string &filename, const Image &anImage, const Functor &aFunctor=Functor())
static bool exportRaw8(const std::string &filename, const Image &anImage, const Functor &aFunctor=Functor())
static bool exportRaw16(const std::string &filename, const Image &anImage, const Functor &aFunctor=Functor())
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
void testWriteAndRead(T aSeed)
void testRawReaderOnRef()
void testImageOnRef(Image const &anImage)
TEST_CASE("Checking RawReader with reference files in 2D", "[reader][2D][raw][raw32][uint32]")
void generateRefImage(Image &anImage, typename Image::Value aSeed)
Image image(domain)
ImageContainerBySTLVector< Domain, Value > Image
HyperRectDomain< Space > Domain
REQUIRE(domain.isInside(aPoint))
Image refImage(domain)