to_qimage.hh 6.81 KB
Newer Older
1
// Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// This file is part of Olena.
//
// Olena is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 2 of the License.
//
// Olena is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Olena.  If not, see <http://www.gnu.org/licenses/>.
//
// As a special exception, you may use this file as part of a free
// software project without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to produce
// an executable, this file does not by itself cause the resulting
// executable to be covered by the GNU General Public License.  This
// exception does not however invalidate any other reasons why the
// executable file might be covered by the GNU General Public License.

/// \file
///
/// Convert a Milena image to a Qimage.

#ifndef MLN_CONVERT_TO_QIMAGE_HH
# define MLN_CONVERT_TO_QIMAGE_HH

# include <QtGui/QImage>

# include <mln/core/concept/image.hh>
# include <mln/geom/nrows.hh>
# include <mln/geom/ncols.hh>
# include <mln/border/resize.hh>


# include <mln/value/qt/rgb32.hh>
# include <mln/value/rgb8.hh>

Guillaume Lazzara's avatar
Guillaume Lazzara committed
44
45
// FIXME: not generic.
# include <mln/core/alias/dpoint2d.hh>
46
47
48
49
50
51
52
53
54
55
56
57

# if QT_VERSION < 0x040000
#  error "Your version of Qt is too old and is not supported."
# endif


namespace mln
{

  namespace convert
  {

58
59
60
61
62
63
64
65
66
67
68
    /// \brief Convert a Milena image to a Qimage.
    ///
    //
    template <typename I>
    inline
    QImage
    to_qimage(const Image<I>& ima);


# ifndef MLN_INCLUDE_ONLY

69
70
    // Implementation

71
    namespace impl
72
73
74
75
76
77
78
79
80
81
    {


      template <typename I>
      inline
      QImage to_qimage_scalar(const Image<I>& ima_)
      {
	const I& ima = exact(ima_);
	mln_precondition(ima.is_valid());

82
	const unsigned
83
84
85
86
87
	  nrows = geom::nrows(ima),
	  ncols = geom::ncols(ima);

# if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
	QImage qima(ncols, nrows, QImage::Format_RGB32);
88
89
	uchar * ptr_qima = qima.bits();
	unsigned padding = 0;
90
91
92
93
94
#  if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
	++ptr_qima;
#  endif // ! Q_BYTE_ORDER
# else
	QImage qima(ncols, nrows, QImage::Format_RGB888);
95
96
	uchar * ptr_qima = qima.bits();
	unsigned padding = ncols % 4;
97
98
99
# endif // ! QT_VERSION

	const mln_value(I)* ptr_ima = &ima(ima.domain().pmin());
100
	unsigned offset = ima.delta_index(dpoint2d(+1, - ncols));
101
102

	// Data is stored as ABGR so we skip the first value which is ignored.
103
104
	for (unsigned row = 0; row < nrows; ++row, ptr_ima += offset)
	{
105
106
107
108
109
110
111
112
113
114
115
	  for (unsigned col = 0; col < ncols; ++col)
	  {
	    const mln_value(I)& v = *ptr_ima++;
	    std::memset(ptr_qima, v, sizeof(mln_value(I)) * 3);

# if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400
	    ptr_qima += 4;
# else
	    ptr_qima += 3;
# endif // ! QT_VERSION
	  }
116
117
	  ptr_qima += padding;
	}
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

	return qima;
      }



# if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400

      template <typename I>
      inline
      QImage to_qimage_rgb8(const Image<I>& ima_)
      {
	const I& ima = exact(ima_);
	mln_precondition(ima.is_valid());

	const int
	  nrows = geom::nrows(ima),
	  ncols = geom::ncols(ima);

	QImage qima(ncols, nrows, QImage::Format_RGB32);
	uchar * ptr_qima = qima.scanLine(0);
	const mln_value(I)* ptr_ima = &ima(ima.domain().pmin());
140
	unsigned offset = ima.delta_index(dpoint2d(+1, - ncols));
141

142
143
	for (unsigned row = 0; row < nrows; ++row, ptr_ima += offset)
	{
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
	  for (unsigned col = 0; col < ncols; ++col)
	  {
	    const mln::value::rgb8& v = *ptr_ima++;
#  if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
	    // Memory representation : BBGGRRFF
	    *ptr_qima++ = v.blue();
	    *ptr_qima++ = v.green();
	    *ptr_qima = v.red();
	    ptr_qima += 2;
#  else /* Q_BIG_ENDIAN */
	    // Memory representation : FFRRGGBB
	    ++ptr_qima;
	    *ptr_qima++ = v.red();
	    *ptr_qima++ = v.green();
	    *ptr_qima = v.blue();
#  endif // ! Q_BYTE_ORDER
	  }
161
	}
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178

	return qima;
      }

# else
      template <typename I>
      inline
      QImage to_qimage_rgb8(const Image<I>& ima_)
      {
	const I& ima = exact(ima_);
	mln_precondition(ima.is_valid());

	const int
	  nrows = geom::nrows(ima),
	  ncols = geom::ncols(ima);


179
180
181
	typedef mln_site(I) P;
	typedef mln_dpsite(P) DP;

182
183
184
	// Required by a one-shot data copy:
	mln::border::resize(ima, 0);

185
186
187
188
	unsigned
	  offset = ima.delta_index(DP(+1, 0)),
	  line_offset = 0;

189
	QImage qima(ncols, nrows, QImage::Format_RGB888);
190
191
192
193
	for (int i = 0 ; i < qima.height(); ++i, line_offset += offset)
	  std::memcpy(qima.scanLine(i),
		      ima.buffer() + line_offset,
		      ima.ncols() * 3);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

	return qima;
      }
# endif // ! QT_VERSION


      template <typename I>
      inline
      QImage to_qimage_qt_rgb32(const Image<I>& ima_)
      {
	const I& ima = exact(ima_);
	mln_precondition(ima.is_valid());

	const int
	  nrows = geom::nrows(ima),
	  ncols = geom::ncols(ima);


	// Required by a one-shot data copy:
	mln::border::resize(ima, 0);

	QImage qima(ncols, nrows, QImage::Format_RGB32);
216
	std::memcpy(qima.bits(), ima.buffer(), ima.nelements() * 4);
217
218
219
220
221

	return qima;
      }


222
    } // end of namespace mln::convert::impl
223
224
225
226
227
228
229
230
231
232
233
234



