to_qimage_nocopy.hh 4.06 KB
Newer Older
1
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
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (C) 2010 EPITA Research and Development Laboratory (LRDE)
//
// 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_NOCOPY_HH
# define MLN_CONVERT_TO_QIMAGE_NOCOPY_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>

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


namespace mln
{

  namespace convert
  {

    // Implementation

57
    namespace impl
58
59
60
61
62
63
64
65
66
67
68
69
70
    {

      template <typename I>
      inline
      QImage to_qimage_nocopy_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);

71
72
73
	typedef mln_value(I) V;
	QImage qima((uchar *)(&ima(ima.domain().pmin())), ncols, nrows,
		    (ncols + 2 * ima.border()) * sizeof(V),
74
75
76
77
78
79
		    QImage::Format_RGB32);

	return qima;
      }


Guillaume Lazzara's avatar
Guillaume Lazzara committed
80
# if QT_VERSION >= 0x040400
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

      template <typename I>
      inline
      QImage to_qimage_nocopy_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);

	typedef mln_value(I) V;
	QImage qima((uchar *)(&ima(ima.domain().pmin())), ncols, nrows,
		    (ncols + 2 * ima.border()) * sizeof(V),
		    QImage::Format_RGB888);

	return qima;
      }

# endif


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

106
107
108
109
110
111
112
113
114
115



    // Dispatch

    namespace internal
    {

      template <typename I>
      inline
116
117
      QImage to_qimage_nocopy_dispatch(const Image<I>& ima,
				       const value::qt::rgb32&)
118
      {
119
	return impl::to_qimage_nocopy_qt_rgb32(ima);
120
121
122
      }


Guillaume Lazzara's avatar
Guillaume Lazzara committed
123
# if QT_VERSION >= 0x040400
124
125
126
127
128
129
130
131
132
133
134
135

      template <typename I>
      inline
      QImage to_qimage_nocopy_dispatch(const Image<I>& ima,
				       const value::rgb8&)
      {
	return impl::to_qimage_nocopy_rgb8(ima);
      }

# endif


136
137
      template <typename I, typename V>
      inline
138
      QImage to_qimage_nocopy_dispatch(const Image<I>&, V&)
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
      {
	// Not supported yet!
	mlc_abort(I)::check();
	return QImage();
      }


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

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



    // Facade

    template <typename I>
    inline
Guillaume Lazzara's avatar
Guillaume Lazzara committed
162
    QImage to_qimage_nocopy(const Image<I>& ima_)
163
    {
Guillaume Lazzara's avatar
Guillaume Lazzara committed
164
165
      trace::entering("convert::to_qimage_nocopy");

166
167
168
169
170
      const I& ima = exact(ima_);
      mln_precondition(ima.is_valid());

      QImage output = internal::to_qimage_nocopy_dispatch(ima);

Guillaume Lazzara's avatar
Guillaume Lazzara committed
171
      trace::exiting("convert::to_qimage_nocopy");
172
173
174
175
176
177
178
179
180
      return output;
    }


  } // end of namespace mln::convert

} // end of namespace mln

#endif // ! MLN_CONVERT_TO_QIMAGE_NOCOPY_HH