kis_transform_worker_test.cpp 34.3 KB
Newer Older
Boudewijn Rempt's avatar
Boudewijn Rempt committed
1
/*
2
 *  Copyright (c) 2007 Boudewijn Rempt boud@valdyas.org
Boudewijn Rempt's avatar
Boudewijn Rempt committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  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.
 */

19
#include "kis_transform_worker_test.h"
20

21
#include <QTest>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
22
#include <KoProgressUpdater.h>
23
#include <KoColor.h>
Boudewijn Rempt's avatar
Boudewijn Rempt committed
24 25
#include <KoColorSpace.h>
#include <KoColorSpaceRegistry.h>
26
#include <QTransform>
27
#include <QVector>
28

Boudewijn Rempt's avatar
Boudewijn Rempt committed
29 30
#include "kis_types.h"
#include "kis_image.h"
31
#include "kis_filter_strategy.h"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
32
#include "kis_paint_device.h"
33
#include "kis_transform_worker.h"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
34
#include "testutil.h"
Boudewijn Rempt's avatar
Boudewijn Rempt committed
35
#include "kis_transaction.h"
36
#include "kis_random_accessor_ng.h"
37

38 39
void KisTransformWorkerTest::testCreation()
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
40 41
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
42
    KoUpdaterPtr updater = pu.startSubtask();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
43 44
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    KisFilterStrategy * filter = new KisBoxFilterStrategy();
45
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
46 47
    KisTransformWorker tw(dev, 1.0, 1.0,
                          1.0, 1.0,
48 49
                          0.0, 0.0,
                          1.5,
50
                          0, 0, updater, filter);
51
}
Boudewijn Rempt's avatar
Boudewijn Rempt committed
52

53
void testMirror(const QRect &imageRect, const QRect &mirrorRect, Qt::Orientation orientation)
Boudewijn Rempt's avatar
Boudewijn Rempt committed
54
{
Boudewijn Rempt's avatar
Boudewijn Rempt committed
55
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
56 57 58 59 60 61 62 63 64 65 66 67 68
    KisPaintDeviceSP dev = new KisPaintDevice(cs);

    qreal axis = QRectF(mirrorRect).center().x();

    KisRandomAccessorSP it = dev->createRandomAccessorNG(imageRect.x(), imageRect.y());

    int i = 0;
    for (int y = imageRect.y(); y < imageRect.y() + imageRect.height(); y++) {
        for (int x = imageRect.x(); x < imageRect.x() + imageRect.width(); x++) {
            it->moveTo(x, y);

            *reinterpret_cast<quint32*>(it->rawData()) = 0xFFFFFFFF - i++;
        }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
69
    }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
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
    QCOMPARE(dev->exactBounds(), imageRect);


    QImage srcImage = dev->convertToQImage(0, mirrorRect.x(), mirrorRect.y(), mirrorRect.width(), mirrorRect.height());
    QImage mirroredImage = srcImage.mirrored(orientation == Qt::Horizontal, orientation == Qt::Vertical);
    QImage result;

    //srcImage.save("input.png");
    //mirroredImage.save("mirror_expected.png");

    QBENCHMARK_ONCE {
        KisTransformWorker::mirror(dev, axis, orientation);
    }
    result = dev->convertToQImage(0, mirrorRect.x(), mirrorRect.y(), mirrorRect.width(), mirrorRect.height());
    QCOMPARE(result, mirroredImage);

    //result.save("mirror1.png");

    KisTransformWorker::mirror(dev, axis, orientation);
    result = dev->convertToQImage(0, mirrorRect.x(), mirrorRect.y(), mirrorRect.width(), mirrorRect.height());
    QCOMPARE(result, srcImage);

    //result.save("mirror2.png");

    KisTransformWorker::mirror(dev, axis, orientation);
    result = dev->convertToQImage(0, mirrorRect.x(), mirrorRect.y(), mirrorRect.width(), mirrorRect.height());
    QCOMPARE(result, mirroredImage);

    //result.save("mirror3.png");
Boudewijn Rempt's avatar
Boudewijn Rempt committed
100
}
Boudewijn Rempt's avatar
Boudewijn Rempt committed
101

