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

« back to all changes in this revision

Viewing changes to krita/plugins/paintops/libbrush/kis_boundary.cc

  • 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:
17
17
 */
18
18
 
19
19
#include "kis_boundary.h"
20
 
#include <QPixmap>
21
20
#include <QPainter>
22
 
#include <QList>
 
21
#include <QPen>
23
22
 
24
23
#include "KoColorSpace.h"
25
24
#include "kis_fixed_paint_device.h"
26
 
#include "kis_iterators_pixel.h"
 
25
#include "kis_outline_generator.h"
27
26
 
28
27
struct KisBoundary::Private {
29
 
    bool isDark(quint8 val);
30
28
    KisFixedPaintDeviceSP m_device;
31
 
    int m_fuzzyness;
32
 
 
33
 
    PointPairListList m_horSegments;
34
 
    PointPairListList m_vertSegments;
35
 
 
 
29
    QVector<QPolygon> m_boundary;
36
30
};
37
31
 
38
32
KisBoundary::KisBoundary(KisFixedPaintDeviceSP dev) : d(new Private)
39
33
{
40
34
    d->m_device = dev;
41
 
    d->m_fuzzyness = 255 / 2;
42
35
}
43
36
 
44
37
KisBoundary::~KisBoundary()
45
38
{
46
39
}
47
40
 
48
 
bool KisBoundary::Private::isDark(quint8 val)
49
 
{
50
 
    return val < m_fuzzyness;
51
 
}
52
 
 
53
 
void KisBoundary::generateBoundary(int w, int h)
 
41
void KisBoundary::generateBoundary()
54
42
{
55
43
    if (!d->m_device)
56
44
        return;
57
45
 
58
 
    const KoColorSpace* cs = d->m_device->colorSpace();
59
 
    int pixelSize = d->m_device->pixelSize();
60
 
 
61
 
    // Yes, we start looking before the begin of the data. There we return the default pixel,
62
 
    // which is transparent.
63
 
    quint8* dataPointer = d->m_device->data();
64
 
    quint8* dataPointerTop = d->m_device->data() - w * pixelSize;
65
 
    quint8* dataPointerBot = d->m_device->data();
66
 
    // Horizontal
67
 
    for (int currentY = -1; currentY < h; currentY++) {
68
 
 
69
 
        d->m_horSegments.append(QList<PointPair>());
70
 
 
71
 
        for (int currentX = 0; currentX < w; currentX++) {
72
 
 
73
 
            bool darkTop;
74
 
            bool darkBot;
75
 
 
76
 
            if (dataPointerTop < dataPointer) {
77
 
                darkTop = OPACITY_TRANSPARENT;
78
 
            } else {
79
 
                darkTop = cs->alpha(dataPointerTop);
80
 
            }
81
 
            darkBot = cs->alpha(dataPointerBot);
82
 
 
83
 
            if (darkTop != darkBot) {
84
 
                // detected a change
85
 
                d->m_horSegments.back().append(qMakePair(QPointF(currentX, currentY + 1), 1));
86
 
            }
87
 
 
88
 
            dataPointerTop++;
89
 
            dataPointerBot++;
90
 
        }
91
 
    }
92
 
 
93
 
    // Vertical
94
 
    for (int currentX = - 1; currentX < w; currentX++) {
95
 
 
96
 
        bool darkLeft;
97
 
        bool darkRight;
98
 
 
99
 
        d->m_vertSegments.append(QList<PointPair>());
100
 
 
101
 
        for (int currentY = 0; currentY < h; currentY++) {
102
 
 
103
 
            quint8* dataPointerLeft = d->m_device->data() + (h * pixelSize) + (currentX * pixelSize);
104
 
            quint8* dataPointerRight = dataPointerTop - pixelSize;
105
 
 
106
 
            darkLeft = cs->alpha(dataPointerLeft);
107
 
            darkRight = cs->alpha(dataPointerRight);
108
 
 
109
 
            if (darkLeft != darkRight) {
110
 
                // detected a change
111
 
                d->m_vertSegments.back().append(qMakePair(QPointF(currentX, currentY), 1));
112
 
            }
113
 
        }
114
 
    }
115
 
 
116
 
}
117
 
 
118
 
const KisBoundary::PointPairListList& KisBoundary::horizontalSegment() const
119
 
{
120
 
    return d->m_horSegments;
121
 
}
122
 
 
123
 
const KisBoundary::PointPairListList& KisBoundary::verticalSegment() const
124
 
{
125
 
    return d->m_vertSegments;
126
 
}
127
 
 
 
46
    KisOutlineGenerator generator(d->m_device->colorSpace(), OPACITY_TRANSPARENT_U8);
 
47
    d->m_boundary = generator.outline(d->m_device->data(), 0, 0, d->m_device->bounds().width(), d->m_device->bounds().height());
 
48
}
 
49
 
 
50
void KisBoundary::paint(QPainter& painter) const
 
51
 
52
    QPen pen;
 
53
    pen.setWidth(0);
 
54
    pen.setBrush(Qt::black);
 
55
    painter.setPen(pen);
 
56
    
 
57
    foreach(const QPolygon & polygon, d->m_boundary) {
 
58
        painter.drawPolygon(polygon);
 
59
    }
 
60
}