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

save.hh

00001 // Copyright (C) 2010 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_RAW_SAVE_HH
00027 # define MLN_IO_RAW_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 raw
00052     {
00053 
00063       //
00064       template <typename I>
00065       void save(const Image<I>& ima_, const std::string& filename);
00066 
00067 
00068 
00069 # ifndef MLN_INCLUDE_ONLY
00070 
00071 
00072       namespace internal
00073       {
00074 
00075         template <typename I>
00076         inline
00077         void save_header(const I& ima, std::ofstream& file)
00078         {
00079           // Milena's file type
00080           file << "milena/raw" << std::endl;
00081 
00082           // Dimension
00083           typedef mln_site(I) P;
00084           file << "dim: " << P::dim << std::endl;
00085 
00086           // Image size.
00087           typedef algebra::vec<P::dim, unsigned> vec_t;
00088           vec_t size = ima.domain().pmax() - ima.domain().pmin();
00089           for (unsigned i = 0; i < P::dim - 1; ++i)
00090             file << size[i] + 1 << " ";
00091           file << size[P::dim - 1] + 1 << std::endl;
00092 
00093           // Value type name
00094           // WARNING: value type name limited to 255 characters...
00095           file << "data type: " << mln_trait_value_name(mln_value(I))
00096                << std::endl;
00097 
00098           // Pmin
00099           file << "top left: ";
00100           for (unsigned i = 0; i < P::dim - 1; ++i)
00101             file << ima.domain().pmin()[i] << " ";
00102           file << ima.domain().pmin()[P::dim - 1] << std::endl;
00103 
00104           // Pmax
00105           file << "bottom right: ";
00106           for (unsigned i = 0; i < P::dim - 1; ++i)
00107             file << ima.domain().pmax()[i] << " ";
00108           file << ima.domain().pmax()[P::dim - 1] << std::endl;
00109         }
00110 
00111 
00112         template <typename I>
00113         inline
00114         void save_data(I& ima, std::ofstream& file)
00115         {
00116           // Handle padding.
00117           unsigned
00118             data_size = sizeof (mln_value(I)) + sizeof (mln_value(I)) % 2;
00119 
00120           mln_box_runstart_piter(I) p(ima.domain());
00121           for_all(p)
00122           {
00123             pixel<I> src(ima, p);
00124             file.write((char*) (&src.val()), p.run_length() * data_size);
00125           }
00126         }
00127 
00128       } // end of namespace mln::io::raw::internal
00129 
00130 
00131 
00132       // Facade
00133 
00134       template <typename I>
00135       void save(const Image<I>& ima_, const std::string& filename)
00136       {
00137         trace::entering("mln::io::raw::save");
00138 
00139         mlc_bool(mln_site_(I)::dim == 2 ||  mln_site_(I)::dim == 3)::check();
00140 
00141         const I& ima = exact(ima_);
00142 
00143         std::ofstream file(filename.c_str());
00144         if (! file)
00145         {
00146           std::cerr << "error: cannot open file '" << filename << "'!";
00147           abort();
00148         }
00149 
00150         std::string info_filename = filename + ".info";
00151         std::ofstream info_file(info_filename.c_str());
00152         if (! info_file)
00153         {
00154           std::cerr << "error: cannot open file '" << info_filename << "'!";
00155           abort();
00156         }
00157 
00158 
00159         internal::save_header(ima, info_file);
00160         internal::save_data(ima, file);
00161 
00162         info_file.close();
00163         file.close();
00164 
00165         trace::exiting("mln::io::raw::save");
00166       }
00167 
00168 
00169 # endif // ! MLN_INCLUDE_ONLY
00170 
00171     } // end of namespace mln::io::raw
00172 
00173   } // end of namespace mln::io
00174 
00175 } // end of namespace mln
00176 
00177 #endif // ! MLN_IO_RAW_SAVE_HH

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