    // Dispatch

    namespace internal
    {

      template <typename I, typename S>
      inline
      QImage to_qimage_dispatch(const Image<I>& ima, const value::Scalar<S>&)
      {
235
	return impl::to_qimage_scalar(ima);
236
237
238
239
240
241
      }

      template <typename I>
      inline
      QImage to_qimage_dispatch(const Image<I>& ima, const value::rgb8&)
      {
242
	return impl::to_qimage_rgb8(ima);
243
244
245
246
247
248
      }

      template <typename I>
      inline
      QImage to_qimage_dispatch(const Image<I>& ima, const value::qt::rgb32&)
      {
249
	return impl::to_qimage_qt_rgb32(ima);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
      }


      template <typename I, typename V>
      inline
      QImage to_qimage_dispatch(const Image<I>& ima, V&)
      {
	// Not supported yet!
	mlc_abort(I)::check();
	return QImage();
      }


      template <typename I>
      inline
      QImage to_qimage_dispatch(const Image<I>& ima)
      {
	typedef mln_value(I) V;
	return to_qimage_dispatch(ima, V());
      }

    } // end of namespace mln::convert::internal



    // Facade

    template <typename I>
    inline
    QImage to_qimage(const Image<I>& ima_)
    {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
281
282
      trace::entering("convert::to_qimage");

283
284
285
286
287
      const I& ima = exact(ima_);
      mln_precondition(ima.is_valid());

      QImage output = internal::to_qimage_dispatch(ima);

Guillaume Lazzara's avatar
Guillaume Lazzara committed
288
      trace::exiting("convert::to_qimage");
289
290
291
292
      return output;
    }


293
294
# endif // ! MLN_INCLUDE_ONLY

295
296
297
298
299
  } // end of namespace mln::convert

} // end of namespace mln

#endif // ! MLN_CONVERT_TO_QIMAGE_HH