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