DGtal  1.4.beta
GenericReader.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 GenericReader.ih
19  * @author Bertrand Kerautret (\c kerautre@loria.fr )
20  * LORIA (CNRS, UMR 7503), University of Nancy, France
21  *
22  * @date 2013/05/01
23  *
24  * Implementation of inline methods defined in GenericReader.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline functions.
36 
37 template <typename TContainer, int TDim, typename TValue>
38 inline
39 TContainer
40 DGtal::GenericReader<TContainer, TDim, TValue>::
41 import( const std::string & filename,
42  std::vector<unsigned int> dimSpace
43  )
44 {
45  DGtal::IOException dgtalio;
46  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
47  if ( extension != "raw" )
48  {
49  trace.error() << "Extension " << extension << " not yet implemented in " << TDim << "D for DGtal GenericReader (only raw images are actually implemented in Nd using any value type)." << std::endl;
50  throw dgtalio;
51  }
52  else
53  {
54  typename TContainer::Point aPointDim;
55  for ( unsigned int i = 0; i < dimSpace.size(); i++)
56  {
57  aPointDim[ i ] = dimSpace[ i ];
58  }
59  return RawReader< TContainer >::template importRaw<TValue> ( filename, aPointDim );
60  }
61 }
62 
63 
64 
65 template <typename TContainer, typename TValue>
66 inline
67 TContainer
68 DGtal::GenericReader<TContainer, 3, TValue>::
69 import( const std::string & filename,
70  unsigned int x,
71  unsigned int y,
72  unsigned int z
73  )
74 {
75  DGtal::IOException dgtalio;
76  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
77 
78  if ( extension == "vol" )
79  {
80  return VolReader<TContainer>::importVol( filename );
81  }
82  else if ( extension == "longvol" || extension == "lvol" )
83  {
84  return LongvolReader<TContainer>::importLongvol( filename );
85  }
86  else if ( extension == "pgm3d" || extension == "pgm3D" ||
87  extension == "p3d" || extension == "pgm" )
88  {
89  return PGMReader<TContainer>::importPGM3D( filename );
90  }
91 #ifdef WITH_ITK
92  else if (std::find(ITK_IO_IMAGE_EXT.begin(), ITK_IO_IMAGE_EXT.end(), extension) != ITK_IO_IMAGE_EXT.end() )
93  {
94 
95  return ITKReader<TContainer>::importITK( filename );
96  }
97 #endif
98 
99  else if ( extension == "raw" )
100  {
101  ASSERT( x != 0 && y != 0 && z != 0 );
102  typename TContainer::Point const pt(x, y, z);
103  return RawReader< TContainer >::template importRaw<TValue>( filename, pt );
104  }
105 
106 #ifdef WITH_HDF5
107  if ( extension == "h5" )
108  return HDF5Reader<TContainer>::importHDF5_3D( filename, "UInt8Array3D" );
109 #endif
110 
111  trace.error() << "Extension " << extension << " in 3D, not yet implemented in DGtal GenericReader." << std::endl;
112  throw dgtalio;
113 }
114 
115 
116 
117 template <typename TContainer>
118 inline
119 TContainer
120 DGtal::GenericReader<TContainer, 3, DGtal::uint32_t>::
121 import( const std::string & filename,
122  unsigned int x,
123  unsigned int y,
124  unsigned int z
125  )
126 {
127  DGtal::IOException dgtalio;
128  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
129 
130  if ( extension == "longvol" || extension == "lvol" )
131  {
132  return LongvolReader<TContainer>::importLongvol( filename );
133  }
134  else if ( extension == "raw" )
135  {
136  ASSERT( x != 0 && y != 0 && z != 0 );
137  typename TContainer::Point const pt(x, y, z);
138  return RawReader< TContainer >::importRaw32 ( filename, pt );
139  }
140 
141 
142  trace.error() << "Extension " << extension << " with DGtal::uint32_t in 3D, not yet implemented in DGtal GenericReader." << std::endl;
143  throw dgtalio;
144 }
145 
146 
147 
148 template <typename TContainer>
149 inline
150 TContainer
151 DGtal::GenericReader<TContainer, 3, DGtal::uint64_t>::
152 import( const std::string & filename)
153 {
154  DGtal::IOException dgtalio;
155  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
156 
157  if ( extension == "longvol" || extension == "lvol" )
158  {
159  return LongvolReader<TContainer>::importLongvol( filename );
160  }
161 
162  trace.error() << "Extension " << extension << " with DGtal::uint64_t in 3D, not yet implemented in DGtal GenericReader." << std::endl;
163  throw dgtalio;
164 }
165 
166 
167 
168 template <typename TContainer, typename TValue>
169 inline
170 TContainer
171 DGtal::GenericReader<TContainer, 2, TValue>::
172 import( const std::string &filename,
173  unsigned int x,
174  unsigned int y
175  )
176 {
177  DGtal::IOException dgtalio;
178  //Getting image extension
179  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
180 
181  if ( extension == "pgm" )
182  {
183  return PGMReader<TContainer>::importPGM(filename);
184  }
185  else if ( extension == "ppm" )
186  {
187  return PPMReader<TContainer>::importPPM(filename);
188  }
189  else if ( extension == "raw" )
190  {
191  ASSERT( x != 0 && y != 0 );
192  typename TContainer::Point const pt (x, y);
193  return RawReader<TContainer>::template importRaw<TValue>( filename, pt );
194  }
195 
196 #ifdef WITH_HDF5
197  if ( extension == "h5" )
198  return HDF5Reader<TContainer>::importHDF5(filename, "image8bit");
199 #endif
200 
201  if( extension == "tga" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
202  {
203  STBReader<TContainer> reader;
204  return reader.import( filename );
205  }
206 
207  trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericReader." << std::endl;
208  throw dgtalio;
209 }
210 
211 
212 
213 template <typename TContainer>
214 inline
215 TContainer
216 DGtal::GenericReader<TContainer, 2, DGtal::uint32_t>::
217 import( const std::string &filename,
218  unsigned int x,
219  unsigned int y
220  )
221 {
222  DGtal::IOException dgtalio;
223  //Getting image extension
224  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
225 
226  if ( extension == "ppm" )
227  {
228  return PPMReader<TContainer>::importPPM(filename);
229  }
230  else if ( extension == "pgm" )
231  {
232  return PGMReader<TContainer>::importPGM(filename);
233  }
234  else if ( extension == "raw" )
235  {
236  ASSERT( x != 0 && y != 0);
237  typename TContainer::Point const pt (x,y);
238  return RawReader<TContainer>::importRaw32( filename, pt );
239  }
240 
241  if ( extension == "tga" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
242  {
243  STBReader<TContainer> reader;
244  return reader.import( filename );
245  }
246 
247  trace.error() << "Extension " << extension<< " with DGtal::uint32_t in 2D, not yet implemented in DGtal GenericReader." << std::endl;
248  throw dgtalio;
249 }
250 
251 
252 
253 
254 // //
255 ///////////////////////////////////////////////////////////////////////////////