File indexing completed on 2025-01-05 03:58:14

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2018-10-25
0007  * Description : An unit-test to extract preview and patch with DMetadata.
0008  *               This stage is used by Export tools.
0009  *
0010  * SPDX-FileCopyrightText: 2019-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "patchpreview_utest.h"
0017 
0018 // Qt includes
0019 
0020 #include <QImage>
0021 #include <QByteArray>
0022 
0023 // Local includes
0024 
0025 #include "dpluginloader.h"
0026 #include "previewloadthread.h"
0027 
0028 QTEST_MAIN(PatchPreviewTest)
0029 
0030 PatchPreviewTest::PatchPreviewTest(QObject* const parent)
0031     : AbstractUnitTest(parent)
0032 {
0033 }
0034 
0035 void PatchPreviewTest::testExtractPreviewAndFixMetadata()
0036 {
0037     patchPreview(m_originalImageFolder + QLatin1String("IMG_2520.CR2"), true, 1024, 100); // See bug #400140
0038 }
0039 
0040 void PatchPreviewTest::patchPreview(const QString& file, bool rescale, int maxDim, int imageQuality)
0041 {
0042     qCDebug(DIGIKAM_TESTS_LOG) << "File to process:" << file;
0043 
0044     QString path = m_tempDir.filePath(QFileInfo(file).fileName().trimmed()) +
0045                    QLatin1String(".jpg");
0046 
0047     qCDebug(DIGIKAM_TESTS_LOG) << "Temporary target file:" << path;
0048 
0049     bool ret = !path.isNull();
0050     QVERIFY(ret);
0051 
0052     // Load preview from original image.
0053 
0054     QImage image = PreviewLoadThread::loadHighQualitySynchronously(file).copyQImage();
0055 
0056     if (image.isNull())
0057     {
0058         image.load(file);
0059     }
0060 
0061     ret = image.isNull();
0062     QVERIFY(!ret);
0063 
0064     if (rescale && (image.width() > maxDim || image.height() > maxDim))
0065     {
0066         image = image.scaled(maxDim, maxDim, Qt::KeepAspectRatio, Qt::SmoothTransformation);
0067     }
0068 
0069     // Save preview in temporary directory.
0070 
0071     ret = image.save(path, "JPEG", imageQuality);
0072     QVERIFY(ret);
0073 
0074     // Load metadata from original image.
0075     QScopedPointer<DMetadata> meta(new DMetadata);
0076     ret = meta->load(file);
0077     QVERIFY(ret);
0078 
0079     QByteArray exif = meta->getExifEncoded();
0080     QByteArray iptc = meta->getIptc();
0081     QByteArray xmp  = meta->getXmp();
0082 
0083     // Backport metadata to preview file.
0084 
0085     meta->load(path);
0086     QVERIFY(ret);
0087 
0088     meta->setExif(exif);
0089     meta->setIptc(iptc);
0090     meta->setXmp(xmp);
0091     meta->setItemDimensions(image.size());
0092     meta->setItemOrientation(MetaEngine::ORIENTATION_NORMAL);
0093     meta->setMetadataWritingMode((int)DMetadata::WRITE_TO_FILE_ONLY);
0094     ret = meta->applyChanges(true);
0095     QVERIFY(ret);
0096 }
0097 
0098 void PatchPreviewTest::initTestCase()
0099 {
0100     AbstractUnitTest::initTestCase();
0101 
0102     QDir dir(qApp->applicationDirPath());
0103     qputenv("DK_PLUGIN_PATH", dir.canonicalPath().toUtf8());
0104     DPluginLoader::instance()->init();
0105 }
0106 
0107 void PatchPreviewTest::cleanupTestCase()
0108 {
0109     AbstractUnitTest::cleanupTestCase();
0110 
0111     DPluginLoader::instance()->cleanUp();
0112 }
0113 
0114 #include "moc_patchpreview_utest.cpp"