$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pbm/save.hh
1 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
2 // 2011, 2012 EPITA Research and Development Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_IO_PBM_SAVE_HH
28 # define MLN_IO_PBM_SAVE_HH
29 
34 
35 # include <iostream>
36 # include <fstream>
37 
38 # include <mln/geom/size2d.hh>
39 # include <mln/metal/equal.hh>
40 # include <mln/metal/bexpr.hh>
41 
42 # include <mln/io/pnm/save.hh>
43 
44 
45 namespace mln
46 {
47 
48  // Fwd decl.
49  namespace value {
50  template <unsigned> struct int_u;
51  template <unsigned> struct int_u_sat;
52  }
53 
54 
55  namespace io
56  {
57 
58  namespace pbm
59  {
60 
68  template <typename I>
69  void save(const Image<I>& ima, const std::string& filename);
70 
71 
72 # ifndef MLN_INCLUDE_ONLY
73 
74  namespace impl
75  {
76 
77  template <typename I>
78  inline
79  void save_(const Image<I>& ima_, const std::string& filename)
80  {
81  const I& ima = exact(ima_);
82  std::ofstream file(filename.c_str());
83 
84  io::pnm::save_header(PBM, ima, filename, file);
85 
87  ncols = static_cast<def::coord>(geom::ncols(ima)),
88  col = 0,
89  stride = 0;
90  unsigned char c = 0;
91 
92  mln_fwd_piter(I) p(ima.domain());
93  for_all(p)
94  {
95  c = static_cast<unsigned char>(c << 1);
96  if (ima(p) == false)
97  ++c; // In pbm, '0' means 'white' so 'object', thus 'true'!
98  if (++col >= ncols)
99  {
100  c = static_cast<unsigned char>(c << (8 - stride - 1));
101  file << c;
102  c = 0;
103  col = stride = 0;
104  }
105  else
106  if (++stride >= 8)
107  {
108  file << c;
109  c = 0;
110  stride = 0;
111  }
112  }
113  mln_postcondition(stride == 0);
114  }
115 
116  } // end of namespace mln::io::impl
117 
118 
119  template <typename I>
120  inline
121  void save(const Image<I>& ima, const std::string& filename)
122  {
123  mln_trace("mln::io::pbm::save");
124  mln::metal::equal<mln_value(I), bool >::check();
125  impl::save_(exact(ima), filename);
126  }
127 
128 # endif // ! MLN_INCLUDE_ONLY
129 
130  } // end of namespace mln::pbm
131 
132  } // end of namespace mln::io
133 
134 } // end of namespace mln
135 
136 
137 #endif // ! MLN_IO_PBM_SAVE_HH