image_write.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_WRITE_HH
00029 # define OLENA_IO_IMAGE_WRITE_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 # include <oln/core/image1d.hh>
00037 
00038 # include <oln/io/image_base.hh>
00039 # include <oln/io/pnm_write_2d.hh>
00040 # include <oln/io/pnm_write_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< writer_id W, typename T >
00058       struct try_writers
00059       {
00067         static bool
00068         by_extension(const T& input, std::ostream& out, const std::string& ext)
00069         {
00070           if (image_writer<W,T>::knows_ext(ext))
00071             if (image_writer<W,T>::write(out, input))
00072               return true;
00073           return try_writers<writer_id(unsigned(W)-1), T>
00074                    ::by_extension(input, out, ext);
00075         }
00076 
00082         static bool
00083         by_data(const T& input, std::ostream& out, const std::string& ext)
00084         {
00085           if (image_writer<W,T>::write(out, input))
00086             {
00087               // std::clog << "[ambiguous extension '" << ext
00088               //        << "' wrote as " << image_writer<W,T>::name() << "] "
00089               //        << std::flush;
00090               return true;
00091             }
00092           return try_writers<writer_id(unsigned(W)-1), T>
00093                    ::by_data(input, out, ext);
00094         }
00095       };
00096 
00097 
00104       template< typename T >
00105       struct try_writers<WriteNone, T>
00106       {
00111         static bool
00112         by_extension(const T&, std::ostream&, const std::string&)
00113         {
00114           return false;
00115         }
00116 
00121         static bool
00122         by_data(const T&, std::ostream&, const std::string&)
00123         {
00124           return false;
00125         }
00126       };
00127 
00131       struct writers_trier
00132       {
00141         template <class T>
00142         static bool
00143         doit(const T& input, std::ostream& out, const std::string ext)
00144         {
00145           bool result = try_writers<WriteAny,T>::by_extension(input, out, ext);
00146           if (!result)
00147             result = try_writers<WriteAny,T>::by_data(input, out, ext);
00148           return result;
00149         }
00150       };
00151 
00152 
00159       template <class E, unsigned N>
00160       inline bool
00161       write(const abstract::image_with_dim<N, E>& input,
00162             const std::string& name)
00163       {
00164         mlc::is_true<N == 2 || N == 3>::ensure();
00165 
00166         typedef try_stream_wrappers_out<StreamAny, E, writers_trier>
00167           stream_trier;
00168 
00169         std::string ext = utils::extension(name);
00170 
00171         if (stream_trier::by_extension(input.exact(), name, ext))
00172           return true;
00173         // std::clog << "[unable to write '" << name << "'] " << std::endl;
00174         return false;
00175       }
00176 
00188       template <class E>
00189       inline bool
00190       write(const abstract::image_with_dim<1, E>& input,
00191             const std::string& name)
00192       {
00193         image2d<oln_value_type(E)> tmp(1, input.ncols());
00194         oln_iter_type(image1d<oln_value_type(E) > ) it(input);
00195         for_all(it)
00196           tmp(0, it.col()) = input[it];
00197         if (!write(tmp, name))
00198           return false;
00199         return true;
00200       }
00201 
00202     } // end of namespace internal
00203 
00204   } // end of namespace io
00205 
00206 } // end of namespace oln
00207 
00208 #endif // ! OLENA_IO_IMAGE_WRITE_HH

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