DGtal  1.2.0
PGMWriter.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 PGMWriter.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 PGMWriter.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 #include <fstream>
33 #include "DGtal/io/Color.h"
34 //////////////////////////////////////////////////////////////////////////////
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
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  std::ofstream out;
51  typename I::Domain domain = aImage.domain();
52  typename I::Value val;
53  typename I::Domain::Point p = I::Domain::Point::diagonal(1);
54  typename I::Domain::Vector size = (domain.upperBound() - domain.lowerBound()) + p;
55 
56  out.open(filename.c_str(), std::ios::out | std::ios::binary);
57 
58  //PPM format
59  if(saveASCII){
60  out << "P2"<<std::endl;
61  }else{
62  out << "P5"<<std::endl;
63  }
64 
65  out << "#DGtal PNM Writer"<<std::endl;
66  out << size[0]<<" "<< size[1]<<std::endl;
67  out << "255" <<std::endl;
68 
69  if(!topbotomOrder){
70  //We scan the domain instead of the image becaus we cannot
71  //trust the image container Iterator
72  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
73  it!=itend;
74  ++it)
75  {
76 
77  val = aImage( (*it) );
78  if(saveASCII){
79  out << ((int) aFunctor(val) )<<" ";
80  }else{
81  out << ((char)((int) aFunctor(val)));
82  }
83  }
84  }else{
85  typename I::Domain::Point ptUpper= domain.upperBound();
86 
87  for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstReverseIterator itY = domain.subRange(1, ptUpper).rbegin(), itYend=domain.subRange(1, ptUpper).rend(); itY!=itYend; ++itY)
88  {
89  typename I::Domain::Point ptUpperY= *itY;
90 
91  for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstIterator it = domain.subRange(0, ptUpperY).begin(), itend=domain.subRange(0, ptUpperY).end();
92  it!=itend;
93  ++it)
94  {
95  val = aImage( (*it) );
96  if(saveASCII){
97  out << ((int) aFunctor(val))<<" ";
98  }else{
99  out << ((char) aFunctor(val));
100  }
101 
102  }
103  }
104  }
105 
106  out.close();
107 
108  ///@todo catch IOerror excpetion
109  return true;
110 }
111 
112 template<typename I,typename C>
113 bool
114 PGMWriter<I,C>::exportPGM3D(const std::string & filename, const I & aImage,
115  const Functor&aFunctor, bool saveASCII)
116 
117 {
118  ///@todo the Value of I should match with the one in C
119 
120  BOOST_STATIC_ASSERT(I::Domain::dimension == 3);
121 
122  std::ofstream out;
123  typename I::Domain domain(aImage.domain().lowerBound(), aImage.domain().upperBound());
124  typename I::Value val;
125  typename I::Domain::Point p = I::Domain::Point::diagonal(1);
126  typename I::Domain::Vector size = (domain.upperBound() - domain.lowerBound()) + p;
127 
128  Color col;
129  out.open(filename.c_str(), std::ios::out | std::ios::binary);
130 
131  std::string extension = filename.substr(filename.find_last_of(".") + 1);
132 
133  //PPM format
134  if(saveASCII){
135  out <<( (extension=="pgm")? "P2": "P2-3D" )<< std::endl;
136  }else{
137  out << ( (extension=="pgm")? "P5" : "P5-3D")<<std::endl;
138  }
139  out << "#DGtal PNM Writer"<<std::endl;
140  out << size[0]<<" "<< size[1]<<" "<< size[2]<<std::endl;
141  out << "255" <<std::endl;
142 
143  //We scan the domain instead of the image because we cannot
144  //trust the image container Iterator
145  for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
146  it!=itend;
147  ++it)
148  {
149 
150  val = aImage( (*it) );
151  if(saveASCII){
152  out << ((int) aFunctor( val ))<<" ";
153  }else{
154  out << ((char)((int) aFunctor(val)));
155  }
156 
157  }
158 
159  out.close();
160 
161  ///@todo catch IOerror excpetion
162  return true;
163 }
164 
165 
166 }//namespace