author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtOpenVG module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qpixmapfilter_vg_p.h" |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
43 |
#include "qvgimagepool_p.h" |
0 | 44 |
#include <QtCore/qvarlengtharray.h> |
45 |
#include <QtGui/qpainter.h> |
|
46 |
||
47 |
QT_BEGIN_NAMESPACE |
|
48 |
||
49 |
#if !defined(QT_SHIVAVG) |
|
50 |
||
51 |
QVGPixmapConvolutionFilter::QVGPixmapConvolutionFilter() |
|
52 |
: QPixmapConvolutionFilter() |
|
53 |
{ |
|
54 |
} |
|
55 |
||
56 |
QVGPixmapConvolutionFilter::~QVGPixmapConvolutionFilter() |
|
57 |
{ |
|
58 |
} |
|
59 |
||
60 |
extern void qt_vg_drawVGImage |
|
61 |
(QPainter *painter, const QPointF& pos, VGImage vgImg); |
|
62 |
extern void qt_vg_drawVGImageStencil |
|
63 |
(QPainter *painter, const QPointF& pos, VGImage vgImg, const QBrush& brush); |
|
64 |
||
65 |
void QVGPixmapConvolutionFilter::draw |
|
66 |
(QPainter *painter, const QPointF &dest, |
|
67 |
const QPixmap &src, const QRectF &srcRect) const |
|
68 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
if (src.isNull()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
70 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
71 |
|
0 | 72 |
if (src.pixmapData()->classId() != QPixmapData::OpenVGClass) { |
73 |
// The pixmap data is not an instance of QVGPixmapData, so fall |
|
74 |
// back to the default convolution filter implementation. |
|
75 |
QPixmapConvolutionFilter::draw(painter, dest, src, srcRect); |
|
76 |
return; |
|
77 |
} |
|
78 |
||
79 |
QVGPixmapData *pd = static_cast<QVGPixmapData *>(src.pixmapData()); |
|
80 |
||
81 |
VGImage srcImage = pd->toVGImage(); |
|
82 |
if (srcImage == VG_INVALID_HANDLE) |
|
83 |
return; |
|
84 |
||
85 |
QSize size = pd->size(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
86 |
VGImage dstImage = QVGImagePool::instance()->createTemporaryImage |
0 | 87 |
(VG_sARGB_8888_PRE, size.width(), size.height(), |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
VG_IMAGE_QUALITY_FASTER, pd); |
0 | 89 |
if (dstImage == VG_INVALID_HANDLE) |
90 |
return; |
|
91 |
||
92 |
int kernelWidth = rows(); |
|
93 |
int kernelHeight = columns(); |
|
94 |
const qreal *kern = convolutionKernel(); |
|
95 |
QVarLengthArray<VGshort> kernel; |
|
96 |
for (int i = 0; i < kernelWidth; ++i) { |
|
97 |
for (int j = 0; j < kernelHeight; ++j) { |
|
98 |
kernel.append((VGshort)(kern[j * kernelWidth + i] * 1024.0f)); |
|
99 |
} |
|
100 |
} |
|
101 |
||
102 |
VGfloat values[4]; |
|
103 |
values[0] = 0.0f; |
|
104 |
values[1] = 0.0f; |
|
105 |
values[2] = 0.0f; |
|
106 |
values[3] = 0.0f; |
|
107 |
vgSetfv(VG_TILE_FILL_COLOR, 4, values); |
|
108 |
||
109 |
vgConvolve(dstImage, srcImage, |
|
110 |
kernelWidth, kernelHeight, 0, 0, |
|
111 |
kernel.constData(), 1.0f / 1024.0f, 0.0f, |
|
112 |
VG_TILE_FILL); |
|
113 |
||
114 |
VGImage child = VG_INVALID_HANDLE; |
|
115 |
||
116 |
if (srcRect.isNull() || |
|
117 |
(srcRect.topLeft().isNull() && srcRect.size() == size)) { |
|
118 |
child = dstImage; |
|
119 |
} else { |
|
120 |
QRect src = srcRect.toRect(); |
|
121 |
child = vgChildImage(dstImage, src.x(), src.y(), src.width(), src.height()); |
|
122 |
} |
|
123 |
||
124 |
qt_vg_drawVGImage(painter, dest, child); |
|
125 |
||
126 |
if(child != dstImage) |
|
127 |
vgDestroyImage(child); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
QVGImagePool::instance()->releaseImage(0, dstImage); |
0 | 129 |
} |
130 |
||
131 |
QVGPixmapColorizeFilter::QVGPixmapColorizeFilter() |
|
132 |
: QPixmapColorizeFilter() |
|
133 |
{ |
|
134 |
} |
|
135 |
||
136 |
QVGPixmapColorizeFilter::~QVGPixmapColorizeFilter() |
|
137 |
{ |
|
138 |
} |
|
139 |
||
140 |
void QVGPixmapColorizeFilter::draw(QPainter *painter, const QPointF &dest, const QPixmap &src, const QRectF &srcRect) const |
|
141 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
if (src.isNull()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
143 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
144 |
|
0 | 145 |
if (src.pixmapData()->classId() != QPixmapData::OpenVGClass) { |
146 |
// The pixmap data is not an instance of QVGPixmapData, so fall |
|
147 |
// back to the default colorize filter implementation. |
|
148 |
QPixmapColorizeFilter::draw(painter, dest, src, srcRect); |
|
149 |
return; |
|
150 |
} |
|
151 |
||
152 |
QVGPixmapData *pd = static_cast<QVGPixmapData *>(src.pixmapData()); |
|
153 |
||
154 |
VGImage srcImage = pd->toVGImage(); |
|
155 |
if (srcImage == VG_INVALID_HANDLE) |
|
156 |
return; |
|
157 |
||
158 |
QSize size = pd->size(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
VGImage dstImage = QVGImagePool::instance()->createTemporaryImage |
0 | 160 |
(VG_sARGB_8888_PRE, size.width(), size.height(), |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
VG_IMAGE_QUALITY_FASTER, pd); |
0 | 162 |
if (dstImage == VG_INVALID_HANDLE) |
163 |
return; |
|
164 |
||
165 |
// Determine the weights for the matrix from the color and strength. |
|
166 |
QColor c = color(); |
|
167 |
VGfloat strength = this->strength(); |
|
168 |
VGfloat weights[3]; |
|
169 |
VGfloat invweights[3]; |
|
170 |
VGfloat alpha = c.alphaF(); |
|
171 |
weights[0] = c.redF() * alpha; |
|
172 |
weights[1] = c.greenF() * alpha; |
|
173 |
weights[2] = c.blueF() * alpha; |
|
174 |
invweights[0] = (1.0f - weights[0]) * strength; |
|
175 |
invweights[1] = (1.0f - weights[1]) * strength; |
|
176 |
invweights[2] = (1.0f - weights[2]) * strength; |
|
177 |
||
178 |
// Grayscale weights. |
|
179 |
static const VGfloat redGray = 11.0f / 32.0f; |
|
180 |
static const VGfloat greenGray = 16.0f / 32.0f; |
|
181 |
static const VGfloat blueGray = 1.0f - (redGray + greenGray); |
|
182 |
||
183 |
VGfloat matrix[5][4]; |
|
184 |
matrix[0][0] = redGray * invweights[0] + (1.0f - strength); |
|
185 |
matrix[0][1] = redGray * invweights[1]; |
|
186 |
matrix[0][2] = redGray * invweights[2]; |
|
187 |
matrix[0][3] = 0.0f; |
|
188 |
matrix[1][0] = greenGray * invweights[0]; |
|
189 |
matrix[1][1] = greenGray * invweights[1] + (1.0f - strength); |
|
190 |
matrix[1][2] = greenGray * invweights[2]; |
|
191 |
matrix[1][3] = 0.0f; |
|
192 |
matrix[2][0] = blueGray * invweights[0]; |
|
193 |
matrix[2][1] = blueGray * invweights[1]; |
|
194 |
matrix[2][2] = blueGray * invweights[2] + (1.0f - strength); |
|
195 |
matrix[2][3] = 0.0f; |
|
196 |
matrix[3][0] = 0.0f; |
|
197 |
matrix[3][1] = 0.0f; |
|
198 |
matrix[3][2] = 0.0f; |
|
199 |
matrix[3][3] = 1.0f; |
|
200 |
matrix[4][0] = weights[0] * strength; |
|
201 |
matrix[4][1] = weights[1] * strength; |
|
202 |
matrix[4][2] = weights[2] * strength; |
|
203 |
matrix[4][3] = 0.0f; |
|
204 |
||
205 |
vgColorMatrix(dstImage, srcImage, matrix[0]); |
|
206 |
||
207 |
VGImage child = VG_INVALID_HANDLE; |
|
208 |
||
209 |
if (srcRect.isNull() || |
|
210 |
(srcRect.topLeft().isNull() && srcRect.size() == size)) { |
|
211 |
child = dstImage; |
|
212 |
} else { |
|
213 |
QRect src = srcRect.toRect(); |
|
214 |
child = vgChildImage(dstImage, src.x(), src.y(), src.width(), src.height()); |
|
215 |
} |
|
216 |
||
217 |
qt_vg_drawVGImage(painter, dest, child); |
|
218 |
||
219 |
if(child != dstImage) |
|
220 |
vgDestroyImage(child); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
QVGImagePool::instance()->releaseImage(0, dstImage); |
0 | 222 |
} |
223 |
||
224 |
QVGPixmapDropShadowFilter::QVGPixmapDropShadowFilter() |
|
225 |
: QPixmapDropShadowFilter() |
|
226 |
{ |
|
227 |
} |
|
228 |
||
229 |
QVGPixmapDropShadowFilter::~QVGPixmapDropShadowFilter() |
|
230 |
{ |
|
231 |
} |
|
232 |
||
233 |
void QVGPixmapDropShadowFilter::draw(QPainter *painter, const QPointF &dest, const QPixmap &src, const QRectF &srcRect) const |
|
234 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
if (src.isNull()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
236 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
|
0 | 238 |
if (src.pixmapData()->classId() != QPixmapData::OpenVGClass) { |
239 |
// The pixmap data is not an instance of QVGPixmapData, so fall |
|
240 |
// back to the default drop shadow filter implementation. |
|
241 |
QPixmapDropShadowFilter::draw(painter, dest, src, srcRect); |
|
242 |
return; |
|
243 |
} |
|
244 |
||
245 |
QVGPixmapData *pd = static_cast<QVGPixmapData *>(src.pixmapData()); |
|
246 |
||
247 |
VGImage srcImage = pd->toVGImage(); |
|
248 |
if (srcImage == VG_INVALID_HANDLE) |
|
249 |
return; |
|
250 |
||
251 |
QSize size = pd->size(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
252 |
VGImage dstImage = QVGImagePool::instance()->createTemporaryImage |
0 | 253 |
(VG_A_8, size.width(), size.height(), |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
254 |
VG_IMAGE_QUALITY_FASTER, pd); |
0 | 255 |
if (dstImage == VG_INVALID_HANDLE) |
256 |
return; |
|
257 |
||
258 |
// Clamp the radius range. We divide by 2 because the OpenVG blur |
|
259 |
// is "too blurry" compared to the default raster implementation. |
|
260 |
VGfloat maxRadius = VGfloat(vgGeti(VG_MAX_GAUSSIAN_STD_DEVIATION)); |
|
261 |
VGfloat radiusF = VGfloat(blurRadius()) / 2.0f; |
|
262 |
if (radiusF < 0.001f) |
|
263 |
radiusF = 0.001f; |
|
264 |
else if (radiusF > maxRadius) |
|
265 |
radiusF = maxRadius; |
|
266 |
||
267 |
// Blur the blackened source image. |
|
268 |
vgGaussianBlur(dstImage, srcImage, radiusF, radiusF, VG_TILE_PAD); |
|
269 |
||
270 |
VGImage child = VG_INVALID_HANDLE; |
|
271 |
||
272 |
QRect srect; |
|
273 |
if (srcRect.isNull() || |
|
274 |
(srcRect.topLeft().isNull() && srcRect.size() == size)) { |
|
275 |
child = dstImage; |
|
276 |
srect = QRect(0, 0, size.width(), size.height()); |
|
277 |
} else { |
|
278 |
srect = srcRect.toRect(); |
|
279 |
child = vgChildImage(dstImage, srect.x(), srect.y(), srect.width(), srect.height()); |
|
280 |
} |
|
281 |
||
282 |
qt_vg_drawVGImageStencil(painter, dest + offset(), child, color()); |
|
283 |
||
284 |
if(child != dstImage) |
|
285 |
vgDestroyImage(child); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
286 |
QVGImagePool::instance()->releaseImage(0, dstImage); |
0 | 287 |
|
288 |
// Now draw the actual pixmap over the top. |
|
289 |
painter->drawPixmap(dest, src, srect); |
|
290 |
} |
|
291 |
||
292 |
QVGPixmapBlurFilter::QVGPixmapBlurFilter(QObject *parent) |
|
293 |
: QPixmapBlurFilter(parent) |
|
294 |
{ |
|
295 |
} |
|
296 |
||
297 |
QVGPixmapBlurFilter::~QVGPixmapBlurFilter() |
|
298 |
{ |
|
299 |
} |
|
300 |
||
301 |
void QVGPixmapBlurFilter::draw(QPainter *painter, const QPointF &dest, const QPixmap &src, const QRectF &srcRect) const |
|
302 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
303 |
if (src.isNull()) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
304 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
305 |
|
0 | 306 |
if (src.pixmapData()->classId() != QPixmapData::OpenVGClass) { |
307 |
// The pixmap data is not an instance of QVGPixmapData, so fall |
|
308 |
// back to the default blur filter implementation. |
|
309 |
QPixmapBlurFilter::draw(painter, dest, src, srcRect); |
|
310 |
return; |
|
311 |
} |
|
312 |
||
313 |
QVGPixmapData *pd = static_cast<QVGPixmapData *>(src.pixmapData()); |
|
314 |
||
315 |
VGImage srcImage = pd->toVGImage(); |
|
316 |
if (srcImage == VG_INVALID_HANDLE) |
|
317 |
return; |
|
318 |
||
319 |
QSize size = pd->size(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
320 |
VGImage dstImage = QVGImagePool::instance()->createTemporaryImage |
0 | 321 |
(VG_sARGB_8888_PRE, size.width(), size.height(), |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
322 |
VG_IMAGE_QUALITY_FASTER, pd); |
0 | 323 |
if (dstImage == VG_INVALID_HANDLE) |
324 |
return; |
|
325 |
||
326 |
// Clamp the radius range. We divide by 2 because the OpenVG blur |
|
327 |
// is "too blurry" compared to the default raster implementation. |
|
328 |
VGfloat maxRadius = VGfloat(vgGeti(VG_MAX_GAUSSIAN_STD_DEVIATION)); |
|
329 |
VGfloat radiusF = VGfloat(radius()) / 2.0f; |
|
330 |
if (radiusF < 0.001f) |
|
331 |
radiusF = 0.001f; |
|
332 |
else if (radiusF > maxRadius) |
|
333 |
radiusF = maxRadius; |
|
334 |
||
335 |
vgGaussianBlur(dstImage, srcImage, radiusF, radiusF, VG_TILE_PAD); |
|
336 |
||
337 |
VGImage child = VG_INVALID_HANDLE; |
|
338 |
||
339 |
if (srcRect.isNull() || |
|
340 |
(srcRect.topLeft().isNull() && srcRect.size() == size)) { |
|
341 |
child = dstImage; |
|
342 |
} else { |
|
343 |
QRect src = srcRect.toRect(); |
|
344 |
child = vgChildImage(dstImage, src.x(), src.y(), src.width(), src.height()); |
|
345 |
} |
|
346 |
||
347 |
qt_vg_drawVGImage(painter, dest, child); |
|
348 |
||
349 |
if(child != dstImage) |
|
350 |
vgDestroyImage(child); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
351 |
QVGImagePool::instance()->releaseImage(0, dstImage); |
0 | 352 |
} |
353 |
||
354 |
#endif |
|
355 |
||
356 |
QT_END_NAMESPACE |