Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
pbm/save.hh
1 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
2 // 2011 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 
66  template <typename I>
67  void save(const Image<I>& ima, const std::string& filename);
68 
69 
70 # ifndef MLN_INCLUDE_ONLY
71 
72  namespace impl
73  {
74 
75  template <typename I>
76  inline
77  void save_(const Image<I>& ima_, const std::string& filename)
78  {
79  const I& ima = exact(ima_);
80  std::ofstream file(filename.c_str());
81 
82  io::pnm::save_header(PBM, ima, filename, file);
83 
85  ncols = static_cast<def::coord>(geom::ncols(ima)),
86  col = 0,
87  stride = 0;
88  unsigned char c = 0;
89 
90  mln_fwd_piter(I) p(ima.domain());
91  for_all(p)
92  {
93  c = static_cast<unsigned char>(c << 1);
94  if (ima(p) == false)
95  ++c; // In pbm, '0' means 'white' so 'object', thus 'true'!
96  if (++col >= ncols)
97  {
98  c = static_cast<unsigned char>(c << (8 - stride - 1));
99  file << c;
100  c = 0;
101  col = stride = 0;
102  }
103  else
104  if (++stride >= 8)
105  {
106  file << c;
107  c = 0;
108  stride = 0;
109  }
110  }
111  mln_postcondition(stride == 0);
112  }
113 
114  } // end of namespace mln::io::impl
115 
116 
117  template <typename I>
118  inline
119  void save(const Image<I>& ima, const std::string& filename)
120  {
121  trace::entering("mln::io::pbm::save");
123  impl::save_(exact(ima), filename);
124  trace::exiting("mln::io::pbm::save");
125  }
126 
127 # endif // ! MLN_INCLUDE_ONLY
128 
129  } // end of namespace mln::pbm
130 
131  } // end of namespace mln::io
132 
133 } // end of namespace mln
134 
135 
136 #endif // ! MLN_IO_PBM_SAVE_HH