$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pbm/load.hh
1 // Copyright (C) 2009, 2012 EPITA Research and Development Laboratory
2 // (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_LOAD_HH
28 # define MLN_IO_PBM_LOAD_HH
29 
34 
35 
36 # include <iostream>
37 # include <fstream>
38 # include <string>
39 
40 # include <mln/core/image/image2d.hh>
41 # include <mln/core/image/image3d.hh>
42 # include <mln/io/pnm/load_header.hh>
43 
44 
45 namespace mln
46 {
47 
48  namespace io
49  {
50 
51  namespace pbm
52  {
53 
54 
63  void load(image2d<bool>& ima,
64  const std::string& filename);
65 
74  image2d<bool> load(const std::string& filename);
75 
76 
77 # ifndef MLN_INCLUDE_ONLY
78 
79  namespace internal
80  {
81 
83  template <typename I>
84  inline
85  void load_ascii(std::ifstream& file, I& ima)
86  {
87  mln_fwd_piter(I) p(ima.domain());
88  for_all(p)
89  {
90  unsigned char value;
91  file >> value;
92 
93  mln_assertion(value == '0' || value == '1');
94  ima(p) = (value == '0'); // In pbm, '0' means 'white' so 'object', thus 'true'!
95  }
96  }
97 
98 
100  template <typename I>
101  inline
102  void load_raw_2d(std::ifstream& file, I& ima)
103  {
104  point2d p = point2d(0, ima.domain().pmin().col());
105  typedef mln_value(I) V;
106  const mln_deduce(I, site, coord)
107  min_row = geom::min_row(ima),
108  max_row = geom::max_row(ima),
109  min_col = geom::min_col(ima),
110  max_col = geom::max_col(ima);
111 
112  char c;
113  int i;
114  for (p.row() = min_row; p.row() <= max_row; ++p.row())
115  {
116  i = 0;
117  for (p.col() = min_col; p.col() <= max_col; ++p.col())
118  {
119  if (i % 8 == 0)
120  file.read((char*)(&c), 1);
121  ima(p) = !(c & 128);
122  c = static_cast<char>(c * 2);
123  ++i;
124  }
125  }
126  }
127 
128 
129  } // end of namespace mln::io::internal
130 
131 
132  inline
133  image2d<bool> load(const std::string& filename)
134  {
135  mln_trace("mln::io::pbm::load");
136  std::ifstream file(filename.c_str());
137  if (! file)
138  {
139  std::cerr << "error: file '" << filename
140  << "' not found!";
141  abort();
142  }
143  char type;
144  int nrows, ncols;
145  io::pnm::read_header('1', '4', file, type, nrows, ncols);
146 
147  image2d<bool> ima(nrows, ncols);
148  if (type == '4')
149  internal::load_raw_2d(file, ima);
150  else
151  if (type == '1')
152  internal::load_ascii(file, ima);
153 
154 
155  return ima;
156  }
157 
158 
159  inline
160  void load(image2d<bool>& ima,
161  const std::string& filename)
162  {
163  ima = load(filename);
164  }
165 
166 
167 # endif // ! MLN_INCLUDE_ONLY
168 
169  } // end of namespace mln::io::pbm
170 
171  } // end of namespace mln::io
172 
173 } // end of namespace mln
174 
175 
176 #endif // ! MLN_IO_PBM_LOAD_HH