Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 00025 00026 #ifndef MLN_IO_DUMP_LOAD_HH 00027 # define MLN_IO_DUMP_LOAD_HH 00028 00032 00033 # include <iostream> 00034 # include <fstream> 00035 00036 # include <mln/core/concept/image.hh> 00037 # include <mln/core/routine/initialize.hh> 00038 # include <mln/core/box_runstart_piter.hh> 00039 # include <mln/core/pixel.hh> 00040 # include <mln/data/memcpy_.hh> 00041 00042 namespace mln 00043 { 00044 00045 namespace io 00046 { 00047 00048 namespace dump 00049 { 00050 00055 template <typename I> 00056 void load(Image<I>& ima_, const std::string& filename); 00057 00058 00059 # ifndef MLN_INCLUDE_ONLY 00060 00061 namespace internal 00062 { 00063 00064 template <typename P> 00065 inline 00066 void read_point(std::ifstream& file, P& p) 00067 { 00068 char tmp[sizeof (P)]; 00069 file.read(tmp, sizeof (P)); 00070 p = *(P*)(void*)(&tmp); 00071 } 00072 00073 00074 template <typename I> 00075 inline 00076 void load_header(Image<I>& ima, std::ifstream& file, 00077 const std::string& filename) 00078 { 00079 // Milena's file type ? 00080 std::string file_type; 00081 file >> file_type; 00082 if (file_type != "milena/dump") 00083 { 00084 std::cerr << "io::dump::load - Error: invalid file type. '" 00085 << filename 00086 << "' is NOT a valid milena/dump file!" 00087 << std::endl; 00088 abort(); 00089 } 00090 00091 // Dimension ? 00092 unsigned dim; 00093 file >> dim; 00094 typedef mln_site(I) P; 00095 if (P::dim != dim) 00096 { 00097 std::cerr << "io::dump::load - Error: invalid image dimension. '" 00098 << filename << "' is a " << dim << "-D image " 00099 << "but you try to load it into a " << P::dim 00100 << "-D image!" 00101 << std::endl; 00102 abort(); 00103 } 00104 00105 // Size information - Skip it, useless. 00106 std::string tmp; 00107 for (unsigned i = 0; i < dim; ++i) 00108 file >> tmp; 00109 // Skipping endline. 00110 char c; 00111 file.get(c); 00112 00113 // Value type name ? 00114 // WARNING: value type name limited to 255 characters... 00115 char value_type[255]; 00116 file.getline(value_type, 255); 00117 if (mln_trait_value_name(mln_value(I)) != std::string(value_type)) 00118 { 00119 std::cerr << "io::dump::load - Error: invalid image value type. '" 00120 << filename << "' is an image of '" << value_type 00121 << "' but you try to load it into an image of '" 00122 << mln_trait_value_name(mln_value(I)) << "'!" 00123 << std::endl; 00124 abort(); 00125 } 00126 00127 // Empty line - may be used for a new information. 00128 file.get(c); 00129 00130 // Empty line - may be used for a new information. 00131 file.get(c); 00132 00133 // Pmin 00134 P pmin; 00135 read_point<P>(file, pmin); 00136 00137 // Pmax 00138 P pmax; 00139 read_point<P>(file, pmax); 00140 00141 // Initialize the image buffer. 00142 mln_concrete(I) result(box<P>(pmin, pmax)); 00143 initialize(ima, result); 00144 mln_assertion(exact(ima).is_valid()); 00145 } 00146 00147 00148 template <typename I> 00149 inline 00150 void load_data(Image<I>& ima_, std::ifstream& file) 00151 { 00152 I& ima = exact(ima_); 00153 00154 // Handle padding. 00155 unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2; 00156 00157 mln_box_runstart_piter(I) p(ima.domain()); 00158 for_all(p) 00159 { 00160 pixel<I> src(ima, p); 00161 file.read((char*) (&src.val()), p.run_length() * data_size); 00162 } 00163 00164 } 00165 00166 } // end of namespace mln::io::dump::internal 00167 00168 00169 00170 template <typename I> 00171 void load(Image<I>& ima, const std::string& filename) 00172 { 00173 trace::entering("mln::io::dump::load"); 00174 00175 std::ifstream file(filename.c_str()); 00176 if (! file) 00177 { 00178 std::cerr << "io::dump::load - Error: cannot open file '" 00179 << filename << "'!" 00180 << std::endl; 00181 abort(); 00182 } 00183 00184 internal::load_header(ima, file, filename); 00185 internal::load_data(ima, file); 00186 00187 mln_postcondition(exact(ima).is_valid()); 00188 00189 trace::exiting("mln::io::dump::load"); 00190 } 00191 00192 00193 # endif // ! MLN_INCLUDE_ONLY 00194 00195 } // end of namespace mln::io::dump 00196 00197 } // end of namespace mln::io 00198 00199 } // end of namespace mln 00200 00201 #endif // ! MLN_IO_DUMP_LOAD_HH