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 def::coord
00159 minrow = geom::min_row(ima),
00160 mincol = geom::min_col(ima),
00161 maxrow = geom::max_row(ima),
00162 maxcol = geom::max_col(ima);
00163
00164 Magick::Pixels view(magick_ima);
00165
00166 Magick::PixelPacket* pixels = view.get(0, 0, ima.ncols(), ima.nrows());
00167 mln_value(I) *ptr_ima = &ima(ima.domain().pmin());
00168
00169 unsigned row_offset = ima.delta_index(dpoint2d(+1, - ncols));
00170
00171 for (def::coord row = minrow; row <= maxrow;
00172 ++row, ptr_ima += row_offset)
00173 for (def::coord col = mincol; col <= maxcol; ++col)
00174 {
00175
00176
00177
00178
00179
00180
00181 value::rgb8 c(pixels->red >> 8 * (sizeof(Magick::Quantum)
00182 - sizeof(value::rgb8::red_t)),
00183 pixels->green >> 8 * (sizeof(Magick::Quantum)
00184 - sizeof(value::rgb8::green_t)),
00185 pixels->blue >> 8 * (sizeof(Magick::Quantum)
00186 - sizeof(value::rgb8::blue_t)));
00187 mln_value(I) res;
00188 if (!impl::do_it(c, res))
00189 {
00190 std::cerr << "while trying to load `" << filename << "'"
00191 << std::endl;
00192 abort();
00193 }
00194 *ptr_ima++ = res;
00195 ++pixels;
00196 }
00197
00198 trace::exiting("mln::io::magick::load");
00199 }
00200
00201
00202
00203 #if 0
00204 template<typename T>
00205 inline
00206 void
00207 load(Image<tiled2d<T> >& ima_, const std::string& filename)
00208 {
00209 trace::entering("mln::io::magick::load");
00210
00211 tiled2d<T>& ima = exact(ima_);
00212
00213 tiled2d<T> result(filename);
00214
00215 ima = result;
00216 trace::exiting("mln::io::magick::load");
00217 }
00218 #endif
00219
00220
00221 # endif // ! MLN_INCLUDE_ONLY
00222
00223 }
00224
00225 }
00226
00227 }
00228
00229
00230 #endif // ! MLN_IO_MAGICK_LOAD_HH