File: kpFreeFormImageSelection.cpp

package info (click to toggle)
kolourpaint 4:18.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,544 kB
  • sloc: cpp: 37,589; xml: 327; sh: 10; makefile: 6
file content (388 lines) | stat: -rw-r--r-- 12,573 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

/*
   Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#define DEBUG_KP_SELECTION 0


#include "layers/selections/image/kpFreeFormImageSelection.h"

#include "kpLogCategories.h"

#include "imagelib/kpPainter.h"


struct kpFreeFormImageSelectionPrivate
{
    QPolygon orgPoints;

    // Various Qt methods that take a QPolygon interpolate points differently
    // (e.g. QPainter::drawPolygon() vs QRegion(QPolygon)) when given consecutive
    // points that are not cardinally adjacent e.g. these 2 points:
    //
    //          #
    //           #
    //
    // are diagonally, but not cardinally, adjacent.  They are rendered
    // inconsistently.  Also, points which are not adjacent at all definitely
    // require interpolation and are inconsistently rendered:
    //
    //          #
    //                         #
    //
    // So, we only pass cardinally interpolated points to those methods to
    // avoid this issue:
    //
    //           ##
    //            #
    //
    // These interpolated points are stored in <cardPointsCache>.  Regarding
    // <cardPointsLoopCache>, see the APIDoc for cardinallyAdjacentPointsLoop().
    QPolygon cardPointsCache, cardPointsLoopCache;
};


kpFreeFormImageSelection::kpFreeFormImageSelection (
        const kpImageSelectionTransparency &transparency)
    : kpAbstractImageSelection (transparency),
      d (new kpFreeFormImageSelectionPrivate ())
{
}

kpFreeFormImageSelection::kpFreeFormImageSelection (const QPolygon &points,
        const kpImage &baseImage,
        const kpImageSelectionTransparency &transparency)
    : kpAbstractImageSelection (points.boundingRect (), baseImage, transparency),
      d (new kpFreeFormImageSelectionPrivate ())
{
    d->orgPoints = points;
    recalculateCardinallyAdjacentPoints ();
}

kpFreeFormImageSelection::kpFreeFormImageSelection (const QPolygon &points,
        const kpImageSelectionTransparency &transparency)
    : kpAbstractImageSelection (points.boundingRect (), transparency),
      d (new kpFreeFormImageSelectionPrivate ())
{
    d->orgPoints = points;
    recalculateCardinallyAdjacentPoints ();
}

kpFreeFormImageSelection::kpFreeFormImageSelection (const kpFreeFormImageSelection &rhs)
    : kpAbstractImageSelection (),
      d (new kpFreeFormImageSelectionPrivate ())
{
    *this = rhs;
}

kpFreeFormImageSelection &kpFreeFormImageSelection::operator= (const kpFreeFormImageSelection &rhs)
{
    kpAbstractImageSelection::operator= (rhs);

    d->orgPoints = rhs.d->orgPoints;
    d->cardPointsCache = rhs.d->cardPointsCache;
    d->cardPointsLoopCache = rhs.d->cardPointsLoopCache;

    return *this;
}

// public virtual [kpAbstractSelection]
kpFreeFormImageSelection *kpFreeFormImageSelection::clone () const
{
    kpFreeFormImageSelection *sel = new kpFreeFormImageSelection ();
    *sel = *this;
    return sel;
}

kpFreeFormImageSelection::~kpFreeFormImageSelection ()
{
    delete d;
}


// public virtual [kpAbstractSelection]
int kpFreeFormImageSelection::serialID () const
{
    return SerialID;
}

// public virtual [base kpAbstractImageSelection]
bool kpFreeFormImageSelection::readFromStream (QDataStream &stream)
{
    if (!kpAbstractImageSelection::readFromStream (stream))
        return false;

    stream >> d->orgPoints;
    recalculateCardinallyAdjacentPoints ();

    return true;
}

// public virtual [base kpAbstractImageSelection]
void kpFreeFormImageSelection::writeToStream (QDataStream &stream) const
{
    kpAbstractImageSelection::writeToStream (stream);

    stream << d->orgPoints;
}


// public virtual [base kpAbstractImageSelection]
kpCommandSize::SizeType kpFreeFormImageSelection::size () const
{
    return kpAbstractImageSelection::size () +
        (kpCommandSize::PolygonSize (d->orgPoints) +
         kpCommandSize::PolygonSize (d->cardPointsCache) +
         kpCommandSize::PolygonSize (d->cardPointsLoopCache));
}

// public virtual [kpAbstractSelection]
bool kpFreeFormImageSelection::isRectangular () const
{
    return false;
}

// public
QPolygon kpFreeFormImageSelection::originalPoints () const
{
    return d->orgPoints;
}


static QPolygon RecalculateCardinallyAdjacentPoints (const QPolygon &points)
{
#if DEBUG_KP_SELECTION
    qCDebug(kpLogLayers) << "kpFreeFormImageSelection.cpp:RecalculateCardinallyAdjacentPoints()"
              << endl;
    qCDebug(kpLogLayers) << "\tpoints=" << points;
#endif

    // Filter out duplicates.
    QPolygon noDups;
    foreach (const QPoint &p, points)
    {
        if (!noDups.isEmpty () && p == noDups.last ())
            continue;

        noDups.append (p);
    }
#if DEBUG_KP_SELECTION
    qCDebug(kpLogLayers) << "\twithout dups=" << noDups;
#endif

    // Interpolate to ensure cardinal adjacency.
    QPolygon cardPoints;
    foreach (const QPoint &p, noDups)
    {
        if (!cardPoints.isEmpty () &&
            !kpPainter::pointsAreCardinallyAdjacent (p, cardPoints.last ()))
        {
            const QPoint lastPoint = cardPoints.last ();

            QList <QPoint> interpPoints = kpPainter::interpolatePoints (
                lastPoint,
                p,
                true/*cardinal adjacency*/);

            Q_ASSERT (interpPoints.size () >= 2);
            Q_ASSERT (interpPoints [0] == lastPoint);
            Q_ASSERT (interpPoints.last () == p);

            for (int i = 1/*skip already existing point*/;
                 i < interpPoints.size ();
                 i++)
            {
                cardPoints.append (interpPoints [i]);
            }
        }
        else
            cardPoints.append (p);
    }
