File indexing completed on 2025-01-19 03:57:49

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-02-04
0007  * Description : an unit-test to set IPTC Preview
0008  *
0009  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "setiptcpreview_utest.h"
0016 
0017 // Qt includes
0018 
0019 #include <QFile>
0020 #include <QImage>
0021 
0022 QTEST_MAIN(SetIptcPreviewTest)
0023 
0024 SetIptcPreviewTest::SetIptcPreviewTest(QObject* const parent)
0025     : AbstractUnitTest(parent)
0026 {
0027 }
0028 
0029 void SetIptcPreviewTest::testSetIptcPreview()
0030 {
0031     setIptcPreview(m_originalImageFolder + QLatin1String("Luftbild_H6_7.tif"));
0032 }
0033 
0034 void SetIptcPreviewTest::setIptcPreview(const QString& file)
0035 {
0036     qCDebug(DIGIKAM_TESTS_LOG) << "File to process:" << file;
0037 
0038     QString path = m_tempDir.filePath(QFileInfo(file).fileName().trimmed());
0039 
0040     qCDebug(DIGIKAM_TESTS_LOG) << "Temporary target file:" << path;
0041 
0042     bool ret = !path.isNull();
0043     QVERIFY(ret);
0044 
0045     QFile::remove(path);
0046     QFile target(file);
0047     ret = target.copy(path);
0048     QVERIFY(ret);
0049 
0050     QImage  preview;
0051     QImage  image(path);
0052     QVERIFY(!image.isNull());
0053 
0054     QSize previewSize = image.size();
0055     previewSize.scale(1280, 1024, Qt::KeepAspectRatio);
0056 
0057     // Ensure that preview is not upscaled
0058     if (previewSize.width() >= (int)image.width())
0059         preview = image.copy();
0060     else
0061         preview = image.scaled(previewSize.width(), previewSize.height(), Qt::IgnoreAspectRatio).copy();
0062 
0063     QVERIFY(!preview.isNull());
0064 
0065     QScopedPointer<DMetadata> meta(new DMetadata);
0066     ret = meta->load(path);
0067     QVERIFY(ret);
0068 
0069     meta->setItemPreview(preview);
0070     ret = meta->applyChanges();
0071     QVERIFY(ret);
0072 
0073     QImage preview2;
0074     QScopedPointer<DMetadata> meta2(new DMetadata);
0075     ret = meta2->load(path);
0076     QVERIFY(ret);
0077 
0078     ret = meta2->getItemPreview(preview2);
0079     QVERIFY(ret);
0080 
0081     QVERIFY(!preview2.isNull());
0082 
0083     QCOMPARE(preview.size(), preview2.size());
0084 }
0085 
0086 #include "moc_setiptcpreview_utest.cpp"