1 /*
2  *  Copyright (c) 2007 Boudewijn Rempt boud@valdyas.org
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_fill_painter_test.h"
20 
21 #include "testutil.h"
22 
23 #include <QTest>
24 #include "kis_fill_painter.h"
25 
26 #include <floodfill/kis_scanline_fill.h>
27 
28 #define THRESHOLD 10
29 
testCreation()30 void KisFillPainterTest::testCreation()
31 {
32     KisFillPainter test;
33 }
34 
benchmarkFillPainter(const QPoint & startPoint,bool useCompositioning)35 void KisFillPainterTest::benchmarkFillPainter(const QPoint &startPoint, bool useCompositioning)
36 {
37     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
38     KisPaintDeviceSP dev = new KisPaintDevice(cs);
39 
40     QImage srcImage(TestUtil::fetchDataFileLazy("heavy_labyrinth.png"));
41     QVERIFY(!srcImage.isNull());
42 
43     QRect imageRect = srcImage.rect();
44 
45     dev->convertFromQImage(srcImage, 0, 0, 0);
46 
47 
48     QBENCHMARK_ONCE {
49         KisFillPainter gc(dev);
50         gc.setFillThreshold(THRESHOLD);
51         gc.setWidth(imageRect.width());
52         gc.setHeight(imageRect.height());
53         gc.setPaintColor(KoColor(Qt::red, dev->colorSpace()));
54         gc.setUseCompositioning(useCompositioning);
55         gc.fillColor(startPoint.x(), startPoint.y(), dev);
56     }
57 
58     QImage resultImage =
59         dev->convertToQImage(0,
60                              imageRect.x(), imageRect.y(),
61                              imageRect.width(), imageRect.height());
62 
63     QString testName = QString("heavy_labyrinth_%1_%2_%3")
64         .arg(startPoint.x())
65         .arg(startPoint.y())
66         .arg(useCompositioning ? "composed" : "direct");
67 
68 
69     QVERIFY(TestUtil::checkQImage(resultImage,
70                                   "fill_painter",
71                                   "general_",
72                                   testName));
73 }
74 
benchmarkFillPainter()75 void KisFillPainterTest::benchmarkFillPainter()
76 {
77     benchmarkFillPainter(QPoint(), false);
78 }
79 
benchmarkFillPainterOffset()80 void KisFillPainterTest::benchmarkFillPainterOffset()
81 {
82     benchmarkFillPainter(QPoint(5,5), false);
83 }
84 
benchmarkFillPainterOffsetCompositioning()85 void KisFillPainterTest::benchmarkFillPainterOffsetCompositioning()
86 {
87     benchmarkFillPainter(QPoint(5,5), true);
88 }
89 
benchmarkFillingScanlineColor()90 void KisFillPainterTest::benchmarkFillingScanlineColor()
91 {
92     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
93     KisPaintDeviceSP dev = new KisPaintDevice(cs);
94 
95     QImage srcImage(TestUtil::fetchDataFileLazy("heavy_labyrinth.png"));
96     QVERIFY(!srcImage.isNull());
97 
98     QRect imageRect = srcImage.rect();
99 
100     dev->convertFromQImage(srcImage, 0, 0, 0);
101 
102 
103     QBENCHMARK_ONCE {
104         KisScanlineFill gc(dev, QPoint(), imageRect);
105         gc.setThreshold(THRESHOLD);
106         gc.fillColor(KoColor(Qt::red, dev->colorSpace()));
107     }
108 
109     QImage resultImage =
110         dev->convertToQImage(0,
111                              imageRect.x(), imageRect.y(),
112                              imageRect.width(), imageRect.height());
113 
114     QVERIFY(TestUtil::checkQImage(resultImage,
115                                   "fill_painter",
116                                   "scanline_",
117                                   "heavy_labyrinth_top_left"));
118 }
119 
benchmarkFillingScanlineSelection()120 void KisFillPainterTest::benchmarkFillingScanlineSelection()
121 {
122     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
123     KisPaintDeviceSP dev = new KisPaintDevice(cs);
124 
125     QImage srcImage(TestUtil::fetchDataFileLazy("heavy_labyrinth.png"));
126     QVERIFY(!srcImage.isNull());
127 
128     QRect imageRect = srcImage.rect();
129 
130     dev->convertFromQImage(srcImage, 0, 0, 0);
131 
132 
133     KisPixelSelectionSP pixelSelection = new KisPixelSelection();
134 
135     QBENCHMARK_ONCE {
136         KisScanlineFill gc(dev, QPoint(), imageRect);
137         gc.setThreshold(THRESHOLD);
138         gc.fillSelection(pixelSelection);
139     }
140 
141     QImage resultImage =
142         pixelSelection->convertToQImage(0,
143                                         imageRect.x(), imageRect.y(),
144                                         imageRect.width(), imageRect.height());
145 
146     QVERIFY(TestUtil::checkQImage(resultImage,
147                                   "fill_painter",
148                                   "scanline_",
149                                   "heavy_labyrinth_top_left_selection"));
150 }
151 
testPatternFill()152 void KisFillPainterTest::testPatternFill()
153 {
154     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
155     KisPaintDeviceSP dst = new KisPaintDevice(cs);
156 
157     KisPaintDeviceSP pattern = new KisPaintDevice(cs);
158     pattern->fill(QRect(0,0,32,32), KoColor(Qt::red, cs));
159     pattern->fill(QRect(32,32,32,32), KoColor(Qt::red, cs));
160     pattern->fill(QRect(32,0,32,32), KoColor(Qt::yellow, cs));
161     pattern->fill(QRect(0,32,32,32), KoColor(Qt::white, cs));
162 
163     const QRect fillRect(-128,-128,384,384);
164     KisFillPainter painter(dst);
165 
166 
167     { // fill aligned
168         const QRect patternRect = pattern->exactBounds();
169         painter.fillRect(fillRect.x(), fillRect.y(), fillRect.width(), fillRect.height(), pattern, patternRect);
170         dst->fill(QRect(0,0,10,10), KoColor(Qt::black, cs));
171 
172         QImage resultImage =
173                 dst->convertToQImage(0,
174                                      fillRect.x(), fillRect.y(),
175                                      fillRect.width(), fillRect.height());
176 
177         QVERIFY(TestUtil::checkQImage(resultImage,
178                                       "fill_painter",
179                                       "patterns_fill_",
180                                       "null_origin"));
181     }
182 
183     { // fill with offset
184         dst->clear();
185         pattern->setX(7);
186         pattern->setY(-13);
187 
188 
189         const QRect patternRect = pattern->exactBounds();
190 
191         painter.fillRect(fillRect.x(), fillRect.y(), fillRect.width(), fillRect.height(), pattern, patternRect);
192         dst->fill(QRect(0,0,10,10), KoColor(Qt::black, cs));
193 
194         QImage resultImage =
195                 dst->convertToQImage(0,
196                                      fillRect.x(), fillRect.y(),
197                                      fillRect.width(), fillRect.height());
198 
199         QVERIFY(TestUtil::checkQImage(resultImage,
200                                       "fill_painter",
201                                       "patterns_fill_",
202                                       "custom_origin"));
203     }
204 
205 }
206 
207 
208 QTEST_MAIN(KisFillPainterTest)
209