src/hbcore/ovgeffects/hbvgcolorizeeffect.cpp
changeset 0 16d8024aca5e
child 5 627c4a0fd0e7
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     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 "hbvgcolorizeeffect_p.h"
       
    27 #include "hbvgcolorizeeffect_p_p.h"
       
    28 #include <QPainter>
       
    29 
       
    30 /*!
       
    31  * \class HbVgColorizeEffect
       
    32  *
       
    33  * \brief OpenVG-based colorize filter effect.
       
    34  *
       
    35  * \internal
       
    36  */
       
    37 
       
    38 HbVgColorizeEffectPrivate::HbVgColorizeEffectPrivate()
       
    39     : color(Qt::white)
       
    40 {
       
    41 }
       
    42 
       
    43 HbVgColorizeEffect::HbVgColorizeEffect(QObject *parent)
       
    44     : HbVgEffect(*new HbVgColorizeEffectPrivate, parent)
       
    45 {
       
    46 }
       
    47 
       
    48 HbVgColorizeEffect::HbVgColorizeEffect(HbVgColorizeEffectPrivate &dd, QObject *parent)
       
    49     : HbVgEffect(dd, parent)
       
    50 {
       
    51 }
       
    52 
       
    53 HbVgColorizeEffect::~HbVgColorizeEffect()
       
    54 {
       
    55 }
       
    56 
       
    57 QColor HbVgColorizeEffect::color() const
       
    58 {
       
    59     Q_D(const HbVgColorizeEffect);
       
    60     return d->color;
       
    61 }
       
    62 
       
    63 void HbVgColorizeEffect::setColor(const QColor &color)
       
    64 {
       
    65     Q_D(HbVgColorizeEffect);
       
    66     if (d->color == color)
       
    67         return;
       
    68     d->color = color;
       
    69     updateEffect();
       
    70     emit colorChanged(color);
       
    71 }
       
    72 
       
    73 QRectF HbVgColorizeEffect::boundingRectFor(const QRectF &rect) const
       
    74 {
       
    75     return rect;
       
    76 }
       
    77 
       
    78 #ifdef HB_EFFECTS_OPENVG
       
    79 
       
    80 const VGfloat Rw = 0.3086f;
       
    81 const VGfloat Gw = 0.6094f;
       
    82 const VGfloat Bw = 0.0820f;
       
    83 
       
    84 void HbVgColorizeEffectPrivate::getColorMatrix(VGfloat *colorMatrix,
       
    85                                                const QColor &color,
       
    86                                                qreal opacity)
       
    87 {
       
    88     const VGfloat o = (VGfloat) opacity;
       
    89     const VGfloat ao = 1 - o;
       
    90     const VGfloat R = (o / 255.0f) * (VGfloat) color.red();
       
    91     const VGfloat G = (o / 255.0f) * (VGfloat) color.green();
       
    92     const VGfloat B = (o / 255.0f) * (VGfloat) color.blue();
       
    93     colorMatrix[0] = R*Rw + ao;
       
    94     colorMatrix[1] = G*Rw;
       
    95     colorMatrix[2] = B*Rw;
       
    96     colorMatrix[3] = 0.0f;
       
    97     colorMatrix[4] = R*Gw;
       
    98     colorMatrix[5] = G*Gw + ao;
       
    99     colorMatrix[6] = B*Gw;
       
   100     colorMatrix[7] = 0.0f;
       
   101     colorMatrix[8] = R*Bw;
       
   102     colorMatrix[9] = G*Bw;
       
   103     colorMatrix[10] = B*Bw + ao;
       
   104     colorMatrix[11] = 0.0f;
       
   105     colorMatrix[12] = 0.0f;
       
   106     colorMatrix[13] = 0.0f;
       
   107     colorMatrix[14] = 0.0f;
       
   108     colorMatrix[15] = 1.0f;
       
   109     colorMatrix[16] = 0.0f;
       
   110     colorMatrix[17] = 0.0f;
       
   111     colorMatrix[18] = 0.0f;
       
   112     colorMatrix[19] = 0.0f;    
       
   113 }
       
   114 
       
   115 #endif
       
   116 
       
   117 void HbVgColorizeEffect::performEffect(QPainter *painter,
       
   118                                    const QPointF &offset,
       
   119                                    const QVariant &vgImage,
       
   120                                    const QSize &vgImageSize)
       
   121 {
       
   122 #ifdef HB_EFFECTS_OPENVG
       
   123     QPixmap cachedPm = cached(vgImageSize);
       
   124     if (!cachedPm.isNull()) {
       
   125         painter->drawPixmap(offset, cachedPm);
       
   126         return;
       
   127     }
       
   128 
       
   129     Q_D(HbVgColorizeEffect);
       
   130     VGImage srcImage = vgImage.value<VGImage>();
       
   131     VGImage dstImage = d->ensurePixmap(&d->dstPixmap, vgImageSize);
       
   132     qreal opacity = clamp(d->opacity, 0.0f, 1.0f);
       
   133     if (opacity > HBVG_EPSILON) {
       
   134         if (d->paramsChanged)
       
   135             HbVgColorizeEffectPrivate::getColorMatrix(d->colorMatrix, d->color, opacity);
       
   136         vgColorMatrix(dstImage, srcImage, d->colorMatrix);
       
   137         painter->drawPixmap(offset, d->dstPixmap);
       
   138         tryCache(d->dstPixmap);
       
   139     } else {
       
   140         painter->drawPixmap(offset, d->srcPixmap);
       
   141     }
       
   142 #else
       
   143     Q_UNUSED(painter);
       
   144     Q_UNUSED(offset);
       
   145     Q_UNUSED(vgImage);
       
   146     Q_UNUSED(vgImageSize);
       
   147 #endif
       
   148 }