File indexing completed on 2024-06-23 04:31:01

0001 /*
0002     SPDX-FileCopyrightText: 2023 g10 Code GmbH
0003     SPDX-FileContributor: Sune Stolborg Vuorela <sune@vuorela.dk>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 #include "imagescaling.h"
0008 #include <QGuiApplication>
0009 #include <QImage>
0010 #include <QTest>
0011 
0012 // Tests the image scaling and fit to canvas that is
0013 // used by the signature element in poppler.
0014 
0015 // For this test, we don't care about the quality of the
0016 // scaling but purely rely on Qt for that. It is mostly a
0017 // test that we place the input image correctly in returned
0018 // image
0019 
0020 class ImageScalingTest : public QObject
0021 {
0022     Q_OBJECT
0023 private Q_SLOTS:
0024     void testNoScaling();
0025     void testNoPaddingEnlarge();
0026     void testNoPaddingShrink();
0027     void testPadLeftRight();
0028     void testPadTopBottom();
0029 };
0030 
0031 void ImageScalingTest::testNoScaling()
0032 {
0033     QSize size(5, 5);
0034     QImage input(size, QImage::Format_ARGB32);
0035     input.fill(Qt::black);
0036     QImage output = imagescaling::scaleAndFitCanvas(input, size);
0037     QImage expected(size, QImage::Format_ARGB32);
0038     expected.fill(Qt::black);
0039     QCOMPARE(expected, output);
0040 }
0041 
0042 void ImageScalingTest::testNoPaddingEnlarge()
0043 {
0044     QImage input(QSize(10, 10), QImage::Format_ARGB32);
0045     input.fill(Qt::black);
0046     QSize size(20, 20);
0047     QImage output = imagescaling::scaleAndFitCanvas(input, size);
0048     QImage expected(size, QImage::Format_ARGB32);
0049     expected.fill(Qt::black);
0050     QCOMPARE(expected, output);
0051 }
0052 
0053 void ImageScalingTest::testNoPaddingShrink()
0054 {
0055     QImage input(QSize(20, 20), QImage::Format_ARGB32);
0056     input.fill(Qt::black);
0057     QSize size(10, 10);
0058     QImage output = imagescaling::scaleAndFitCanvas(input, size);
0059     QImage expected(size, QImage::Format_ARGB32);
0060     expected.fill(Qt::black);
0061     QCOMPARE(expected, output);
0062 }
0063 
0064 void ImageScalingTest::testPadLeftRight()
0065 {
0066     QImage input(QSize(5, 5), QImage::Format_ARGB32);
0067     input.fill(Qt::black);
0068     QSize size(9, 5);
0069     QImage output = imagescaling::scaleAndFitCanvas(input, size);
0070     // check top row
0071     QCOMPARE(output.pixelColor(0, 0), Qt::transparent);
0072     QCOMPARE(output.pixelColor(1, 0), Qt::transparent);
0073     QCOMPARE(output.pixelColor(2, 0), Qt::black);
0074     QCOMPARE(output.pixelColor(3, 0), Qt::black);
0075     QCOMPARE(output.pixelColor(4, 0), Qt::black);
0076     QCOMPARE(output.pixelColor(5, 0), Qt::black);
0077     QCOMPARE(output.pixelColor(6, 0), Qt::black);
0078     QCOMPARE(output.pixelColor(7, 0), Qt::transparent);
0079     QCOMPARE(output.pixelColor(8, 0), Qt::transparent);
0080     // check columns
0081     for (int x = 0; x < output.size().width(); x++) {
0082         QColor top = output.pixelColor(x, 0);
0083         for (int y = 1; y < output.size().height(); y++) {
0084             QCOMPARE(output.pixelColor(x, y), top);
0085         }
0086     }
0087 }
0088 
0089 void ImageScalingTest::testPadTopBottom()
0090 {
0091     QImage input(QSize(5, 5), QImage::Format_ARGB32);
0092     input.fill(Qt::black);
0093     QSize size(5, 9);
0094     QImage output = imagescaling::scaleAndFitCanvas(input, size);
0095     // check first column
0096     QCOMPARE(output.pixelColor(0, 0), Qt::transparent);
0097     QCOMPARE(output.pixelColor(0, 1), Qt::transparent);
0098     QCOMPARE(output.pixelColor(0, 2), Qt::black);
0099     QCOMPARE(output.pixelColor(0, 3), Qt::black);
0100     QCOMPARE(output.pixelColor(0, 4), Qt::black);
0101     QCOMPARE(output.pixelColor(0, 5), Qt::black);
0102     QCOMPARE(output.pixelColor(0, 6), Qt::black);
0103     QCOMPARE(output.pixelColor(0, 7), Qt::transparent);
0104     QCOMPARE(output.pixelColor(0, 8), Qt::transparent);
0105     // check rows
0106     for (int y = 0; y < output.size().height(); y++) {
0107         QColor first = output.pixelColor(0, y);
0108         for (int x = 1; x < output.size().width(); x++) {
0109             QCOMPARE(output.pixelColor(x, y), first);
0110         }
0111     }
0112 }
0113 
0114 QTEST_MAIN(ImageScalingTest)
0115 #include "testimagescaling.moc"
0116 
0117 // No need to export it, but we need to be able to call the functions
0118 #include "imagescaling.cpp"