~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to krita/plugins/paintops/libbrush/kis_qimage_mask.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "kis_global.h"
30
30
 
31
31
 
32
 
KisQImagemask::KisQImagemask(const QImage& img, bool hasColor)
 
32
KisQImagemask::KisQImagemask(const QImage& image, bool hasColor) : m_data(image.width(), image.height(), QImage::Format_Indexed8)
33
33
{
34
 
    m_width = img.width();
35
 
    m_height = img.height();
36
 
 
37
34
    if (hasColor) {
38
 
        copyAlpha(img);
39
 
    } else {
40
 
        computeAlpha(img);
41
 
    }
42
 
}
43
 
 
44
 
KisQImagemask::KisQImagemask(const QImage& img)
45
 
{
46
 
    m_width = img.width();
47
 
    m_height = img.height();
48
 
 
49
 
    if (!img.allGray()) {
50
 
        copyAlpha(img);
51
 
    } else {
52
 
        computeAlpha(img);
53
 
    }
54
 
}
55
 
 
56
 
KisQImagemask::KisQImagemask(qint32 width, qint32 height)
57
 
{
58
 
    m_width = width;
59
 
    m_height = height;
60
 
 
61
 
    m_data.clear();
62
 
    m_data.insert(0, width * height, OPACITY_TRANSPARENT);
 
35
        copyAlpha(image);
 
36
    } else {
 
37
        computeAlpha(image);
 
38
    }
 
39
}
 
40
 
 
41
KisQImagemask::KisQImagemask(const QImage& image) : m_data(image.width(), image.height(), QImage::Format_Indexed8)
 
42
{
 
43
    if (!image.allGray()) {
 
44
        copyAlpha(image);
 
45
    } else {
 
46
        computeAlpha(image);
 
47
    }
 
48
}
 
49
 
 
50
KisQImagemask::KisQImagemask(qint32 width, qint32 height) : m_data(width, height, QImage::Format_Indexed8)
 
51
{
 
52
    m_data.fill(0);
63
53
}
64
54
 
65
55
KisQImagemask::~KisQImagemask()
68
58
 
69
59
qint32 KisQImagemask::width() const
70
60
{
71
 
    return m_width;
 
61
    return m_data.width();
72
62
}
73
63
 
74
64
qint32 KisQImagemask::height() const
75
65
{
76
 
    return m_height;
 
66
    return m_data.height();
77
67
}
78
 
void KisQImagemask::copyAlpha(const QImage& img)
 
68
void KisQImagemask::copyAlpha(const QImage& image)
79
69
{
80
 
    for (int y = 0; y < img.height(); y++) {
81
 
        const QRgb *scanline = reinterpret_cast<const QRgb *>(img.scanLine(y));
82
 
        for (int x = 0; x < img.width(); x++) {
 
70
    for (int y = 0; y < image.height(); y++) {
 
71
        const QRgb *scanline = reinterpret_cast<const QRgb *>(image.scanLine(y));
 
72
        for (int x = 0; x < image.width(); x++) {
83
73
            QRgb c = scanline[x];
84
74
            quint8 a = (qGray(c) * qAlpha(c)) / 255;
85
 
            m_data.push_back(a);
 
75
            m_data.scanLine(y)[x] = a;
86
76
        }
87
77
    }
88
78
}
89
79
 
90
 
void KisQImagemask::computeAlpha(const QImage& img)
 
80
void KisQImagemask::computeAlpha(const QImage& image)
91
81
{
92
82
    // The brushes are mostly grayscale on a white background,
93
83
    // although some do have a colors. The alpha channel is seldom
96
86
    // invert it, because 255, 255, 255 is white, which is
97
87
    // completely transparent, but 255 corresponds to
98
88
    // OPACITY_OPAQUE.
99
 
    for (int y = 0; y < img.height(); y++) {
100
 
        const QRgb *scanline = reinterpret_cast<const QRgb *>(img.scanLine(y));
101
 
        for (int x = 0; x < img.width(); x++) {
102
 
            m_data.push_back(255 - qRed(scanline[x]));
 
89
    for (int y = 0; y < image.height(); y++) {
 
90
        const QRgb *scanline = reinterpret_cast<const QRgb *>(image.scanLine(y));
 
91
        for (int x = 0; x < image.width(); x++) {
 
92
            m_data.scanLine(y)[x] = (255 - qRed(scanline[x]));
103
93
        }
104
94
    }
105
95
}
124
114
    return outputMask;
125
115
}
126
116
 
127
 
 
 
117
void KisQImagemask::rotation(double angle)
 
118
{
 
119
    // For some reason rotating an Indexed8 image is broken so convert to RGB32
 
120
    // Would probably be faster to have a native own implementation
 
121
    QVector<QRgb> table;
 
122
    for (int i = 0; i < 256; ++i) table.append(qRgb(i, i, i));
 
123
    m_data.setColorTable(table);
 
124
    QImage tmp = m_data.convertToFormat(QImage::Format_RGB32);
 
125
    tmp = tmp.transformed(QMatrix().rotate(-angle * 180 / M_PI));
 
126
    m_data = QImage(tmp.width(), tmp.height(), QImage::Format_Indexed8);
 
127
    // Do not use convertToFormat to go back to Indexed8, since it is quiet
 
128
    // a slow general operation, while we know that we are outputing a grayscale image
 
129
    for (int y = 0; y < tmp.height(); ++y) {
 
130
        for (int x = 0; x < tmp.width(); ++x) {
 
131
            m_data.scanLine(y)[x] = tmp.scanLine(y)[4 * x];
 
132
        }
 
133
    }
 
134
}