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

load.hh

00001 // Copyright (C) 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_PFM_LOAD_HH
00027 # define MLN_IO_PFM_LOAD_HH
00028 
00033 
00034 # include <iostream>
00035 # include <fstream>
00036 
00037 # include <mln/core/image/image2d.hh>
00038 # include <mln/value/int_u8.hh>
00039 
00040 
00041 namespace mln
00042 {
00043 
00044   namespace io
00045   {
00046 
00047     namespace pfm
00048     {
00049 
00056       void load(image2d<float>& ima,
00057                 const std::string& filename);
00058 
00065       image2d<float> load(const std::string& filename);
00066 
00067 # ifndef MLN_INCLUDE_ONLY
00068 
00069       namespace internal
00070       {
00071 
00072         inline
00073         void abort()
00074         {
00075           std::cerr << " aborting." << std::endl;
00076           exit(0);
00077         }
00078 
00079         inline
00080         bool read_pfm_header(std::istream& file,
00081                              int& nrows, int& ncols,
00082                              bool test = false)
00083         {
00084           std::string tag;
00085 
00086           // get size
00087           file >> nrows;
00088           file >> ncols;
00089 
00090           if (file.get() != '\n')
00091             goto err;
00092 
00093           // check tag == float
00094           file >> tag;
00095           if (tag != "float")
00096             goto err;
00097 
00098           if (file.get() != '\n')
00099             goto err;
00100 
00101           return true;
00102 
00103         err:
00104           if (! test)
00105             {
00106               std::cerr << "error: badly formed header!"
00107                         << std::endl;
00108               abort();
00109             }
00110           return false;
00111         }
00112 
00113 
00115         template <typename I>
00116         inline
00117         void load_raw_2d(std::ifstream& file, I& ima)
00118         {
00119           point2d p = point2d(0, 0);
00120 
00121           const mln_deduce(I, site, coord)
00122             min_row = geom::min_row(ima),
00123             max_row = geom::max_row(ima);
00124 
00125           unsigned int
00126             ncols = geom::ncols(ima);
00127 
00128           for (p.row() = min_row; p.row() <= max_row; ++p.row())
00129             file.read((char*)(&(ima(p))),
00130                       sizeof(float) * ncols);
00131 
00132         }
00133 
00134 
00135       } // end of namespace mln::io::internal
00136 
00137       inline
00138       image2d<float> load(const std::string& filename)
00139       {
00140         trace::entering("mln::io::pfm::load");
00141 
00142         std::ifstream file(filename.c_str());
00143         if (! file)
00144           {
00145             std::cerr << "error: file '" << filename
00146                       << "' not found!";
00147             abort();
00148           }
00149         int nrows, ncols;
00150         internal::read_pfm_header(file, nrows, ncols);
00151 
00152         image2d<float> ima(nrows, ncols);
00153         internal::load_raw_2d(file, ima);
00154 
00155         trace::exiting("mln::io::pfm::load");
00156 
00157         return ima;
00158       }
00159 
00160 
00161       inline
00162       void load(image2d<float>& ima,
00163                 const std::string& filename)
00164       {
00165         ima = load(filename);
00166       }
00167 
00168 # endif // ! MLN_INCLUDE_ONLY
00169 
00170     } // end of namespace mln::io::pfm
00171 
00172   } // end of namespace mln::io
00173 
00174 } // end of namespace mln
00175 
00176 
00177 #endif // ! MLN_IO_PFM_LOAD_HH

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