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 #ifndef MLN_IO_MAGICK_LOAD_HH
00027 # define MLN_IO_MAGICK_LOAD_HH
00028
00037
00038 # include <cstdlib>
00039
00040 # include <Magick++.h>
00041
00042 # include <mln/core/image/image2d.hh>
00043
00044 # include <mln/value/int_u8.hh>
00045 # include <mln/value/rgb8.hh>
00046
00047
00048 namespace mln
00049 {
00050
00051 namespace io
00052 {
00053
00054 namespace magick
00055 {
00056
00061 template <typename I>
00062 void load(Image<I>& ima, const std::string& filename);
00063
00064
00065
00066 #if 0
00067
00072 template <typename T>
00073 void load(Image<tiled2d<T> >& ima, const std::string& filename);
00074 #endif
00075
00076
00077 # ifndef MLN_INCLUDE_ONLY
00078
00079 namespace impl
00080 {
00081
00082 inline
00083 bool
00084 do_it(const value::rgb8& in, bool& out)
00085 {
00086 if (in.red() != in.green() || in.green() != in.blue())
00087 {
00088 std::cerr <<
00089 "error: attempt to load what looks like a color\n"
00090 "(mln::value::rgb8) image into a Boolean (bool) image" <<
00091 std::endl;
00092 return false;
00093 }
00094 if (in.red() != 0 &&
00095 in.red() != mln_max(value::rgb8::red_t))
00096 {
00097 std::cerr <<
00098 "error: attempt to load what looks like a grayscale\n"
00099 "(mln::value::int_u8) image into a Boolean (bool) image" <<
00100 std::endl;
00101 return false;
00102 }
00103
00104 out = (in.red() != 0);
00105 return true;
00106 }
00107
00108 inline
00109 bool
00110 do_it(const value::rgb8& in, value::int_u8& out)
00111 {
00112 if (in.red() != in.green() || in.green() != in.blue())
00113 {
00114 std::cerr <<
00115 "error: attempt to load what looks like a color\n"
00116 "(mln::value::rgb8) image into a grayscale\n"
00117 "(mln::int_u8 values) image" << std::endl;
00118 return false;
00119 }
00120
00121 out = in.red();
00122 return true;
00123 }
00124
00125 inline
00126 bool
00127 do_it(const value::rgb8& in, value::rgb8& out)
00128 {
00129 out = in;
00130 return true;
00131 }
00132
00133 }
00134
00135
00136 template <typename I>
00137 inline
00138 void load(Image<I>& ima_, const std::string& filename)
00139 {
00140 trace::entering("mln::io::magick::load");
00141
00142
00143 mln::metal::equal<Magick::Quantum, unsigned char>::check();
00144
00145 I& ima = exact(ima_);
00146
00147
00148
00149 Magick::Image magick_ima(filename);
00150 magick_ima.read(filename);
00151 magick_ima.type(Magick::TrueColorType);
00152 int nrows = magick_ima.rows();
00153 int ncols = magick_ima.columns();
00154 mln_site(I) pmin(0, 0);
00155 mln_site(I) pmax(nrows - 1, ncols - 1);
00156 mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
00157 initialize(ima, result);
00158
00159 Magick::Pixels view(magick_ima);
00160
00161 Magick::PixelPacket* pixels = view.get(0, 0, ima.ncols(), ima.nrows());
00162 mln_piter(I) p(ima.domain());
00163 for_all(p)
00164 {
00165 value::rgb8 c(pixels->red, pixels->green, pixels->blue);
00166 mln_value(I) res;
00167 if (!impl::do_it(c, res))
00168 {
00169 std::cerr << "while trying to load `" << filename << "'"
00170 << std::endl;
00171 abort();
00172 }
00173 ima(p) = res;
00174 ++pixels;
00175 }
00176
00177 trace::exiting("mln::io::magick::load");
00178 }
00179
00180
00181
00182 #if 0
00183 template<typename T>
00184 inline
00185 void
00186 load(Image<tiled2d<T> >& ima_, const std::string& filename)
00187 {
00188 trace::entering("mln::io::magick::load");
00189
00190 tiled2d<T>& ima = exact(ima_);
00191
00192 tiled2d<T> result(filename);
00193
00194 ima = result;
00195 trace::exiting("mln::io::magick::load");
00196 }
00197 #endif
00198
00199
00200 # endif // ! MLN_INCLUDE_ONLY
00201
00202 }
00203
00204 }
00205
00206 }
00207
00208
00209 #endif // ! MLN_IO_MAGICK_LOAD_HH