File: kpAbstractImageSelection.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 (582 lines) | stat: -rw-r--r-- 17,603 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582

/*
   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/kpAbstractImageSelection.h"

#include <QBitmap>
#include <QPainter>

#include "kpLogCategories.h"

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

// Returns whether <sel> can be set to have <baseImage>.
// In other words, this is the precondition for <sel>.setBaseImage(<baseImage).
//
// This checks that they have compatible relative dimensions.
static bool CanSetBaseImageTo (kpAbstractImageSelection *sel, const kpImage &baseImage)
{
    if (baseImage.isNull ())
    {
        // Always allowed to wipe out selection content, changing it into a
        // border.
        return true;
    }

    return (baseImage.width () == sel->width () &&
            baseImage.height () == sel->height ());
}

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

struct kpAbstractImageSelectionPrivate
{
    kpImage baseImage;

    kpImageSelectionTransparency transparency;

    // The mask for the image, after selection transparency (a.k.a. background
    // subtraction) is applied.
    QBitmap transparencyMaskCache;  // OPT: calculate lazily i.e. on-demand only
};

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

// protected
kpAbstractImageSelection::kpAbstractImageSelection (
        const kpImageSelectionTransparency &transparency)
    : kpAbstractSelection (),
      d (new kpAbstractImageSelectionPrivate ())
{
    setTransparency (transparency);
}

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

// protected
kpAbstractImageSelection::kpAbstractImageSelection (const QRect &rect,
        const kpImage &baseImage,
        const kpImageSelectionTransparency &transparency)
    : kpAbstractSelection (rect),
      d (new kpAbstractImageSelectionPrivate ())
{
    // This also checks that <rect> and <baseImage> have compatible
    // relative dimensions.
    setBaseImage (baseImage);

    setTransparency (transparency);
}

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

// protected
kpAbstractImageSelection::kpAbstractImageSelection (const QRect &rect,
        const kpImageSelectionTransparency &transparency)
    : kpAbstractSelection (rect),
      d (new kpAbstractImageSelectionPrivate ())
{
    setTransparency (transparency);
}

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

// protected
kpAbstractImageSelection &kpAbstractImageSelection::operator= (
        const kpAbstractImageSelection &rhs)
{
    kpAbstractSelection::operator= (rhs);

    d->baseImage = rhs.d->baseImage;

    d->transparency = rhs.d->transparency;
    d->transparencyMaskCache = rhs.d->transparencyMaskCache;

    return *this;
}

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

// protected
kpAbstractImageSelection::~kpAbstractImageSelection ()
{
    delete d;
}

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

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

    QImage qimage;
    stream >> qimage;
#if DEBUG_KP_SELECTION && 1
    qCDebug(kpLogLayers) << "\timage: w=" << qimage.width () << " h=" << qimage.height ()
               << " depth=" << qimage.depth () << endl;
#endif

    if (!qimage.isNull ())
    {
        // Image size does not match the selection's dimensions?
        // This call only accesses our superclass' fields, which have already
        // been read in.
        if (!::CanSetBaseImageTo (this, qimage))
        {
            return false;
        }

        d->baseImage = qimage;
    }
    // (was just a selection border in the clipboard, even though KolourPaint's
    //  GUI doesn't allow you to copy such a thing into the clipboard)
    else
        d->baseImage = kpImage ();

    // TODO: Reset transparency mask?
    // TODO: Concrete subclass need to emit changed()?
    //       [we can't since changed() must be called after all reading
    //        is complete and subclasses always call this method
    //        _before_ their reading logic]
    return true;
}

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

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

    if (!d->baseImage.isNull ())
    {
        const QImage image = d->baseImage;
    #if DEBUG_KP_SELECTION && 1
        qCDebug(kpLogLayers) << "\twrote image rect=" << image.rect ();
    #endif
        stream << image;
    }
    else
    {
    #if DEBUG_KP_SELECTION && 1
        qCDebug(kpLogLayers) << "\twrote no image because no pixmap";
    #endif
        stream << QImage ();
    }
}

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

// public virtual [kpAbstractSelection]
QString kpAbstractImageSelection::name () const
{
    return i18n ("Selection");
}

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

// public virtual [base kpAbstractSelection]
kpCommandSize::SizeType kpAbstractImageSelection::size () const
{
    return kpAbstractSelection::size () +
        kpCommandSize::ImageSize (d->baseImage) +
        (d->transparencyMaskCache.width() * d->transparencyMaskCache.height()) / 8;
}

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

// public
kpCommandSize::SizeType kpAbstractImageSelection::sizeWithoutImage () const
{
    return (size () - kpCommandSize::ImageSize (d->baseImage));
}

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

// public virtual [kpAbstractSelection]
int kpAbstractImageSelection::minimumWidth () const
{
    return 1;
}

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

// public virtual [kpAbstractSelection]
int kpAbstractImageSelection::minimumHeight () const
{
    return 1;
}

//---------------------------------------------------------------------
// public virtual
QBitmap kpAbstractImageSelection::shapeBitmap (bool nullForRectangular) const
{
    (void) nullForRectangular;

    Q_ASSERT (boundingRect ().isValid ());

    QBitmap maskBitmap (width (), height ());
    maskBitmap.fill (Qt::color0/*transparent*/);

    {
        QPainter painter(&maskBitmap);

        painter.setPen (Qt::color1/*opaque*/);
        painter.setBrush (Qt::color1/*opaque*/);

        QPolygon points = calculatePoints ();
        points.translate (-x (), -y ());

        // Unlike QPainter::drawRect(), this draws the points literally
        // without being 1 pixel wider and higher.  This requires a QPen
        // or it will draw 1 pixel narrower and shorter.
        painter.drawPolygon (points, Qt::OddEvenFill);
    }

    return maskBitmap;
}

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

