$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fld/save.hh
1 // Copyright (C) 2008, 2009, 2010 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_FLD_SAVE_HH
28 # define MLN_IO_FLD_SAVE_HH
29 
34 
35 # include <fstream>
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/concept/gpoint.hh>
39 # include <mln/io/fld/header.hh>
40 # include <mln/io/fld/write_header.hh>
41 # include <mln/io/fld/max_components.hh>
42 
43 # include <mln/algebra/vec.hh>
44 
45 # include <mln/geom/bbox.hh>
46 
47 namespace mln
48 {
49 
50  namespace io
51  {
52 
53  namespace fld
54  {
55 
60  template <typename I>
61  void save(const Image<I>& ima_, const char* filename);
62 
63 
64 # ifndef MLN_INCLUDE_ONLY
65 
66  namespace internal
67  {
68  template <typename I>
69  inline
70  void save_data_contiguous(std::ofstream& file, const I& ima)
71  {
72  typedef mln_site(I) P;
73  typedef mln_value(I) V;
74  enum { dim = P::dim };
75 
76  P pmin = ima.domain().pmin();
77  P pmax = ima.domain().pmax();
78 
79  std::size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
80  std::size_t n = len * sizeof(V);
81  P p = pmin;
82  if (dim == 1)
83  {
84  file.write((char*)(&ima(p)), n);
85  return;
86  }
87 
88  while (true)
89  {
90  file.write((char*)(&ima(p)), n);
91  ++p[dim - 2];
92 
93  for (int i = dim - 2; p[i] > pmax[i]; --i)
94  {
95  if (i == 0)
96  return;
97  p[i] = pmin[i];
98  ++p[i - 1];
99  }
100  }
101  }
102 
103  template <typename I>
104  inline
105  fld::fld_header make_header(const I& ima)
106  {
107  fld_header hdr;
108  typedef mln_site(I) P;
109  typedef mln_value(I) V;
110  enum { dim = P::dim };
111 
112  hdr.ndim = dim;
113  hdr.nspace = dim;
114  hdr.veclen = mln_dim(V);
115  hdr.dim = new int[dim];
116  hdr.min_ext = new float[dim];
117  hdr.max_ext = new float[dim];
118 
119  box<P> bbox = geom::bbox(ima);
120  P pmin = bbox.pmin();
121  P pmax = bbox.pmax();
122 
123  for (unsigned i = 0; i < dim; i++)
124  {
125  hdr.dim[i] = pmax[i] - pmin[i] + 1;
126  hdr.min_ext[i] = pmin[i];
127  hdr.max_ext[i] = pmax[i];
128  }
129 
130  unsigned max_c = max_component(V ());
131  if (max_c == max_component(data_type::BYTE))
132  hdr.data = data_type::BYTE;
133  else if (max_c == max_component(data_type::SHORT))
134  hdr.data = data_type::SHORT;
135  else if (max_c == max_component(data_type::INTEGER))
136  hdr.data = data_type::INTEGER;
137  else if (max_c == max_component(data_type::FLOAT))
138  hdr.data = data_type::FLOAT;
139  else if (max_c == max_component(data_type::DOUBLE))
140  hdr.data = data_type::DOUBLE;
141  else
142  hdr.data = data_type::UNKNOWN;
143 
144  hdr.field = field_type::UNIFORM;
145 
146  return hdr;
147  }
148 
149  } // end of namespace mln::io::fld::internal
150 
151  template <typename I>
152  void save(const Image<I>& ima_, const char* filename)
153  {
154  mln_trace("mln::io::fld::save");
155  // For the moment, just the fast version.
156  mlc_is(mln_trait_image_speed(I), trait::image::speed::fastest)::check();
157 
158  const I& ima = exact(ima_);
159  mln_precondition(ima.is_valid());
160 
161  std::ofstream file(filename);
162  fld_header hdr = internal::make_header(ima);
163 
164  write_header(file, hdr);
165  internal::save_data_contiguous(file, ima);
166 
167  file.close();
168  }
169 
170 
171 # endif // ! MLN_INCLUDE_ONLY
172 
173  } // end of namespace mln::io::fld
174 
175  } // end of namespace mln::io
176 
177 } // end of namespace mln
178 #endif // !MLN_IO_FLD_SAVE_HH