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_SAVE_HH
00027 # define MLN_IO_MAGICK_SAVE_HH
00028
00037
00038 # include <cstdlib>
00039
00040 # include <Magick++.h>
00041
00042 # include <mln/metal/equal.hh>
00043
00044 # include <mln/core/image/image2d.hh>
00045
00046 # include <mln/value/int_u8.hh>
00047 # include <mln/value/rgb8.hh>
00048
00049
00050 namespace mln
00051 {
00052
00053 namespace io
00054 {
00055
00056 namespace magick
00057 {
00058
00063 template <typename I>
00064 void
00065 save(const Image<I>& ima, const std::string& filename);
00066
00067
00068
00069 #if 0
00070
00074 template <typename T>
00075 void
00076 save(const Image< tiled2d<T> >& ima, const std::string& filename);
00077 #endif
00078
00079
00080 # ifndef MLN_INCLUDE_ONLY
00081
00082 namespace impl
00083 {
00084
00085 inline
00086 Magick::Color get_color(bool value)
00087 {
00088 return Magick::ColorMono(value);
00089 }
00090
00091 inline
00092 Magick::Color get_color(const value::int_u8& value)
00093 {
00094
00095 mln::metal::equal<Magick::Quantum, unsigned char>::check();
00096 return Magick::Color(value, value, value);
00097 }
00098
00099 inline
00100 Magick::Color get_color(const value::rgb8& value)
00101 {
00102
00103 mln::metal::equal<Magick::Quantum, unsigned char>::check();
00104 return Magick::Color(value.red(), value.green(), value.blue());
00105 }
00106
00107 }
00108
00109
00110 template <typename I>
00111 inline
00112 void
00113 save(const Image<I>& ima_, const std::string& filename)
00114 {
00115 trace::entering("mln::io::magick::save");
00116
00117 mln_precondition(mln_site_(I)::dim == 2);
00118
00119 if (!(mln::metal::equal<mln_value(I), bool>::value ||
00120 mln::metal::equal<mln_value(I), value::int_u8>::value ||
00121 mln::metal::equal<mln_value(I), value::rgb8>::value))
00122 {
00123 std::cerr <<
00124 "error: trying to save an unsupported format\n"
00125 "supported formats are:\n"
00126 " binary (bool)\n"
00127 " 8-bit grayscale (mln::value::int_u8)\n"
00128 " 3x8-bit truecolor (rgb8)" << std::endl;
00129 abort();
00130 }
00131
00132 const I& ima = exact(ima_);
00133
00134 Magick::Image magick_ima;
00135
00136
00137
00138 magick_ima.size(Magick::Geometry(ima.ncols(), ima.nrows()));
00139
00140 Magick::Pixels view(magick_ima);
00141
00142 Magick::PixelPacket* pixels = view.get(0, 0, ima.ncols(), ima.nrows());
00143 mln_piter(I) p(ima.domain());
00144 for_all(p)
00145 *pixels++ = impl::get_color(ima(p));
00146
00147 view.sync();
00148 magick_ima.write(filename);
00149
00150 trace::exiting("mln::io::magick::save");
00151 }
00152
00153
00154
00155 #if 0
00156 template <typename T>
00157 void
00158 save(const Image< tiled2d<T> >& ima_, const std::string& filename)
00159 {
00160 trace::entering("mln::io::magick::save");
00161
00162 tiled2d<T>& ima = exact(ima_);
00163
00164 ima.buffer().write(filename);
00165
00166 trace::exiting("mln::io::magick::save");
00167 }
00168 #endif
00169
00170
00171 # endif // ! MLN_INCLUDE_ONLY
00172
00173 }
00174
00175 }
00176
00177 }
00178
00179
00180 #endif // ! MLN_IO_MAGICK_SAVE_HH