DGtal 1.4.0
Loading...
Searching...
No Matches
PPMWriter.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 PPMWriter.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 PPMWriter.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#include "DGtal/kernel/domains/HyperRectDomain.h"
35//////////////////////////////////////////////////////////////////////////////
36
37///////////////////////////////////////////////////////////////////////////////
38// IMPLEMENTATION of inline methods.
39///////////////////////////////////////////////////////////////////////////////
40
41
42namespace DGtal {
43
44 template<typename I,typename C>
45 bool
46 PPMWriter<I,C>::exportPPM(const std::string & filename, const I & aImage,
47 const Functor & aFunctor, bool topbotomOrder)
48 {
49 BOOST_STATIC_ASSERT(I::Domain::dimension == 2);
50
51 std::ofstream out;
52 typename I::Domain domain = aImage.domain();
53 typename I::Domain::Point p = I::Domain::Point::diagonal(1);
54 typename I::Domain::Vector size = (domain.upperBound() - domain.lowerBound()) + p;
55
56 typename I::Value val;
57 DGtal::Color col;
58 out.open(filename.c_str(), std::ios::out | std::ios::binary);
59
60 //PPM format
61 out << "P3"<<std::endl;
62 out << "#DGtal PNM Writer"<<std::endl<<std::endl;
63 out << size[0]<<" "<< size[1]<<std::endl;
64 out << "255" <<std::endl;
65
66 if(!topbotomOrder)
67 {
68 //We scan the domain instead of the image becaus we cannot
69 //trust the image container Iterator
70 for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
71 it!=itend;
72 ++it)
73 {
74 val = aImage( (*it) );
75 col = aFunctor( val );
76 out << (int)col.red()<<" "<<(int)col.green()<<" "<<(int)col.blue()<<" ";
77 }
78 }
79 else
80 {
81 typename I::Domain::Point ptUpper= domain.upperBound();
82
83 for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstReverseIterator itY = domain.subRange(1, ptUpper).rbegin(),
84 itYend=domain.subRange(1, ptUpper).rend(); itY!=itYend; ++itY)
85 {
86 typename I::Domain::Point ptUpperY= *itY;
87
88 for(typename HyperRectDomain<typename I::Domain::Space>::ConstSubRange::ConstIterator it = domain.subRange(0, ptUpperY).begin(),
89 itend=domain.subRange(0, ptUpperY).end();
90 it!=itend;
91 ++it)
92 {
93 val = aImage( (*it) );
94 col = aFunctor( val );
95 out << (int)col.red()<<" "<<(int)col.green()<<" "<<(int)col.blue()<<" ";
96 }
97 }
98 }
99
100 out.close();
101
102 ///@todo catch IOerror excpetion
103 return true;
104 }
105
106 template<typename I,typename C>
107bool
108PPMWriter<I,C>::exportPPM3D(const std::string & filename, const I & aImage,
109 const Functor&aFunctor)
110{
111 BOOST_STATIC_ASSERT(I::Domain::dimension == 3);
112
113 std::ofstream out;
114 typename I::Domain domain(aImage.lowerBound(), aImage.upperBound());
115 typename I::Domain::Point p = I::Domain::Point::diagonal(1);
116 typename I::Domain::Vector size = (domain.upperBound() - domain.lowerBound()) + p;
117
118 typename I::Value val;
119 DGtal::Color col;
120
121 out.open(filename.c_str(), std::ios::out | std::ios::binary);
122
123 //PPM format
124 out << "P3-3D"<<std::endl;
125 out << "#DGtal PNM Writer"<<std::endl<<std::endl;
126 out << size[0]<<" "<< size[1]<<" "<< size[2]<<std::endl;
127 out << "255" <<std::endl;
128
129 //We scan the domain instead of the image becaus we cannot
130 //trust the image container Iterator
131 for(typename I::Domain::ConstIterator it = domain.begin(), itend=domain.end();
132 it!=itend;
133 ++it)
134 {
135
136 val = aImage( (*it) );
137 col = aFunctor( val );
138 out << (int)col.red()<<" "<<(int)col.green()<<" "<<(int)col.blue()<<" "; }
139
140 out.close();
141
142 ///@todo catch IOerror excpetion
143 return true;
144}
145
146
147}//namespace