00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00088
00089
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
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 }
00203
00204 }
00205
00206 }
00207
00208 #endif // ! OLENA_IO_IMAGE_WRITE_HH