DGtal  1.5.beta
GenericWriter.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 GenericWriter.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 GenericWriter.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #ifdef WITH_HDF5
33 #include "DGtal/io/writers/HDF5Writer.h"
34 #endif
35 #include "DGtal/io/writers/LongvolWriter.h"
36 #include "DGtal/io/writers/VolWriter.h"
37 #include "DGtal/io/writers/PPMWriter.h"
38 #include "DGtal/io/writers/PGMWriter.h"
39 #include "DGtal/io/writers/RawWriter.h"
40 #include "DGtal/io/writers/STBWriter.h"
41 #ifdef WITH_ITK
42 #include "DGtal/io/writers/ITKWriter.h"
43 #endif
44 
45 
46 //////////////////////////////////////////////////////////////////////////////
47 
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 // Implementation of inline functions //
51 
52 
53 template < typename TContainer, int TDim, typename TValue, typename TFunctor >
54 inline
55 bool
56 DGtal::GenericWriter<TContainer, TDim, TValue, TFunctor>::
57 exportFile( const std::string & filename,
58  const TContainer & anImage,
59  const TFunctor & aFunctor
60  )
61 {
62  DGtal::IOException dgtalio;
63  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
64  if(extension != "raw")
65  {
66  trace.error() << "Extension " << extension << " in " << TDim <<"D, not yet implemented in DGtal GenericWriter (only raw images are actually implemented in Nd using any value type)." << std::endl;
67  throw dgtalio;
68  }
69  else
70  {
71  return RawWriter<TContainer>::template exportRaw<TValue>( filename, anImage, aFunctor );
72  }
73 
74  return true;
75 }
76 
77 template < typename TContainer, typename TValue, typename TFunctor >
78 inline
79 bool
80 DGtal::GenericWriter<TContainer, 3, TValue, TFunctor>::
81 exportFile( const std::string & filename,
82  const TContainer & anImage,
83  const std::string & datasetName,
84  const TFunctor & aFunctor
85  )
86 {
87  DGtal::IOException dgtalio;
88  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
89 
90  // To remove compiler warnings
91  boost::ignore_unused_variable_warning( datasetName );
92 
93  if (
94 #ifdef WITH_HDF5
95  extension == "h5" ||
96 #endif
97  extension == "vol" || extension == "pgm3d" || extension == "pgm3D" || extension == "p3d" || extension == "pgm"
98  )
99  {
100  trace.error() << "Value type of " << extension << " file extension should be unsigned char."
101  << std::endl;
102  throw dgtalio;
103  }
104  else if ( extension == "raw" )
105  {
106  return RawWriter<TContainer>::template exportRaw<TValue>( filename, anImage, aFunctor );
107  }
108  #ifdef WITH_ITK
109  else if (std::find(ITK_IO_IMAGE_EXT.begin(), ITK_IO_IMAGE_EXT.end(), extension) != ITK_IO_IMAGE_EXT.end() )
110  {
111  return ITKWriter<TContainer, TFunctor>::exportITK( filename, anImage, aFunctor );
112  }
113 #endif
114  else
115  {
116  trace.error() << "Extension " << extension << " in 3D, not yet implemented in DGtal GenericWriter." << std::endl;
117  throw dgtalio;
118  }
119 
120  return false;
121 }
122 
123 
124 
125 
126 template <typename TContainer, typename TFunctor>
127 inline
128 bool
129 DGtal::GenericWriter<TContainer, 3, DGtal::uint64_t, TFunctor>::
130 exportFile( const std::string & filename,
131  const TContainer & anImage,
132  const TFunctor & aFunctor
133  )
134 {
135  DGtal::IOException dgtalio;
136  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
137 
138  if ( extension == "longvol" )
139  {
140  return LongvolWriter<TContainer, TFunctor>::exportLongvol( filename, anImage );
141  }
142  else if ( extension == "raw" )
143  {
144  return RawWriter<TContainer>::template exportRaw<DGtal::uint64_t>( filename, anImage, aFunctor );
145  }
146  #ifdef WITH_ITK
147  else if (std::find(ITK_IO_IMAGE_EXT.begin(), ITK_IO_IMAGE_EXT.end(), extension) != ITK_IO_IMAGE_EXT.end() )
148  {
149  return ITKWriter<TContainer, TFunctor>::exportITK( filename, anImage, aFunctor );
150  }
151 #endif
152  else
153  {
154  trace.error() << "Extension " << extension<< " with DGtal::uint64_t in 3D, not yet implemented in DGtal GenericWriter." << std::endl;
155  throw dgtalio;
156  }
157 
158  return false;
159 }
160 
161 
162 
163 template <typename TContainer, typename TFunctor>
164 inline
165 bool
166 DGtal::GenericWriter<TContainer, 3, unsigned char, TFunctor>::
167 exportFile( const std::string & filename,
168  const TContainer & anImage,
169  const std::string & datasetName,
170  const TFunctor & aFunctor
171  )
172 {
173  DGtal::IOException dgtalio;
174  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
175 
176 #ifdef WITH_HDF5
177  if ( extension == "h5" )
178  {
179  return HDF5Writer<TContainer,TFunctor>::exportHDF5_3D( filename, anImage, datasetName, aFunctor );
180  } else
181 #else
182  boost::ignore_unused_variable_warning(datasetName);
183 #endif
184  if ( extension == "vol" )
185  {
186  return VolWriter<TContainer,TFunctor>::exportVol( filename, anImage, true, aFunctor );
187  }
188  else if ( extension == "pgm3d" || extension == "pgm3D" || extension == "p3d" || extension == "pgm" )
189  {
190  return PGMWriter<TContainer, TFunctor>::exportPGM3D( filename, anImage, aFunctor );
191  }
192  else if( extension == "raw" )
193  {
194  return RawWriter< TContainer, TFunctor >::exportRaw8( filename, anImage, aFunctor );
195  }
196  #ifdef WITH_ITK
197  else if (std::find(ITK_IO_IMAGE_EXT.begin(), ITK_IO_IMAGE_EXT.end(), extension) != ITK_IO_IMAGE_EXT.end() )
198  {
199  return ITKWriter<TContainer, TFunctor>::exportITK( filename, anImage, aFunctor );
200  }
201 #endif
202  else
203  {
204  trace.error() << "Extension " << extension<< " with unsigned char in 3D, not yet implemented in DGtal GenericWriter." << std::endl;
205  throw dgtalio;
206  }
207 
208  return false;
209 }
210 
211 template <typename TContainer, typename TValue, typename TFunctor>
212 inline
213 bool
214 DGtal::GenericWriter<TContainer, 2, TValue, TFunctor>::
215 exportFile( const std::string & filename,
216  const TContainer & anImage,
217  const TFunctor & aFunctor
218  )
219 {
220  DGtal::IOException dgtalio;
221  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
222  if ( extension == "pgm" || extension == "pbm" )
223  {
224  trace.error() << "Value type of " << extension<< " file extension should be unsigned char." << std::endl;
225  throw dgtalio;
226  }
227  else if ( extension == "raw" )
228  {
229  return RawWriter< TContainer, TFunctor >::template exportRaw<TValue>( filename, anImage, aFunctor );
230  }
231  else
232  {
233  trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericWriter." << std::endl;
234  throw dgtalio;
235  }
236  return false;
237 }
238 
239 
240 
241 template <typename TContainer, typename TFunctor>
242 inline
243 bool
244 DGtal::GenericWriter<TContainer, 2, DGtal::Color, TFunctor>::
245 exportFile( const std::string & filename,
246  const TContainer & anImage,
247  const TFunctor & aFunctor
248  )
249 {
250  DGtal::IOException dgtalio;
251  const std::string extension = filename.substr( filename.find_last_of(".") + 1 );
252  if ( extension == "ppm" )
253  {
254  return PPMWriter< TContainer, TFunctor >::exportPPM( filename, anImage, aFunctor );
255  }
256  else if ( extension == "tga" )
257  return STBWriter< TContainer, TFunctor >::exportTGA( filename, anImage, aFunctor );
258  else if (extension == "bmp")
259  return STBWriter< TContainer, TFunctor >::exportBMP( filename, anImage, aFunctor );
260  else if (extension == "png")
261  return STBWriter< TContainer, TFunctor >::exportPNG( filename, anImage, aFunctor );
262  else if (extension == "jpg")
263  return STBWriter< TContainer, TFunctor >::exportJPG( filename, anImage, aFunctor );
264 
265  else if ( extension == "raw" )
266  {
267  return RawWriter< TContainer, TFunctor >::template exportRaw<DGtal::Color>( filename, anImage, aFunctor );
268  }
269  else
270  {
271  trace.error() << "Extension " << extension<< " in 2D, not yet implemented in DGtal GenericWriter." << std::endl;
272  throw dgtalio;
273  }
274 
275  return false;
276 }
277 
278 
279 
280 
281 template <typename TContainer, typename TFunctor >
282 inline
283 bool
284 DGtal::GenericWriter<TContainer, 2, unsigned char, TFunctor>::
285 exportFile( const std::string & filename,
286  const TContainer & anImage,
287  const TFunctor & aFunctor
288  )
289 {
290  DGtal::IOException dgtalio;
291  //Getting image extension
292  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
293  if ( extension == "pgm" )
294  {
295  return PGMWriter<TContainer, TFunctor>::exportPGM( filename, anImage, aFunctor );
296  }
297  else if ( extension == "raw" )
298  {
299  return RawWriter<TContainer, TFunctor>::exportRaw8( filename, anImage, aFunctor );
300  }
301  else
302  {
303  trace.error() << "Extension " << extension<< " with unsigned char in 2D, not yet implemented in DGtal GenericWriter." << std::endl;
304  throw dgtalio;
305  }
306 
307  return true;
308 }
309 
310 template <typename TImageContainer >
311 inline
312 bool
313 DGtal::operator >> ( const TImageContainer & aContainer,
314  const std::string & aFilename )
315 {
316  return DGtal::GenericWriter<TImageContainer>::exportFile(aFilename, aContainer);
317 }
318 
319 
320 
321 
322 // //
323 ///////////////////////////////////////////////////////////////////////////////