chalk

kis_autobrush_resource.cpp
1/*
2 * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include "kis_autobrush_resource.h"
20#include <kdebug.h>
21
22void KisAutobrushShape::createBrush( TQImage* img)
23{
24 img->create(m_w, m_h, 32);
25 for(int j = 0; j < m_h; j++)
26 {
27 for(int i = 0; i < m_w; i++)
28 {
29 TQ_INT8 v = valueAt(i,j);
30 img->setPixel( i, j, tqRgb(v,v,v));
31 }
32 }
33}
34
35KisAutobrushCircleShape::KisAutobrushCircleShape(TQ_INT32 w, TQ_INT32 h, double fh, double fv)
36 : KisAutobrushShape( w, h, w / 2.0 - fh, h / 2.0 - fv),
37 m_xcentre ( w / 2.0 ),
38 m_ycentre ( h / 2.0 ),
39 m_xcoef ( 2.0 / w ),
40 m_ycoef ( 2.0 / h ),
41 m_xfadecoef ( (m_fh == 0) ? 1 : ( 1.0 / m_fh)),
42 m_yfadecoef ( (m_fv == 0) ? 1 : ( 1.0 / m_fv))
43{
44}
45TQ_INT8 KisAutobrushCircleShape::valueAt(TQ_INT32 x, TQ_INT32 y)
46{
47 double xr = (x - m_xcentre) + 0.5;
48 double yr = (y - m_ycentre) + 0.5;
49 double n = norme( xr * m_xcoef, yr * m_ycoef);
50 if( n > 1 )
51 {
52 return 255;
53 }
54 else
55 {
56 double normeFade = norme( xr * m_xfadecoef, yr * m_yfadecoef );
57 if( normeFade > 1)
58 {
59 double xle, yle;
60 // xle stands for x-coordinate limit exterior
61 // yle stands for y-coordinate limit exterior
62 // we are computing the coordinate on the external ellipse in order to compute
63 // the fade value
64 if( xr == 0 )
65 {
66 xle = 0;
67 yle = yr > 0 ? 1/m_ycoef : -1/m_ycoef;
68 } else {
69 double c = yr / (double)xr;
70 xle = sqrt(1 / norme( m_xcoef, c * m_ycoef ));
71 xle = xr > 0 ? xle : -xle;
72 yle = xle * c;
73 }
74 // On the internal limit of the fade area, normeFade is equal to 1
75 double normeFadeLimitE = norme( xle * m_xfadecoef, yle * m_yfadecoef );
76 return (uchar)(255 * ( normeFade - 1 ) / ( normeFadeLimitE - 1 ));
77 } else {
78 return 0;
79 }
80 }
81}
82
83KisAutobrushRectShape::KisAutobrushRectShape(TQ_INT32 w, TQ_INT32 h, double fh, double fv)
84 : KisAutobrushShape( w, h, w / 2.0 - fh, h / 2.0 - fv),
85 m_xcentre ( w / 2.0 ),
86 m_ycentre ( h / 2.0 ),
87 m_c( fv/fh)
88{
89}
90TQ_INT8 KisAutobrushRectShape::valueAt(TQ_INT32 x, TQ_INT32 y)
91{
92 double xr = TQABS(x - m_xcentre);
93 double yr = TQABS(y - m_ycentre);
94 if( xr > m_fh || yr > m_fv )
95 {
96 if( yr <= ((xr - m_fh) * m_c + m_fv ) )
97 {
98 return (uchar)(255 * (xr - m_fh) / (m_w - m_fh));
99 } else {
100 return (uchar)(255 * (yr - m_fv) / (m_w - m_fv));
101 }
102 }
103 else {
104 return 0;
105 }
106}