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