30 #ifndef MLN_CONVERT_TO_QIMAGE_HH
31 # define MLN_CONVERT_TO_QIMAGE_HH
33 # include <QtGui/QImage>
35 # include <mln/core/concept/image.hh>
36 # include <mln/geom/nrows.hh>
37 # include <mln/geom/ncols.hh>
38 # include <mln/border/resize.hh>
41 # include <mln/value/qt/rgb32.hh>
42 # include <mln/value/rgb8.hh>
45 # include <mln/core/alias/dpoint2d.hh>
47 # if QT_VERSION < 0x040000
48 # error "Your version of Qt is too old and is not supported."
67 # ifndef MLN_INCLUDE_ONLY
77 QImage to_qimage_scalar(
const Image<I>& ima_)
79 const I& ima = exact(ima_);
80 mln_precondition(ima.is_valid());
86 # if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
87 QImage qima(ncols, nrows, QImage::Format_RGB32);
88 uchar * ptr_qima = qima.bits();
90 # if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
92 # endif // ! Q_BYTE_ORDER
94 QImage qima(ncols, nrows, QImage::Format_RGB888);
95 uchar * ptr_qima = qima.bits();
96 unsigned padding =
ncols % 4;
97 # endif // ! QT_VERSION
99 const mln_value(I)* ptr_ima = &ima(ima.domain().pmin());
100 unsigned offset = ima.delta_index(
dpoint2d(+1, - ncols));
103 for (
unsigned row = 0; row < nrows; ++row, ptr_ima += offset)
105 for (
unsigned col = 0; col <
ncols; ++col)
107 const mln_value(I)& v = *ptr_ima++;
108 std::memset(ptr_qima, v,
sizeof(mln_value(I)) * 3);
110 # if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
114 # endif // ! QT_VERSION
124 # if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
126 template <
typename I>
128 QImage to_qimage_rgb8(
const Image<I>& ima_)
130 const I& ima = exact(ima_);
131 mln_precondition(ima.is_valid());
137 QImage qima(ncols, nrows, QImage::Format_RGB32);
138 uchar * ptr_qima = qima.scanLine(0);
139 const mln_value(I)* ptr_ima = &ima(ima.domain().pmin());
140 unsigned offset = ima.delta_index(
dpoint2d(+1, - ncols));
142 for (
unsigned row = 0; row < nrows; ++row, ptr_ima += offset)
144 for (
unsigned col = 0; col < ncols; ++col)
147 # if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
149 *ptr_qima++ = v.blue();
150 *ptr_qima++ = v.green();
156 *ptr_qima++ = v.
red();
157 *ptr_qima++ = v.green();
158 *ptr_qima = v.blue();
159 # endif // ! Q_BYTE_ORDER
167 template <
typename I>
169 QImage to_qimage_rgb8(
const Image<I>& ima_)
171 const I& ima = exact(ima_);
172 mln_precondition(ima.is_valid());
179 typedef mln_site(I) P;
180 typedef mln_dpsite(P) DP;
183 mln::border::resize(ima, 0);
186 offset = ima.delta_index(DP(+1, 0)),
189 QImage qima(ncols, nrows, QImage::Format_RGB888);
190 for (
int i = 0 ; i < qima.height(); ++i, line_offset += offset)
191 std::memcpy(qima.scanLine(i),
192 ima.buffer() + line_offset,
197 # endif // ! QT_VERSION
200 template <
typename I>
202 QImage to_qimage_qt_rgb32(
const Image<I>& ima_)
204 const I& ima = exact(ima_);
205 mln_precondition(ima.is_valid());
215 QImage qima(ncols, nrows, QImage::Format_RGB32);
216 std::memcpy(qima.bits(), ima.buffer(), ima.nelements() * 4);
231 template <
typename I,
typename S>
233 QImage to_qimage_dispatch(
const Image<I>& ima,
const value::Scalar<S>&)
235 return impl::to_qimage_scalar(ima);
238 template <
typename I>
240 QImage to_qimage_dispatch(
const Image<I>& ima,
const value::rgb8&)
242 return impl::to_qimage_rgb8(ima);
245 template <
typename I>
247 QImage to_qimage_dispatch(
const Image<I>& ima,
const value::qt::rgb32&)
249 return impl::to_qimage_qt_rgb32(ima);
253 template <
typename I,
typename V>
255 QImage to_qimage_dispatch(
const Image<I>& ima, V&)
260 mlc_abort(I)::check();
265 template <
typename I>
267 QImage to_qimage_dispatch(
const Image<I>& ima)
269 typedef mln_value(I) V;
270 return to_qimage_dispatch(ima, V());
279 template <typename I>
283 trace::entering(
"convert::to_qimage");
285 const I& ima = exact(ima_);
286 mln_precondition(ima.is_valid());
288 QImage output = internal::to_qimage_dispatch(ima);
290 trace::exiting(
"convert::to_qimage");
295 # endif // ! MLN_INCLUDE_ONLY
301 #endif // ! MLN_CONVERT_TO_QIMAGE_HH