summaryrefslogtreecommitdiffstats
path: root/chalk/core/kis_alpha_mask.cpp
blob: 152963087c185e0a57bbe0f43896ed62f4d81e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 *  Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <cfloat>
#include <tqimage.h>
#include <tqvaluevector.h>

#include <kdebug.h>

#include "kis_global.h"
#include "kis_alpha_mask.h"

KisAlphaMask::KisAlphaMask(const TQImage& img, bool hasColor)
{
    m_width = img.width();
    m_height = img.height();

    if (hasColor) {
        copyAlpha(img);
    }
    else {
        computeAlpha(img);
    }
}

KisAlphaMask::KisAlphaMask(const TQImage& img)
{
    m_width = img.width();
    m_height = img.height();

    if (!img.allGray()) {
        copyAlpha(img);
    }
    else {
        computeAlpha(img);
    }
}

KisAlphaMask::KisAlphaMask(TQ_INT32 width, TQ_INT32 height)
{
    m_width = width;
    m_height = height;

    m_data.resize(width * height, OPACITY_TRANSPARENT);
}

KisAlphaMask::~KisAlphaMask()
{
}

TQ_INT32 KisAlphaMask::width() const
{
    return m_width;
}

TQ_INT32 KisAlphaMask::height() const
{
    return m_height;
}

void KisAlphaMask::setAlphaAt(TQ_INT32 x, TQ_INT32 y, TQ_UINT8 alpha)
{
    if (y >= 0 && y < m_height && x >= 0 && x < m_width) {
        m_data[(y * m_width) + x] = alpha;
    }
}

void KisAlphaMask::copyAlpha(const TQImage& img)
{
    for (int y = 0; y < img.height(); y++) {
        for (int x = 0; x < img.width(); x++) {
                        TQRgb c = img.pixel(x,y);
                        TQ_UINT8 a = (tqGray(c) * tqAlpha(c)) / 255;
            m_data.push_back(a);

        }
    }
}

void KisAlphaMask::computeAlpha(const TQImage& img)
{
    // The brushes are mostly grayscale on a white background,
    // although some do have a colors. The alpha channel is seldom
    // used, so we take the average gray value of this pixel of
    // the brush as the setting for the opacitiy. We need to
    // invert it, because 255, 255, 255 is white, which is
    // completely transparent, but 255 corresponds to
    // OPACITY_OPAQUE.

    for (int y = 0; y < img.height(); y++) {
        for (int x = 0; x < img.width(); x++) {
            m_data.push_back(255 - tqRed(img.pixel(x, y)));
        }
    }
}

KisAlphaMaskSP KisAlphaMask::interpolate(KisAlphaMaskSP mask1, KisAlphaMaskSP mask2, double t)
{
    Q_ASSERT((mask1->width() == mask2->width()) && (mask1->height() == mask2->height()));
    Q_ASSERT(t > -DBL_EPSILON && t < 1 + DBL_EPSILON);

    int width = mask1->width();
    int height = mask1->height();
    KisAlphaMaskSP outputMask = new KisAlphaMask(width, height);
    TQ_CHECK_PTR(outputMask);

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            TQ_UINT8 d = static_cast<TQ_UINT8>((1 - t) * mask1->alphaAt(x, y) + t * mask2->alphaAt(x, y));
            outputMask->setAlphaAt(x, y, d);
        }
    }

    return outputMask;
}