DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PPMWriter.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  PPMWriter<I,C>::exportPPM(const std::string & filename, const I & aImage,
46  const Functor & aFunctor, 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  DGtal::Color col;
55  out.open(filename.c_str());
56 
57  //PPM format
58  out << "P3"<<endl;
59  out << "#DGtal PNM Writer"<<endl<<endl;
60  out << ext[0]<<" "<< ext[1]<<endl;
61  out << "255" <<endl;
62 
63  if(!topbotomOrder)
64  {
65  //We scan the domain instead of the image becaus we cannot
66  //trust the image container Iterator
67  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
68  it!=itend;
69  ++it)
70  {
71  val = aImage( (*it) );
72  col = aFunctor( val );
73  out << (int)col.red()<<" "<<(int)col.green()<<" "<<(int)col.blue()<<" ";
74  }
75  }
76  else
77  {
78  typename I::Domain::Point ptUpper= domain.upperBound();
79 
80  for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstReverseIterator itY = domain.subRange(1, ptUpper).rbegin(),
81  itYend=domain.subRange(1, ptUpper).rend(); itY!=itYend; ++itY)
82  {
83  typename I::Domain::Point ptUpperY= *itY;
84 
85  for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstIterator it = domain.subRange(0, ptUpperY).begin(),
86  itend=domain.subRange(0, ptUpperY).end();
87  it!=itend;
88  ++it)
89  {
90  val = aImage( (*it) );
91  col = aFunctor( val );
92  out << (int)col.red()<<" "<<(int)col.green()<<" "<<(int)col.blue()<<" ";
93  }
94  }
95  }
96 
97  out.close();
98 
100  return true;
101  }
102 
103  template<typename I,typename C>
104 bool
105 PPMWriter<I,C>::exportPPM3D(const std::string & filename, const I & aImage,
106  const Functor&aFunctor)
107 {
108  BOOST_STATIC_ASSERT(I::Domain::dimension == 3);
109 
110  ofstream out;
111  typename I::Domain::Vector ext = aImage.extent();
112  typename I::Domain domain(aImage.lowerBound(), aImage.upperBound());
113  typename I::Value val;
114  DGtal::Color col;
115 
116  out.open(filename.c_str());
117 
118  //PPM format
119  out << "P3-3D"<<endl;
120  out << "#DGtal PNM Writer"<<endl<<endl;
121  out << ext[0]<<" "<< ext[1]<<" "<< ext[2]<<endl;
122  out << "255" <<endl;
123 
124  //We scan the domain instead of the image becaus we cannot
125  //trust the image container Iterator
126  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
127  it!=itend;
128  ++it)
129  {
130 
131  val = aImage( (*it) );
132  col = aFunctor( val );
133  out << (int)col.red()<<" "<<(int)col.green()<<" "<<(int)col.blue()<<" "; }
134 
135  out.close();
136 
138  return true;
139 }
140 
141 
142 }//namespace