Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
pfm/load.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_IO_PFM_LOAD_HH
27 # define MLN_IO_PFM_LOAD_HH
28 
33 
34 # include <iostream>
35 # include <fstream>
36 
37 # include <mln/core/image/image2d.hh>
38 # include <mln/value/int_u8.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace io
45  {
46 
47  namespace pfm
48  {
49 
56  void load(image2d<float>& ima,
57  const std::string& filename);
58 
65  image2d<float> load(const std::string& filename);
66 
67 # ifndef MLN_INCLUDE_ONLY
68 
69  namespace internal
70  {
71 
72  inline
73  void abort()
74  {
75  std::cerr << " aborting." << std::endl;
76  exit(0);
77  }
78 
79  inline
80  bool read_pfm_header(std::istream& file,
81  int& nrows, int& ncols,
82  bool test = false)
83  {
84  std::string tag;
85 
86  // get size
87  file >> nrows;
88  file >> ncols;
89 
90  if (file.get() != '\n')
91  goto err;
92 
93  // check tag == float
94  file >> tag;
95  if (tag != "float")
96  goto err;
97 
98  if (file.get() != '\n')
99  goto err;
100 
101  return true;
102 
103  err:
104  if (! test)
105  {
106  std::cerr << "error: badly formed header!"
107  << std::endl;
108  abort();
109  }
110  return false;
111  }
112 
113 
115  template <typename I>
116  inline
117  void load_raw_2d(std::ifstream& file, I& ima)
118  {
119  point2d p = point2d(0, 0);
120 
121  const mln_deduce(I, site, coord)
122  min_row = geom::min_row(ima),
123  max_row = geom::max_row(ima);
124 
125  unsigned int
126  ncols = geom::ncols(ima);
127 
128  for (p.row() = min_row; p.row() <= max_row; ++p.row())
129  file.read((char*)(&(ima(p))),
130  sizeof(float) * ncols);
131 
132  }
133 
134 
135  } // end of namespace mln::io::internal
136 
137  inline
138  image2d<float> load(const std::string& filename)
139  {
140  trace::entering("mln::io::pfm::load");
141 
142  std::ifstream file(filename.c_str());
143  if (! file)
144  {
145  std::cerr << "error: file '" << filename
146  << "' not found!";
147  abort();
148  }
149  int nrows, ncols;
150  internal::read_pfm_header(file, nrows, ncols);
151 
152  image2d<float> ima(nrows, ncols);
153  internal::load_raw_2d(file, ima);
154 
155  trace::exiting("mln::io::pfm::load");
156 
157  return ima;
158  }
159 
160 
161  inline
162  void load(image2d<float>& ima,
163  const std::string& filename)
164  {
165  ima = load(filename);
166  }
167 
168 # endif // ! MLN_INCLUDE_ONLY
169 
170  } // end of namespace mln::io::pfm
171 
172  } // end of namespace mln::io
173 
174 } // end of namespace mln
175 
176 
177 #endif // ! MLN_IO_PFM_LOAD_HH