#if DEBUG_KP_SELECTION
    qCDebug(kpLogLayers) << "\tcardinally adjacent=" << cardPoints;
#endif

    return cardPoints;
}

// protected
void kpFreeFormImageSelection::recalculateCardinallyAdjacentPoints ()
{
    d->cardPointsCache = ::RecalculateCardinallyAdjacentPoints (d->orgPoints);


    QPolygon pointsLoop = d->cardPointsCache;
    if (!pointsLoop.isEmpty ())
        pointsLoop.append (pointsLoop.first ());

    // OPT: We know this method only needs to act on the last 2 points of
    //      "pointLoop", since the previous points are definitely cardinally
    //      adjacent.
    d->cardPointsLoopCache = ::RecalculateCardinallyAdjacentPoints (pointsLoop);
}

// public
QPolygon kpFreeFormImageSelection::cardinallyAdjacentPoints () const
{
    return d->cardPointsCache;
}

// public
QPolygon kpFreeFormImageSelection::cardinallyAdjacentPointsLoop () const
{
    return d->cardPointsLoopCache;
}


// public virtual [kpAbstractSelection]
QPolygon kpFreeFormImageSelection::calculatePoints () const
{
    return d->cardPointsLoopCache;
}


// protected virtual [kpAbstractSelection]
QRegion kpFreeFormImageSelection::shapeRegion () const
{
    const QRegion region = QRegion (d->cardPointsLoopCache, Qt::OddEvenFill);

    // In Qt4, while QPainter::drawRect() gives you rectangles 1 pixel
    // wider and higher, QRegion(QPolygon) gives you regions 1 pixel
    // narrower and shorter!  Compensate for this by merging shifted
    // versions of the region.  This seems to be consistent with shapeBitmap()
    // but I am a bit worried.
    //
    // Regarding alternative solutions:
    // 1. Instead of doing this region shifting and merging, if we were to
    //    construct a QRegion simply from a point array with 4 points for
    //    every point in "d->cardPointsLoopCache" (4 points = original point + 3
    //    translations below), it probably wouldn't work because the order of
    //    the points in any point array matter for the odd-even fill
    //    algorithm.  This would probably manifest as problems with
    //    self-intersecting borders.
    // 2. Constructing a QRegion from QBitmap (from shapeBitmap()) is probably
    //    very slow since it would have to read each pixel of the QBitmap.
    //    Having said that, this is probably the safest option as region shifting
    //    is dodgy.  Also, this would guarantee that shapeBitmap() and shapeRegion()
    //    are consistent and we wouldn't need cardinally adjacent points either
    //    (d->cardPointsCache and d->cardPointsLoopCache).
    const QRegion regionX = region.translated (1, 0);
    const QRegion regionY = region.translated (0, 1);
    const QRegion regionXY = region.translated (1, 1);

    return region.united (regionX).united (regionY).united (regionXY);
}


