DGtal  1.5.beta
LongvolReader.h
1 
17 #pragma once
18 
31 #if defined(LongvolReader_RECURSES)
32 #error Recursive header files inclusion detected in LongvolReader.h
33 #else // defined(LongvolReader_RECURSES)
35 #define LongvolReader_RECURSES
36 
37 #if !defined LongvolReader_h
39 #define LongvolReader_h
40 
42 // Inclusions
43 #include <iostream>
44 #include <sstream>
45 #include <string>
46 #include <cstdio>
47 #include "DGtal/base/Common.h"
48 #include <boost/static_assert.hpp>
49 #include "DGtal/helpers/StdDefs.h"
50 #include "DGtal/base/CUnaryFunctor.h"
51 
53 
54 namespace DGtal
55 {
56 
58  // template class LongvolReader
89  template <typename TImageContainer,
90  typename TFunctor= functors::Cast< typename TImageContainer::Value > >
92  {
93  // ----------------------- Standard services ------------------------------
94 
96  typedef typename TImageContainer::Value Value;
97  typedef TFunctor Functor;
98 
100  BOOST_STATIC_ASSERT(ImageContainer::Domain::dimension == 3);
101 
102 
114  static ImageContainer importLongvol(const std::string & filename,
115  const Functor & aFunctor = Functor());
116 
117 
118 
119  private:
120 
129  template <typename Word>
130  static
131  std::stringstream& read_word( std::stringstream & fin, Word& aValue )
132  {
133  char* raw = reinterpret_cast<char*>(&aValue);
134  for (std::size_t i = 0; i < sizeof(Word); ++i)
135  raw[i] = fin.get();
136  return fin;
137  }
138 
139 
140  typedef unsigned char voxel;
145  /* In recent C++, we should use a std::map, but we prefer (badly) code it
146  * by hand for compatibility with old compilers.
147  * At this time, there is a limit of 30 fields in header :-}
148  */
149  struct HeaderField {
151  HeaderField( const char *t, const char *v ) :
152  type( strdup(t) ), value( strdup(v) ) {}
154  free( type );
155  free( value );
156  }
158  HeaderField( const HeaderField &h ) :
159  type( strdup(h.type) ), value( strdup(h.value) ) {};
161  HeaderField() : type(NULL), value(NULL) {};
164  free( type );
165  free( value );
166  if (h.type != NULL) {
167  type = strdup( h.type );
168  value = strdup( h.value );
169  }
170  return *this;
171  }
173  char *type;
175  char *value;
176  };
177 
178 
180  static const char *getHeaderValue( const char *type, const HeaderField * header );
181 
183  static int getHeaderValueAsInt( const char *type, int *dest , const HeaderField * header);
184 
186  static int getHeaderField( const char *type, const HeaderField * header ) ;
187 
189  static const char *requiredHeaders[];
190 
191  }; // end of class LongvolReader
192 
193 
194 } // namespace DGtal
195 
196 
198 // Includes inline functions.
199 #include "DGtal/io/readers/LongvolReader.ih"
200 
201 // //
203 
204 #endif // !defined LongvolReader_h
205 
206 #undef LongvolReader_RECURSES
207 #endif // else defined(LongvolReader_RECURSES)
DGtal is the top-level namespace which contains all DGtal functions and types.
HeaderField()
Default constructor.
char * type
Type of field (e.g. Voxel-Size)
char * value
Value of field (e.g. 2)
HeaderField(const HeaderField &h)
Copy constructor.
HeaderField(const char *t, const char *v)
Constructor. The string are copied.
const HeaderField & operator=(const HeaderField &h)
Assignement operator.
Aim: implements methods to read a "Longvol" file format (with DGtal::uint64_t value type).
Definition: LongvolReader.h:92
static const char * requiredHeaders[]
Global list of required fields in a .longvol file.
TImageContainer::Value Value
Definition: LongvolReader.h:96
static ImageContainer importLongvol(const std::string &filename, const Functor &aFunctor=Functor())
static std::stringstream & read_word(std::stringstream &fin, Word &aValue)
static int getHeaderValueAsInt(const char *type, int *dest, const HeaderField *header)
Returns non-zero if failure.
TImageContainer ImageContainer
Definition: LongvolReader.h:95
BOOST_STATIC_ASSERT(ImageContainer::Domain::dimension==3)
static int getHeaderField(const char *type, const HeaderField *header)
Internal method which returns the index of a field or -1 if not found.
static const char * getHeaderValue(const char *type, const HeaderField *header)
Returns NULL if this field is not found.
BOOST_CONCEPT_ASSERT((concepts::CUnaryFunctor< TFunctor, DGtal::uint64_t, Value >))
unsigned char voxel
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:90
ImageContainerBySTLVector< HyperRectDomain< Z2i::Space >, std::unordered_set< Z2i::Point > > TImageContainer