File indexing completed on 2024-11-10 04:00:28

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Lukáš Tvrdý <lukast.dev@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include <simpletest.h>
0008 
0009 #include <kundo2command.h>
0010 #include "kis_benchmark_values.h"
0011 
0012 #include "kis_random_accessor_ng.h"
0013 
0014 #include <KoColorSpace.h>
0015 #include <KoColorSpaceRegistry.h>
0016 #include <KoColor.h>
0017 #include <KoCompositeOpRegistry.h>
0018 
0019 #include <kis_image.h>
0020 
0021 #include "kis_floodfill_benchmark.h"
0022 
0023 #include <kis_fill_painter.h>
0024 
0025 void KisFloodFillBenchmark::initTestCase()
0026 {
0027     m_colorSpace = KoColorSpaceRegistry::instance()->rgb8();
0028     m_deviceStandardFloodFill = new KisPaintDevice(m_colorSpace);
0029     m_color = KoColor(m_colorSpace);
0030 
0031     QColor qcolor(Qt::red);
0032     srand(31524744);
0033 
0034     int tilew = 38;
0035     int tileh = 56;
0036 
0037     m_color.fromQColor(QColor(0,0,0,0)); // default pixel
0038     m_deviceStandardFloodFill->fill( 0,0,GMP_IMAGE_WIDTH, GMP_IMAGE_HEIGHT,m_color.data() );
0039 
0040     // fill the image with red ellipses (like some random dabs)
0041     m_color.fromQColor(Qt::red);
0042     KisPainter painter(m_deviceStandardFloodFill);
0043     painter.setFillStyle(KisPainter::FillStyleForegroundColor);
0044     painter.setPaintColor(m_color);
0045 
0046     int x = 0;
0047     int y = 0;
0048     for (int i = 0; i < 100;i++){
0049         x = rand() % GMP_IMAGE_WIDTH;
0050         y = rand() % GMP_IMAGE_HEIGHT;
0051         // plus 10 so that we don't fill the ellipse
0052         painter.paintEllipse(x+ 10, y+ 10, tilew, tileh);
0053     }
0054 
0055     // copy to other tests
0056     m_deviceWithoutSelectionAsBoundary = new KisPaintDevice(m_colorSpace);
0057     m_deviceWithSelectionAsBoundary = new KisPaintDevice(m_colorSpace);
0058 
0059 
0060     KisPainter::copyAreaOptimized(QPoint(), m_deviceStandardFloodFill,
0061                                   m_deviceWithoutSelectionAsBoundary, m_deviceWithoutSelectionAsBoundary->exactBounds());
0062     KisPainter::copyAreaOptimized(QPoint(), m_deviceStandardFloodFill,
0063                                   m_deviceWithSelectionAsBoundary, m_deviceWithSelectionAsBoundary->exactBounds());
0064 
0065     //m_deviceWithoutSelectionAsBoundary = m_deviceStandardFloodFill->
0066 
0067     const KoColorSpace* alphacs = KoColorSpaceRegistry::instance()->alpha8();
0068     KoColor defaultSelected = KoColor(alphacs);
0069     defaultSelected.fromQColor(QColor(255, 255, 255));
0070     m_existingSelection = new KisPaintDevice(alphacs);
0071     m_existingSelection->fill(0, 0, GMP_IMAGE_WIDTH, GMP_IMAGE_HEIGHT, defaultSelected.data());
0072 
0073 }
0074 
0075 void KisFloodFillBenchmark::benchmarkFlood()
0076 {
0077     KoColor fg(m_colorSpace);
0078     KoColor bg(m_colorSpace);
0079     fg.fromQColor(Qt::blue);
0080     bg.fromQColor(Qt::black);
0081 
0082     QBENCHMARK
0083     {
0084         KisFillPainter fillPainter(m_deviceStandardFloodFill);
0085         //setupPainter(&fillPainter);
0086         fillPainter.setPaintColor( fg );
0087         fillPainter.setBackgroundColor( bg );
0088 
0089         fillPainter.beginTransaction(kundo2_noi18n("Flood Fill"));
0090 
0091         //fillPainter.setProgress(updater->startSubtask());
0092         fillPainter.setOpacity(OPACITY_OPAQUE_U8);
0093         // default
0094         fillPainter.setFillThreshold(15);
0095         fillPainter.setCompositeOpId(COMPOSITE_OVER);
0096         fillPainter.setCareForSelection(true);
0097         fillPainter.setWidth(GMP_IMAGE_WIDTH);
0098         fillPainter.setHeight(GMP_IMAGE_HEIGHT);
0099 
0100         // fill twice
0101         fillPainter.fillColor(1, 1, m_deviceStandardFloodFill);
0102 
0103         fillPainter.deleteTransaction();
0104     }
0105 
0106     // uncomment this to see the output
0107     //QImage out = m_device->convertToQImage(m_colorSpace->profile(),0,0,GMP_IMAGE_WIDTH,GMP_IMAGE_HEIGHT);
0108     //out.save("fill_output.png");
0109 }
0110 
0111 void KisFloodFillBenchmark::benchmarkFloodWithoutSelectionAsBoundary()
0112 {
0113     KoColor fg(m_colorSpace);
0114     KoColor bg(m_colorSpace);
0115     fg.fromQColor(Qt::blue);
0116     bg.fromQColor(Qt::black);
0117 
0118     QBENCHMARK
0119     {
0120         KisFillPainter fillPainter(m_deviceWithoutSelectionAsBoundary);
0121         fillPainter.setPaintColor( fg );
0122         fillPainter.setBackgroundColor( bg );
0123 
0124         fillPainter.beginTransaction(kundo2_noi18n("Flood Fill"));
0125 
0126         fillPainter.setOpacity(OPACITY_OPAQUE_U8);
0127         // default
0128         fillPainter.setFillThreshold(15);
0129         fillPainter.setCompositeOpId(COMPOSITE_OVER);
0130         fillPainter.setCareForSelection(true);
0131         fillPainter.setWidth(GMP_IMAGE_WIDTH);
0132         fillPainter.setHeight(GMP_IMAGE_HEIGHT);
0133         fillPainter.setUseSelectionAsBoundary(false);
0134         fillPainter.setUseCompositing(true);
0135 
0136         fillPainter.createFloodSelection(1, 1, m_deviceWithoutSelectionAsBoundary, m_existingSelection);
0137 
0138         fillPainter.deleteTransaction();
0139     }
0140 }
0141 
0142 void KisFloodFillBenchmark::benchmarkFloodWithSelectionAsBoundary()
0143 {
0144     KoColor fg(m_colorSpace);
0145     KoColor bg(m_colorSpace);
0146     fg.fromQColor(Qt::blue);
0147     bg.fromQColor(Qt::black);
0148 
0149     QBENCHMARK
0150     {
0151         KisFillPainter fillPainter(m_deviceWithSelectionAsBoundary);
0152         fillPainter.setPaintColor( fg );
0153         fillPainter.setBackgroundColor( bg );
0154 
0155         fillPainter.beginTransaction(kundo2_noi18n("Flood Fill"));
0156 
0157         fillPainter.setOpacity(OPACITY_OPAQUE_U8);
0158         // default
0159         fillPainter.setFillThreshold(15);
0160         fillPainter.setCompositeOpId(COMPOSITE_OVER);
0161         fillPainter.setCareForSelection(true);
0162         fillPainter.setWidth(GMP_IMAGE_WIDTH);
0163         fillPainter.setHeight(GMP_IMAGE_HEIGHT);
0164         fillPainter.setUseSelectionAsBoundary(true);
0165         fillPainter.setUseCompositing(true);
0166 
0167         fillPainter.createFloodSelection(1, 1, m_deviceWithSelectionAsBoundary, m_existingSelection);
0168 
0169         fillPainter.deleteTransaction();
0170     }
0171 }
0172 
0173 
0174 void KisFloodFillBenchmark::cleanupTestCase()
0175 {
0176 
0177 }
0178 
0179 SIMPLE_TEST_MAIN(KisFloodFillBenchmark)