krita

kis_filter_strategy.cc

00001 /*
00002  *  Copyright (c) 2004 Michael Thaler <michael.thaler@physik.tu-muenchen.de>
00003  *  Copyright (c) 2005 Casper Boemann <cbr@boemann.dk>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 #include <kdebug.h>
00020 #include <klocale.h>
00021 #include "kis_debug_areas.h"
00022 #include "kis_filter_strategy.h"
00023 #include <math.h>
00024 
00025 double KisHermiteFilterStrategy::valueAt(double t) const {
00026         /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */
00027         if(t < 0.0) t = -t;
00028         if(t < 1.0) return((2.0 * t - 3.0) * t * t + 1.0);
00029         return(0.0);
00030 }
00031 
00032 Q_UINT32 KisHermiteFilterStrategy::intValueAt(Q_INT32 t) const {
00033         /* f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1 */
00034         if(t < 0) t = -t;
00035         if(t < 256)
00036     {
00037         t =(2 * t - 3*256) * t * t +(256<<16);
00038 
00039         //go from .24 fixed point to .8 fixedpoint (hack only works with positve numbers, which it is)
00040         t = (t + 0x8000) >> 16;
00041 
00042         // go from .8 fixed point to 8bitscale. ie t = (t*255)/256;
00043         if(t >= 128)
00044             return t - 1;
00045         return t;
00046     }
00047         return(0);
00048 }
00049 
00050 double KisBoxFilterStrategy::valueAt(double t) const {
00051         if((t > -0.5) && (t <= 0.5)) return(1.0);
00052         return(0.0);
00053 }
00054 
00055 Q_UINT32 KisBoxFilterStrategy::intValueAt(Q_INT32 t) const {
00056         /* f(t) = 1, -0.5 < t <= 0.5 */
00057     if((t > -128) && (t <= 128))
00058         return 255;
00059     return 0;
00060 }
00061 
00062 double KisTriangleFilterStrategy::valueAt(double t) const {
00063         if(t < 0.0) t = -t;
00064         if(t < 1.0) return(1.0 - t);
00065         return(0.0);
00066 }
00067 
00068 Q_UINT32 KisTriangleFilterStrategy::intValueAt(Q_INT32 t) const {
00069         /* f(t) = |t|, -1 <= t <= 1 */
00070         if(t < 0) t = -t;
00071         if(t < 256)
00072     {
00073          // calc 256-1 but also go from .8 fixed point to 8bitscale. ie t = (t*255)/256; ie: if(t>=128) return t-1;
00074         if(t>=128) return 256 - t;
00075         return 255 - t;
00076     }
00077         return(0);
00078 }
00079 
00080 
00081 double KisBellFilterStrategy::valueAt(double t) const {
00082         if(t < 0) t = -t;
00083         if(t < .5) return(.75 - (t * t));
00084         if(t < 1.5) {
00085                 t = (t - 1.5);
00086                 return(.5 * (t * t));
00087         }
00088         return(0.0);
00089 }
00090 
00091 double KisBSplineFilterStrategy::valueAt(double t) const {
00092         double tt;
00093 
00094         if(t < 0) t = -t;
00095         if(t < 1) {
00096                 tt = t * t;
00097                 return((.5 * tt * t) - tt + (2.0 / 3.0));
00098         } else if(t < 2) {
00099                 t = 2 - t;
00100                 return((1.0 / 6.0) * (t * t * t));
00101         }
00102         return(0.0);
00103 }
00104 
00105 double KisLanczos3FilterStrategy::valueAt(double t) const {
00106         if(t < 0) t = -t;
00107         if(t < 3.0) return(sinc(t) * sinc(t/3.0));
00108         return(0.0);
00109 }
00110 
00111 double KisLanczos3FilterStrategy::sinc(double x) const {
00112         const double pi=3.1415926535897932385;
00113         x *= pi;
00114         if(x != 0) return(sin(x) / x);
00115         return(1.0);
00116 }
00117 
00118 double KisMitchellFilterStrategy::valueAt(double t) const {
00119         const double B=1.0/3.0;
00120         const double C=1.0/3.0;
00121         double tt;
00122 
00123         tt = t * t;
00124         if(t < 0) t = -t;
00125         if(t < 1.0) {
00126                 t = (((12.0 - 9.0 * B - 6.0 * C) * (t * tt)) + ((-18.0 + 12.0 * B + 6.0 * C) * tt) + (6.0 - 2 * B));
00127                 return(t / 6.0);
00128         } else if(t < 2.0) {
00129                 t = (((-1.0 * B - 6.0 * C) * (t * tt)) + ((6.0 * B + 30.0 * C) * tt) + ((-12.0 * B - 48.0 * C) * t) + (8.0 * B + 24 * C));
00130                 return(t / 6.0);
00131                 }
00132         return(0.0);
00133 }
00134 
00135 KisFilterStrategyRegistry *KisFilterStrategyRegistry::m_singleton = 0;
00136 
00137 KisFilterStrategyRegistry::KisFilterStrategyRegistry()
00138 {
00139     Q_ASSERT(KisFilterStrategyRegistry::m_singleton == 0);
00140     KisFilterStrategyRegistry::m_singleton = this;
00141 }
00142 
00143 KisFilterStrategyRegistry::~KisFilterStrategyRegistry()
00144 {
00145 }
00146 
00147 KisFilterStrategyRegistry* KisFilterStrategyRegistry::instance()
00148 {
00149     if(KisFilterStrategyRegistry::m_singleton == 0)
00150     {
00151         KisFilterStrategyRegistry::m_singleton = new KisFilterStrategyRegistry();
00152         Q_CHECK_PTR(KisFilterStrategyRegistry::m_singleton);
00153         m_singleton->add(new KisHermiteFilterStrategy);
00154         m_singleton->add(new KisBoxFilterStrategy);
00155         m_singleton->add(new KisTriangleFilterStrategy);
00156         m_singleton->add(new KisBellFilterStrategy);
00157         m_singleton->add(new KisBSplineFilterStrategy);
00158         //m_singleton->add(new KisLanczos3FilterStrategy);
00159         m_singleton->add(new KisMitchellFilterStrategy);
00160     }
00161     return KisFilterStrategyRegistry::m_singleton;
00162 }
00163 
KDE Home | KDE Accessibility Home | Description of Access Keys