00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MLN_IO_FLD_LOAD_HH
00027 # define MLN_IO_FLD_LOAD_HH
00028
00045
00046 # include <mln/core/concept/image.hh>
00047 # include <mln/io/fld/header.hh>
00048 # include <mln/io/fld/load_header.hh>
00049 # include <mln/io/fld/max_components.hh>
00050
00051 # include <mln/algebra/vec.hh>
00052 # include <mln/value/rgb.hh>
00053 # include <mln/value/int_u8.hh>
00054
00055 # include <mln/geom/nsites.hh>
00056
00057 # include <fstream>
00058 # include <iostream>
00059
00060 namespace mln
00061 {
00062
00063 namespace io
00064 {
00065
00066 namespace fld
00067 {
00068
00074 template <typename I>
00075 inline
00076 void
00077 load(Image<I>& ima_, const char* filename);
00078
00079 # ifndef MLN_INCLUDE_ONLY
00080
00081 namespace internal
00082 {
00083
00084 void
00085 abort_load(const char* msg, const char* filename)
00086 {
00087 std::cerr << "Error: file '" << filename << "'"
00088 << "cannot be loaded." << std::endl
00089 << "Error description: " << msg << std::endl;
00090 abort();
00091 }
00092
00093
00094 template <unsigned int n>
00095 inline
00096 void read_value(std::ifstream& file, value::rgb<n>& v)
00097 {
00098 typedef typename value::int_u<n>::enc E;
00099
00100 E c;
00101 file.read((char*)(&c), sizeof(E));
00102 v.red() = c;
00103 file.read((char*)(&c), sizeof(E));
00104 v.green() = c;
00105 file.read((char*)(&c), sizeof(E));
00106 v.blue() = c;
00107 }
00108
00109
00110 template <class V>
00111 inline
00112 void read_value(std::ifstream& file, value::Scalar<V>& v)
00113 {
00114 typedef typename V::enc E;
00115
00116 E c;
00117 file.read((char*)(&c), sizeof(E));
00118 exact(v) = c;
00119 }
00120
00121
00122 template <typename V>
00123 inline
00124 void read_value(std::ifstream& file, V& v)
00125 {
00126 V c;
00127 file.read((char*)(&c), sizeof(V));
00128 v = c;
00129 }
00130
00131
00132 template <typename I>
00133 inline
00134 void load_raw_uncontiguous(std::ifstream& file, I& ima)
00135 {
00136 mln_piter(I) p(ima.domain());
00137 read_value(file, ima(p));
00138 }
00139
00140
00141 template <typename I>
00142 inline
00143 void load_raw_contiguous(std::ifstream& file, I& ima)
00144 {
00145 mln_site(I) pmin = ima.domain().pmin();
00146 mln_site(I) pmax = ima.domain().pmax();
00147
00148 typedef mln_site(I) P;
00149 enum { dim = P::dim };
00150
00151
00152 typedef mln_value(I) V;
00153
00154
00155 std::size_t len = pmax[dim - 1] - pmin[dim - 1] + 1;
00156 std::size_t n = len * sizeof(V);
00157
00158 P p = pmin;
00159 if (dim == 1)
00160 {
00161 file.read((char*)(&ima(p)), n);
00162 return;
00163 }
00164
00165 while (true)
00166 {
00167 file.read((char*)(&ima(p)), n);
00168 ++p[dim - 2];
00169
00170 for (int i = dim - 2; p[i] > pmax[i]; --i)
00171 {
00172 if (i == 0)
00173 return;
00174 p[i] = pmin[i];
00175 ++p[i - 1];
00176 }
00177 }
00178 }
00179
00180 template <typename I>
00181 inline
00182 void load_raw(std::ifstream& file, I& ima)
00183 {
00184 if (sizeof(value::int_u8) == 1)
00185 load_raw_contiguous(file, ima);
00186 else
00187 load_raw_uncontiguous(file, ima);
00188 }
00189
00190 }
00191
00192 template <typename I>
00193 inline
00194 void
00195 load(Image<I>& ima_, const char* filename)
00196 {
00197 trace::entering("mln::io::fld::load");
00198
00199 std::ifstream file(filename);
00200 if (! file)
00201 internal::abort_load("Fail to open the file.", filename);
00202
00203 typedef mln_value(I) V;
00204 typedef mln_site(I) P;
00205
00206 I& ima = exact(ima_);
00207 fld_header hder = fld::read_header(file);
00208 int nspace = P::dim;
00209 int veclen = mln_dim(V);
00210
00211 if (nspace != hder.nspace)
00212 internal::abort_load("The dimension of the input does not match the one from the file.", filename);
00213 if (nspace > 3)
00214 internal::abort_load("The loader does not handle image dimension greater than three.", filename);
00215 if (veclen != hder.veclen)
00216 internal::abort_load("The dimension of the value does not match the one from the file.", filename);
00217 if (max_component(V ()) != max_component(hder.data))
00218 internal::abort_load("The data type of the input mismatches the one from the file.", filename);
00219
00220 box<mln_site(I)> bbox;
00221 for (int i = 0; i < hder.ndim; ++i)
00222 {
00223 bbox.pmin()[i] = hder.min_ext[i];
00224 bbox.pmax()[i] = hder.max_ext[i];
00225 }
00226
00227 ima.init_(bbox);
00228 internal::load_raw(file, ima);
00229
00230 file.close();
00231 trace::exiting("mln::io::fld::load");
00232 }
00233
00234 # endif // ! MLN_INCLUDE_ONLY
00235
00236 }
00237
00238 }
00239
00240 }
00241
00242 #endif // !MLN_IO_FLD_LOAD_HH