// public
kpImage kpAbstractImageSelection::givenImageMaskedByShape (const kpImage &image) const
{
#if DEBUG_KP_SELECTION
    qCDebug(kpLogLayers) << "kpAbstractImageSelection::givenImageMaskedByShape() boundingRect="
              << boundingRect () << endl;
#endif
    Q_ASSERT (image.width () == width () && image.height () == height ());

    if (isRectangular ())
        return image;

    const QRegion mRegion = shapeRegion ().translated (-topLeft ());

#if DEBUG_KP_SELECTION
    qCDebug(kpLogLayers) << "\tshapeRegion=" << shapeRegion ()
              << " [rect=" << shapeRegion ().boundingRect () << "]"
              << " calculatePoints=" << calculatePoints ()
              << " [rect=" << calculatePoints ().boundingRect () << "]"
              << endl;
#endif

    kpImage retImage(width (), height (), QImage::Format_ARGB32_Premultiplied);
    retImage.fill(0);  // transparent

    QPainter painter(&retImage);
    painter.setClipRegion(mRegion);
    painter.drawImage(0, 0, image);
    painter.end();

    return retImage;
}

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

// public virtual [kpAbstractSelection]
bool kpAbstractImageSelection::hasContent () const
{
    return !d->baseImage.isNull ();
}

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

// public virtual [kpAbstractSelection]
void kpAbstractImageSelection::deleteContent ()
{
    if (!hasContent ())
        return;

    setBaseImage (kpImage ());
}

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


// public
kpImage kpAbstractImageSelection::baseImage () const
{
    return d->baseImage;
}

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

// public
void kpAbstractImageSelection::setBaseImage (const kpImage &baseImage)
{
    Q_ASSERT (::CanSetBaseImageTo (this, baseImage));

    // qt doc: the image format must be set to Format_ARGB32Premultiplied or Format_ARGB32
    // for the composition modes to have any effect
    d->baseImage = baseImage.convertToFormat(QImage::Format_ARGB32_Premultiplied);

    recalculateTransparencyMaskCache ();

    emit changed (boundingRect ());
}

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

// public
kpImageSelectionTransparency kpAbstractImageSelection::transparency () const
{
    return d->transparency;
}

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

// public
bool kpAbstractImageSelection::setTransparency (
        const kpImageSelectionTransparency &transparency,
        bool checkTransparentPixmapChanged)
{
    if (d->transparency == transparency)
        return false;

    d->transparency = transparency;

    bool haveChanged = true;

    QBitmap oldTransparencyMaskCache = d->transparencyMaskCache;
    recalculateTransparencyMaskCache ();

    if ( oldTransparencyMaskCache.size() == d->transparencyMaskCache.size() )
    {
        if (d->transparencyMaskCache.isNull ())
        {
        #if DEBUG_KP_SELECTION
            qCDebug(kpLogLayers) << "\tboth old and new pixmaps are null - nothing changed";
        #endif
            haveChanged = false;
        }
        else if (checkTransparentPixmapChanged)
        {
            QImage oldTransparencyMaskImage = oldTransparencyMaskCache.toImage();
            QImage newTransparencyMaskImage = d->transparencyMaskCache.toImage();

            bool changed = false;
            for (int y = 0; y < oldTransparencyMaskImage.height () && !changed; y++)
            {
                for (int x = 0; x < oldTransparencyMaskImage.width () && !changed; x++)
                {
                    if (kpPixmapFX::getColorAtPixel (oldTransparencyMaskImage, x, y) !=
                        kpPixmapFX::getColorAtPixel (newTransparencyMaskImage, x, y))
                    {
                    #if DEBUG_KP_SELECTION
                        qCDebug(kpLogLayers) << "\tdiffer at " << QPoint (x, y)
                                   << " old=" << kpPixmapFX::getColorAtPixel (oldTransparencyMaskImage, x, y).toQRgb ()
                                   << " new=" << kpPixmapFX::getColorAtPixel (newTransparencyMaskImage, x, y).toQRgb ()
                                   << endl;
                    #endif
                        changed = true;
                        break;
                    }
                }
            }

            if (!changed)
                haveChanged = false;
        }
    }


    if (haveChanged)
        emit changed (boundingRect ());

    return haveChanged;
}

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

