DGtal  0.9.2
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 <string>
45 #include <cstdio>
46 #include "DGtal/base/Common.h"
47 #include <boost/static_assert.hpp>
48 #include "DGtal/helpers/StdDefs.h"
49 #include "DGtal/base/CUnaryFunctor.h"
50 
52 
53 namespace DGtal
54 {
55 
57  // template class LongvolReader
88  template <typename TImageContainer,
89  typename TFunctor= functors::Cast< typename TImageContainer::Value > >
91  {
92  // ----------------------- Standard services ------------------------------
93 
94  typedef TImageContainer ImageContainer;
95  typedef typename TImageContainer::Value Value;
96  typedef TFunctor Functor;
97 
99  BOOST_STATIC_ASSERT(ImageContainer::Domain::dimension == 3);
100 
101 
113  static ImageContainer importLongvol(const std::string & filename,
114  const Functor & aFunctor = Functor()) throw(DGtal::IOException);
115 
116 
117 
118  private:
119 
128  template <typename Word>
129  static
130  FILE* read_word( FILE* fin, Word& aValue )
131  {
132  aValue = 0;
133  for (unsigned size = 0; size < sizeof( Word ); ++size)
134  aValue |= getc(fin) << (8 * size);
135  return fin;
136  }
137 
138  typedef unsigned char voxel;
143  /* In recent C++, we should use a std::map, but we prefer (badly) code it
144  * by hand for compatibility with old compilers.
145  * At this time, there is a limit of 30 fields in header :-}
146  */
147  struct HeaderField {
149  HeaderField( const char *t, const char *v ) :
150  type( strdup(t) ), value( strdup(v) ) {}
152  free( type );
153  free( value );
154  }
156  HeaderField( const HeaderField &h ) :
157  type( strdup(h.type) ), value( strdup(h.value) ) {};
159  HeaderField() : type(NULL), value(NULL) {};
162  free( type );
163  free( value );
164  if (h.type != NULL) {
165  type = strdup( h.type );
166  value = strdup( h.value );
167  }
168  return *this;
169  }
171  char *type;
173  char *value;
174  };
175 
176 
178  static const char *getHeaderValue( const char *type, const HeaderField * header );
179 
181  static int getHeaderValueAsInt( const char *type, int *dest , const HeaderField * header);
182 
184  static int getHeaderField( const char *type, const HeaderField * header ) ;
185 
187  static const char *requiredHeaders[];
188 
189  }; // end of class LongvolReader
190 
191 
192 } // namespace DGtal
193 
194 
196 // Includes inline functions.
197 #include "DGtal/io/readers/LongvolReader.ih"
198 
199 // //
201 
202 #endif // !defined LongvolReader_h
203 
204 #undef LongvolReader_RECURSES
205 #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:94
BOOST_STATIC_ASSERT(ImageContainer::Domain::dimension==3)
static int getHeaderValueAsInt(const char *type, int *dest, const HeaderField *header)
Returns non-zero if failure.
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:95
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:90
static FILE * read_word(FILE *fin, Word &aValue)
HeaderField(const HeaderField &h)
Copy constructor.
HeaderField(const char *t, const char *v)
Constructor. The string are copied.