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
00035
00036 # include <mln/core/image/image2d.hh>
00037 # include <mln/metal/equal.hh>
00038 # include <mln/value/int_u8.hh>
00039 # include <mln/value/rgb8.hh>
00040 # include <Magick++.h>
00041
00042
00043 namespace mln
00044 {
00045
00046 namespace io
00047 {
00048
00049 namespace magick
00050 {
00051
00057 template <typename I>
00058 void save(const Image<I>& ima,
00059 const std::string& filename);
00060
00066
00067
00068
00069
00070
00071 # ifndef MLN_INCLUDE_ONLY
00072
00073 inline
00074 Magick::Color get_color(bool value)
00075 {
00076 return Magick::ColorMono(value);
00077 }
00078
00079 inline
00080 Magick::Color get_color(const value::int_u8& value)
00081 {
00082 return Magick::ColorGray(256 - value);
00083 }
00084
00085 inline
00086 Magick::Color get_color(const value::rgb8& value)
00087 {
00088 return Magick::ColorRGB(256 - value.red(),
00089 256 - value.green(),
00090 256 - value.blue());
00091 }
00092
00093 template <typename I>
00094 inline
00095 void save(const Image<I>& ima_, const std::string& filename)
00096 {
00097 trace::entering("mln::io::magick::save");
00098
00099 mln_precondition(mln_site_(I)::dim == 2);
00100 const I& ima = exact(ima_);
00101 if (!(mln::metal::equal<mln_value(I), bool>::value ||
00102 mln::metal::equal<mln_value(I), value::int_u8>::value ||
00103 mln::metal::equal<mln_value(I), value::rgb8>::value))
00104 {
00105 std::cerr << "error: trying to save an unsupported format" << std::endl;
00106 std::cerr << "supported formats: binary, 8bits grayscale (int_u8), 8bits truecolor (rgb8)" << std::endl;
00107 abort();
00108 }
00109
00110 Magick::Image im_file;
00111 im_file.size(Magick::Geometry(ima.nrows(), ima.ncols()));
00112
00113 Magick::PixelPacket* pixel_cache = im_file.getPixels(0, 0, ima.nrows(), ima.ncols());
00114 Magick::PixelPacket* pixel;
00115 mln_piter(I) p(ima.domain());
00116 for_all(p)
00117 {
00118 pixel = pixel_cache + (int) p.to_site().to_vec()[0] * ima.ncols()
00119 + (int) p.to_site().to_vec()[1];
00120 *pixel = get_color(ima(p));
00121 }
00122 im_file.syncPixels();
00123 im_file.write(filename);
00124
00125 trace::exiting("mln::io::magick::save");
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 # endif // ! MLN_INCLUDE_ONLY
00142
00143 }
00144
00145 }
00146
00147 }
00148
00149
00150 #endif // ! MLN_IO_MAGICK_SAVE_HH