102
void KisTransformWorkerTest::testMirrorX_Even()
Boudewijn Rempt's avatar
Boudewijn Rempt committed
103
{
104 105
    testMirror(QRect(10,10,30,30), QRect(1,1,70,70), Qt::Horizontal);
}
Boudewijn Rempt's avatar
Boudewijn Rempt committed
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120
void KisTransformWorkerTest::testMirrorX_Odd()
{
    testMirror(QRect(10,10,30,30), QRect(1,1,71,71), Qt::Horizontal);
}

void KisTransformWorkerTest::testMirrorY_Even()
{
    testMirror(QRect(10,10,30,30), QRect(1,1,70,70), Qt::Vertical);
}

void KisTransformWorkerTest::testMirrorY_Odd()
{
    testMirror(QRect(10,10,30,30), QRect(1,1,71,71), Qt::Vertical);
}
Boudewijn Rempt's avatar
Boudewijn Rempt committed
121

122 123 124 125 126 127 128 129
void KisTransformWorkerTest::benchmarkMirrorX()
{
    testMirror(QRect(10,10,4000,4000), QRect(1,1,7000,7000), Qt::Horizontal);
}

void KisTransformWorkerTest::benchmarkMirrorY()
{
    testMirror(QRect(10,10,4000,4000), QRect(1,1,7000,7000), Qt::Vertical);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
130
}
Boudewijn Rempt's avatar
Boudewijn Rempt committed
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
void KisTransformWorkerTest::testOffset()
{
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QString imageName("mirror_source.png");
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + imageName);
    QPoint bottomRight(image.width(), image.height());
    KisPaintDeviceSP dev2 = new KisPaintDevice(cs);

    QVector<QPoint> offsetPoints;
    offsetPoints.append(QPoint(image.width() / 2, image.height() / 2));     // offset to 1/2 of image
    offsetPoints.append(QPoint(image.width() / 4, image.height() / 4)); // offset to 1/4 of image
    offsetPoints.append(QPoint(image.width() - image.width() / 4, image.height() - image.height() / 4)); // offset to 3/4 of image
    offsetPoints.append(QPoint(image.width() / 4, 0));      // offset with y == 0
    offsetPoints.append(QPoint(0, image.height() / 4));     // offset with x == 0

    QPoint errpoint;
    QPoint backOffsetPoint;
    QImage result;
    int test = 0;
151
    QPoint origin(0,0);
152
    Q_FOREACH (QPoint offsetPoint, offsetPoints)
