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

save.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 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_PNM_SAVE_HH
00027 # define MLN_IO_PNM_SAVE_HH
00028 
00033 
00034 # include <iostream>
00035 # include <fstream>
00036 
00037 # include <mln/core/concept/image.hh>
00038 # include <mln/core/alias/point2d.hh>
00039 # include <mln/value/concept/scalar.hh>
00040 
00041 # include <mln/value/rgb.hh>
00042 # include <mln/value/rgb8.hh>
00043 # include <mln/value/int_u8.hh>
00044 
00045 # include <mln/metal/templated_by.hh>
00046 # include <mln/metal/not_equal.hh>
00047 
00048 # include <mln/io/pnm/save_header.hh>
00049 # include <mln/io/pnm/macros.hh>
00050 
00051 # include <mln/geom/size2d.hh>
00052 
00053 
00054 namespace mln
00055 {
00056 
00057   namespace io
00058   {
00059 
00060     namespace pnm
00061     {
00062 
00070       template <typename I>
00071       void save(char type, const Image<I>& ima_, const std::string& filename);
00072 
00073 
00074 
00075 # ifndef MLN_INCLUDE_ONLY
00076 
00077       namespace impl
00078       {
00079 
00080         // write a rgb value into for uncontiguous datas
00081         template <unsigned int n>
00082         inline
00083         void write_value(std::ofstream& file,
00084                          const value::rgb<n>& c)
00085         {
00086           typedef typename value::int_u<n>::enc E;
00087 
00088           E v = c.red().to_enc();
00089           file.write((char*)&v, sizeof(E));
00090           v = c.green().to_enc();
00091           file.write((char*)&v, sizeof(E));
00092           v = c.blue().to_enc();
00093           file.write((char*)&v, sizeof(E));
00094         }
00095 
00096         // write a scalar value into for uncontiguous datas
00097         template <typename V>
00098         inline
00099         void write_value(std::ofstream& file,
00100                          const V& v)
00101         {
00102           mlc_not_equal(V,bool)::check();
00103           file.write((char*)(&v), sizeof(V));
00104         }
00105 
00106         // write a scalar value into for uncontiguous datas
00107         template <typename S>
00108         inline
00109         void write_value(std::ofstream& file,
00110                          const value::Scalar<S>& s)
00111         {
00112           typedef typename S::enc E;
00113 
00114           E c = s.to_enc();
00115           file.write((char*)(&c), sizeof(E));
00116         }
00117 
00118         // save data for (sizeof(int_u8) != 1) and non fastest images
00119         template <typename I>
00120         inline
00121         void save_data_uncontiguous(std::ofstream& file,
00122                                     const I& ima)
00123         {
00124           const def::coord
00125             min_row = geom::min_row(ima),
00126             max_row = geom::max_row(ima),
00127             min_col = geom::min_col(ima),
00128             max_col = geom::max_col(ima);
00129 
00130           point2d p;
00131           for (p.row() = min_row; p.row() <= max_row; ++p.row())
00132             for (p.col() = min_col; p.col() <= max_col; ++p.col())
00133               write_value(file, ima(p));
00134         }
00135 
00136         // save data when (sizeof(int_u8) == 1) with fastest images
00137         // (faster)
00138         template <typename I>
00139         inline
00140         void save_data_contiguous(std::ofstream& file,
00141                                   const I& ima_)
00142         {
00143           const I& ima = exact(ima_);
00144           const def::coord
00145             min_row = geom::min_row(ima),
00146             max_row = geom::max_row(ima);
00147           point2d p;
00148           p.col() = geom::min_col(ima);
00149           std::size_t len = geom::ncols(ima) * sizeof(mln_value(I));
00150           for (p.row() = min_row; p.row() <= max_row; ++p.row())
00151             file.write((char*)(& ima(p)), len);
00152         }
00153 
00154 
00155         // caller for fastest images
00156         template <typename I>
00157         inline
00158         void save_data_(std::ofstream& file,
00159                         trait::image::speed::fastest, const I& ima)
00160         {
00161           if (sizeof(value::int_u8) == 1)
00162             save_data_contiguous(file, ima);
00163           else
00164             save_data_uncontiguous(file, ima);
00165         }
00166 
00167         // caller for non fastest images
00168         template <typename I>
00169         inline
00170         void save_data_(std::ofstream& file,
00171                         trait::image::speed::any, const I& ima)
00172         {
00173           save_data_uncontiguous(file, ima);
00174         }
00175 
00176       } // end of namespace mln::io::pnm::impl
00177 
00178 
00179         // Facades.
00180 
00181       template <typename I>
00182       inline
00183       void save(char type, const Image<I>& ima_, const std::string& filename)
00184       {
00185         trace::entering("mln::io::pnm::save");
00186         const I& ima = exact(ima_);
00187         std::ofstream file(filename.c_str());
00188         io::pnm::save_header(type, ima, filename, file);
00189 
00190         impl::save_data_(file,
00191                          mln_trait_image_speed(I)(), ima);
00192         trace::exiting("mln::io::pnm::save");
00193       }
00194 
00195 # endif // ! MLN_INCLUDE_ONLY
00196 
00197     } // end of namespace mln::io::pnm
00198 
00199   } // end of namespace mln::io
00200 
00201 } // end of namespace mln
00202 
00203 
00204 #endif // ! MLN_IO_PNM_SAVE_HH

Generated on Thu Sep 8 2011 18:32:25 for Milena (Olena) by  doxygen 1.7.1