From 4aed2c8219774f5d797760606b8489a92ddc5163 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebase@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- kdesktop/kshadowengine.cpp | 194 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 kdesktop/kshadowengine.cpp (limited to 'kdesktop/kshadowengine.cpp') diff --git a/kdesktop/kshadowengine.cpp b/kdesktop/kshadowengine.cpp new file mode 100644 index 000000000..12d0319d3 --- /dev/null +++ b/kdesktop/kshadowengine.cpp @@ -0,0 +1,194 @@ +/* This file is proposed to be part of the KDE libraries. + * Copyright (C) 2003 Laur Ivan + * + * Many thanks to: + * - Bernardo Hung for the enhanced shadow + * algorithm (currently used) + * - Tim Jansen for the API updates and fixes. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License version 2 as published by the Free Software Foundation. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#include "kshadowengine.h" +#include +#include "kshadowsettings.h" + +KShadowEngine::KShadowEngine() : + m_shadowSettings( new KShadowSettings ) +{ +} + +KShadowEngine::~KShadowEngine() +{ + delete m_shadowSettings; +} + +KShadowEngine::KShadowEngine(KShadowSettings *fx) : + m_shadowSettings(0L) +{ + setShadowSettings(fx); +} + + +void KShadowEngine::setShadowSettings(KShadowSettings *fx) +{ + delete m_shadowSettings; + + m_shadowSettings = fx; +} + +KShadowSettings *KShadowEngine::shadowSettings() +{ + return m_shadowSettings; +} + +QImage KShadowEngine::makeShadow(const QPixmap& textPixmap, const QColor &bgColor) +{ + QImage result; + + // create a new image for for the shaddow + int w = textPixmap.width(); + int h = textPixmap.height(); + + // avoid calling these methods for every pixel + int bgRed = bgColor.red(); + int bgGreen = bgColor.green(); + int bgBlue = bgColor.blue(); + + int thick = m_shadowSettings->thickness() >> 1; + + double alphaShadow; + + /* + * This is the source pixmap + */ + QImage img = textPixmap.convertToImage().convertDepth(32); + + /* + * Resize the image if necessary + */ + if ((result.width() != w) || (result.height() != h)) + { + result.create(w, h, 32); + } + + result.fill(0); // all black + result.setAlphaBuffer(true); + + for (int i = thick; i < w - thick; i++) + { + for (int j = thick; j < h - thick; j++) + { + switch (m_shadowSettings->algorithm()) + { + case KShadowSettings::DoubleLinearDecay: + alphaShadow = doubleLinearDecay(img, i, j); + break; + case KShadowSettings::RadialDecay: + alphaShadow = radialDecay(img, i, j); + break; + case KShadowSettings::NoDecay: + alphaShadow = noDecay(img, i, j); + break; + case KShadowSettings::DefaultDecay: + default: + alphaShadow = defaultDecay(img, i, j); + } + + alphaShadow = (alphaShadow > m_shadowSettings->maxOpacity()) ? m_shadowSettings->maxOpacity() : alphaShadow; + + // update the shadow's i,j pixel. + result.setPixel(i,j, qRgba(bgRed, bgGreen , bgBlue, (int) alphaShadow)); + } + } + return result; +} + +// Multiplication factor for pixels directly above, under, or next to the text +#define AXIS_FACTOR 2.0 +// Multiplication factor for pixels diagonal to the text +#define DIAGONAL_FACTOR 1.0 + +double KShadowEngine::defaultDecay(QImage& source, int i, int j) +{ + if ((i < 1) || (j < 1) || (i > source.width() - 2) || (j > source.height() - 2)) + return 0; + + double alphaShadow; + alphaShadow =(qGray(source.pixel(i-1,j-1)) * DIAGONAL_FACTOR + + qGray(source.pixel(i-1,j )) * AXIS_FACTOR + + qGray(source.pixel(i-1,j+1)) * DIAGONAL_FACTOR + + qGray(source.pixel(i ,j-1)) * AXIS_FACTOR + + 0 + + qGray(source.pixel(i ,j+1)) * AXIS_FACTOR + + qGray(source.pixel(i+1,j-1)) * DIAGONAL_FACTOR + + qGray(source.pixel(i+1,j )) * AXIS_FACTOR + + qGray(source.pixel(i+1,j+1)) * DIAGONAL_FACTOR) / m_shadowSettings->multiplicationFactor(); + + return alphaShadow; +} + +double KShadowEngine::doubleLinearDecay(QImage& source, int i, int j) +{ + //printf("img: %p, %d %d\n", (char *) &source, i, j); + return defaultDecay( source, i, j ); // for now +} + +double KShadowEngine::radialDecay(QImage& source, int i, int j) +{ + //printf("img: %p, %d %d\n", (char *) &source, i, j); + return defaultDecay( source, i, j ); // for now +} + +double KShadowEngine::noDecay(QImage& source, int i, int j) +{ + // create a new image for for the shaddow + int w = source.width(); + int h = source.height(); + int sx, sy; + //int thick = m_shadowSettings->thickness() >> 1; + + double alphaShadow = 0; + double opacity = 0; + for (int k = 1; k <= m_shadowSettings->thickness(); k++) { + /* Generate a shadow THICKNESS pixels thicker + * on either side than the text image. Ensure + * that i +/- k and j +/- k are within the + * bounds of the text pixmap. + */ + opacity = 0; + for (int l = -k; l <= k; l++) { + if (i < k) + sx = 0; + else if (i >= w - k) + sx = w - 1; + else + sx = i + l; + + for (int m = -k; m <= k; m++) { + if (j < k) + sy = 0; + else if (j >= h - k) + sy = h - 1; + else + sy = j + m; + + opacity += qGray(source.pixel(sx, sy)); + } + } + alphaShadow += opacity / m_shadowSettings->multiplicationFactor(); + } + return alphaShadow; +} -- cgit v1.2.3