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