• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

load.hh

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

Generated on Tue Oct 4 2011 15:24:03 for Milena (Olena) by  doxygen 1.7.1