File indexing completed on 2024-06-16 04:20:08

0001 /*
0002     SPDX-FileCopyrightText: 2013 Fabio D 'Urso <fabiodurso@hotmail.it>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "../../settings_core.h"
0008 #include "../generator_kimgio.h"
0009 
0010 #include <core/observer.h>
0011 #include <core/page.h>
0012 #include <gui/pagepainter.h>
0013 
0014 #include <QTest>
0015 
0016 #include <KPluginMetaData>
0017 #include <QImage>
0018 #include <QImageReader>
0019 #include <QMimeDatabase>
0020 #include <QPainter>
0021 #include <QTemporaryFile>
0022 
0023 class KIMGIOTest : public QObject
0024 {
0025     Q_OBJECT
0026 
0027 private Q_SLOTS:
0028     void initTestCase();
0029     void testExifOrientation_data();
0030     void testExifOrientation();
0031 };
0032 
0033 void KIMGIOTest::initTestCase()
0034 {
0035     // Make sure we find the okularGenerator_kimgio that we build just now and not the system one
0036     QFileInfo lib(QStringLiteral(GENERATOR_PATH));
0037     QVERIFY2(lib.exists(), GENERATOR_PATH);
0038     QStringList libPaths = QCoreApplication::libraryPaths();
0039     libPaths.prepend(lib.absolutePath());
0040     QCoreApplication::setLibraryPaths(libPaths);
0041     QVERIFY(KPluginMetaData(QStringLiteral("okularGenerator_kimgio")).isValid());
0042     // make sure we didn't break the search path for image formats:
0043     auto availableFormats = QImageReader::supportedImageFormats();
0044     QVERIFY2(availableFormats.contains("jpeg"), availableFormats.join(", ").constData());
0045 }
0046 
0047 // The following images have different Exif orientation tags, but they all
0048 // are a 3x2 rectangle whose top-left pixel is black, and whose other pixels are
0049 // white. Note that, due to JPEG lossy compression, some pixels are not pure
0050 // white. In testExifOrientation, we only check the top-left and bottom-right
0051 // corners.
0052 void KIMGIOTest::testExifOrientation_data()
0053 {
0054     QTest::addColumn<QString>("imgPath");
0055 
0056     // No Exif metadata at all
0057     QTest::newRow("No Exif metadata") << KDESRCDIR "tests/data/testExifOrientation-noexif.jpg";
0058 
0059     // No Exif orientation information
0060     QTest::newRow("Unspecified") << KDESRCDIR "tests/data/testExifOrientation-unspecified.jpg";
0061 
0062     // Valid Orientation values
0063     QTest::newRow("Horizontal (normal)") << KDESRCDIR "tests/data/testExifOrientation-0.jpg";
0064     QTest::newRow("Mirror horizontal") << KDESRCDIR "tests/data/testExifOrientation-0mirror.jpg";
0065     QTest::newRow("Rotate 90 CW") << KDESRCDIR "tests/data/testExifOrientation-90.jpg";
0066     QTest::newRow("Mirror horizontal and rotate 90 CW") << KDESRCDIR "tests/data/testExifOrientation-90mirror.jpg";
0067     QTest::newRow("Rotate 180") << KDESRCDIR "tests/data/testExifOrientation-180.jpg";
0068     QTest::newRow("Mirror vertical") << KDESRCDIR "tests/data/testExifOrientation-180mirror.jpg";
0069     QTest::newRow("Rotate 270 CW") << KDESRCDIR "tests/data/testExifOrientation-270.jpg";
0070     QTest::newRow("Mirror horizontal and rotate 270 CW") << KDESRCDIR "tests/data/testExifOrientation-270mirror.jpg";
0071 }
0072 
0073 void KIMGIOTest::testExifOrientation()
0074 {
0075     QFETCH(QString, imgPath);
0076     QMimeDatabase db;
0077 
0078     Okular::SettingsCore::instance(QStringLiteral("kimgiotest"));
0079     Okular::Document *m_document = new Okular::Document(nullptr);
0080     const QMimeType mime = db.mimeTypeForFile(imgPath);
0081 
0082     Okular::DocumentObserver *dummyDocumentObserver = new Okular::DocumentObserver();
0083     m_document->addObserver(dummyDocumentObserver);
0084 
0085     // Load image
0086     QCOMPARE((int)m_document->openDocument(imgPath, QUrl(), mime), (int)Okular::Document::OpenSuccess);
0087     m_document->setRotation(0); // Test the default rotation
0088     QCOMPARE(m_document->pages(), 1u);
0089 
0090     // Check size
0091     QCOMPARE(m_document->page(0)->width(), double(3));
0092     QCOMPARE(m_document->page(0)->height(), double(2));
0093 
0094     // Generate pixmap
0095     Okular::PixmapRequest *req = new Okular::PixmapRequest(dummyDocumentObserver, 0, 3, 2, qApp->devicePixelRatio(), 1, Okular::PixmapRequest::NoFeature);
0096     m_document->requestPixmaps({req});
0097     QVERIFY(m_document->page(0)->hasPixmap(dummyDocumentObserver, 3, 2));
0098 
0099     // Obtain image
0100     QImage img(3, 2, QImage::Format_ARGB32_Premultiplied);
0101     QPainter p(&img);
0102     PagePainter::paintPageOnPainter(&p, m_document->page(0), dummyDocumentObserver, 0, 3, 2, QRect(0, 0, 3, 2));
0103 
0104     // Verify pixel data
0105     QCOMPARE(img.pixel(0, 0), qRgb(0, 0, 0));
0106     QCOMPARE(img.pixel(2, 1), qRgb(255, 255, 255));
0107 
0108     m_document->removeObserver(dummyDocumentObserver);
0109     delete dummyDocumentObserver;
0110     delete m_document;
0111 }
0112 
0113 QTEST_MAIN(KIMGIOTest)
0114 #include "kimgiotest.moc"