File: qimage_based_test.h

package info (click to toggle)
krita 1%3A3.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 430,944 kB
  • ctags: 68,203
  • sloc: cpp: 798,916; xml: 7,448; ansic: 3,844; perl: 434; sh: 140; makefile: 20
file content (316 lines) | stat: -rw-r--r-- 10,581 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
/*
 *  Copyright (c) 2011 Dmitry Kazakov <dimula73@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __QIMAGE_BASED_TEST_H
#define __QIMAGE_BASED_TEST_H

#include "testutil.h"


#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>

#include <KoShapeContainer.h>
#include <KoShapeRegistry.h>
#include <KoShapeFactoryBase.h>
#include "KisDocument.h"
#include "kis_shape_layer.h"

#include "kis_undo_stores.h"
#include "kis_image.h"
#include "kis_selection.h"
#include "kis_paint_layer.h"
#include "kis_adjustment_layer.h"
#include "kis_transparency_mask.h"
#include "kis_clone_layer.h"

#include "filter/kis_filter.h"
#include "filter/kis_filter_registry.h"

#include "commands/kis_selection_commands.h"


namespace TestUtil
{

class QImageBasedTest
{
public:
    QImageBasedTest(const QString &directoryName)
        : m_directoryName(directoryName)
    {
    }

    // you need to declare your own test function
    // See KisProcessingTest for example

protected:

    /**
     * Creates a complex image connected to a surrogate undo store
     */
    KisImageSP createImage(KisSurrogateUndoStore *undoStore) {
        QImage sourceImage(fetchDataFileLazy("hakonepa.png"));

        QRect imageRect = QRect(QPoint(0,0), sourceImage.size());

        QRect transpRect(50,50,300,300);
        QRect blurRect(66,66,300,300);
        QPoint blurShift(34,34);
        QPoint cloneShift(75,75);

        const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
        KisImageSP image = new KisImage(undoStore, imageRect.width(), imageRect.height(), cs, "merge test");

        KisFilterSP filter = KisFilterRegistry::instance()->value("blur");
        Q_ASSERT(filter);
        KisFilterConfigurationSP configuration = filter->defaultConfiguration(0);
        Q_ASSERT(configuration);

        KisAdjustmentLayerSP blur1 = new KisAdjustmentLayer(image, "blur1", configuration, 0);
        blur1->internalSelection()->clear();
        blur1->internalSelection()->pixelSelection()->select(blurRect);
        blur1->setX(blurShift.x());
        blur1->setY(blurShift.y());

        KisPaintLayerSP paintLayer1 = new KisPaintLayer(image, "paint1", OPACITY_OPAQUE_U8);
        paintLayer1->paintDevice()->convertFromQImage(sourceImage, 0, 0, 0);

        KisCloneLayerSP cloneLayer1 =
            new KisCloneLayer(paintLayer1, image, "clone1", OPACITY_OPAQUE_U8);
        cloneLayer1->setX(cloneShift.x());
        cloneLayer1->setY(cloneShift.y());

        image->addNode(cloneLayer1);
        image->addNode(blur1);
        image->addNode(paintLayer1);

        KisTransparencyMaskSP transparencyMask1 = new KisTransparencyMask();
        transparencyMask1->setName("tmask1");
        transparencyMask1->testingInitSelection(transpRect, paintLayer1);

        image->addNode(transparencyMask1, paintLayer1);

        return image;
    }

    /**
     * Creates a simple image with one empty layer and connects it to
     * a surrogate undo store
     */
    KisImageSP createTrivialImage(KisSurrogateUndoStore *undoStore) {
        QRect imageRect = QRect(0, 0, 640, 441);

        const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
        KisImageSP image = new KisImage(undoStore, imageRect.width(), imageRect.height(), cs, "merge test");

        KisPaintLayerSP paintLayer1 = new KisPaintLayer(image, "paint1", OPACITY_OPAQUE_U8);
        image->addNode(paintLayer1);

        return image;
    }

    void addGlobalSelection(KisImageSP image) {
        QRect selectionRect(40,40,300,300);

        KisSelectionSP selection = new KisSelection(new KisSelectionDefaultBounds(0, image));
        KisPixelSelectionSP pixelSelection = selection->pixelSelection();
        pixelSelection->select(selectionRect);

        KUndo2Command *cmd = new KisSetGlobalSelectionCommand(image, selection);
        image->undoAdapter()->addCommand(cmd);
    }

    void addShapeLayer(KisDocument *doc, KisImageSP image) {

        KisShapeLayerSP shapeLayer = new KisShapeLayer(doc->shapeController(), image.data(), "shape", OPACITY_OPAQUE_U8);
        image->addNode(shapeLayer);

        KoShapeFactoryBase *f1 = KoShapeRegistry::instance()->get("StarShape");
        KoShapeFactoryBase *f2 = KoShapeRegistry::instance()->get("RectangleShape");

        KoShape *shape1 = f1->createDefaultShape();
        KoShape *shape2 = f2->createDefaultShape();

        shape1->setPosition(QPointF(100,100));
        shape2->setPosition(QPointF(200,200));

        shapeLayer->addShape(shape1);
        shapeLayer->addShape(shape2);

        QApplication::processEvents();
    }

    bool checkLayersInitial(KisImageWSP image, int baseFuzzyness = 0) {
        QString prefix = "initial_with_selection";
        QString prefix2 = findNode(image->root(), "shape") ? "_with_shape" : "";
        return checkLayers(image, prefix + prefix2, baseFuzzyness);
    }

    bool checkLayersInitialRootOnly(KisImageWSP image, int baseFuzzyness = 0) {
        QString prefix = "initial_with_selection";
        QString prefix2 = findNode(image->root(), "shape") ? "_with_shape" : "";
        return checkLayers(image, prefix + prefix2, baseFuzzyness, false);
    }

    /**
     * Checks the content of image's layers against the set of
     * QImages stored in @p prefix subfolder
     */
    bool checkLayers(KisImageWSP image, const QString &prefix, int baseFuzzyness = 0, bool recursive = true) {
        QVector<QImage> images;
        QVector<QString> names;

        fillNamesImages(image->root(), image->bounds(), images, names, recursive);

        bool valid = true;

        const int stackSize = images.size();
        for(int i = 0; i < stackSize; i++) {
            if(!checkOneQImage(images[i], prefix, names[i], baseFuzzyness)) {
                valid = false;
            }
        }

        return valid;
    }

    /**
     * Checks the content of one image's layer against the QImage
     * stored in @p prefix subfolder
     */
    bool checkOneLayer(KisImageWSP image, KisNodeSP node,  const QString &prefix, int baseFuzzyness = 0) {
        QVector<QImage> images;
        QVector<QString> names;

        fillNamesImages(node, image->bounds(), images, names);

        return checkOneQImage(images.first(), prefix, names.first(), baseFuzzyness);
    }

    // add default bounds param
    bool checkOneDevice(KisPaintDeviceSP device,
                        const QString &prefix,
                        const QString &name,
                        int baseFuzzyness = 0)
    {
        QImage image = device->convertToQImage(0);
        return checkOneQImage(image, prefix, name, baseFuzzyness);
    }

    KisNodeSP findNode(KisNodeSP root, const QString &name) {
        return TestUtil::findNode(root, name);
    }

