~ubuntu-branches/ubuntu/oneiric/koffice/oneiric-updates

« back to all changes in this revision

Viewing changes to krita/plugins/paintops/softbrush/kis_alpha_mask.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2010-10-27 17:52:57 UTC
  • mfrom: (0.12.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101027175257-s04zqqk5bs8ckm9o
Tags: 1:2.2.83-0ubuntu1
* Merge with Debian git remaining changes:
 - Add build-deps on librcps-dev, opengtl-dev, libqtgtl-dev, freetds-dev,
   create-resources, libspnav-dev
 - Remove needless build-dep on libwv2-dev
 - koffice-libs recommends create-resources
 - krita recommends pstoedit
 - Keep our patches
* New upstream release 2.3 beta 3
  - Remove debian/patches fixed by upstream
  - Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (c) 2009,2010 Lukáš Tvrdý <lukast.dev@gmail.com>
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_alpha_mask.h"
20
 
#include "kis_debug.h"
21
 
 
22
 
#include <cstdio>
23
 
#include <cmath>
24
 
#include <QImage>
25
 
 
26
 
KisCircleAlphaMask::KisCircleAlphaMask(int radius){
27
 
    m_sigma = 1.0;
28
 
    m_data = 0;
29
 
    resize(radius);
30
 
}
31
 
 
32
 
 
33
 
 
34
 
KisCircleAlphaMask::~KisCircleAlphaMask()
35
 
{
36
 
    delete [] m_data;
37
 
    m_data = 0;
38
 
}
39
 
 
40
 
inline void KisCircleAlphaMask::initSigma(qreal sigma)
41
 
{
42
 
    m_sigma = sigma;
43
 
    m_sigmaSquared = - 2.0 * m_sigma * m_sigma;
44
 
}
45
 
 
46
 
void KisCircleAlphaMask::setSigma(qreal sigma, qreal sigmaConst)
47
 
{
48
 
    initSigma(sigma);
49
 
    m_sigmaConst = sigmaConst;
50
 
}
51
 
 
52
 
 
53
 
void KisCircleAlphaMask::setSigma(qreal sigma)
54
 
{
55
 
    initSigma(sigma);
56
 
    m_sigmaConst = 1.0 / (2.0 * M_PI * m_sigma * m_sigma);
57
 
}
58
 
 
59
 
 
60
 
 
61
 
// TODO QPoint pos , we will see
62
 
void KisCircleAlphaMask::generateCircleDistanceMap(bool invert)
63
 
{
64
 
    int pos = 0;
65
 
    int yy = 0; // power of y
66
 
    qreal value;
67
 
    for (int y = 0; y <= m_radius; y++){
68
 
        yy = y*y;
69
 
        for (int x = 0; x <= m_radius; x++, pos++){
70
 
            value = sqrt(x*x + yy)/(m_radius);
71
 
            (value > 1.0) ? m_data[pos] = 0.0 : m_data[pos] = (invert ? 1.0 - value : value);
72
 
        }
73
 
    }
74
 
}
75
 
 
76
 
void KisCircleAlphaMask::generateGaussMap ( bool invert )
77
 
{
78
 
    Q_UNUSED(invert);
79
 
    //qreal factor = 1.0;
80
 
 
81
 
    // determine the "clever" radius
82
 
    int ix = 0;
83
 
    bool run = true;
84
 
 
85
 
    while (run){
86
 
        quint8 alpha = qRound(255 * gaussAt(ix, 0) );
87
 
        if (alpha > 0)
88
 
        {
89
 
            ix++;
90
 
        }else{
91
 
            run = false;
92
 
        }
93
 
    }
94
 
    // TODO: clean up after sucessful debugging
95
 
    int radius = ix;
96
 
    qreal step = radius / (qreal)m_radius;
97
 
    kDebug() << "Radius: " << m_radius << " | Computed radius: " << radius << "| Sigma: " << m_sigma << " |Step: " << step;;
98
 
    if (radius == 0) return;
99
 
 
100
 
    int pos = 0;
101
 
    qreal px = 0.0;
102
 
    qreal py = 0.0;
103
 
    qreal pyy = 0.0;
104
 
    for (int y = 0; y <= m_radius; y++) {
105
 
        for (int x = 0; x <= m_radius; x++,pos++) {
106
 
            m_data[pos] = gaussAt(px, py);
107
 
            px += step;
108
 
        }
109
 
        px = 0.0;
110
 
        py += step;
111
 
        pyy = py*py;
112
 
    }
113
 
 
114
 
    //qreal dist = maxLen - minLen;
115
 
 
116
 
// normalize?
117
 
//     pos = 0;
118
 
//     while (pos < m_size){
119
 
//     //           m_data[pos] = (m_data[pos] - minLen) / dist;
120
 
//         m_data[pos] = m_data[pos] * 1.0/maxLen;
121
 
//         pos++;
122
 
//     }
123
 
 
124
 
#if 0
125
 
     pos = 0;
126
 
     for (int y = 0; y <= m_radius; y++) {
127
 
          for (int x = 0; x <= m_radius; x++,pos++) {
128
 
                 printf("%.3f ",m_data[pos]);
129
 
         }
130
 
         printf("\n");
131
 
     }
132
 
#endif
133
 
}
134
 
 
135
 
 
136
 
 
137
 
 
138
 
QImage KisCircleAlphaMask::toQImage()
139
 
{
140
 
    QImage img = QImage(m_width, m_width, QImage::Format_ARGB32);
141
 
 
142
 
    int pos = 0;
143
 
    int alpha;
144
 
    for (int y=0; y < m_width; y++){
145
 
        QRgb *pixel = reinterpret_cast<QRgb *>(img.scanLine(y));
146
 
        for (int x=0; x < m_width; x++, pos++){
147
 
            alpha = qRound(255 * m_data[pos]);
148
 
            pixel[y] = qRgba(alpha, alpha, alpha, 255);
149
 
        }
150
 
    }
151
 
 
152
 
    return img;
153
 
}
154
 
 
155
 
 
156
 
 
157
 
 
158
 
void KisCircleAlphaMask::resize(int radius)
159
 
{
160
 
    m_radius = radius;
161
 
 
162
 
    m_width = m_radius + 1;
163
 
 
164
 
    m_size = m_width * m_width;
165
 
    m_data = new qreal[m_size];
166
 
    memset(m_data, 0 , m_size * sizeof(qreal));
167
 
}
168
 
 
169
 
 
170
 
 
171
 
void KisCircleAlphaMask::smooth(qreal edge0, qreal edge1)
172
 
{
173
 
    int pos = 0;
174
 
    while (pos < m_size){
175
 
        m_data[pos] = smoothstep(edge0, edge1, m_data[pos]);
176
 
        pos++;
177
 
    }
178
 
}
179
 
 
180
 
 
181
 
inline qreal KisCircleAlphaMask::smoothstep(qreal edge0, qreal edge1, qreal x)
182
 
{
183
 
    if (x < edge0)      return 0.0;
184
 
    if (x >= edge1)     return 1.0;
185
 
    x = (x - edge0) / (edge1 - edge0);
186
 
    return x*x*(3 - 2 * x);
187
 
}
188
 
 
189