27 #ifndef MLN_IO_MAGICK_SAVE_HH
28 # define MLN_IO_MAGICK_SAVE_HH
41 # include <Magick++.h>
43 # include <mln/metal/equal.hh>
45 # include <mln/core/image/image2d.hh>
47 # include <mln/value/int_u8.hh>
48 # include <mln/value/rgb8.hh>
49 # include <mln/value/qt/rgb32.hh>
68 save(
const Image<I>& ima,
const std::string& filename);
80 template <
typename I,
typename J>
82 save(
const Image<I>& ima,
const Image<J>& opacity_mask,
83 const std::string& filename);
94 save(
const Image< tiled2d<T> >& ima,
const std::string& filename);
98 # ifndef MLN_INCLUDE_ONLY
104 Magick::Color get_color(
bool value)
106 return Magick::ColorMono(value);
118 (value << 8 * (
sizeof(Magick::Quantum) -
sizeof(
value::int_u8)),
119 value << 8 * (
sizeof(Magick::Quantum) -
sizeof(
value::int_u8)),
120 value << 8 * (
sizeof(Magick::Quantum) -
sizeof(
value::int_u8)));
133 (value.red() << 8 * (
sizeof(Magick::Quantum)
134 -
sizeof(value::rgb8::red_t)),
135 value.green() << 8 * (
sizeof(Magick::Quantum)
136 -
sizeof(value::rgb8::green_t)),
137 value.blue() << 8 * (
sizeof(Magick::Quantum)
138 -
sizeof(value::rgb8::blue_t)));
147 template <
typename I>
149 paste_data(
const Image<I>& ima_, Magick::Image& magick_ima)
151 const I& ima = exact(ima_);
163 magick_ima.modifyImage();
165 Magick::Pixels view(magick_ima);
167 Magick::PixelPacket* pixels = view.get(0, 0, ncols, nrows);
168 const mln_value(I) *ptr_ima = &ima(ima.domain().pmin());
170 unsigned row_offset = ima.delta_index(
dpoint2d(+1, - ncols));
173 ++row, ptr_ima += row_offset)
174 for (
def::coord col = mincol; col <= maxcol; ++col)
175 *pixels++ = impl::get_color(*ptr_ima++);
180 template <
typename I,
typename J>
182 paste_data_opacity(
const Image<I>& ima_,
183 const Image<J>& opacity_mask_,
184 Magick::Image& magick_ima)
186 const I& ima = exact(ima_);
187 const J& opacity_mask = exact(opacity_mask_);
199 magick_ima.modifyImage();
201 Magick::Pixels view(magick_ima);
203 Magick::PixelPacket* pixels = view.get(0, 0, ncols, nrows);
204 const mln_value(I) *ptr_ima = &ima(ima.domain().pmin());
205 const mln_value(J) *ptr_opacity_mask = &opacity_mask(opacity_mask.domain().pmin());
207 unsigned row_offset = ima.delta_index(
dpoint2d(+1, - ncols));
208 unsigned opacity_row_offset = opacity_mask.delta_index(
dpoint2d(+1, - ncols));
211 ++row, ptr_ima += row_offset,
212 ptr_opacity_mask += opacity_row_offset)
213 for (
def::coord col = mincol; col <= maxcol; ++col)
215 *pixels = impl::get_color(*ptr_ima++);
216 (*pixels).opacity = ((*ptr_opacity_mask++) ? 255 : 0);
226 template <
typename I,
typename J>
229 const std::string& filename)
231 trace::entering(
"mln::io::magick::save");
233 mln_precondition(mln_site_(I)::dim == 2);
241 "error: trying to save an unsupported format\n"
242 "supported formats are:\n"
244 " 8-bit grayscale (mln::value::int_u8)\n"
245 " 3x8-bit truecolor (rgb8)" << std::endl;
249 const I& ima = exact(ima_);
250 const J& opacity_mask = exact(opacity_mask_);
265 Magick::Image magick_ima(Magick::Geometry(ncols, nrows),
"white");
267 if (opacity_mask.is_valid())
269 magick_ima.type(Magick::TrueColorMatteType);
270 internal::paste_data_opacity(ima, opacity_mask, magick_ima);
274 magick_ima.type(Magick::TrueColorType);
275 internal::paste_data(ima, magick_ima);
278 magick_ima.write(filename);
280 trace::exiting(
"mln::io::magick::save");
285 template <
typename I>
290 mln_ch_value(I,
bool) opacity_mask;
291 save(ima, opacity_mask, filename);
297 template <
typename T>
299 save(
const Image< tiled2d<T> >& ima_,
const std::string& filename)
301 trace::entering(
"mln::io::magick::save");
303 tiled2d<T>& ima = exact(ima_);
305 ima.buffer().write(filename);
307 trace::exiting(
"mln::io::magick::save");
312 # endif // ! MLN_INCLUDE_ONLY
321 #endif // ! MLN_IO_MAGICK_SAVE_HH