private:
    bool checkOneQImage(const QImage &image,
                        const QString &prefix,
                        const QString &name,
                        int baseFuzzyness)
    {
        QString realName = prefix + "_" + name + ".png";
        QString expectedName = prefix + "_" + name + "_expected.png";

        bool valid = true;

        QString fullPath = fetchDataFileLazy(m_directoryName + QDir::separator() +
                                             prefix + QDir::separator() + realName);

        if (fullPath.isEmpty()) {
            // Try without the testname subdirectory
            fullPath = fetchDataFileLazy(prefix + QDir::separator() +
                                         realName);
        }

        if (fullPath.isEmpty()) {
            // Try without the prefix subdirectory
            fullPath = fetchDataFileLazy(m_directoryName + QDir::separator() +
                                         realName);
        }

        QImage ref(fullPath);

        QPoint temp;
        int fuzzy = baseFuzzyness;

        {
            QStringList terms = name.split('_');
            if(terms[0] == "root" ||
               terms[0] == "blur1" ||
               terms[0] == "shape") {

                fuzzy++;
            }
        }

        if(ref != image &&
           !TestUtil::compareQImages(temp, ref, image, fuzzy)) {


            dbgKrita << "--- Wrong image:" << realName;
            valid = false;

            image.save(QString(FILES_OUTPUT_DIR) + QDir::separator() + realName);
            ref.save(QString(FILES_OUTPUT_DIR) + QDir::separator() + expectedName);
        }

        return valid;
    }

    void fillNamesImages(KisNodeSP node, const QRect &rc,
                         QVector<QImage> &images,
                         QVector<QString> &names,
                         bool recursive = true) {

        while (node) {
            if(node->paintDevice()) {
                names.append(node->name() + "_paintDevice");
                images.append(node->paintDevice()->
                              convertToQImage(0, rc.x(), rc.y(),
                                              rc.width(), rc.height()));
            }

            if(node->original() && node->original() != node->paintDevice()) {
                names.append(node->name() + "_original");
                images.append(node->original()->
                              convertToQImage(0, rc.x(), rc.y(),
                                              rc.width(), rc.height()));
            }

            if(node->projection() && node->projection() != node->paintDevice()) {
                names.append(node->name() + "_projection");
                images.append(node->projection()->
                              convertToQImage(0, rc.x(), rc.y(),
                                              rc.width(), rc.height()));
            }

            if (recursive) {
                fillNamesImages(node->firstChild(), rc, images, names);
            }
            node = node->nextSibling();
        }
    }

private:
    QString m_directoryName;

};

}

#endif /* __QIMAGE_BASED_TEST_H */