DGtal  1.5.beta
RawWriter.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 RawWriter.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2010/07/22
23  *
24  * Implementation of inline methods defined in RawWriter.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstddef>
32 #include <cstdlib>
33 #include <fstream>
34 //////////////////////////////////////////////////////////////////////////////
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
39 
40 template <typename I,typename C>
41 template <typename Word>
42 bool
43 DGtal::RawWriter<I, C>::exportRaw(const std::string& filename, const I& aImage, const Functor& aFunctor)
44 {
45  BOOST_CONCEPT_ASSERT(( DGtal::concepts::CUnaryFunctor<Functor, Value, Word> ));
46 
47  std::ofstream out;
48  typename I::Domain domain = aImage.domain();
49  typename I::Value val;
50 
51  out.open(filename.c_str(), std::ios_base::binary);
52 
53  //We scan the domain
54  for (typename I::Domain::ConstIterator it=domain.begin(), itend=domain.end(); it!=itend; ++it)
55  {
56  val = aImage(*it);
57  raw_writer_write_word<Word>(out, aFunctor(val));
58  }
59 
60  out.close();
61 
62  ///@todo catch IOerror exception
63  return true;
64 }
65 
66 template <typename I,typename C>
67 bool
68 DGtal::RawWriter<I, C>::exportRaw8(const std::string& filename, const I& aImage, const Functor& aFunctor)
69 {
70  return exportRaw<uint8_t>(filename, aImage, aFunctor);
71 }
72 
73 template <typename I,typename C>
74 bool
75 DGtal::RawWriter<I, C>::exportRaw16(const std::string& filename, const I& aImage, const Functor& aFunctor)
76 {
77  return exportRaw<uint16_t>(filename, aImage, aFunctor);
78 }
79 
80 template <typename I,typename C>
81 bool
82 DGtal::RawWriter<I, C>::exportRaw32(const std::string& filename, const I& aImage, const Functor& aFunctor)
83 {
84  return exportRaw<uint32_t>(filename, aImage, aFunctor);
85 }
86 
87 namespace DGtal
88 {
89 
90 template <>
91 inline
92 std::ostream&
93 raw_writer_write_word<uint8_t>(std::ostream& outs, uint8_t value)
94 {
95  outs.put(value);
96  return outs;
97 }
98 
99 }
100 
101 template <typename Word>
102 std::ostream&
103 DGtal::raw_writer_write_word(std::ostream& outs, Word value)
104 {
105  for ( std::size_t i = 0; i < sizeof(Word); ++i )
106  outs.put( reinterpret_cast<unsigned char*>(&value)[i] );
107 
108  return outs;
109 }
110