// private
void kpAbstractImageSelection::recalculateTransparencyMaskCache ()
{
#if DEBUG_KP_SELECTION
    qCDebug(kpLogLayers) << "kpAbstractImageSelection::recalculateTransparencyMaskCache()";
#endif

    if (d->baseImage.isNull ())
    {
    #if DEBUG_KP_SELECTION
        qCDebug(kpLogLayers) << "\tno image - no need for transparency mask";
    #endif
        d->transparencyMaskCache = QBitmap ();
        return;
    }

    if (d->transparency.isOpaque ())
    {
    #if DEBUG_KP_SELECTION
        qCDebug(kpLogLayers) << "\topaque - no need for transparency mask";
    #endif
        d->transparencyMaskCache = QBitmap ();
        return;
    }

    d->transparencyMaskCache = QBitmap(d->baseImage.size());

    QPainter transparencyMaskPainter (&d->transparencyMaskCache);

    bool hasTransparent = false;
    for (int y = 0; y < d->baseImage.height (); y++)
    {
        for (int x = 0; x < d->baseImage.width (); x++)
        {
            const kpColor pixelCol = kpPixmapFX::getColorAtPixel (d->baseImage, x, y);
            if (pixelCol == kpColor::Transparent ||
                pixelCol.isSimilarTo (d->transparency.transparentColor (),
                                      d->transparency.processedColorSimilarity ()))
            {
                transparencyMaskPainter.setPen (Qt::color1/*transparent*/);
                hasTransparent = true;
            }
            else
            {
                transparencyMaskPainter.setPen (Qt::color0/*opaque*/);
            }

            transparencyMaskPainter.drawPoint (x, y);
        }
    }

    transparencyMaskPainter.end ();

    if (!hasTransparent)
    {
    #if DEBUG_KP_SELECTION
        qCDebug(kpLogLayers) << "\tcolour useless - completely opaque";
    #endif
        d->transparencyMaskCache = QBitmap ();
        return;
    }
}

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

// public
kpImage kpAbstractImageSelection::transparentImage () const
{
    kpImage image = baseImage ();

    if (!d->transparencyMaskCache.isNull ())
    {
      QPainter painter(&image);
      painter.setCompositionMode(QPainter::CompositionMode_Clear);
      painter.drawPixmap(0, 0, d->transparencyMaskCache);
    }

    return image;
}

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

// public
void kpAbstractImageSelection::fill (const kpColor &color)
{
    QImage newImage(width(), height(), QImage::Format_ARGB32_Premultiplied);
    newImage.fill(color.toQRgb());

    // LOTODO: Maybe disable Image/Clear menu item if transparent color
    if ( !color.isTransparent() )
    {
      QPainter painter(&newImage);
      painter.setCompositionMode(QPainter::CompositionMode_Clear);
      painter.drawPixmap(0, 0, shapeBitmap());
    }

    setBaseImage (newImage);
}

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

// public virtual
void kpAbstractImageSelection::flip (bool horiz, bool vert)
{
#if DEBUG_KP_SELECTION && 1
    qCDebug(kpLogLayers) << "kpAbstractImageSelection::flip(horiz=" << horiz
               << ",vert=" << vert << ")" << endl;
#endif

    if (!d->baseImage.isNull ())
    {
    #if DEBUG_KP_SELECTION && 1
        qCDebug(kpLogLayers) << "\thave pixmap - flipping that";
    #endif
        d->baseImage = d->baseImage.mirrored(horiz, vert);
    }

    if (!d->transparencyMaskCache.isNull ())
    {
    #if DEBUG_KP_SELECTION && 1
        qCDebug(kpLogLayers) << "\thave transparency mask - flipping that";
    #endif
        QImage image = d->transparencyMaskCache.toImage().mirrored(horiz, vert);
        d->transparencyMaskCache = QBitmap::fromImage(image);
    }

    emit changed (boundingRect ());
}

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

static void Paint (const kpAbstractImageSelection *sel, const kpImage &srcImage,
                   QImage *destImage, const QRect &docRect)
{
    if (!srcImage.isNull ())
    {
        kpPixmapFX::paintPixmapAt (destImage,
                                   sel->topLeft () - docRect.topLeft (),
                                   srcImage);
    }
}

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

// public virtual [kpAbstractSelection]
void kpAbstractImageSelection::paint (QImage *destImage,
        const QRect &docRect) const
{
    ::Paint (this, transparentImage (), destImage, docRect);
}

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

// public
void kpAbstractImageSelection::paintWithBaseImage (QImage *destImage,
        const QRect &docRect) const
{
    ::Paint (this, baseImage (), destImage, docRect);
}

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