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