image_read.hh

00001 // Copyright (C) 2001, 2002, 2003, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library 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
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_IO_IMAGE_READ_HH
00029 # define OLENA_IO_IMAGE_READ_HH
00030 
00031 # include <mlc/bool.hh>
00032 
00033 # include <oln/config/system.hh>
00034 # include <oln/core/abstract/image_with_dim.hh>
00035 # include <oln/core/image2d.hh>
00036 
00037 # include <oln/io/image_base.hh>
00038 # include <oln/io/pnm_read.hh>
00039 # include <oln/io/pnm_read_2d.hh>
00040 # include <oln/io/pnm_read_3d.hh>
00041 # include <oln/io/stream_wrapper.hh>
00042 # include <oln/io/utils.hh>
00043 
00044 # include <list>
00045 # include <iostream>
00046 
00047 namespace oln {
00048 
00049   namespace io {
00050 
00051     namespace internal {
00052 
00057       template< reader_id R, typename T >
00058       struct try_readers
00059       {
00067         static bool
00068         by_extension(T& output, std::istream& in, const std::string& ext)
00069         {
00070           if (image_reader<R,T>::knows_ext(ext))
00071             if (image_reader<R,T>::read(in, output))
00072               return true;
00073           in.seekg(0, std::ios::beg);
00074           return try_readers<reader_id(unsigned(R)-1), T>
00075                    ::by_extension(output, in, ext);
00076         }
00077 
00084         static bool
00085         by_data(T& output, std::istream& in)
00086         {
00087           if (image_reader<R,T>::read(in, output))
00088             return true;
00089           in.seekg(0, std::ios::beg);
00090           return try_readers<reader_id(unsigned(R)-1), T>::by_data(output, in);
00091         }
00092       };
00093 
00100       template< typename T >
00101       struct try_readers<ReadNone, T>
00102       {
00107         static bool
00108         by_extension(T&, std::istream&, const std::string&)
00109         {
00110           return false;
00111         }
00112 
00117         static bool
00118         by_data(T&, std::istream&)
00119         {
00120           return false;
00121         }
00122       };
00123 
00127       struct readers_trier
00128       {
00137         template <class T>
00138         static bool
00139         doit(T& output, std::istream& in, const std::string ext)
00140         {
00141           bool result = try_readers<ReadAny,T>::by_extension(output, in, ext);
00142           if (!result)
00143             result = try_readers<ReadAny,T>::by_data(output, in);
00144           return result;
00145         }
00146       };
00147 
00158       template <unsigned N, class E>
00159       inline bool
00160       read(abstract::image_with_dim<N,E>& output, const std::string& name)
00161       {
00162         mlc::is_true<N == 2 || N == 3>::ensure();
00163 
00165         typedef try_stream_wrappers_in<StreamAny, E, readers_trier>
00166           stream_trier;
00167 
00168         std::string ext = internal::utils::extension(name);
00169 
00170         if (stream_trier::by_extension(output.exact(), name, ext))
00171           return true;
00172         if (stream_trier::by_data(output.exact(), name))
00173           return true;
00174 
00175         // No valid file found using name directly. Let's try other
00176         // possible filenames based on name.
00177 
00178         std::list<std::string> names;
00179         stream_wrappers_find_files<StreamAny>::doit(names, name);
00180 
00181         for (std::list<std::string>::iterator it = names.begin();
00182              it !=  names.end();
00183              ++it)
00184           {
00185             std::string ext = utils::extension(*it);
00186             if (stream_trier::by_extension(output.exact(), *it, ext))
00187               {
00188                 // std::clog << "[" << name << " found as " << *it << "] "
00189                 //           << std::flush;
00190                 return true;
00191               }
00192           }
00193         for (std::list<std::string>::iterator it = names.begin();
00194              it !=  names.end();
00195              ++it)
00196           if (stream_trier::by_data(output.exact(), *it))
00197             {
00198               // std::clog << "['" << name << "' found as '" << *it << "'] "
00199               //        << std::flush;
00200               return true;
00201             }
00202         // std::clog << "[unable to read '" << name << "'] " << std::endl;
00203         return false;
00204       }
00205 
00217       template <class E>
00218       inline bool
00219       read(abstract::image_with_dim<1, E>& output, const std::string& name)
00220       {
00221         image2d<oln_value_type(E)> tmp;
00222         if (!read(tmp, name))
00223           return false;
00224         if (tmp.nrows() != 1)
00225           return false;
00226         typename image2d<oln_value_type(E)>::iter_type it(tmp);
00227         output.exact() = E(tmp.ncols());
00228         for_all(it)
00229           output(it.col()) = tmp[it];
00230         return true;
00231       }
00232 
00233     } // end of namespace internal
00234 
00235   } // end of namespace io
00236 
00237 } // end of namespace oln
00238 
00239 #endif // ! OLENA_IO_IMAGE_READ_HH

Generated on Thu Apr 15 20:13:12 2004 for Olena by doxygen 1.3.6-20040222