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
00033
00034 # include <mln/core/image/image2d.hh>
00035 # include <mln/value/int_u8.hh>
00036 # include <mln/value/rgb8.hh>
00037 # include <Magick++.h>
00038
00039
00040 namespace mln
00041 {
00042
00043 namespace io
00044 {
00045
00046 namespace magick
00047 {
00048
00055 template <typename I>
00056 void load(Image<I>& ima, const std::string& filename);
00057
00064
00065
00066
00067
00068 # ifndef MLN_INCLUDE_ONLY
00069
00070 inline
00071 bool do_it(const value::rgb8& in, bool& out, const std::string& filename)
00072 {
00073 if (in.red() == 255u && in.green() == 255u && in.blue() == 255u)
00074 {
00075 out = true;
00076 return true;
00077 }
00078 if (in.red() == 0u && in.green() == 0u && in.blue() == 0u)
00079 {
00080 out = false;
00081 return true;
00082 }
00083 if (in.red() == in.green() && in.green() == in.blue())
00084 std::cerr << "error: trying to load '" << filename << "' which is a grayscale image into a bool image" << std::endl;
00085 else
00086 std::cerr << "error: trying to load '" << filename << "' which is a truecolor image into a bool image" << std::endl;
00087 return false;
00088 }
00089
00090 inline
00091 bool do_it(const value::rgb8& in, value::int_u8& out, const std::string& filename)
00092 {
00093 if (in.red() == in.green() && in.green() == in.blue())
00094 {
00095 out = in.red();
00096 return true;
00097 }
00098 std::cerr << "error: trying to load '" << filename << "' which is a truecolor image into a grayscale image" << std::endl;
00099 return false;
00100 }
00101
00102 inline
00103 bool do_it(const value::rgb8& in, value::rgb8& out, const std::string& filename)
00104 {
00105 (void) filename;
00106 out = in;
00107 return true;
00108 }
00109
00110
00111 template <typename I>
00112 inline
00113 void load(Image<I>& ima_, const std::string& filename)
00114 {
00115 trace::entering("mln::io::magick::load");
00116
00117 I& ima = exact(ima_);
00118
00119
00120
00121
00122
00123
00124
00125
00126 Magick::Image im_file(filename);
00127 im_file.modifyImage();
00128 im_file.type(Magick::TrueColorType);
00129 int columns = im_file.columns();
00130 int rows = im_file.rows();
00131
00132
00133
00134
00135
00136
00137 const Magick::PixelPacket *pixel_cache = im_file.getConstPixels(0, 0, columns, rows);
00138
00139 algebra::vec<mln_site_(I)::dim, unsigned int> vmin;
00140 algebra::vec<mln_site_(I)::dim, unsigned int> vmax;
00141 vmin[0] = 0;
00142 vmin[1] = 0;
00143 vmax[0] = rows - 1;
00144 vmax[1] = columns - 1;
00145 mln_site(I) pmin(vmin);
00146 mln_site(I) pmax(vmax);
00147 mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
00148 initialize(ima, result);
00149 mln_piter(I) p(ima.domain());
00150 for_all(p)
00151 {
00152 const Magick::PixelPacket *pixel = pixel_cache + (int) p.to_site().to_vec()[0] * columns
00153 + (int) p.to_site().to_vec()[1];
00154
00155 value::rgb8 pix(pixel->red % 256, pixel->green % 256, pixel->blue % 256);
00156 mln_value(I) res;
00157 if (!do_it(pix, res, filename))
00158 abort();
00159 ima(p) = res;
00160 }
00161
00162 trace::exiting("mln::io::magick::load");
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 # endif // ! MLN_INCLUDE_ONLY
00183
00184 }
00185
00186 }
00187
00188 }
00189
00190
00191 #endif // ! MLN_IO_MAGICK_LOAD_HH