• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

blur.cpp

Go to the documentation of this file.
00001 #ifndef BLUR_CPP
00002 #define BLUR_CPP
00003 
00004 /*
00005  *   Copyright 2007 Jani Huhtanen <jani.huhtanen@tut.fi>
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU Library General Public License version 2 as
00009  *   published by the Free Software Foundation
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this program; if not, write to the
00018  *   Free Software Foundation, Inc.,
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  */
00021 
00022 #include <cmath>
00023 
00024 // Exponential blur, Jani Huhtanen, 2006
00025 //
00026 template<int aprec, int zprec>
00027 static inline void blurinner(unsigned char *bptr, int &zR, int &zG, int &zB, int &zA, int alpha);
00028 
00029 template<int aprec,int zprec>
00030 static inline void blurrow(QImage &im, int line, int alpha);
00031 
00032 template<int aprec, int zprec>
00033 static inline void blurcol(QImage &im, int col, int alpha);
00034 
00035 /*
00036 *  expblur(QImage &img, int radius)
00037 *
00038 *  In-place blur of image 'img' with kernel
00039 *  of approximate radius 'radius'.
00040 *
00041 *  Blurs with two sided exponential impulse
00042 *  response.
00043 *
00044 *  aprec = precision of alpha parameter
00045 *  in fixed-point format 0.aprec
00046 *
00047 *  zprec = precision of state parameters
00048 *  zR,zG,zB and zA in fp format 8.zprec
00049 */
00050 template<int aprec,int zprec>
00051 void expblur(QImage &img, int radius)
00052 {
00053     if(radius < 1) {
00054     return;
00055   }
00056 
00057   /* Calculate the alpha such that 90% of
00058      the kernel is within the radius.
00059      (Kernel extends to infinity)
00060   */
00061   int alpha = (int)((1 << aprec) * (1.0f - std::exp(-2.3f / (radius + 1.f))));
00062 
00063   for (int row=0; row<img.height(); row++) {
00064     blurrow<aprec,zprec>(img, row, alpha);
00065   }
00066 
00067   for (int col=0; col<img.width(); col++) {
00068     blurcol<aprec,zprec>(img, col, alpha);
00069   }
00070   return;
00071 }
00072 
00073 template<int aprec, int zprec>
00074 static inline void blurinner(unsigned char *bptr, int &zR, int &zG, int &zB, int &zA, int alpha)
00075 {
00076   int R, G, B, A;
00077   R = *bptr;
00078   G = *(bptr + 1);
00079   B = *(bptr + 2);
00080   A = *(bptr + 3);
00081 
00082   zR += (alpha * ((R << zprec) - zR)) >> aprec;
00083   zG += (alpha * ((G << zprec) - zG)) >> aprec;
00084   zB += (alpha * ((B << zprec) - zB)) >> aprec;
00085   zA += (alpha * ((A << zprec) - zA)) >> aprec;
00086 
00087   *bptr =     zR >> zprec;
00088   *(bptr+1) = zG >> zprec;
00089   *(bptr+2) = zB >> zprec;
00090   *(bptr+3) = zA >> zprec;
00091 }
00092 
00093 template<int aprec,int zprec>
00094 static inline void blurrow(QImage &im, int line, int alpha)
00095 {
00096   int zR, zG, zB, zA;
00097 
00098   QRgb *ptr = (QRgb *)im.scanLine(line);
00099 
00100   zR = *((unsigned char *)ptr    ) << zprec;
00101   zG = *((unsigned char *)ptr + 1) << zprec;
00102   zB = *((unsigned char *)ptr + 2) << zprec;
00103   zA = *((unsigned char *)ptr + 3) << zprec;
00104 
00105   for (int index=1; index<im.width(); index++) {
00106       blurinner<aprec,zprec>((unsigned char *)&ptr[index],zR,zG,zB,zA,alpha);
00107   }
00108   for (int index=im.width()-2; index>=0; index--) {
00109       blurinner<aprec,zprec>((unsigned char *)&ptr[index],zR,zG,zB,zA,alpha);
00110   }
00111 }
00112 
00113 template<int aprec, int zprec>
00114 static inline void blurcol(QImage &im, int col, int alpha)
00115 {
00116   int zR, zG, zB, zA;
00117 
00118   QRgb *ptr = (QRgb *)im.bits();
00119   ptr += col;
00120 
00121   zR = *((unsigned char *)ptr    ) << zprec;
00122   zG = *((unsigned char *)ptr + 1) << zprec;
00123   zB = *((unsigned char *)ptr + 2) << zprec;
00124   zA = *((unsigned char *)ptr + 3) << zprec;
00125 
00126   for (int index=im.width(); index<(im.height()-1)*im.width(); index+=im.width()) {
00127       blurinner<aprec,zprec>((unsigned char *)&ptr[index], zR, zG, zB, zA, alpha);
00128   }
00129 
00130   for (int index=(im.height()-2)*im.width(); index>=0; index-=im.width()) {
00131       blurinner<aprec,zprec>((unsigned char *)&ptr[index], zR, zG, zB, zA, alpha);
00132   }
00133 }
00134 
00135 template<class T>
00136 inline const T &qClamp(const T &x, const T &low, const T &high)
00137 {
00138     if (x <  low) {
00139         return low;
00140     } else if (x > high) {
00141         return high;
00142     } else {
00143         return x;
00144     }
00145 }
00146 
00147 #endif

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal