|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (developer.feedback@nokia.com) |
|
6 ** |
|
7 ** This file is part of the HbCore module of the UI Extensions for Mobile. |
|
8 ** |
|
9 ** GNU Lesser General Public License Usage |
|
10 ** This file may be used under the terms of the GNU Lesser General Public |
|
11 ** License version 2.1 as published by the Free Software Foundation and |
|
12 ** appearing in the file LICENSE.LGPL included in the packaging of this file. |
|
13 ** Please review the following information to ensure the GNU Lesser General |
|
14 ** Public License version 2.1 requirements will be met: |
|
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
16 ** |
|
17 ** In addition, as a special exception, Nokia gives you certain additional |
|
18 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
20 ** |
|
21 ** If you have questions regarding the use of this file, please contact |
|
22 ** Nokia at developer.feedback@nokia.com. |
|
23 ** |
|
24 ****************************************************************************/ |
|
25 |
|
26 #include "hbvgbceffect_p.h" |
|
27 #include "hbvgbceffect_p_p.h" |
|
28 #include <QPainter> |
|
29 |
|
30 /*! |
|
31 * \class HbVgBcEffect |
|
32 * |
|
33 * \brief OpenVG-based brightness-contrast filter effect. |
|
34 * |
|
35 * \internal |
|
36 */ |
|
37 |
|
38 HbVgBcEffectPrivate::HbVgBcEffectPrivate() |
|
39 : brightness(0), contrast(1) |
|
40 { |
|
41 } |
|
42 |
|
43 HbVgBcEffect::HbVgBcEffect(QObject *parent) |
|
44 : HbVgEffect(*new HbVgBcEffectPrivate, parent) |
|
45 { |
|
46 } |
|
47 |
|
48 HbVgBcEffect::HbVgBcEffect(HbVgBcEffectPrivate &dd, QObject *parent) |
|
49 : HbVgEffect(dd, parent) |
|
50 { |
|
51 } |
|
52 |
|
53 HbVgBcEffect::~HbVgBcEffect() |
|
54 { |
|
55 } |
|
56 |
|
57 qreal HbVgBcEffect::brightness() const |
|
58 { |
|
59 Q_D(const HbVgBcEffect); |
|
60 return d->brightness; |
|
61 } |
|
62 |
|
63 void HbVgBcEffect::setBrightness(qreal brightness) |
|
64 { |
|
65 Q_D(HbVgBcEffect); |
|
66 if (d->brightness == brightness) |
|
67 return; |
|
68 d->brightness = brightness; |
|
69 updateEffect(); |
|
70 emit brightnessChanged(brightness); |
|
71 } |
|
72 |
|
73 qreal HbVgBcEffect::contrast() const |
|
74 { |
|
75 Q_D(const HbVgBcEffect); |
|
76 return d->contrast; |
|
77 } |
|
78 |
|
79 void HbVgBcEffect::setContrast(qreal contrast) |
|
80 { |
|
81 Q_D(HbVgBcEffect); |
|
82 if (d->contrast == contrast) |
|
83 return; |
|
84 d->contrast = contrast; |
|
85 updateEffect(); |
|
86 emit contrastChanged(contrast); |
|
87 } |
|
88 |
|
89 QRectF HbVgBcEffect::boundingRectFor(const QRectF &rect) const |
|
90 { |
|
91 return rect; |
|
92 } |
|
93 |
|
94 void HbVgBcEffect::performEffect(QPainter *painter, |
|
95 const QPointF &offset, |
|
96 const QVariant &vgImage, |
|
97 const QSize &vgImageSize) |
|
98 { |
|
99 #ifdef HB_EFFECTS_OPENVG |
|
100 QPixmap cachedPm = cached(vgImageSize); |
|
101 if (!cachedPm.isNull()) { |
|
102 painter->drawPixmap(offset, cachedPm); |
|
103 return; |
|
104 } |
|
105 |
|
106 Q_D(HbVgBcEffect); |
|
107 VGImage srcImage = vgImage.value<VGImage>(); |
|
108 VGImage dstImage = d->ensurePixmap(&d->dstPixmap, vgImageSize); |
|
109 qreal opacity = clamp(d->opacity, 0.0f, 1.0f); |
|
110 if (opacity > HBVG_EPSILON) { |
|
111 if (d->paramsChanged) { |
|
112 // brightness [-1, 1] |
|
113 const VGfloat offset_br = clamp(d->brightness, -1.0f, 1.0f); |
|
114 const VGfloat scale_br = 1.0f - 0.5f * ((offset_br < 0.0f) ? -offset_br : offset_br); |
|
115 |
|
116 // contrast [0, N] |
|
117 const VGfloat scale_con = clamp(d->contrast, 0.0f, 100.0f); |
|
118 const VGfloat offset_con = -0.5f * scale_con + 0.5f ; |
|
119 |
|
120 // combine the effects of brightness and contrast |
|
121 const VGfloat off = offset_br + offset_con; |
|
122 const VGfloat sc = scale_br * scale_con; |
|
123 |
|
124 // take opacity into account |
|
125 const VGfloat o = (VGfloat) opacity; |
|
126 const VGfloat oOff = off * o; |
|
127 const VGfloat oSc = (sc * o) + (1.0f - o); |
|
128 |
|
129 d->colorMatrix[0] = oSc; |
|
130 d->colorMatrix[1] = 0.0f; |
|
131 d->colorMatrix[2] = 0.0f; |
|
132 d->colorMatrix[3] = 0.0f; |
|
133 d->colorMatrix[4] = 0.0f; |
|
134 d->colorMatrix[5] = oSc; |
|
135 d->colorMatrix[6] = 0.0f; |
|
136 d->colorMatrix[7] = 0.0f; |
|
137 d->colorMatrix[8] = 0.0f; |
|
138 d->colorMatrix[9] = 0.0f; |
|
139 d->colorMatrix[10] = oSc; |
|
140 d->colorMatrix[11] = 0.0f; |
|
141 d->colorMatrix[12] = 0.0f; |
|
142 d->colorMatrix[13] = 0.0f; |
|
143 d->colorMatrix[14] = 0.0f; |
|
144 d->colorMatrix[15] = 1.0f; |
|
145 d->colorMatrix[16] = oOff; |
|
146 d->colorMatrix[17] = oOff; |
|
147 d->colorMatrix[18] = oOff; |
|
148 d->colorMatrix[19] = 0.0f; |
|
149 } |
|
150 vgColorMatrix(dstImage, srcImage, d->colorMatrix); |
|
151 painter->drawPixmap(offset, d->dstPixmap); |
|
152 tryCache(d->dstPixmap); |
|
153 } else { |
|
154 painter->drawPixmap(offset, d->srcPixmap); |
|
155 } |
|
156 #else |
|
157 Q_UNUSED(painter); |
|
158 Q_UNUSED(offset); |
|
159 Q_UNUSED(vgImage); |
|
160 Q_UNUSED(vgImageSize); |
|
161 #endif |
|
162 } |