153 154
    {
        dev2->convertFromQImage(image, 0);
155
        KisTransformWorker::offset(dev2, offsetPoint, QRect(origin, image.size()) );
156
        backOffsetPoint = bottomRight - offsetPoint;
157
        KisTransformWorker::offset(dev2, backOffsetPoint , QRect(origin, image.size()) );
158 159 160 161 162 163 164 165 166 167 168 169
        result = dev2->convertToQImage(0, 0, 0, image.width(), image.height());
        if (!TestUtil::compareQImages(errpoint, image, result))
        {
            // They are the same, but should be mirrored
            image.save(QString("offset_test_%1_source.png").arg(test));
            result.save(QString("offset_test_%1_result.png").arg(test));
            QFAIL(QString("Failed to offset the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
        }
    }
}


Boudewijn Rempt's avatar
Boudewijn Rempt committed
170 171 172 173 174
void KisTransformWorkerTest::testMirrorTransactionX()
{
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev2 = new KisPaintDevice(cs);
175
    dev2->convertFromQImage(image, 0);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
176

177
    KisTransaction t(kundo2_noi18n("mirror"), dev2);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
178 179
    KisTransformWorker::mirrorX(dev2);
    t.end();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
180

Boudewijn Rempt's avatar
Boudewijn Rempt committed
181
    QImage result = dev2->convertToQImage(0, 0, 0, image.width(), image.height());
Boudewijn Rempt's avatar
Boudewijn Rempt committed
182

Boudewijn Rempt's avatar
Boudewijn Rempt committed
183
    image = image.mirrored(true, false);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
184

Boudewijn Rempt's avatar
Boudewijn Rempt committed
185 186 187 188 189
    QPoint errpoint;
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        // They are the same, but should be mirrored
        image.save("mirror_test_3_source.png");
        result.save("mirror_test_3_result.png");
190
        QFAIL(QString("Failed to mirror the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
Boudewijn Rempt's avatar
Boudewijn Rempt committed
191 192 193

    }
}
Boudewijn Rempt's avatar
Boudewijn Rempt committed
194
void KisTransformWorkerTest::testMirrorTransactionY()
Boudewijn Rempt's avatar
Boudewijn Rempt committed
195
{
196
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
197 198
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev2 = new KisPaintDevice(cs);
199
    dev2->convertFromQImage(image, 0);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
200

201
    KisTransaction t(kundo2_noi18n("mirror"), dev2);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
202 203
    KisTransformWorker::mirrorY(dev2);
    t.end();
Boudewijn Rempt's avatar
Boudewijn Rempt committed
204

Boudewijn Rempt's avatar
Boudewijn Rempt committed
205
    QImage result = dev2->convertToQImage(0, 0, 0, image.width(), image.height());
Boudewijn Rempt's avatar
Boudewijn Rempt committed
206

Boudewijn Rempt's avatar
Boudewijn Rempt committed
207
    image = image.mirrored(false, true);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
208

Boudewijn Rempt's avatar
Boudewijn Rempt committed
209 210 211 212 213
    QPoint errpoint;
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        // They are the same, but should be mirrored
        image.save("mirror_test_4_source.png");
        result.save("mirror_test_4_result.png");
214
        QFAIL(QString("Failed to mirror the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
Boudewijn Rempt's avatar
Boudewijn Rempt committed
215
    }
216 217 218 219 220 221 222 223 224 225 226
}

void KisTransformWorkerTest::testScaleUp()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
227
    dev->convertFromQImage(image, 0);
228 229

    KisFilterStrategy * filter = new KisBoxFilterStrategy();
230
    KisTransaction t(dev);
231 232 233 234
    KisTransformWorker tw(dev, 2.4, 2.4,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
235
                          0, 0, updater, filter);
236 237 238 239
    tw.run();
    t.end();

    QRect rc = dev->exactBounds();
240

241 242
    QCOMPARE(rc.width(), qCeil(image.width() * 2.4));
    QCOMPARE(rc.height(), qCeil(image.height() * 2.4));
243

244 245 246 247 248 249
    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "test_scaleup_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("test_scaleup_source.png");
        result.save("test_scaleup_result.png");
250
        QFAIL(QString("Failed to scale the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
251
    }
252 253 254 255 256 257 258 259 260 261 262 263
}


void KisTransformWorkerTest::testXScaleUp()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
264
    dev->convertFromQImage(image, 0);
265 266

    KisFilterStrategy * filter = new KisBoxFilterStrategy();
267
    KisTransaction t(dev);
268 269 270 271
    KisTransformWorker tw(dev, 2.0, 1.0,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
272
                          0, 0, updater, filter);
273 274
    tw.run();
    t.end();
275

276 277 278 279 280
    QRect rc = dev->exactBounds();

    QVERIFY(rc.width() == image.width() * 2);
    QVERIFY(rc.height() == image.height());

281 282 283 284 285 286
    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "scaleupx_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("test_x_scaleup_source.png");
        result.save("test_x_scaleup_result.png");
287
        QFAIL(QString("Failed to scale up the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
288
    }
289 290 291 292 293 294 295 296 297 298 299
}

void KisTransformWorkerTest::testYScaleUp()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
300
    dev->convertFromQImage(image, 0);
301 302 303

    KisFilterStrategy * filter = new KisBoxFilterStrategy();

304
    KisTransaction t(dev);
305 306 307 308
    KisTransformWorker tw(dev, 1.0, 2.0,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
309
                          0, 0, updater, filter);
310 311
    tw.run();
    t.end();
312

313 314
    QRect rc = dev->exactBounds();

315 316
    QCOMPARE(rc.width(), image.width());
    QCOMPARE(rc.height(), image.height() * 2);
317

318 319 320 321 322 323
    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "scaleupy_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("test_y_scaleup_source.png");
        result.save("test_y_scaleup_result.png");
324
        QFAIL(QString("Failed to scale up the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
325
    }
326 327
}

328 329 330 331 332 333 334 335 336
void KisTransformWorkerTest::testIdentity()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
337
    dev->convertFromQImage(image, 0);
338 339
    KisFilterStrategy * filter = new KisBoxFilterStrategy();

340
    KisTransaction t(dev);
341 342 343 344
    KisTransformWorker tw(dev, 1.0, 1.0,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
345
                          0, 0, updater, filter);
346 347
    tw.run();
    t.end();
348

349 350 351 352 353 354 355 356 357 358
    QRect rc = dev->exactBounds();

    QVERIFY(rc.width() ==image.width());
    QVERIFY(rc.height() == image.height());

    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("test_identity_source.png");
        result.save("test_identity_result.png");
359
        QFAIL(QString("Failed to apply identity transformation to image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
360 361
    }
}
362 363 364 365 366 367 368 369 370 371

void KisTransformWorkerTest::testScaleDown()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
372
    dev->convertFromQImage(image, 0);
373 374
    KisFilterStrategy * filter = new KisBoxFilterStrategy();

375
    KisTransaction t(dev);
376 377 378 379
    KisTransformWorker tw(dev, 0.123, 0.123,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
380
                          0, 0, updater, filter);
381 382
    tw.run();
    t.end();
383

384 385
    QRect rc = dev->exactBounds();

386 387
    QCOMPARE(rc.width(), qCeil(image.width() * 0.123));
    QCOMPARE(rc.height(), qCeil(image.height() * 0.123));
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

//    KisTransaction t2("test", dev);
//    KisRandomAccessorSP ac = dev->createRandomAccessorNG(rc.x(), rc.y());
//    for(int x = rc.x(); x < rc.width(); ++x) {
//        for(int y = rc.y(); y < rc.height(); ++y) {
//            ac->moveTo(x, y);
//            cs->setOpacity(ac->rawData(), 0.5, 1);
//        }
//    }
//    t2.end();

    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "test_scaledown_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("test_scaledown_source.png");
        result.save("test_scaledown_result.png");
405
        QFAIL(QString("Failed to scale down the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
406 407 408 409 410 411 412 413 414 415 416 417 418
    }
}


void KisTransformWorkerTest::testXScaleDown()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
419
    dev->convertFromQImage(image, 0);
420 421 422

    KisFilterStrategy * filter = new KisBoxFilterStrategy();

423
    KisTransaction t(dev);
424 425 426 427
    KisTransformWorker tw(dev, 0.123, 1.0,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
428
                          0, 0, updater, filter);
429 430
    tw.run();
    t.end();
431

432 433 434 435 436 437 438
    QRect rc = dev->exactBounds();
    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "scaledownx_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("scaledownx_source.png");
        result.save("scaledownx_result.png");
439
        QFAIL(QString("Failed to scale down the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
440 441 442 443 444 445 446 447 448 449 450 451
    }
}

void KisTransformWorkerTest::testYScaleDown()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
452
    dev->convertFromQImage(image, 0);
453 454

    KisFilterStrategy * filter = new KisBoxFilterStrategy();
455
    KisTransaction t(dev);
456 457 458 459
    KisTransformWorker tw(dev, 1.0, 0.123,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
460
                          0, 0, updater, filter);
461 462
    tw.run();
    t.end();
463

464 465
    QRect rc = dev->exactBounds();

466 467
    QCOMPARE(rc.width(), image.width());
    QCOMPARE(rc.height(), qCeil(image.height() * 0.123));
468 469 470 471 472 473 474

    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "scaledowny_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("scaledowny_source.png");
        result.save("scaledowny_result.png");
475
        QFAIL(QString("Failed to scale down the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
476 477 478 479 480 481 482 483 484 485 486 487
    }
}

void KisTransformWorkerTest::testXShear()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
488
    dev->convertFromQImage(image, 0);
489 490 491

    KisFilterStrategy * filter = new KisBoxFilterStrategy();

492
    KisTransaction t(dev);
493 494
    KisTransformWorker tw(dev, 1.0, 1.0,
                          1.0, 0.0,
495
                          300., 200.,
496
                          0.0,
497
                          0, 0, updater, filter);
498 499 500 501 502
    tw.run();
    t.end();

    QRect rc = dev->exactBounds();

503 504
    QVERIFY(rc.width() == 959);
    QVERIFY(rc.height() == image.height());
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

//    KisTransaction t2("test", dev);
//    KisRandomAccessorSP ac = dev->createRandomAccessorNG(rc.x(), rc.y());
//    for(int x = rc.x(); x < rc.width(); ++x) {
//        for(int y = rc.y(); y < rc.height(); ++y) {
//            ac->moveTo(x, y);
//            cs->setOpacity(ac->rawData(), 0.5, 1);
//        }
//    }
//    t2.end();

    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "shearx_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("shearx_source.png");
        result.save("shearx_result.png");
522
        QFAIL(QString("Failed to shear the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
523 524 525 526 527 528 529 530 531 532 533 534 535
    }
}


void KisTransformWorkerTest::testYShear()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
536
    dev->convertFromQImage(image, 0);
Boudewijn Rempt's avatar
Boudewijn Rempt committed
537

538 539
    KisFilterStrategy * filter = new KisBoxFilterStrategy();

540
    KisTransaction t(dev);
541 542
    KisTransformWorker tw(dev, 1.0, 1.0,
                          0.0, 1.0,
543
                          300., 200.,
544
                          0.0,
545
                          0, 0, updater, filter);
546 547 548 549 550
    tw.run();
    t.end();

    QRect rc = dev->exactBounds();

551 552
    QVERIFY(rc.width() == image.width());
    QVERIFY(rc.height() == 959);
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

//    KisTransaction t2("test", dev);
//    KisRandomAccessorSP ac = dev->createRandomAccessorNG(rc.x(), rc.y());
//    for(int x = rc.x(); x < rc.width(); ++x) {
//        for(int y = rc.y(); y < rc.height(); ++y) {
//            ac->moveTo(x, y);
//            cs->setOpacity(ac->rawData(), 0.5, 1);
//        }
//    }
//    t2.end();

    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + "sheary_result.png");
    if (!TestUtil::compareQImages(errpoint, image, result)) {
        image.save("sheary_source.png");
        result.save("sheary_result.png");
570
        QFAIL(QString("Failed to shear the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
571
    }
Boudewijn Rempt's avatar
Boudewijn Rempt committed
572 573 574

}

Boudewijn Rempt's avatar
Boudewijn Rempt committed
575

576 577 578 579 580 581 582 583 584
bool fuzzyCompareRects(const QRectF rc1, const QRectF rc2, qreal accuracy)
{
    bool result =
        qAbs(rc1.x() - rc2.x()) < accuracy &&
        qAbs(rc1.y() - rc2.y()) < accuracy &&
        qAbs(rc1.width() - rc2.width()) < 2 * accuracy &&
        qAbs(rc1.height() - rc2.height()) < 2 * accuracy;

    if(!result) {
585 586 587 588 589
        dbgKrita << "Failed to fuzzy compare rects";
        dbgKrita << "\t" << ppVar(accuracy);
        dbgKrita << "\t" << "actual  " << rc1;
        dbgKrita << "\t" << "expected" << rc2;
        dbgKrita << "+---------------------------+";
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    }

    return result;
}

void KisTransformWorkerTest::testMatrices()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();
    KisFilterStrategy *filter = new KisBoxFilterStrategy();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    KisPaintDeviceSP dev = new KisPaintDevice(cs);

    QRect fillRect(0,0,300,200);
    KoColor fillColor(Qt::white, cs);
    dev->fill(fillRect, fillColor);

    qreal scaleX = 1.5, scaleY = 1.5;
    qreal shearX = 1, shearY = 1.33;
    qreal shearOrigX = 150, shearOrigY = 100;
    qreal angle = M_PI/6;
    qreal transX = 77, transY = 33;

615
    KisTransaction t(dev);
616 617 618 619 620
    KisTransformWorker tw(dev, scaleX, scaleY,
                          shearX, shearY,
                          shearOrigX, shearOrigY,
                          angle,
                          transX, transY,
621
                          updater, filter);
622 623 624 625 626 627 628 629 630 631 632
    tw.run();
    t.end();

    QPolygonF referencePolygon =
        tw.transform().map(QPolygonF(QRectF(fillRect)));

    QVERIFY(fuzzyCompareRects(dev->exactBounds(),
                              referencePolygon.boundingRect(), 3));
}


633
void testRotationImpl(qreal angle, QString filePrefix, bool useUniformColor = false, const QString &filterId = "NearestNeighbor")
634 635 636 637 638 639 640 641
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    KisPaintDeviceSP dev = new KisPaintDevice(cs);

642 643 644 645 646 647 648 649 650 651 652
    QImage image;

    if (!useUniformColor) {
        image = QImage(QString(FILES_DATA_DIR) + QDir::separator() + "mirror_source.png");
        dev->convertFromQImage(image, 0);
    } else {
        dev->fill(QRect(120, 130, 374, 217), KoColor(QColor(150, 180, 230), cs));
    }

    KisFilterStrategy * filter = KisFilterStrategyRegistry::instance()->value(filterId);
    Q_ASSERT(filter);
653

654
    KisTransaction t(dev);
655 656 657
    KisTransformWorker tw(dev, 1.0, 1.0,
                          0.0, 0.0,
                          0.0, 0.0,
658 659
                          angle,
                          0, 0, updater, filter);
660 661 662 663 664 665 666
    tw.run();
    t.end();

    QRect rc = dev->exactBounds();

    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    QPoint errpoint;
667 668 669 670 671 672

    QString resFileName = QString("%1_result.png").arg(filePrefix);
    QString refFileName = QString("%1_expected.png").arg(filePrefix);

    image = QImage();
    image.load(QString(FILES_DATA_DIR) + QDir::separator() + resFileName);
673
    if (!TestUtil::compareQImages(errpoint, image, result)) {
674
        dbgKrita << filePrefix;
675 676
        image.save(refFileName);
        result.save(resFileName);
677
        QFAIL(QString("Failed to rotate the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
678 679 680
    }
}

681 682 683 684 685 686 687 688
void KisTransformWorkerTest::testRotation()
{
    testRotationImpl(M_PI/6, "rotation_30");
    testRotationImpl(M_PI/3, "rotation_60");
    testRotationImpl(M_PI/2,  "rotation_90");
    testRotationImpl(2*M_PI/3, "rotation_120");
    testRotationImpl(7*M_PI/6, "rotation_210");
    testRotationImpl(5*M_PI/3, "rotation_300");
689 690 691
    testRotationImpl(M_PI/6, "rotation_30_uniform_blin", true, "Bilinear");
    testRotationImpl(M_PI/6, "rotation_30_uniform_bcub", true, "Bicubic");
    testRotationImpl(M_PI/6, "rotation_30_uniform_lanc", true, "Lanczos3");
692 693
}

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
void KisTransformWorkerTest::testRotationSpecialCases()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();
    KisFilterStrategy *filter = new KisBoxFilterStrategy();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    KisPaintDeviceSP dev = new KisPaintDevice(cs);

    QRect fillRect(0,0,600,300);
    KoColor fillColor(Qt::white, cs);
    dev->fill(fillRect, fillColor);

    qreal scaleX = 0.5, scaleY = 0.5;
    qreal shearX = 0, shearY = 0;
    qreal shearOrigX = 0, shearOrigY = 0;
    qreal angle = M_PI;
    qreal transX = 300, transY = 150;

714
    KisTransaction t(dev);
715 716 717 718 719
    KisTransformWorker tw(dev, scaleX, scaleY,
                          shearX, shearY,
                          shearOrigX, shearOrigY,
                          angle,
                          transX, transY,
720
                          updater, filter);
721 722 723 724 725 726
    tw.run();
    t.end();

    QCOMPARE(dev->exactBounds(), QRect(0,0,300,150));
}

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
void KisTransformWorkerTest::testScaleUp5times()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    QImage image(QSize(2000,2000), QImage::Format_ARGB32_Premultiplied);
    image.fill(QColor(Qt::green).rgba());

    int checkSize = 20;
    QImage tile(checkSize * 2, checkSize * 2, QImage::Format_ARGB32_Premultiplied);
    QPainter pt(&tile);
    pt.fillRect(tile.rect(), Qt::green);
    pt.fillRect(0, 0, checkSize, checkSize, Qt::white);
    pt.fillRect(checkSize, checkSize, checkSize, checkSize, Qt::white);
    pt.end();

    pt.begin(&image);
    pt.setBrush(QBrush(tile));
    pt.drawRect(image.rect());
    pt.end();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
    dev->convertFromQImage(image, 0);

    KisFilterStrategy * filter = new KisBicubicFilterStrategy();
754
    KisTransaction t(dev);
755 756 757 758 759 760 761

    qreal SCALE = 5.0;

    KisTransformWorker tw(dev, SCALE, SCALE,
                          0.0, 0.0,
                          0.0, 0.0,
                          0.0,
762
                          0, 0, updater, filter);
763 764 765 766 767 768 769 770 771 772 773 774
    tw.run();
    t.end();

    QRect rc = dev->exactBounds();

#if 0
    // here you can check the input and result images
    image.save("test_scale_2000_2000_input.bmp");
    QImage result = dev->convertToQImage(0, rc.x(), rc.y(), rc.width(), rc.height());
    result.save("test_scale_2000_2000_" + QString::number(SCALE) + "_result.bmp");
#endif

775 776
    QCOMPARE(rc.width(), qCeil(image.width() * SCALE));
    QCOMPARE(rc.height(), qCeil(image.height() * SCALE));
777 778 779 780 781 782 783 784 785 786 787
}

void KisTransformWorkerTest::rotate90Left()
{
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "transform_rotate_test.png");
    KisPaintDeviceSP dev2 = new KisPaintDevice(cs);
    dev2->convertFromQImage(image, 0);

    QRect boundRect = dev2->exactBounds();

788
    KisTransaction t(kundo2_noi18n("rotate left 90"), dev2);
789
    QRect rc = KisTransformWorker::rotateLeft90(dev2, boundRect, 0, 0);
790 791
    t.end();

792
    QImage result = dev2->convertToQImage(0, rc.x(), rc.y(), image.width(), image.height());
793 794 795 796 797 798 799 800 801
    QTransform tf;
    QImage rotatedimage = image.transformed(tf.rotate(270));

    QPoint errpoint;
    if (!TestUtil::compareQImages(errpoint, rotatedimage, result)) {
        // They are the same, but should be mirrored
        image.save("rotate_90_left_test_1_source.png");
        rotatedimage.save("rotate_90_left_test_1_rotated_source.png");
        result.save("rotate_90_left_test_1_result.png");
802
        QFAIL(QString("Failed to rotate 90 left the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
803 804 805 806 807 808 809 810 811 812 813 814
    }
}

void KisTransformWorkerTest::rotate90Right()
{
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "transform_rotate_test.png");
    KisPaintDeviceSP dev2 = new KisPaintDevice(cs);
    dev2->convertFromQImage(image, 0);

    QRect boundRect = dev2->exactBounds();

815
    KisTransaction t(kundo2_noi18n("rotate right 90"), dev2);
816
    QRect rc = KisTransformWorker::rotateRight90(dev2, boundRect, 0, 0);
817 818 819 820 821
    t.end();

    QTransform tf;
    QImage rotatedimage = image.transformed(tf.rotate(90));

822
    QImage result = dev2->convertToQImage(0, rc.x(), rc.y(), image.width(), image.height());
823 824 825 826 827 828 829

    QPoint errpoint;
    if (!TestUtil::compareQImages(errpoint, rotatedimage, result)) {
        // They are the same, but should be mirrored
        image.save("rotate_90_right_test_1_source.png");
        rotatedimage.save("rotate_90_right_test_1_rotated_source.png");
        result.save("rotate_90_right_1_result.png");
830
        QFAIL(QString("Failed to rotate 90 right the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
831 832 833 834 835 836 837 838 839 840 841 842
    }
}

void KisTransformWorkerTest::rotate180()
{
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + "transform_rotate_test.png");
    KisPaintDeviceSP dev2 = new KisPaintDevice(cs);
    dev2->convertFromQImage(image, 0);

    QRect boundRect = dev2->exactBounds();

843
    KisTransaction t(kundo2_noi18n("rotate 180"), dev2);
844
    QRect rc = KisTransformWorker::rotate180(dev2, boundRect, 0, 0);
845 846
    t.end();

847
    QImage result = dev2->convertToQImage(0, rc.x(), rc.y(), image.width(), image.height());
848 849 850 851 852 853 854 855 856 857

    QTransform tf;
    QImage rotatedimage = image.transformed(tf.rotate(180));

    QPoint errpoint;
    if (!TestUtil::compareQImages(errpoint, rotatedimage, result)) {
        // They are the same, but should be mirrored
        image.save("rotate_180_1_source.png");
        rotatedimage.save("rotate_180_1_rotated_source.png");
        result.save("rotate_180_1_result.png");
858
        QFAIL(QString("Failed to rotate 180 the image, first different pixel: %1,%2 \n").arg(errpoint.x()).arg(errpoint.y()).toLatin1());
859 860 861
    }
}

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
void generateTestImage(QString inputFileName, qreal scale, qreal rotation, qreal xshear, KisFilterStrategy *filter, bool saveImage = true)
{
    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(QString(FILES_DATA_DIR) + QDir::separator() + inputFileName);
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
    dev->convertFromQImage(image, 0);

    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    KisTransformWorker tw(dev, scale, scale,
                          xshear, 0.0,
                          0.0, 0.0,
                          rotation,
                          0, 0,
                          updater, filter);


    tw.run();

    if (saveImage) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
884
        QStringList tmp = inputFileName.split('.');
885 886 887 888
        QString filename =
            QString("transform_%1_%2_%3_%4_%5_new.png")
            .arg(tmp[0]).arg(scale).arg(rotation).arg(xshear).arg(filter->name());

889
        dbgKrita << filename;
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

        dev->convertToQImage(0).save(filename);
    }
}

void KisTransformWorkerTest::benchmarkScale()
{
    QBENCHMARK {
        generateTestImage("hakonepa.png", 1.379,0.0,0.0,new KisBicubicFilterStrategy(), false);
    }
}

void KisTransformWorkerTest::benchmarkRotate()
{
    QBENCHMARK {
        generateTestImage("hakonepa.png", 1.0,M_PI/6.0,0.0,new KisBicubicFilterStrategy(), false);
    }
}

909 910 911 912 913 914 915
void KisTransformWorkerTest::benchmarkRotate1Q()
{
    QBENCHMARK {
        generateTestImage("hakonepa.png", 1.0,2 * M_PI/3.0,0.0,new KisBicubicFilterStrategy(), false);
    }
}

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
void KisTransformWorkerTest::benchmarkShear()
{
    QBENCHMARK {
        generateTestImage("hakonepa.png", 1.0,0.0,0.479,new KisBicubicFilterStrategy(), false);
    }
}

void KisTransformWorkerTest::benchmarkScaleRotateShear()
{
    QBENCHMARK {
        generateTestImage("hakonepa.png", 1.379,M_PI/6.0,0.479,new KisBicubicFilterStrategy(), false);
    }
}

void KisTransformWorkerTest::generateTestImages()
{
    QList<KisFilterStrategy*> filters;
    filters << new KisBoxFilterStrategy();
    filters << new KisHermiteFilterStrategy();
    filters << new KisBicubicFilterStrategy();
936
    filters << new KisBilinearFilterStrategy();
937 938 939 940 941 942 943 944 945
    filters << new KisBellFilterStrategy();
    filters << new KisBSplineFilterStrategy();
    filters << new KisLanczos3FilterStrategy();
    filters << new KisMitchellFilterStrategy();

    QStringList names;
    names << "hakonepa.png";
    //names << "star-chart-bars-full-600dpi.png";

946 947
    Q_FOREACH (const QString &name, names) {
        Q_FOREACH (KisFilterStrategy *filter, filters) {
948 949 950 951 952 953 954 955 956 957 958 959
            generateTestImage(name, 0.5,0.0,0.0,filter);
            generateTestImage(name, 0.734,0.0,0.0,filter);
            generateTestImage(name, 1.387,0.0,0.0,filter);
            generateTestImage(name, 2.0,0.0,0.0,filter);
            generateTestImage(name, 3.789,0.0,0.0,filter);

            generateTestImage(name, 1.0,M_PI/6,0.0,filter);

            generateTestImage(name, 1.0,0.0,0.5,filter);
        }
    }
}
960

961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
#include "kis_perspectivetransform_worker.h"


void KisTransformWorkerTest::testPartialProcessing()
{
    TestUtil::TestProgressBar bar;
    KoProgressUpdater pu(&bar);
    KoUpdaterPtr updater = pu.startSubtask();

    const KoColorSpace * cs = KoColorSpaceRegistry::instance()->rgb8();
    QImage image(TestUtil::fetchDataFileLazy("test_transform_quality.png"));
    KisPaintDeviceSP dev = new KisPaintDevice(cs);
    dev->convertFromQImage(image, 0);

    KisTransaction t(dev);

    QTransform transform = QTransform::fromScale(2.0, 1.1);
    transform.shear(1.1, 0);
    transform.rotateRadians(M_PI / 18);

    KisPerspectiveTransformWorker tw(0, transform, updater);
    tw.runPartialDst(dev, dev, QRect(1200, 1200, 150, 150));
    tw.runPartialDst(dev, dev, QRect(1350, 1200, 150, 150));
    tw.runPartialDst(dev, dev, QRect(1200, 1350, 150, 150));
    tw.runPartialDst(dev, dev, QRect(1350, 1350, 150, 150));

    t.end();

    QImage result = dev->convertToQImage(0);
    TestUtil::checkQImage(result, "transform_test", "partial", "single");
}

993
QTEST_MAIN(KisTransformWorkerTest)