DGtal  0.9.4.1
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)
34 
35 #define LongvolReader_RECURSES
36 
37 #if !defined LongvolReader_h
38 
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 
95  typedef TImageContainer ImageContainer;
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  aValue = 0;
134  char c;
135  for (unsigned int size = 0; size < sizeof( Word ); ++size)
136  {
137  fin.get( c ) ;
138  unsigned char cc=static_cast<unsigned char>(c);
139  aValue |= (cc << (8 * size));
140  }
141  return fin;
142  }
143 
144 
145  typedef unsigned char voxel;
150  /* In recent C++, we should use a std::map, but we prefer (badly) code it
151  * by hand for compatibility with old compilers.
152  * At this time, there is a limit of 30 fields in header :-}
153  */
154  struct HeaderField {
156  HeaderField( const char *t, const char *v ) :
157  type( strdup(t) ), value( strdup(v) ) {}
159  free( type );
160  free( value );
161  }
163  HeaderField( const HeaderField &h ) :
164  type( strdup(h.type) ), value( strdup(h.value) ) {};
166  HeaderField() : type(NULL), value(NULL) {};
169  free( type );
170  free( value );
171  if (h.type != NULL) {
172  type = strdup( h.type );
173  value = strdup( h.value );
174  }
175  return *this;
176  }
178  char *type;
180  char *value;
181  };
182 
183 
185  static const char *getHeaderValue( const char *type, const HeaderField * header );
186 
188  static int getHeaderValueAsInt( const char *type, int *dest , const HeaderField * header);
189 
191  static int getHeaderField( const char *type, const HeaderField * header ) ;
192 
194  static const char *requiredHeaders[];
195 
196  }; // end of class LongvolReader
197 
198 
199 } // namespace DGtal
200 
201 
203 // Includes inline functions.
204 #include "DGtal/io/readers/LongvolReader.ih"
205 
206 // //
208 
209 #endif // !defined LongvolReader_h
210 
211 #undef LongvolReader_RECURSES
212 #endif // else defined(LongvolReader_RECURSES)
HeaderField()
Default constructor.
static const char * getHeaderValue(const char *type, const HeaderField *header)
Returns NULL if this field is not found.
TImageContainer ImageContainer
Definition: LongvolReader.h:95
BOOST_STATIC_ASSERT(ImageContainer::Domain::dimension==3)
static int getHeaderValueAsInt(const char *type, int *dest, const HeaderField *header)
Returns non-zero if failure.
static std::stringstream & read_word(std::stringstream &fin, Word &aValue)
Aim: Defines a unary functor, which associates arguments to results.
Definition: CUnaryFunctor.h:89
char * type
Type of field (e.g. Voxel-Size)
static int getHeaderField(const char *type, const HeaderField *header)
Internal method which returns the index of a field or -1 if not found.
TImageContainer::Value Value
Definition: LongvolReader.h:96
DGtal is the top-level namespace which contains all DGtal functions and types.
static ImageContainer importLongvol(const std::string &filename, const Functor &aFunctor=Functor())
BOOST_CONCEPT_ASSERT((concepts::CUnaryFunctor< TFunctor, DGtal::uint64_t, Value >))
const HeaderField & operator=(const HeaderField &h)
Assignement operator.
char * value
Value of field (e.g. 2)
static const char * requiredHeaders[]
Global list of required fields in a .longvol file.
unsigned char voxel
Aim: implements methods to read a "Longvol" file format (with DGtal::uint64_t value type)...
Definition: LongvolReader.h:91
HeaderField(const HeaderField &h)
Copy constructor.
HeaderField(const char *t, const char *v)
Constructor. The string are copied.