kis_cage_transform_worker_test.cpp 7.97 KB
Newer Older
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
/*
 *  Copyright (c) 2014 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.
 */

#include "kis_cage_transform_worker_test.h"

#include <qtest_kde.h>

#include <KoUpdater.h>

#include "testutil.h"

#include <kis_cage_transform_worker.h>
#include <algorithm>

30
void testCage(bool clockwise, bool unityTransform, bool benchmarkPrepareOnly = false, int pixelPrecision = 8, bool testQImage = false)
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
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(TestUtil::fetchDataFileLazy("test_cage_transform.png"));

    KisPaintDeviceSP dev = new KisPaintDevice(cs);
    dev->convertFromQImage(image, 0);

    QVector<QPointF> origPoints;
    QVector<QPointF> transfPoints;

    QRectF bounds(dev->exactBounds());

    origPoints << bounds.topLeft();
    origPoints << 0.5 * (bounds.topLeft() + bounds.topRight());
    origPoints << 0.5 * (bounds.topLeft() + bounds.bottomRight());
    origPoints << 0.5 * (bounds.topRight() + bounds.bottomRight());
    origPoints << bounds.bottomRight();
    origPoints << bounds.bottomLeft();

    if (!clockwise) {
        std::reverse(origPoints.begin(), origPoints.end());
    }

    if (unityTransform) {
        transfPoints = origPoints;
    } else {
        transfPoints << bounds.topLeft();
        transfPoints << 0.5 * (bounds.topLeft() + bounds.topRight());
        transfPoints << 0.5 * (bounds.bottomLeft() + bounds.bottomRight());
        transfPoints << 0.5 * (bounds.bottomLeft() + bounds.bottomRight()) +
            (bounds.bottomLeft() - bounds.topLeft());
        transfPoints << bounds.bottomLeft() +
            (bounds.bottomLeft() - bounds.topLeft());
        transfPoints << bounds.bottomLeft();

        if (!clockwise) {
            std::reverse(transfPoints.begin(), transfPoints.end());
        }
    }

    KisCageTransformWorker worker(dev,
                                  origPoints,
                                  updater,
                                  pixelPrecision);

80 81 82
    QImage result;
    QPointF srcQImageOffset(0, 0);
    QPointF dstQImageOffset;
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
    QBENCHMARK_ONCE {
        if (!testQImage) {
            worker.prepareTransform();
            if (!benchmarkPrepareOnly) {
                worker.setTransformedCage(transfPoints);
                worker.run();

            }
        } else {
            QImage srcImage(image);
            image = QImage(image.size(), QImage::Format_ARGB32);
            QPainter gc(&image);
            gc.drawImage(QPoint(), srcImage);

            image.convertToFormat(QImage::Format_ARGB32);

            KisCageTransformWorker qimageWorker(image,
                                                srcQImageOffset,
                                                origPoints,
                                                updater,
                                                pixelPrecision);
            qimageWorker.prepareTransform();
            qimageWorker.setTransformedCage(transfPoints);
            result = qimageWorker.runOnQImage(&dstQImageOffset);
108 109 110
        }
    }

111 112 113
    QString testName = QString("%1_%2")
        .arg(clockwise ? "clk" : "cclk")
        .arg(unityTransform ? "unity" : "normal");
114

115 116 117
    if (testQImage) {
        QVERIFY(TestUtil::checkQImage(result, "cage_transform_test", "cage_qimage", testName));
    } else if (!benchmarkPrepareOnly && pixelPrecision == 8) {
118

119
        result = dev->convertToQImage(0);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        QVERIFY(TestUtil::checkQImage(result, "cage_transform_test", "cage", testName));
    }
}

void KisCageTransformWorkerTest::testCageClockwise()
{
    testCage(true, false);
}

void KisCageTransformWorkerTest::testCageClockwisePrepareOnly()
{
    testCage(true, false, true);
}

void KisCageTransformWorkerTest::testCageClockwisePixePrecision4()
{
    testCage(true, false, false, 4);
}

139 140 141 142 143
void KisCageTransformWorkerTest::testCageClockwisePixePrecision8QImage()
{
    testCage(true, false, false, 8, true);
}

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
void KisCageTransformWorkerTest::testCageCounterclockwise()
{
    testCage(false, false);
}

