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_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
00176
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
00189
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
00199
00200 return true;
00201 }
00202
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 }
00234
00235 }
00236
00237 }
00238
00239 #endif // ! OLENA_IO_IMAGE_READ_HH