31 #ifndef MLN_CONVERT_TO_QIMAGE_HH
32 # define MLN_CONVERT_TO_QIMAGE_HH
34 # include <QtGui/QImage>
36 # include <mln/core/concept/image.hh>
37 # include <mln/geom/nrows.hh>
38 # include <mln/geom/ncols.hh>
39 # include <mln/border/resize.hh>
42 # include <mln/value/qt/rgb32.hh>
43 # include <mln/value/rgb8.hh>
46 # include <mln/core/alias/dpoint2d.hh>
48 # if QT_VERSION < 0x040000
49 # error "Your version of Qt is too old and is not supported."
68 # ifndef MLN_INCLUDE_ONLY
78 QImage to_qimage_scalar(
const Image<I>& ima_)
80 const I& ima =
exact(ima_);
81 mln_precondition(ima.is_valid());
87 # if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
88 QImage qima(
ncols, nrows, QImage::Format_RGB32);
89 uchar * ptr_qima = qima.bits();
91 # if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
93 # endif // ! Q_BYTE_ORDER
95 QImage qima(
ncols, nrows, QImage::Format_RGB888);
96 uchar * ptr_qima = qima.bits();
97 unsigned padding =
ncols % 4;
98 # endif // ! QT_VERSION
100 const mln_value(I)* ptr_ima = &ima(ima.domain().pmin());
101 unsigned offset = ima.delta_offset(dpoint2d(+1, -
ncols));
104 for (
unsigned row = 0; row <
nrows; ++row, ptr_ima += offset)
106 for (
unsigned col = 0; col <
ncols; ++col)
108 const mln_value(I)& v = *ptr_ima++;
109 std::memset(ptr_qima, v,
sizeof(mln_value(I)) * 3);
111 # if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
115 # endif // ! QT_VERSION
125 # if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
127 template <
typename I>
129 QImage to_qimage_rgb8(
const Image<I>& ima_)
131 const I& ima =
exact(ima_);
132 mln_precondition(ima.is_valid());
138 QImage qima(ncols, nrows, QImage::Format_RGB32);
139 uchar * ptr_qima = qima.scanLine(0);
140 const mln_value(I)* ptr_ima = &ima(ima.domain().pmin());
141 unsigned offset = ima.delta_offset(dpoint2d(+1, - ncols));
143 for (
unsigned row = 0; row <
nrows; ++row, ptr_ima += offset)
145 for (
unsigned col = 0; col <
ncols; ++col)
148 # if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
150 *ptr_qima++ = v.
blue();
151 *ptr_qima++ = v.
green();
157 *ptr_qima++ = v.
red();
158 *ptr_qima++ = v.
green();
159 *ptr_qima = v.
blue();
160 # endif // ! Q_BYTE_ORDER
168 template <
typename I>
170 QImage to_qimage_rgb8(
const Image<I>& ima_)
172 const I& ima =
exact(ima_);
173 mln_precondition(ima.is_valid());
180 typedef mln_site(I) P;
181 typedef mln_dpsite(P) DP;
187 offset = ima.delta_offset(DP(+1, 0)),
190 QImage qima(ncols, nrows, QImage::Format_RGB888);
191 for (
int i = 0 ; i < qima.
height(); ++i, line_offset += offset)
192 std::memcpy(qima.scanLine(i),
193 ima.buffer() + line_offset,
198 # endif // ! QT_VERSION
201 template <
typename I>
203 QImage to_qimage_qt_rgb32(
const Image<I>& ima_)
205 const I& ima =
exact(ima_);
206 mln_precondition(ima.is_valid());
216 QImage qima(ncols, nrows, QImage::Format_RGB32);
217 std::memcpy(qima.bits(), ima.buffer(), ima.nelements() * 4);
232 template <
typename I,
typename S>
234 QImage to_qimage_dispatch(
const Image<I>& ima,
const value::Scalar<S>&)
236 return impl::to_qimage_scalar(ima);
239 template <
typename I>
241 QImage to_qimage_dispatch(
const Image<I>& ima,
const value::rgb8&)
243 return impl::to_qimage_rgb8(ima);
246 template <
typename I>
248 QImage to_qimage_dispatch(
const Image<I>& ima,
const value::qt::rgb32&)
250 return impl::to_qimage_qt_rgb32(ima);
254 template <
typename I,
typename V>
256 QImage to_qimage_dispatch(
const Image<I>& ima, V&)
261 mlc_abort(I)::check();
266 template <
typename I>
268 QImage to_qimage_dispatch(
const Image<I>& ima)
270 typedef mln_value(I) V;
271 return to_qimage_dispatch(ima, V());
280 template <typename I>
284 mln_trace(
"convert::to_qimage");
286 const I& ima =
exact(ima_);
287 mln_precondition(ima.is_valid());
289 QImage output = internal::to_qimage_dispatch(ima);
295 # endif // ! MLN_INCLUDE_ONLY
301 #endif // ! MLN_CONVERT_TO_QIMAGE_HH