DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RawReader.ih
1 
30 
31 #include <cstdlib>
33 
35 // Interface - public :
36 
37 template <typename T>
38 inline
39 T
40 DGtal::RawReader<T>::importRaw8 (const std::string & filename, const Vector & extent ) throw(DGtal::IOException)
41 {
42  FILE * fin;
43  DGtal::IOException dgtalerror;
44 
45  fin = fopen( filename.c_str() , "r" );
46 
47  if (fin == NULL)
48  trace.error() << "RawReader : can't open "<< filename<<endl;
49 
50 
51  typename T::Point firstPoint;
52  typename T::Point lastPoint;
53 
54  firstPoint = T::Point::zero;
55  lastPoint = extent;
56  unsigned int size=1;
57  for(unsigned int i=0; i < T::Domain::dimension; i++)
58  {
59  size *= lastPoint[i];
60  lastPoint[i]--;
61  }
62 
63 
64  typename T::Domain domain(firstPoint,lastPoint);
65  T image(domain);
66  typename T::Value val;
67 
68  //We scan the Raw file
69  typename T::Domain::ConstIterator it = domain.begin(),itend=domain.end();
70  unsigned int count=0;
71 
72  while ((fin) && (it != itend))
73  {
74  val = getc(fin);
75  image.setValue( (*it),val);
76  it++;
77  count++;
78  }
79 
80  fclose( fin );
81 
82  if (count != size )
83  {
84  trace.error() << "RawReader: error while opening file "<<filename<<endl;
85  throw dgtalerror;
86  }
87  else
88  return image;
89 }
90