DGtal  0.9.2
RawReader.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file RawReader.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2010/07/25
23  *
24  * Implementation of inline methods defined in RawReader.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstddef>
32 #include <cstdlib>
33 //////////////////////////////////////////////////////////////////////////////
34 
35 ///////////////////////////////////////////////////////////////////////////////
36 // Interface - public :
37 
38 template <typename T, typename TFunctor>
39 template <typename Word>
40 T
41 DGtal::RawReader<T, TFunctor>::importRaw(const std::string& filename, const Vector& extent, const Functor& aFunctor)
42 throw(DGtal::IOException)
43 {
44  BOOST_CONCEPT_ASSERT(( concepts::CUnaryFunctor<TFunctor, Word, Value > )) ;
45 
46  FILE * fin;
47  fin = fopen( filename.c_str() , "rb" );
48 
49  if (fin == NULL)
50  trace.error() << "RawReader : can't open "<< filename << std::endl;
51 
52  typename T::Point firstPoint;
53  typename T::Point lastPoint;
54 
55  firstPoint = T::Point::zero;
56  lastPoint = extent;
57  unsigned int size=1;
58  for(unsigned int i=0; i < T::Domain::dimension; i++)
59  {
60  size *= lastPoint[i];
61  lastPoint[i]--;
62  }
63 
64  typename T::Domain domain(firstPoint, lastPoint);
65  T image(domain);
66 
67  //We scan the Raw file
68  typename T::Domain::ConstIterator it=domain.begin(), itend=domain.end();
69  unsigned int count=0;
70  Word val;
71 
72  while ((fin) && (it != itend))
73  {
74  raw_reader_read_word(fin, val);
75  image.setValue(*it, aFunctor(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 << std::endl;
85  throw DGtal::IOException();
86  }
87 
88  return image;
89 }
90 
91 template <typename T, typename TFunctor>
92 T
93 DGtal::RawReader<T, TFunctor>::importRaw8(const std::string& filename, const Vector& extent, const Functor& aFunctor)
94 throw(DGtal::IOException)
95 {
96  return importRaw<uint8_t>(filename, extent, aFunctor);
97 }
98 
99 template <typename T, typename TFunctor>
100 T
101 DGtal::RawReader<T, TFunctor>::importRaw16(const std::string& filename, const Vector& extent, const Functor& aFunctor)
102 throw(DGtal::IOException)
103 {
104  return importRaw<uint16_t>(filename, extent, aFunctor);
105 }
106 
107 template <typename T, typename TFunctor>
108 T
109 DGtal::RawReader<T, TFunctor>::importRaw32(const std::string& filename, const Vector& extent, const Functor& aFunctor)
110 throw(DGtal::IOException)
111 {
112  return importRaw<uint32_t>(filename, extent, aFunctor);
113 }
114 
115 template <typename Word>
116 FILE*
117 DGtal::raw_reader_read_word( FILE* fin, Word& aValue )
118 {
119  aValue = 0;
120  for ( std::size_t i = 0; i < sizeof( Word ); ++i )
121  reinterpret_cast<unsigned char*>(&aValue)[i] = getc(fin);
122 
123  return fin;
124 }