$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pfm/load.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 EPITA Research and Development
2 // 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_PFM_LOAD_HH
28 # define MLN_IO_PFM_LOAD_HH
29 
34 
35 # include <iostream>
36 # include <fstream>
37 
38 # include <mln/core/image/image2d.hh>
39 # include <mln/value/int_u8.hh>
40 
41 
42 namespace mln
43 {
44 
45  namespace io
46  {
47 
48  namespace pfm
49  {
50 
59  void load(image2d<float>& ima,
60  const std::string& filename);
61 
70  image2d<float> load(const std::string& filename);
71 
72 # ifndef MLN_INCLUDE_ONLY
73 
74  namespace internal
75  {
76 
77  inline
78  void abort()
79  {
80  std::cerr << " aborting." << std::endl;
81  exit(0);
82  }
83 
84  inline
85  bool read_pfm_header(std::istream& file,
86  int& nrows, int& ncols,
87  bool test = false)
88  {
89  std::string tag;
90 
91  // get size
92  file >> nrows;
93  file >> ncols;
94 
95  if (file.get() != '\n')
96  goto err;
97 
98  // check tag == float
99  file >> tag;
100  if (tag != "float")
101  goto err;
102 
103  if (file.get() != '\n')
104  goto err;
105 
106  return true;
107 
108  err:
109  if (! test)
110  {
111  std::cerr << "error: badly formed header!"
112  << std::endl;
113  abort();
114  }
115  return false;
116  }
117 
118 
120  template <typename I>
121  inline
122  void load_raw_2d(std::ifstream& file, I& ima)
123  {
124  point2d p = point2d(0, 0);
125 
126  const mln_deduce(I, site, coord)
127  min_row = geom::min_row(ima),
128  max_row = geom::max_row(ima);
129 
130  unsigned int
131  ncols = geom::ncols(ima);
132 
133  for (p.row() = min_row; p.row() <= max_row; ++p.row())
134  file.read((char*)(&(ima(p))),
135  sizeof(float) * ncols);
136 
137  }
138 
139 
140  } // end of namespace mln::io::internal
141 
142  inline
143  image2d<float> load(const std::string& filename)
144  {
145  mln_trace("mln::io::pfm::load");
146 
147  std::ifstream file(filename.c_str());
148  if (! file)
149  {
150  std::cerr << "error: file '" << filename
151  << "' not found!";
152  abort();
153  }
154  int nrows, ncols;
155  internal::read_pfm_header(file, nrows, ncols);
156 
157  image2d<float> ima(nrows, ncols);
158  internal::load_raw_2d(file, ima);
159 
160 
161  return ima;
162  }
163 
164 
165  inline
166  void load(image2d<float>& ima,
167  const std::string& filename)
168  {
169  ima = load(filename);
170  }
171 
172 # endif // ! MLN_INCLUDE_ONLY
173 
174  } // end of namespace mln::io::pfm
175 
176  } // end of namespace mln::io
177 
178 } // end of namespace mln
179 
180 
181 #endif // ! MLN_IO_PFM_LOAD_HH