chalk

kis_alpha_mask.cpp
1/*
2 * Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
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 <cfloat>
20#include <tqimage.h>
21#include <tqvaluevector.h>
22
23#include <kdebug.h>
24
25#include "kis_global.h"
26#include "kis_alpha_mask.h"
27
28KisAlphaMask::KisAlphaMask(const TQImage& img, bool hasColor)
29{
30 m_width = img.width();
31 m_height = img.height();
32
33 if (hasColor) {
34 copyAlpha(img);
35 }
36 else {
37 computeAlpha(img);
38 }
39}
40
41KisAlphaMask::KisAlphaMask(const TQImage& img)
42{
43 m_width = img.width();
44 m_height = img.height();
45
46 if (!img.allGray()) {
47 copyAlpha(img);
48 }
49 else {
50 computeAlpha(img);
51 }
52}
53
54KisAlphaMask::KisAlphaMask(TQ_INT32 width, TQ_INT32 height)
55{
56 m_width = width;
57 m_height = height;
58
59 m_data.resize(width * height, OPACITY_TRANSPARENT);
60}
61
62KisAlphaMask::~KisAlphaMask()
63{
64}
65
66TQ_INT32 KisAlphaMask::width() const
67{
68 return m_width;
69}
70
71TQ_INT32 KisAlphaMask::height() const
72{
73 return m_height;
74}
75
76void KisAlphaMask::setAlphaAt(TQ_INT32 x, TQ_INT32 y, TQ_UINT8 alpha)
77{
78 if (y >= 0 && y < m_height && x >= 0 && x < m_width) {
79 m_data[(y * m_width) + x] = alpha;
80 }
81}
82
83void KisAlphaMask::copyAlpha(const TQImage& img)
84{
85 for (int y = 0; y < img.height(); y++) {
86 for (int x = 0; x < img.width(); x++) {
87 TQRgb c = img.pixel(x,y);
88 TQ_UINT8 a = (tqGray(c) * tqAlpha(c)) / 255;
89 m_data.push_back(a);
90
91 }
92 }
93}
94
95void KisAlphaMask::computeAlpha(const TQImage& img)
96{
97 // The brushes are mostly grayscale on a white background,
98 // although some do have a colors. The alpha channel is seldom
99 // used, so we take the average gray value of this pixel of
100 // the brush as the setting for the opacitiy. We need to
101 // invert it, because 255, 255, 255 is white, which is
102 // completely transparent, but 255 corresponds to
103 // OPACITY_OPAQUE.
104
105 for (int y = 0; y < img.height(); y++) {
106 for (int x = 0; x < img.width(); x++) {
107 m_data.push_back(255 - tqRed(img.pixel(x, y)));
108 }
109 }
110}
111
112KisAlphaMaskSP KisAlphaMask::interpolate(KisAlphaMaskSP mask1, KisAlphaMaskSP mask2, double t)
113{
114 Q_ASSERT((mask1->width() == mask2->width()) && (mask1->height() == mask2->height()));
115 Q_ASSERT(t > -DBL_EPSILON && t < 1 + DBL_EPSILON);
116
117 int width = mask1->width();
118 int height = mask1->height();
119 KisAlphaMaskSP outputMask = new KisAlphaMask(width, height);
120 TQ_CHECK_PTR(outputMask);
121
122 for (int x = 0; x < width; x++) {
123 for (int y = 0; y < height; y++) {
124 TQ_UINT8 d = static_cast<TQ_UINT8>((1 - t) * mask1->alphaAt(x, y) + t * mask2->alphaAt(x, y));
125 outputMask->setAlphaAt(x, y, d);
126 }
127 }
128
129 return outputMask;
130}
131
132
KisAlphaMask is intended to create alpha values from a TQImage for use in brush creation.
TQ_INT32 width() const
KisAlphaMask(const TQImage &img)
Create an alpha mask based on the specified TQImage.
TQ_INT32 height() const