// public virtual [kpAbstractSelection]
bool kpFreeFormImageSelection::contains (const QPoint &point) const
{
    if (!boundingRect ().contains (point))
        return false;

    // We can't use the baseImage() (when non-null) and get the transparency of
    // the pixel at <point>, instead of this region test, as the pixel may be
    // transparent but still within the border.
    return shapeRegion ().contains (point);
}


// public virtual [base kpAbstractSelection]
void kpFreeFormImageSelection::moveBy (int dx, int dy)
{
    d->orgPoints.translate (dx, dy);

    d->cardPointsCache.translate (dx, dy);
    d->cardPointsLoopCache.translate (dx, dy);

    // Call base last since it fires the changed() signal and we only
    // want that to fire at the very end of this method, after all
    // the selection state has been changed.
    kpAbstractImageSelection::moveBy (dx, dy);
}

//---------------------------------------------------------------------

static void FlipPoints (QPolygon *points,
        bool horiz, bool vert,
        const QRect &oldRect)
{
    points->translate (-oldRect.x (), -oldRect.y ());

    const QMatrix matrix (horiz ? -1 : +1,  // m11
                          0,  // m12
                          0,  // m21
                          vert  ? -1 : +1,  // m22
                          horiz ? (oldRect.width() - 1) : 0,  // dx
                          vert  ? (oldRect.height() - 1) : 0);  // dy

#if !defined (QT_NO_DEBUG) && !defined (NDEBUG)
    QPolygon oldPoints = *points;
#endif

    *points = matrix.map (*points);

#if !defined (QT_NO_DEBUG) && !defined (NDEBUG)
    // Sanity check: flipping the points twice gives us the original points.
    Q_ASSERT (oldPoints == matrix.map (*points));
#endif

    points->translate (oldRect.x (), oldRect.y ());
}

//---------------------------------------------------------------------

// public virtual [base kpAbstractImageSelection]
void kpFreeFormImageSelection::flip (bool horiz, bool vert)
{
    ::FlipPoints (&d->orgPoints, horiz, vert, boundingRect ());

    ::FlipPoints (&d->cardPointsCache, horiz, vert, boundingRect ());
    ::FlipPoints (&d->cardPointsLoopCache, horiz, vert, boundingRect ());


    // Call base last since it fires the changed() signal and we only
    // want that to fire at the very end of this method, after all
    // the selection state has been changed.
    kpAbstractImageSelection::flip (horiz, vert);
}

//---------------------------------------------------------------------

// public virtual [kpAbstractSelection]
void kpFreeFormImageSelection::paintBorder (QImage *destPixmap, const QRect &docRect,
        bool selectionFinished) const
{
    if (selectionFinished)
        paintPolygonalBorder (cardinallyAdjacentPointsLoop (),
            destPixmap, docRect, selectionFinished);
    else
        paintPolygonalBorder (cardinallyAdjacentPoints (),
            destPixmap, docRect, selectionFinished);
}