DGtal  1.5.beta
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 {
43  BOOST_CONCEPT_ASSERT(( concepts::CUnaryFunctor<TFunctor, Word, Value > )) ;
44 
45  FILE * fin;
46  fin = fopen( filename.c_str() , "rb" );
47 
48  if (fin == NULL)
49  trace.error() << "RawReader : can't open "<< filename << std::endl;
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  typename T::Domain domain(firstPoint, lastPoint);
64  T image(domain);
65 
66  //We scan the Raw file
67  typename T::Domain::ConstIterator it=domain.begin(), itend=domain.end();
68  unsigned int count=0;
69  Word val;
70 
71  while ((fin) && (it != itend))
72  {
73  raw_reader_read_word(fin, val);
74  image.setValue(*it, aFunctor(val));
75  it++;
76  count++;
77  }
78 
79  fclose(fin);
80 
81  if (count != size)
82  {
83  trace.error() << "RawReader: error while opening file " << filename << std::endl;
84  throw DGtal::IOException();
85  }
86 
87  return image;
88 }
89 
90 template <typename T, typename TFunctor>
91 T
92 DGtal::RawReader<T, TFunctor>::importRaw8(const std::string& filename, const Vector& extent, const Functor& aFunctor)
93 {
94  return importRaw<uint8_t>(filename, extent, aFunctor);
95 }
96 
97 template <typename T, typename TFunctor>
98 T
99 DGtal::RawReader<T, TFunctor>::importRaw16(const std::string& filename, const Vector& extent, const Functor& aFunctor)
100 {
101  return importRaw<uint16_t>(filename, extent, aFunctor);
102 }
103 
104 template <typename T, typename TFunctor>
105 T
106 DGtal::RawReader<T, TFunctor>::importRaw32(const std::string& filename, const Vector& extent, const Functor& aFunctor)
107 {
108  return importRaw<uint32_t>(filename, extent, aFunctor);
109 }
110 
111 template <typename Word>
112 FILE*
113 DGtal::raw_reader_read_word( FILE* fin, Word& aValue )
114 {
115  aValue = 0;
116  for ( std::size_t i = 0; i < sizeof( Word ); ++i )
117  reinterpret_cast<unsigned char*>(&aValue)[i] = getc(fin);
118 
119  return fin;
120 }