File indexing completed on 2024-12-22 04:10:27

0001 /*
0002  *  SPDX-FileCopyrightText: 2007 Boudewijn Rempt boud @valdyas.org
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_transparency_mask_test.h"
0008 
0009 #include <simpletest.h>
0010 #include "kis_transparency_mask.h"
0011 #include "kis_paint_layer.h"
0012 #include "kis_image.h"
0013 #include "kis_fill_painter.h"
0014 #include <testutil.h>
0015 #include "kis_selection.h"
0016 #include "kis_pixel_selection.h"
0017 #define IMAGE_WIDTH 1000
0018 #define IMAGE_HEIGHT 1000
0019 
0020 KisPaintDeviceSP createDevice()
0021 {
0022     const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
0023     KisPaintDeviceSP dev = new KisPaintDevice(cs);
0024     KisFillPainter gc(dev);
0025     KoColor c(Qt::red, dev->colorSpace());
0026     gc.fillRect(0, 0, 100, 100, c);
0027     c = KoColor(Qt::blue, dev->colorSpace());
0028     gc.fillRect(100, 0, 100, 100, c);
0029     gc.end();
0030 
0031     return dev;
0032 }
0033 
0034 #define initImage(image, layer, device, mask) do {                      \
0035     image = new KisImage(0, IMAGE_WIDTH, IMAGE_HEIGHT, 0, "tests");     \
0036     device = createDevice();                                            \
0037     layer = new KisPaintLayer(image, "paint1", 100, device);                  \
0038     mask = new KisTransparencyMask(image, "tmask");                                   \
0039     image->addNode(layer);                                              \
0040     image->addNode(mask, layer);                                        \
0041     } while(0)
0042 
0043 void KisTransparencyMaskTest::testApply()
0044 {
0045     QPoint errpoint;
0046 
0047     KisImageSP image;
0048     KisPaintLayerSP layer;
0049     KisPaintDeviceSP dev;
0050     KisTransparencyMaskSP mask;
0051 
0052 
0053     QRect applyRect(0, 0, 200, 100);
0054 
0055     // Everything is selected
0056     initImage(image, layer, dev, mask);
0057     mask->initSelection(layer);
0058     mask->apply(dev, applyRect, applyRect, KisNode::N_FILTHY);
0059     QImage qimage = dev->convertToQImage(0, 0, 0, 200, 100);
0060 
0061     if (!TestUtil::compareQImages(errpoint,
0062                                   QImage(QString(FILES_DATA_DIR) + '/' + "transparency_mask_test_2.png"),
0063                                   qimage)) {
0064         QFAIL(QString("Failed to mask out image, first different pixel: %1,%2 ").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
0065     }
0066 
0067     // Invert the mask, so that nothing will be selected, then select a rect
0068     initImage(image, layer, dev, mask);
0069     mask->initSelection(layer);
0070     mask->selection()->pixelSelection()->invert();
0071     mask->apply(dev, applyRect, applyRect, KisNode::N_FILTHY);
0072     qimage = dev->convertToQImage(0, 0, 0, 200, 100);
0073 
0074     if (!TestUtil::compareQImages(errpoint,
0075                                   QImage(QString(FILES_DATA_DIR) + '/' + "transparency_mask_test_1.png"),
0076                                   qimage)) {
0077         QFAIL(QString("Failed to mask in image, first different pixel: %1,%2 ").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
0078     }
0079 
0080     initImage(image, layer, dev, mask);
0081     mask->initSelection(layer);
0082     mask->selection()->pixelSelection()->invert();
0083     mask->select(QRect(50, 0, 100, 100));
0084     mask->apply(dev, applyRect, applyRect, KisNode::N_FILTHY);
0085     qimage = dev->convertToQImage(0, 0, 0, 200, 100);
0086 
0087     if (!TestUtil::compareQImages(errpoint,
0088                                   QImage(QString(FILES_DATA_DIR) + '/' + "transparency_mask_test_3.png"),
0089                                   qimage)) {
0090 
0091         QFAIL(QString("Failed to apply partial mask, first different pixel: %1,%2 ").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
0092     }
0093 
0094 }
0095 
0096 #include "kis_full_refresh_walker.h"
0097 #include "kis_async_merger.h"
0098 
0099 void KisTransparencyMaskTest::testMoveParentLayer()
0100 {
0101     KisImageSP image;
0102     KisPaintLayerSP layer;
0103     KisPaintDeviceSP dev;
0104     KisTransparencyMaskSP mask;
0105 
0106     initImage(image, layer, dev, mask);
0107     mask->initSelection(layer);
0108     mask->selection()->pixelSelection()->invert();
0109     mask->select(QRect(50, 50, 100, 100));
0110 
0111     KisFullRefreshWalker walker(image->bounds());
0112     KisAsyncMerger merger;
0113 
0114     walker.collectRects(layer, image->bounds());
0115     merger.startMerge(walker);
0116 
0117     // image->projection()->convertToQImage(0, 0,0,300,300).save("proj_before.png");
0118 
0119     QRect initialRect(0,0,200,100);
0120     QCOMPARE(layer->exactBounds(), initialRect);
0121     QCOMPARE(image->projection()->exactBounds(), QRect(50,50,100,50));
0122 
0123 
0124     layer->setX(100);
0125     layer->setY(100);
0126 
0127     dbgKrita << "Sel. rect before:" << mask->selection()->selectedExactRect();
0128 
0129     mask->setX(100);
0130     mask->setY(100);
0131 
0132     dbgKrita << "Sel. rect after:" << mask->selection()->selectedExactRect();
0133 
0134     QRect finalRect(100,100,200,100);
0135     QCOMPARE(layer->exactBounds(), finalRect);
0136 
0137     walker.collectRects(layer, initialRect | finalRect);
0138     merger.startMerge(walker);
0139 
0140     // image->projection()->convertToQImage(0, 0,0,300,300).save("proj_after.png");
0141     QCOMPARE(image->projection()->exactBounds(), QRect(150,150,100,50));
0142 }
0143 
0144 void KisTransparencyMaskTest::testMoveMaskItself()
0145 {
0146     KisImageSP image;
0147     KisPaintLayerSP layer;
0148     KisPaintDeviceSP dev;
0149     KisTransparencyMaskSP mask;
0150 
0151     initImage(image, layer, dev, mask);
0152     mask->initSelection(layer);
0153     mask->selection()->pixelSelection()->invert();
0154     mask->select(QRect(50, 50, 100, 100));
0155 
0156     KisFullRefreshWalker walker(image->bounds());
0157     KisAsyncMerger merger;
0158 
0159     walker.collectRects(layer, image->bounds());
0160     merger.startMerge(walker);
0161 
0162     // image->projection()->convertToQImage(0, 0,0,300,300).save("proj_before.png");
0163 
0164     QRect initialRect(0,0,200,100);
0165     QCOMPARE(layer->exactBounds(), initialRect);
0166     QCOMPARE(image->projection()->exactBounds(), QRect(50,50,100,50));
0167 
0168 
0169     //layer->setX(100);
0170     //layer->setY(100);
0171 
0172     dbgKrita << "Sel. rect before:" << mask->selection()->selectedExactRect();
0173 
0174     mask->setX(50);
0175     mask->setY(25);
0176 
0177     dbgKrita << "Sel. rect after:" << mask->selection()->selectedExactRect();
0178 
0179 
0180     QCOMPARE(mask->selection()->selectedExactRect(), QRect(100, 75, 100, 100));
0181     QCOMPARE(layer->paintDevice()->exactBounds(), initialRect);
0182     QCOMPARE(layer->projection()->exactBounds(), QRect(50, 50, 100, 50));
0183 
0184     dbgKrita << "";
0185 
0186     QRect updateRect(0,0,300,300);
0187 
0188     walker.collectRects(mask, updateRect);
0189     merger.startMerge(walker);
0190 
0191     // image->projection()->convertToQImage(0, 0,0,300,300).save("proj_after.png");
0192 
0193     QCOMPARE(layer->paintDevice()->exactBounds(), initialRect);
0194     QCOMPARE(layer->projection()->exactBounds(), QRect(100, 75, 100, 25));
0195 }
0196 
0197 SIMPLE_TEST_MAIN(KisTransparencyMaskTest)