save.hh 4.37 KB
Newer Older
Roland Levillain's avatar
Roland Levillain committed
1
2
// Copyright (C) 2008, 2009, 2010 EPITA Research and Development
// Laboratory (LRDE)
Edwin Carlinet's avatar
Edwin Carlinet committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.

#ifndef MLN_IO_FLD_SAVE_HH
# define MLN_IO_FLD_SAVE_HH
Roland Levillain's avatar
Roland Levillain committed
29

Edwin Carlinet's avatar
Edwin Carlinet committed
30
31
32
33
34
/// \file
/// \brief Save an image to AVS field file format.
///
/// \todo Handle not high speed images.

Roland Levillain's avatar
Roland Levillain committed
35
36
# include <fstream>

Edwin Carlinet's avatar
Edwin Carlinet committed
37
38
39
40
41
42
43
44
# include <mln/core/concept/image.hh>
# include <mln/core/concept/gpoint.hh>
# include <mln/io/fld/header.hh>
# include <mln/io/fld/write_header.hh>
# include <mln/io/fld/max_components.hh>

# include <mln/algebra/vec.hh>

Roland Levillain's avatar
Roland Levillain committed
45
# include <mln/geom/bbox.hh>
Edwin Carlinet's avatar
Edwin Carlinet committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

namespace mln
{

  namespace io
  {

    namespace fld
    {

      template <typename I>
      void save(const Image<I>& ima_, const char* filename);


# ifndef MLN_INCLUDE_ONLY

      namespace internal
      {
	template <typename I>
	inline
	void save_data_contiguous(std::ofstream& file, const I& ima)
	{
	  typedef mln_site(I) P;
	  typedef mln_value(I) V;
	  enum { dim = P::dim };

	  P pmin = ima.domain().pmin();
	  P pmax = ima.domain().pmax();

	  std::size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
	  std::size_t n = len * sizeof(V);
	  P p = pmin;
	  if (dim == 1)
	    {
	      file.write((char*)(&ima(p)), n);
	      return;
	    }

	  while (true)
	    {
	      file.write((char*)(&ima(p)), n);
	      ++p[dim - 2];

	      for (int i = dim - 2; p[i] > pmax[i]; --i)
		{
		  if (i == 0)
		    return;
		  p[i] = pmin[i];
		  ++p[i - 1];
		}
	    }
	}

	template <typename I>
	inline
	fld::fld_header make_header(const I& ima)
	{
	  fld_header hdr;
	  typedef mln_site(I) P;
	  typedef mln_value(I) V;
	  enum { dim = P::dim };

	  hdr.ndim = dim;
	  hdr.nspace = dim;
	  hdr.veclen = mln_dim(V);
	  hdr.dim = new int[dim];
	  hdr.min_ext = new float[dim];
	  hdr.max_ext = new float[dim];

	  box<P> bbox = geom::bbox(ima);
	  P pmin = bbox.pmin();
	  P pmax = bbox.pmax();

	  for (unsigned i = 0; i < dim; i++)
	    {
	      hdr.dim[i] = pmax[i] - pmin[i] + 1;
	      hdr.min_ext[i] = pmin[i];
	      hdr.max_ext[i] = pmax[i];
	    }

	  unsigned max_c = max_component(V ());
	  if (max_c == max_component(data_type::BYTE))
	    hdr.data = data_type::BYTE;
	  else if (max_c == max_component(data_type::SHORT))
	    hdr.data = data_type::SHORT;
	  else if (max_c == max_component(data_type::INTEGER))
	    hdr.data = data_type::INTEGER;
	  else if (max_c == max_component(data_type::FLOAT))
	    hdr.data = data_type::FLOAT;
	  else if (max_c == max_component(data_type::DOUBLE))
	    hdr.data = data_type::DOUBLE;
	  else
	    hdr.data = data_type::UNKNOWN;

	  hdr.field = field_type::UNIFORM;

	  return hdr;
	}

      } // end of namespace mln::io::fld::internal

      template <typename I>
      void save(const Image<I>& ima_, const char* filename)
      {
	trace::entering("mln::io::fld::save");
	// For the moment, just the fast version.
	mlc_is(mln_trait_image_speed(I), trait::image::speed::fastest)::check();

	const I& ima = exact(ima_);
	mln_precondition(ima.is_valid());

	std::ofstream file(filename);
	fld_header hdr = internal::make_header(ima);

	write_header(file, hdr);
	internal::save_data_contiguous(file, ima);

	file.close();
	trace::exiting("mln::io::fld::save");
      }


# endif // ! MLN_INCLUDE_ONLY

    } // end of namespace mln::io::fld

  } // end of namespace mln::io

} // end of namespace mln
#endif // !MLN_IO_FLD_SAVE_HH