27 #ifndef MLN_IO_FLD_SAVE_HH
28 # define MLN_IO_FLD_SAVE_HH
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>
43 # include <mln/algebra/vec.hh>
45 # include <mln/geom/bbox.hh>
57 void save(
const Image<I>& ima_,
const char* filename);
60 # ifndef MLN_INCLUDE_ONLY
66 void save_data_contiguous(std::ofstream& file,
const I& ima)
68 typedef mln_site(I) P;
69 typedef mln_value(I) V;
70 enum { dim = P::dim };
72 P pmin = ima.domain().pmin();
73 P pmax = ima.domain().pmax();
75 std::size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
76 std::size_t n = len *
sizeof(V);
80 file.write((
char*)(&ima(p)), n);
86 file.write((
char*)(&ima(p)), n);
89 for (
int i = dim - 2; p[i] > pmax[i]; --i)
101 fld::fld_header make_header(
const I& ima)
104 typedef mln_site(I) P;
105 typedef mln_value(I) V;
106 enum { dim = P::dim };
110 hdr.veclen = mln_dim(V);
111 hdr.dim =
new int[dim];
112 hdr.min_ext =
new float[dim];
113 hdr.max_ext =
new float[dim];
116 P pmin = bbox.pmin();
117 P pmax = bbox.pmax();
119 for (
unsigned i = 0; i < dim; i++)
121 hdr.dim[i] = pmax[i] - pmin[i] + 1;
122 hdr.min_ext[i] = pmin[i];
123 hdr.max_ext[i] = pmax[i];
126 unsigned max_c = max_component(V ());
127 if (max_c == max_component(data_type::BYTE))
128 hdr.data = data_type::BYTE;
129 else if (max_c == max_component(data_type::SHORT))
130 hdr.data = data_type::SHORT;
131 else if (max_c == max_component(data_type::INTEGER))
132 hdr.data = data_type::INTEGER;
133 else if (max_c == max_component(data_type::FLOAT))
134 hdr.data = data_type::FLOAT;
135 else if (max_c == max_component(data_type::DOUBLE))
136 hdr.data = data_type::DOUBLE;
138 hdr.data = data_type::UNKNOWN;
140 hdr.field = field_type::UNIFORM;
147 template <
typename I>
148 void save(
const Image<I>& ima_,
const char* filename)
150 trace::entering(
"mln::io::fld::save");
152 mlc_is(mln_trait_image_speed(I), trait::image::speed::fastest)::check();
154 const I& ima = exact(ima_);
155 mln_precondition(ima.is_valid());
157 std::ofstream file(filename);
158 fld_header hdr = internal::make_header(ima);
161 internal::save_data_contiguous(file, ima);
164 trace::exiting(
"mln::io::fld::save");
168 # endif // ! MLN_INCLUDE_ONLY
175 #endif // !MLN_IO_FLD_SAVE_HH