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

save.hh

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_SAVE_HH
00027 # define MLN_IO_DUMP_SAVE_HH
00028 
00034 
00035 # include <iostream>
00036 # include <fstream>
00037 
00038 # include <mln/core/concept/image.hh>
00039 # include <mln/core/box_runstart_piter.hh>
00040 # include <mln/core/pixel.hh>
00041 # include <mln/data/memcpy_.hh>
00042 # include <mln/trait/value_.hh>
00043 
00044 
00045 namespace mln
00046 {
00047 
00048   namespace io
00049   {
00050 
00051     namespace dump
00052     {
00053 
00058       //
00059       template <typename I>
00060       void save(const Image<I>& ima_, const std::string& filename);
00061 
00062 
00063 
00064 # ifndef MLN_INCLUDE_ONLY
00065 
00066 
00067       namespace internal
00068       {
00069 
00070         template <typename I>
00071         inline
00072         void save_header(const I& ima,
00073                          std::ofstream& file)
00074         {
00075           // Milena's file type
00076           file << "milena/dump" << std::endl;
00077 
00078           // Dimension
00079           typedef mln_site(I) P;
00080           file << P::dim << std::endl;
00081 
00082           // Image size.
00083           typedef algebra::vec<P::dim, unsigned> vec_t;
00084           vec_t size = ima.domain().pmax() - ima.domain().pmin();
00085           for (unsigned i = 0; i < P::dim - 1; ++i)
00086             file << size[i] + 1 << " ";
00087           file << size[P::dim - 1] + 1 << std::endl;
00088 
00089           // Value type name
00090           // WARNING: value type name limited to 255 characters...
00091           file << mln_trait_value_name(mln_value(I)) << std::endl;
00092 
00093           // Empty line - may be used for a new information.
00094           file << std::endl;
00095 
00096           // Empty line - may be used for a new information.
00097           file << std::endl;
00098 
00099           // Pmin
00100           mln_site(I) p = ima.domain().pmin();
00101           file.write((char*) (&p), sizeof (P));
00102 
00103           // Pmax
00104           p = ima.domain().pmax();
00105           file.write((char*) (&p), sizeof (P));
00106         }
00107 
00108 
00109         template <typename I>
00110         inline
00111         void save_data(I& ima, std::ofstream& file)
00112         {
00113           // Handle padding.
00114           unsigned data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
00115 
00116           mln_box_runstart_piter(I) p(ima.domain());
00117           for_all(p)
00118           {
00119             pixel<I> src(ima, p);
00120             file.write((char*) (&src.val()), p.run_length() * data_size);
00121           }
00122         }
00123 
00124       } // end of namespace mln::io::dump::internal
00125 
00126 
00127 
00128       // Facade
00129 
00130       template <typename I>
00131       void save(const Image<I>& ima_, const std::string& filename)
00132       {
00133         trace::entering("mln::io::dump::save");
00134 
00135         const I& ima = exact(ima_);
00136 
00137         std::ofstream file(filename.c_str());
00138         if (! file)
00139         {
00140           std::cerr << "error: cannot open file '" << filename << "'!";
00141           abort();
00142         }
00143 
00144         internal::save_header(ima, file);
00145         internal::save_data(ima, file);
00146 
00147 
00148         file.close();
00149 
00150         trace::exiting("mln::io::dump::save");
00151       }
00152 
00153 
00154 # endif // ! MLN_INCLUDE_ONLY
00155 
00156     } // end of namespace mln::io::dump
00157 
00158   } // end of namespace mln::io
00159 
00160 } // end of namespace mln
00161 
00162 #endif // ! MLN_IO_DUMP_SAVE_HH

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