krita

kis_autobrush_resource.cc

00001 /*
00002  *  Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018  
00019 #include "kis_autobrush_resource.h"
00020 #include <kdebug.h>
00021 
00022 void KisAutobrushShape::createBrush( QImage* img)
00023 {
00024     img->create(m_w, m_h, 32);
00025     for(int j = 0; j < m_h; j++)
00026     {
00027         for(int i = 0; i < m_w; i++)
00028         {
00029             Q_INT8 v = valueAt(i,j);
00030             img->setPixel( i, j, qRgb(v,v,v));
00031         }
00032     }
00033 }
00034 
00035 KisAutobrushCircleShape::KisAutobrushCircleShape(Q_INT32 w, Q_INT32 h, double fh, double fv)
00036     : KisAutobrushShape( w, h, w / 2.0 - fh, h / 2.0 - fv),
00037         m_xcentre ( w / 2.0 ),
00038         m_ycentre ( h / 2.0 ),
00039         m_xcoef ( 2.0 / w ),
00040         m_ycoef ( 2.0 / h ),
00041         m_xfadecoef ( (m_fh == 0) ? 1 : ( 1.0 / m_fh)),
00042         m_yfadecoef ( (m_fv == 0) ? 1 : ( 1.0 / m_fv))
00043 {
00044 }
00045 Q_INT8 KisAutobrushCircleShape::valueAt(Q_INT32 x, Q_INT32 y)
00046 {
00047     double xr = (x - m_xcentre) + 0.5;
00048     double yr = (y - m_ycentre) + 0.5;
00049     double n = norme( xr * m_xcoef, yr * m_ycoef);
00050     if( n > 1 )
00051     {
00052         return 255;
00053     }
00054     else
00055     {
00056         double normeFade = norme( xr * m_xfadecoef, yr * m_yfadecoef );
00057         if( normeFade > 1)
00058         {
00059             double xle, yle;
00060             // xle stands for x-coordinate limit exterior
00061             // yle stands for y-coordinate limit exterior
00062             // we are computing the coordinate on the external ellipse in order to compute
00063             // the fade value
00064             if( xr == 0 )
00065             {
00066                 xle = 0;
00067                 yle = yr > 0 ? 1/m_ycoef : -1/m_ycoef;
00068             } else {
00069                 double c = yr / (double)xr;
00070                 xle = sqrt(1 / norme( m_xcoef, c * m_ycoef ));
00071                 xle = xr > 0 ? xle : -xle;
00072                 yle = xle * c;
00073             }
00074             // On the internal limit of the fade area, normeFade is equal to 1
00075             double normeFadeLimitE = norme( xle * m_xfadecoef, yle * m_yfadecoef );
00076             return (uchar)(255 * ( normeFade - 1 ) / ( normeFadeLimitE - 1 ));
00077         } else {
00078             return 0;
00079         }
00080     }
00081 }
00082 
00083 KisAutobrushRectShape::KisAutobrushRectShape(Q_INT32 w, Q_INT32 h, double fh, double fv)
00084     : KisAutobrushShape( w, h, w / 2.0 - fh, h / 2.0 - fv),
00085         m_xcentre ( w / 2.0 ),
00086         m_ycentre ( h / 2.0 ),
00087         m_c( fv/fh)
00088 {
00089 }
00090 Q_INT8 KisAutobrushRectShape::valueAt(Q_INT32 x, Q_INT32 y)
00091 {
00092     double xr = QABS(x - m_xcentre);
00093     double yr = QABS(y - m_ycentre);
00094     if( xr > m_fh || yr > m_fv )
00095     {
00096         if( yr <= ((xr - m_fh) * m_c + m_fv )  )
00097         {
00098             return (uchar)(255 * (xr - m_fh) / (m_w - m_fh));
00099         } else {
00100             return (uchar)(255 * (yr - m_fv) / (m_w - m_fv));
00101         }
00102     }
00103     else {
00104         return 0;
00105     }
00106 }
KDE Home | KDE Accessibility Home | Description of Access Keys