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.
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.
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/>.
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
24 * Implementation of inline methods defined in PGMWriter.h
26 * This file is part of the DGtal library.
30 //////////////////////////////////////////////////////////////////////////////
33 #include "DGtal/io/Color.h"
34 //////////////////////////////////////////////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////////
37 // IMPLEMENTATION of inline methods.
38 ///////////////////////////////////////////////////////////////////////////////
43 template<typename I,typename C>
45 PGMWriter<I,C>::exportPGM(const std::string & filename, const I & aImage,
46 const Functor & aFunctor, bool saveASCII, bool topbotomOrder)
48 BOOST_STATIC_ASSERT(I::Domain::dimension == 2);
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;
56 out.open(filename.c_str(), std::ios::out | std::ios::binary);
60 out << "P2"<<std::endl;
62 out << "P5"<<std::endl;
65 out << "#DGtal PNM Writer"<<std::endl;
66 out << size[0]<<" "<< size[1]<<std::endl;
67 out << "255" <<std::endl;
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();
77 val = aImage( (*it) );
79 out << ((int) aFunctor(val) )<<" ";
81 out << ((char)((int) aFunctor(val)));
85 typename I::Domain::Point ptUpper= domain.upperBound();
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)
89 typename I::Domain::Point ptUpperY= *itY;
91 for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstIterator it = domain.subRange(0, ptUpperY).begin(), itend=domain.subRange(0, ptUpperY).end();
95 val = aImage( (*it) );
97 out << ((int) aFunctor(val))<<" ";
99 out << ((char) aFunctor(val));
108 ///@todo catch IOerror excpetion
112 template<typename I,typename C>
114 PGMWriter<I,C>::exportPGM3D(const std::string & filename, const I & aImage,
115 const Functor&aFunctor, bool saveASCII)
118 ///@todo the Value of I should match with the one in C
120 BOOST_STATIC_ASSERT(I::Domain::dimension == 3);
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;
129 out.open(filename.c_str(), std::ios::out | std::ios::binary);
131 std::string extension = filename.substr(filename.find_last_of(".") + 1);
135 out <<( (extension=="pgm")? "P2": "P2-3D" )<< std::endl;
137 out << ( (extension=="pgm")? "P5" : "P5-3D")<<std::endl;
139 out << "#DGtal PNM Writer"<<std::endl;
140 out << size[0]<<" "<< size[1]<<" "<< size[2]<<std::endl;
141 out << "255" <<std::endl;
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();
150 val = aImage( (*it) );
152 out << ((int) aFunctor( val ))<<" ";
154 out << ((char)((int) aFunctor(val)));
161 ///@todo catch IOerror excpetion