DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PGMWriter.ih
1 
30 
31 #include <cstdlib>
32 #include <fstream>
33 #include "DGtal/io/Color.h"
35 
37 // IMPLEMENTATION of inline methods.
39 
40 
41 namespace DGtal {
42 
43 template<typename I,typename C>
44 bool
45 PGMWriter<I,C>::exportPGM(const std::string & filename, const I & aImage,
46  const Functor & aFunctor, bool saveASCII, bool topbotomOrder)
47 {
48  BOOST_STATIC_ASSERT(I::Domain::dimension == 2);
49 
50  ofstream out;
51  typename I::Domain::Vector ext = aImage.extent();
52  typename I::Domain domain = aImage.domain();
53  typename I::Value val;
54 
55  out.open(filename.c_str());
56 
57  //PPM format
58  if(saveASCII){
59  out << "P2"<<endl;
60  }else{
61  out << "P5"<<endl;
62  }
63 
64  out << "#DGtal PNM Writer"<<endl<<endl;
65  out << ext[0]<<" "<< ext[1]<<endl;
66  out << "255" <<endl;
67 
68  if(!topbotomOrder){
69  //We scan the domain instead of the image becaus we cannot
70  //trust the image container Iterator
71  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
72  it!=itend;
73  ++it)
74  {
75 
76  val = aImage( (*it) );
77  if(saveASCII){
78  out << ((int) aFunctor(val) )<<" ";
79  }else{
80  out << ((char)((int) aFunctor(val)));
81  }
82  }
83  }else{
84  typename I::Domain::Point ptUpper= domain.upperBound();
85 
86  for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstReverseIterator itY = domain.subRange(1, ptUpper).rbegin(), itYend=domain.subRange(1, ptUpper).rend(); itY!=itYend; ++itY)
87  {
88  typename I::Domain::Point ptUpperY= *itY;
89 
90  for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstIterator it = domain.subRange(0, ptUpperY).begin(), itend=domain.subRange(0, ptUpperY).end();
91  it!=itend;
92  ++it)
93  {
94  val = aImage( (*it) );
95  if(saveASCII){
96  out << ((int) aFunctor(val))<<" ";
97  }else{
98  out << ((char) aFunctor(val));
99  }
100 
101  }
102  }
103  }
104 
105  out.close();
106 
108  return true;
109 }
110 
111 template<typename I,typename C>
112 bool
113 PGMWriter<I,C>::exportPGM3D(const std::string & filename, const I & aImage,
114  const Functor&aFunctor)
115 {
117 
118  BOOST_STATIC_ASSERT(I::Domain::dimension == 3);
119 
120  ofstream out;
121  typename I::Domain::Vector ext = aImage.extent();
122  typename I::Domain domain(aImage.lowerBound(), aImage.upperBound());
123  typename I::Value val;
124  Color col;
125 
126  out.open(filename.c_str());
127 
128  //PPM format
129  out << "P2-3D"<<endl;
130  out << "#DGtal PNM Writer"<<endl<<endl;
131  out << ext[0]<<" "<< ext[1]<<" "<< ext[2]<<endl;
132  out << "255" <<endl;
133 
134  //We scan the domain instead of the image becaus we cannot
135  //trust the image container Iterator
136  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
137  it!=itend;
138  ++it)
139  {
140 
141  val = aImage( (*it) );
142  out << ((int) aFunctor( val ))<<" ";
143  }
144 
145  out.close();
146 
148  return true;
149 }
150 
151 
152 }//namespace