File indexing completed on 2024-12-22 04:10:29
0001 /* 0002 * SPDX-FileCopyrightText: 2017 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "KisWatershedWorkerTest.h" 0008 0009 #include <simpletest.h> 0010 0011 #include "kis_paint_device.h" 0012 #include "kis_painter.h" 0013 0014 #include "kis_paint_device_debug_utils.h" 0015 0016 #include "kis_gaussian_kernel.h" 0017 #include "krita_utils.h" 0018 0019 #include "lazybrush/kis_lazy_fill_tools.h" 0020 #include "testutil.h" 0021 #include "testing_timed_default_bounds.h" 0022 0023 0024 #include <lazybrush/KisWatershedWorker.h> 0025 0026 inline KisPaintDeviceSP loadTestImage(const QString &name, bool convertToAlpha) 0027 { 0028 QImage image(TestUtil::fetchDataFileLazy(name)); 0029 KisPaintDeviceSP dev = new KisPaintDevice(KoColorSpaceRegistry::instance()->rgb8()); 0030 dev->convertFromQImage(image, 0); 0031 if (convertToAlpha) { 0032 dev = KisPainter::convertToAlphaAsAlpha(dev); 0033 } 0034 0035 return dev; 0036 } 0037 0038 void KisWatershedWorkerTest::testWorker() 0039 { 0040 KisPaintDeviceSP mainDev = loadTestImage("fill1_main.png", false); 0041 KisPaintDeviceSP aLabelDev = loadTestImage("fill1_a_extra.png", true); 0042 KisPaintDeviceSP bLabelDev = loadTestImage("fill1_b.png", true); 0043 KisPaintDeviceSP resultColoring = new KisPaintDevice(mainDev->colorSpace()); 0044 0045 KisPaintDeviceSP filteredMainDev = KisPainter::convertToAlphaAsGray(mainDev); 0046 const QRect filterRect = filteredMainDev->exactBounds(); 0047 0048 mainDev->setDefaultBounds(new TestUtil::TestingTimedDefaultBounds(filterRect)); 0049 aLabelDev->setDefaultBounds(mainDev->defaultBounds()); 0050 bLabelDev->setDefaultBounds(mainDev->defaultBounds()); 0051 resultColoring->setDefaultBounds(mainDev->defaultBounds()); 0052 filteredMainDev->setDefaultBounds(mainDev->defaultBounds()); 0053 0054 KisGaussianKernel::applyLoG(filteredMainDev, 0055 filterRect, 0056 2, 0057 -1.0, 0058 QBitArray(), 0); 0059 0060 KisLazyFillTools::normalizeAlpha8Device(filteredMainDev, filterRect); 0061 0062 KIS_DUMP_DEVICE_2(filteredMainDev, filterRect, "main", "dd"); 0063 KIS_DUMP_DEVICE_2(aLabelDev, filterRect, "alabel", "dd"); 0064 0065 KisWatershedWorker worker(filteredMainDev, resultColoring, filterRect); 0066 worker.addKeyStroke(aLabelDev, KoColor(Qt::red, mainDev->colorSpace())); 0067 worker.addKeyStroke(bLabelDev, KoColor(Qt::blue, mainDev->colorSpace())); 0068 worker.run(0.7); 0069 0070 } 0071 0072 void KisWatershedWorkerTest::testWorkerSmall() 0073 { 0074 KisPaintDeviceSP mainDev = loadTestImage("fill5_main.png", false); 0075 KisPaintDeviceSP aLabelDev = loadTestImage("fill5_a.png", true); 0076 KisPaintDeviceSP bLabelDev = loadTestImage("fill5_b.png", true); 0077 KisPaintDeviceSP resultColoring = new KisPaintDevice(mainDev->colorSpace()); 0078 0079 KisPaintDeviceSP filteredMainDev = KisPainter::convertToAlphaAsGray(mainDev); 0080 const QRect filterRect = filteredMainDev->exactBounds(); 0081 // KisGaussianKernel::applyLoG(filteredMainDev, 0082 // filterRect, 0083 // 2, 0084 // QBitArray(), 0); 0085 0086 KisLazyFillTools::normalizeAndInvertAlpha8Device(filteredMainDev, filterRect); 0087 0088 KIS_DUMP_DEVICE_2(filteredMainDev, filterRect, "main", "dd"); 0089 KIS_DUMP_DEVICE_2(aLabelDev, filterRect, "alabel", "dd"); 0090 KIS_DUMP_DEVICE_2(bLabelDev, filterRect, "blabel", "dd"); 0091 0092 KisWatershedWorker worker(filteredMainDev, resultColoring, filterRect); 0093 worker.addKeyStroke(aLabelDev, KoColor(Qt::red, mainDev->colorSpace())); 0094 worker.addKeyStroke(bLabelDev, KoColor(Qt::blue, mainDev->colorSpace())); 0095 worker.run(); 0096 0097 QCOMPARE(worker.testingGroupPositiveEdge(1, 0), 35); 0098 QCOMPARE(worker.testingGroupNegativeEdge(1, 0), 0); 0099 QCOMPARE(worker.testingGroupForeignEdge(1, 0), 5); 0100 0101 QCOMPARE(worker.testingGroupPositiveEdge(1, 255), 3); 0102 QCOMPARE(worker.testingGroupNegativeEdge(1, 255), 15); 0103 QCOMPARE(worker.testingGroupForeignEdge(1, 255), 8); 0104 0105 QCOMPARE(worker.testingGroupPositiveEdge(2, 0), 22); 0106 QCOMPARE(worker.testingGroupNegativeEdge(2, 0), 0); 0107 QCOMPARE(worker.testingGroupForeignEdge(2, 0), 6); 0108 0109 QCOMPARE(worker.testingGroupPositiveEdge(2, 255), 1); 0110 QCOMPARE(worker.testingGroupNegativeEdge(2, 255), 6); 0111 QCOMPARE(worker.testingGroupForeignEdge(2, 255), 7); 0112 } 0113 0114 void KisWatershedWorkerTest::testWorkerSmallWithAllies() 0115 { 0116 KisPaintDeviceSP mainDev = loadTestImage("fill5_main.png", false); 0117 KisPaintDeviceSP aLabelDev = loadTestImage("fill5_a_extra.png", true); 0118 KisPaintDeviceSP bLabelDev = loadTestImage("fill5_b.png", true); 0119 KisPaintDeviceSP resultColoring = new KisPaintDevice(mainDev->colorSpace()); 0120 0121 KisPaintDeviceSP filteredMainDev = KisPainter::convertToAlphaAsGray(mainDev); 0122 const QRect filterRect = filteredMainDev->exactBounds(); 0123 // KisGaussianKernel::applyLoG(filteredMainDev, 0124 // filterRect, 0125 // 2, 0126 // QBitArray(), 0); 0127 0128 KisLazyFillTools::normalizeAndInvertAlpha8Device(filteredMainDev, filterRect); 0129 0130 KIS_DUMP_DEVICE_2(filteredMainDev, filterRect, "main", "dd"); 0131 KIS_DUMP_DEVICE_2(aLabelDev, filterRect, "alabel", "dd"); 0132 KIS_DUMP_DEVICE_2(bLabelDev, filterRect, "blabel", "dd"); 0133 0134 KisWatershedWorker worker(filteredMainDev, resultColoring, filterRect); 0135 worker.addKeyStroke(aLabelDev, KoColor(Qt::red, mainDev->colorSpace())); 0136 worker.addKeyStroke(bLabelDev, KoColor(Qt::blue, mainDev->colorSpace())); 0137 worker.run(); 0138 0139 QCOMPARE(worker.testingGroupPositiveEdge(1, 0), 29); 0140 QCOMPARE(worker.testingGroupNegativeEdge(1, 0), 0); 0141 QCOMPARE(worker.testingGroupForeignEdge(1, 0), 0); 0142 QCOMPARE(worker.testingGroupAllyEdge(1, 0), 1); 0143 0144 QCOMPARE(worker.testingGroupPositiveEdge(1, 255), 2); 0145 QCOMPARE(worker.testingGroupNegativeEdge(1, 255), 11); 0146 QCOMPARE(worker.testingGroupForeignEdge(1, 255), 4); 0147 QCOMPARE(worker.testingGroupAllyEdge(1, 255), 5); 0148 QCOMPARE(worker.testingGroupConflicts(1, 255, 3), 4); 0149 0150 QCOMPARE(worker.testingGroupPositiveEdge(2, 0), 16); 0151 QCOMPARE(worker.testingGroupNegativeEdge(2, 0), 0); 0152 QCOMPARE(worker.testingGroupForeignEdge(2, 0), 5); 0153 QCOMPARE(worker.testingGroupAllyEdge(2, 0), 1); 0154 QCOMPARE(worker.testingGroupConflicts(2, 0, 3), 5); 0155 0156 QCOMPARE(worker.testingGroupPositiveEdge(2, 255), 1); 0157 QCOMPARE(worker.testingGroupNegativeEdge(2, 255), 7); 0158 QCOMPARE(worker.testingGroupForeignEdge(2, 255), 1); 0159 QCOMPARE(worker.testingGroupAllyEdge(2, 255), 5); 0160 QCOMPARE(worker.testingGroupConflicts(2, 255, 3), 1); 0161 0162 QCOMPARE(worker.testingGroupPositiveEdge(3, 0), 13); 0163 QCOMPARE(worker.testingGroupNegativeEdge(3, 0), 0); 0164 QCOMPARE(worker.testingGroupForeignEdge(3, 0), 5); 0165 QCOMPARE(worker.testingGroupAllyEdge(3, 0), 0); 0166 QCOMPARE(worker.testingGroupConflicts(3, 0, 2), 5); 0167 0168 QCOMPARE(worker.testingGroupPositiveEdge(3, 255), 1); 0169 QCOMPARE(worker.testingGroupNegativeEdge(3, 255), 4); 0170 QCOMPARE(worker.testingGroupForeignEdge(3, 255), 5); 0171 QCOMPARE(worker.testingGroupAllyEdge(3, 255), 0); 0172 QCOMPARE(worker.testingGroupConflicts(3, 255, 1), 4); 0173 QCOMPARE(worker.testingGroupConflicts(3, 255, 2), 1); 0174 0175 worker.testingTryRemoveGroup(2, 0); 0176 0177 // check the group was really removed! 0178 QCOMPARE(worker.testingGroupPositiveEdge(2, 0), 0); 0179 QCOMPARE(worker.testingGroupNegativeEdge(2, 0), 0); 0180 QCOMPARE(worker.testingGroupForeignEdge(2, 0), 0); 0181 QCOMPARE(worker.testingGroupAllyEdge(2, 0), 0); 0182 QCOMPARE(worker.testingGroupConflicts(2, 0, 3), 0); 0183 } 0184 0185 SIMPLE_TEST_MAIN(KisWatershedWorkerTest)