kwin Library API Documentation

shadow.cpp

00001 /* Copyright (C) 2003 by Sandro Giessl
00002  * based on the nice CVS KDesktop KShadowEngine class. thanks!
00003  * looking forward to see KShadowEngine in kdefx somewhen btw.. :)
00004  * ------------------------------------------------------------------------
00005  * these are the original copyright notes:
00006  * This file is proposed to be part of the KDE libraries.
00007  * Copyright (C) 2003 Laur Ivan <laurivan@eircom.net>
00008  *
00009  * Many thanks to:
00010  *  - Bernardo Hung <deciare@gta.igs.net> for the enhanced shadow
00011  *    algorithm (currently used)
00012  *  - Tim Jansen <tim@tjansen.de> for the API updates and fixes.
00013  *
00014  * This library is free software; you can redistribute it and/or
00015  * modify it under the terms of the GNU Library General Public
00016  * License version 2 as published by the Free Software Foundation.
00017  *
00018  * This library is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021  * Library General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU Library General Public License
00024  * along with this library; see the file COPYING.LIB.  If not, write to
00025  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00026  * Boston, MA 02111-1307, USA.
00027  */
00028 
00029 #include "shadow.h"
00030 #include <qcolor.h>
00031 
00032 ShadowEngine::ShadowEngine()
00033 {
00034     thickness_ = 1;
00035     multiplicationFactor_ = 10.0;
00036 }
00037 
00038 ShadowEngine::~ShadowEngine()
00039 {
00040 }
00041 
00042 QImage ShadowEngine::makeShadow(const QPixmap& textPixmap, const QColor &bgColor)
00043 {
00044   QImage result;
00045 
00046   // create a new image for for the shaddow
00047   int w = textPixmap.width();
00048   int h = textPixmap.height();
00049 
00050   // avoid calling these methods for every pixel
00051   int bgRed = bgColor.red();
00052   int bgGreen = bgColor.green();
00053   int bgBlue = bgColor.blue();
00054 
00055   double alphaShadow;
00056 
00057   /*
00058    *    This is the source pixmap
00059    */
00060   QImage img = textPixmap.convertToImage().convertDepth(32);
00061 
00062   /*
00063    *    Resize the image if necessary
00064    */
00065   if ((result.width() != w) || (result.height() != h))
00066   {
00067     result.create(w, h, 32);
00068   }
00069 
00070   result.fill(0); // all black
00071   result.setAlphaBuffer(true);
00072 
00073   for (int i = thickness_; i < w - thickness_; i++)
00074   {
00075     for (int j = thickness_; j < h - thickness_; j++)
00076     {
00077         alphaShadow = decay(img, i, j);
00078         alphaShadow = (alphaShadow > 180.0) ? 180.0 : alphaShadow;
00079       // update the shadow's i,j pixel.
00080       result.setPixel(i,j, qRgba(bgRed, bgGreen , bgBlue, (int) alphaShadow));
00081     }
00082   }
00083   return result;
00084 }
00085 
00086 double ShadowEngine::decay(QImage& source, int i, int j)
00087 {
00088   // create a new image for the shadow
00089   int w = source.width();
00090   int h = source.height();
00091   int sx, sy;
00092 
00093   double alphaShadow = 0;
00094   double opacity = 0;
00095   for (int k = 1; k <= thickness_; k++) {
00096     /* Generate a shadow thickness_ pixels thicker
00097      * on either side than the text image. Ensure
00098      * that i +/- k and j +/- k are within the
00099      * bounds of the text pixmap.
00100      */
00101     opacity = 0;
00102     for (int l = -k; l <= k; l++) {
00103       if (i < k)
00104     sx = 0;
00105       else if (i >= w - k)
00106     sx = w - 1;
00107       else
00108     sx = i + l;
00109 
00110       for (int m = -k; m <= k; m++) {
00111     if (j < k)
00112       sy = 0;
00113     else if (j >= h - k)
00114       sy = h - 1;
00115     else
00116       sy = j + m;
00117 
00118     opacity += qGray(source.pixel(sx, sy));
00119       }
00120     }
00121     alphaShadow += opacity / multiplicationFactor_;
00122   }
00123   return alphaShadow;
00124 }
KDE Logo
This file is part of the documentation for kwin Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Nov 4 00:45:28 2005 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003