DGtal  0.9.2
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  ) throw( DGtal::IOException )
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  ) throw( DGtal::IOException )
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" || extension == "p3d" || extension == "pgm" )
87  {
88  return PGMReader<TContainer>::importPGM3D( filename );
89  }
90  else if ( extension == "raw" )
91  {
92  ASSERT( x != 0 && y != 0 && z != 0 );
93  typename TContainer::Point const pt(x, y, z);
94  return RawReader< TContainer >::template importRaw<TValue>( filename, pt );
95  }
96 
97 #ifdef WITH_HDF5
98  if ( extension == "h5" )
99  return HDF5Reader<TContainer>::importHDF5_3D( filename, "UInt8Array3D" );
100 #endif
101 
102  trace.error() << "Extension " << extension << " in 3D, not yet implemented in DGtal GenericReader." << std::endl;
103  throw dgtalio;
104 }
105 
106 
107 
108 template <typename TContainer>
109 inline
110 TContainer
111 DGtal::GenericReader<TContainer, 3, DGtal::uint32_t>::
112 import( const std::string & filename,
113  unsigned int x,
114  unsigned int y,
115  unsigned int z
116  ) throw( DGtal::IOException )
117 {
118  DGtal::IOException dgtalio;
119  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
120 
121  if ( extension == "longvol" || extension == "lvol" )
122  {
123  return LongvolReader<TContainer>::importLongvol( filename );
124  }
125  else if ( extension == "raw" )
126  {
127  ASSERT( x != 0 && y != 0 && z != 0 );
128  typename TContainer::Point const pt(x, y, z);
129  return RawReader< TContainer >::importRaw32 ( filename, pt );
130  }
131 
132 
133  trace.error() << "Extension " << extension << " with DGtal::uint32_t in 3D, not yet implemented in DGtal GenericReader." << std::endl;
134  throw dgtalio;
135 }
136 
137 
138 
139 template <typename TContainer>
140 inline
141 TContainer
142 DGtal::GenericReader<TContainer, 3, DGtal::uint64_t>::
143 import( const std::string & filename) throw( DGtal::IOException )
144 {
145  DGtal::IOException dgtalio;
146  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
147 
148  if ( extension == "longvol" || extension == "lvol" )
149  {
150  return LongvolReader<TContainer>::importLongvol( filename );
151  }
152 
153  trace.error() << "Extension " << extension << " with DGtal::uint64_t in 3D, not yet implemented in DGtal GenericReader." << std::endl;
154  throw dgtalio;
155 }
156 
157 
158 
159 template <typename TContainer, typename TValue>
160 inline
161 TContainer
162 DGtal::GenericReader<TContainer, 2, TValue>::
163 import( const std::string &filename,
164  unsigned int x,
165  unsigned int y
166  ) throw( DGtal::IOException )
167 {
168  DGtal::IOException dgtalio;
169  //Getting image extension
170  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
171 
172  if ( extension == "pgm" )
173  {
174  return PGMReader<TContainer>::importPGM(filename);
175  }
176  else if ( extension == "ppm" )
177  {
178  return PPMReader<TContainer>::importPPM(filename);
179  }
180  else if ( extension == "raw" )
181  {
182  ASSERT( x != 0 && y != 0 );
183  typename TContainer::Point const pt (x, y);
184  return RawReader<TContainer>::template importRaw<TValue>( filename, pt );
185  }
186 
187 #ifdef WITH_HDF5
188  if ( extension == "h5" )
189  return HDF5Reader<TContainer>::importHDF5(filename, "image8bit");
190 #endif
191 
192  if( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
193  {
194 #ifdef WITH_MAGICK
195  MagickReader<TContainer> reader;
196  return reader.importImage( filename );
197 #else
198  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
199  throw dgtalio;
200 #endif
201  }
202 
203  trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericReader." << std::endl;
204  throw dgtalio;
205 }
206 
207 
208 
209 template <typename TContainer>
210 inline
211 TContainer
212 DGtal::GenericReader<TContainer, 2, DGtal::uint32_t>::
213 import( const std::string &filename,
214  unsigned int x,
215  unsigned int y
216  ) throw( DGtal::IOException )
217 {
218  DGtal::IOException dgtalio;
219  //Getting image extension
220  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
221 
222  if ( extension == "ppm" )
223  {
224  return PPMReader<TContainer>::importPPM(filename);
225  }
226  else if ( extension == "pgm" )
227  {
228  return PGMReader<TContainer>::importPGM(filename);
229  }
230  else if ( extension == "raw" )
231  {
232  ASSERT( x != 0 && y != 0);
233  typename TContainer::Point const pt (x,y);
234  return RawReader<TContainer>::importRaw32( filename, pt );
235  }
236 
237  if ( extension == "gif" || extension == "jpg" || extension == "png" || extension == "jpeg" || extension == "bmp" )
238  {
239 #ifdef WITH_MAGICK
240  MagickReader<TContainer> reader;
241  return reader.importImage( filename );
242 #else
243  trace.error() << "Extension " << extension<< " not yet implemented in DGtal but you can add Magick option to deal with this image type." << std::endl;
244  throw dgtalio;
245 #endif
246  }
247 
248  trace.error() << "Extension " << extension<< " with DGtal::uint32_t in 2D, not yet implemented in DGtal GenericReader." << std::endl;
249  throw dgtalio;
250 }
251 
252 
253 
254 
255 // //
256 ///////////////////////////////////////////////////////////////////////////////