void KisCageTransformWorkerTest::testCageClockwiseUnity()
{
    testCage(true, true);
}

void KisCageTransformWorkerTest::testCageCounterclockwiseUnity()
{
    testCage(false, true);
}

#include "kis_green_coordinates_math.h"

void KisCageTransformWorkerTest::testUnityGreenCoordinates()
{
    QVector<QPointF> origPoints;
    QVector<QPointF> transfPoints;

    QRectF bounds(0,0,300,300);

    origPoints << bounds.topLeft();
    origPoints << 0.5 * (bounds.topLeft() + bounds.topRight());
    origPoints << 0.5 * (bounds.topLeft() + bounds.bottomRight());
    origPoints << 0.5 * (bounds.topRight() + bounds.bottomRight());
    origPoints << bounds.bottomRight();
    origPoints << bounds.bottomLeft();

    transfPoints = origPoints;

    QVector<QPointF> points;
    points << QPointF(10,10);
    points << QPointF(140,10);
    points << QPointF(140,140);
    points << QPointF(10,140);

    points << QPointF(10,160);
    points << QPointF(140,160);
    points << QPointF(140,290);
    points << QPointF(10,290);

    points << QPointF(160,160);
    points << QPointF(290,160);
    points << QPointF(290,290);
    points << QPointF(160,290);

    KisGreenCoordinatesMath cage;

    cage.precalculateGreenCoordinates(origPoints, points);
    cage.generateTransformedCageNormals(transfPoints);

    QVector<QPointF> newPoints;

    for (int i = 0; i < points.size(); i++) {
        newPoints << cage.transformedPoint(i, transfPoints);
        QCOMPARE(points[i], newPoints.last());
    }
}

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
#include "kis_algebra_2d.h"

void KisCageTransformWorkerTest::testTransformAsBase()
{
    QPointF t(1.0, 0.0);
    QPointF b1(1.0, 0.0);
    QPointF b2(2.0, 0.0);
    QPointF result;


    t = QPointF(1.0, 0.0);
    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(2.0, 0.0);
    result = KisAlgebra2D::transformAsBase(t, b1, b2);
    QCOMPARE(result, QPointF(2.0, 0.0));

    t = QPointF(1.0, 0.0);
    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(0.0, 1.0);
    result = KisAlgebra2D::transformAsBase(t, b1, b2);
    QCOMPARE(result, QPointF(0.0, 1.0));

    t = QPointF(1.0, 0.0);
    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(0.0, 2.0);
    result = KisAlgebra2D::transformAsBase(t, b1, b2);
    QCOMPARE(result, QPointF(0.0, 2.0));

    t = QPointF(0.0, 1.0);
    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(2.0, 0.0);
    result = KisAlgebra2D::transformAsBase(t, b1, b2);
    QCOMPARE(result, QPointF(0.0, 2.0));

    t = QPointF(0.0, 1.0);
    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(0.0, 1.0);
    result = KisAlgebra2D::transformAsBase(t, b1, b2);
    QCOMPARE(result, QPointF(-1.0, 0.0));

    t = QPointF(0.0, 1.0);
    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(0.0, 2.0);
    result = KisAlgebra2D::transformAsBase(t, b1, b2);
    QCOMPARE(result, QPointF(-2.0, 0.0));
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
void KisCageTransformWorkerTest::testAngleBetweenVectors()
{
    QPointF b1(1.0, 0.0);
    QPointF b2(2.0, 0.0);
    qreal result;

    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(0.0, 1.0);
    result = KisAlgebra2D::angleBetweenVectors(b1, b2);
    QCOMPARE(result, M_PI_2);

    b1 = QPointF(1.0, 0.0);
    b2 = QPointF(std::sqrt(0.5), std::sqrt(0.5));
    result = KisAlgebra2D::angleBetweenVectors(b1, b2);
    QCOMPARE(result, M_PI / 4);

    QTransform t;
    t.rotateRadians(M_PI / 4);
    QCOMPARE(t.map(b1), b2);
}
273 274

QTEST_KDEMAIN(KisCageTransformWorkerTest, GUI)