src/hbcore/ovgeffects/hbvgoutlineeffect.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 "hbvgoutlineeffect_p.h"
       
    27 #include "hbvgoutlineeffect_p_p.h"
       
    28 #include <QPainter>
       
    29 
       
    30 /*!
       
    31  * \class HbVgOutlineEffect
       
    32  *
       
    33  * \brief OpenVG-based outline filter effect.
       
    34  *
       
    35  * \internal
       
    36  */
       
    37 
       
    38 HbVgOutlineEffectPrivate::HbVgOutlineEffectPrivate()
       
    39     : outline(1, 1), color(Qt::black), steepness(20), offset(0, 0)
       
    40 {
       
    41 }
       
    42 
       
    43 HbVgOutlineEffect::HbVgOutlineEffect(QObject *parent)
       
    44     : HbVgEffect(*new HbVgOutlineEffectPrivate, parent)
       
    45 {
       
    46 }
       
    47 
       
    48 HbVgOutlineEffect::HbVgOutlineEffect(HbVgOutlineEffectPrivate &dd, QObject *parent)
       
    49     : HbVgEffect(dd, parent)
       
    50 {
       
    51 }
       
    52 
       
    53 HbVgOutlineEffect::~HbVgOutlineEffect()
       
    54 {
       
    55 }
       
    56 
       
    57 QPointF HbVgOutlineEffect::outline() const
       
    58 {
       
    59     Q_D(const HbVgOutlineEffect);
       
    60     return d->outline;
       
    61 }
       
    62 
       
    63 void HbVgOutlineEffect::setOutline(const QPointF &outline)
       
    64 {
       
    65     Q_D(HbVgOutlineEffect);
       
    66     if (d->outline == outline)
       
    67         return;
       
    68     d->outline = outline;
       
    69     updateEffectBoundingRect();
       
    70     emit outlineChanged(outline);
       
    71 }
       
    72 
       
    73 QColor HbVgOutlineEffect::color() const
       
    74 {
       
    75     Q_D(const HbVgOutlineEffect);
       
    76     return d->color;
       
    77 }
       
    78 
       
    79 void HbVgOutlineEffect::setColor(const QColor &color)
       
    80 {
       
    81     Q_D(HbVgOutlineEffect);
       
    82     if (d->color == color)
       
    83         return;
       
    84     d->color = color;
       
    85     updateEffect();
       
    86     emit colorChanged(color);
       
    87 }
       
    88 
       
    89 qreal HbVgOutlineEffect::steepness() const
       
    90 {
       
    91     Q_D(const HbVgOutlineEffect);
       
    92     return d->steepness;
       
    93 }
       
    94 
       
    95 void HbVgOutlineEffect::setSteepness(qreal steepness)
       
    96 {
       
    97     Q_D(HbVgOutlineEffect);
       
    98     if (d->steepness == steepness)
       
    99         return;
       
   100     d->steepness = steepness;
       
   101     updateEffect();
       
   102     emit steepnessChanged(steepness);
       
   103 }
       
   104 
       
   105 QPointF HbVgOutlineEffect::offset() const
       
   106 {
       
   107     Q_D(const HbVgOutlineEffect);
       
   108     return d->offset;
       
   109 }
       
   110 
       
   111 void HbVgOutlineEffect::setOffset(const QPointF &offset)
       
   112 {
       
   113     Q_D(HbVgOutlineEffect);
       
   114     if (d->offset == offset)
       
   115         return;
       
   116     d->offset = offset;
       
   117     updateEffectBoundingRect();
       
   118     emit offsetChanged(offset);
       
   119 }
       
   120 
       
   121 QRectF HbVgOutlineEffect::boundingRectFor(const QRectF &rect) const
       
   122 {
       
   123     Q_D(const HbVgOutlineEffect);
       
   124     QSizeF mappedOutline = d->mapSize(QSizeF(d->outline.x(), d->outline.y()));
       
   125     QPointF mappedOffset = d->mapOffset(d->offset);
       
   126 
       
   127     qreal ox1 = rect.left() - mappedOutline.width() + mappedOffset.x();
       
   128     qreal oy1 = rect.top() - mappedOutline.height() + mappedOffset.y();
       
   129     qreal ox2 = rect.right() + mappedOutline.width() + mappedOffset.x();
       
   130     qreal oy2 = rect.bottom() + mappedOutline.height() + mappedOffset.y();
       
   131 
       
   132     qreal x1 = qMin(rect.left(), ox1);
       
   133     qreal y1 = qMin(rect.top(), oy1);
       
   134     qreal x2 = qMax(rect.right(), ox2);
       
   135     qreal y2 = qMax(rect.bottom(), oy2);
       
   136 
       
   137     return QRectF(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
       
   138 }
       
   139 
       
   140 QPixmap HbVgOutlineEffect::makeOutline(const QVariant &vgImage, const QSize &vgImageSize)
       
   141 {
       
   142 #ifdef HB_EFFECTS_OPENVG
       
   143     QPixmap cachedPm = cached(vgImageSize);
       
   144     if (!cachedPm.isNull())
       
   145         return cachedPm;
       
   146 
       
   147     Q_D(HbVgOutlineEffect);
       
   148     VGImage srcImage = vgImage.value<VGImage>();
       
   149     VGImage dstImage = d->ensurePixmap(&d->dstPixmap, vgImageSize);
       
   150 
       
   151     if (d->paramsChanged) {
       
   152         VGubyte stpc = (VGubyte) clamp(d->steepness, 0.0f, 32.0f);
       
   153         VGubyte unnormalisedOpacity = (VGubyte) (clamp(d->opacity, 0.0f, 1.0f) * 255.0f);
       
   154         for (int i = 0; i < 256; ++i) {
       
   155             VGubyte alpha = (i*stpc > unnormalisedOpacity) ? unnormalisedOpacity : i*stpc;
       
   156             d->lut[i] = 0x00000000
       
   157                 | (d->color.red() << 24)
       
   158                 | (d->color.green() << 16)
       
   159                 | (d->color.blue() << 8)
       
   160                 | alpha;
       
   161         }
       
   162     }
       
   163 
       
   164     QSizeF mappedOutline = d->mapSize(QSizeF(d->outline.x(), d->outline.y()));
       
   165     VGfloat outline_x = (VGfloat) clamp(mappedOutline.width(), HBVG_EPSILON, 16.0f);
       
   166     VGfloat outline_y = (VGfloat) clamp(mappedOutline.height(), HBVG_EPSILON, 16.0f);
       
   167     if (outline_x > HBVG_EPSILON && outline_y > HBVG_EPSILON) {
       
   168         VGImage tmpImage = d->ensurePixmap(&d->tmpPixmap, vgImageSize);
       
   169         vgGaussianBlur(tmpImage, srcImage, outline_x, outline_y, VG_TILE_PAD);
       
   170         vgLookupSingle(dstImage, tmpImage, d->lut, VG_ALPHA, VG_TRUE, VG_FALSE);
       
   171     } else {
       
   172         vgCopyImage(dstImage, 0, 0, srcImage, 0, 0,
       
   173                     vgImageSize.width(), vgImageSize.height(), VG_FALSE);
       
   174     }
       
   175 
       
   176     tryCache(d->dstPixmap);
       
   177 
       
   178     return d->dstPixmap;
       
   179 #else
       
   180     Q_UNUSED(vgImage);
       
   181     Q_UNUSED(vgImageSize);
       
   182     return QPixmap();
       
   183 #endif
       
   184 }
       
   185 
       
   186 void HbVgOutlineEffect::performEffect(QPainter *painter,
       
   187                                       const QPointF &offset,
       
   188                                       const QVariant &vgImage,
       
   189                                       const QSize &vgImageSize)
       
   190 {
       
   191 #ifdef HB_EFFECTS_OPENVG
       
   192     Q_D(HbVgOutlineEffect);
       
   193     QPixmap outlinePm = makeOutline(vgImage, vgImageSize);
       
   194     painter->drawPixmap(offset, outlinePm);
       
   195     painter->drawPixmap(offset, d->srcPixmap);
       
   196 #else
       
   197     Q_UNUSED(painter);
       
   198     Q_UNUSED(offset);
       
   199     Q_UNUSED(vgImage);
       
   200     Q_UNUSED(vgImageSize);
       
   201 #endif
       
   202 }