summaryrefslogtreecommitdiffstats
path: root/krita/core/kis_alpha_mask.h
blob: 1da3384d7074a294ed19a6c677f6755090005f4d (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
/*
 *  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.
 */
#ifndef KIS_ALPHA_MASK_
#define KIS_ALPHA_MASK_

#include <qimage.h>
#include <qvaluevector.h>

#include <ksharedptr.h>

#include "kis_global.h"
#include "kis_types.h"

/**
 * KisAlphaMask is intended to create alpha values from a QImage for use
 * in brush creation. It is not a generic alpha mask that can be used with
 * KisPaintDevices: use a KisSelection for that.
 */
class KisAlphaMask : public KShared {
    
 public:
    /**
       Create an alpha mask based on the specified QImage. If the image is
       not a grayscale, the mask value is calculated from the effective grey
       level and alpha value.
    */
    KisAlphaMask(const QImage& img);

    /**
       As above except quicker as the image does not need to be scanned
       to see if it has any colour pixels.
    */
    KisAlphaMask(const QImage& img, bool hasColor);

    /**
       Create a transparent mask.
    */
    KisAlphaMask(Q_INT32 width, Q_INT32 height);

    virtual ~KisAlphaMask();

    /**
       @return the number of alpha values in a scanline.
    */
    Q_INT32 height() const;

    /**
       @return the number of lines in the mask.
     */
       Q_INT32 width() const;

    /**
       @return the alpha value at the specified position.

       Returns Q_UINT8 OPACITY_TRANSPARENT if the value is
       outside the bounds of the mask.

       XXX: this is, of course, not the best way of masking.
       Better would be to let KisAlphaMask fill a chunk of memory
       with the alpha values at the right position, something like
       void applyMask(Q_UINT8 *pixeldata, Q_INT32 pixelWidth,
       Q_INT32 alphaPos). That would be fastest, or we could
       provide an iterator over the mask, that would be nice, too.
    */
    inline Q_UINT8 alphaAt(Q_INT32 x, Q_INT32 y) const
    {
	if (y >= 0 && y < m_height && x >= 0 && x < m_width) {
	    return m_data[(y * m_width) + x];
	}
	else {
	    return OPACITY_TRANSPARENT;
	}
    }

    void setAlphaAt(Q_INT32 x, Q_INT32 y, Q_UINT8 alpha);

    // Create a new mask by interpolating between mask1 and mask2 as t
    // goes from 0 to 1.
    static KisAlphaMaskSP interpolate(KisAlphaMaskSP mask1, KisAlphaMaskSP mask2, double t);

private:
    void computeAlpha(const QImage& img);
    void copyAlpha(const QImage& img);

    QValueVector<Q_UINT8> m_data;
    Q_INT32 m_width;
    Q_INT32 m_height;
};

#endif // KIS_ALPHA_MASK_