File indexing completed on 2025-01-19 03:59:30

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2013-08-19
0007  * Description : Thread actions task for image quality sorter.
0008  *
0009  * SPDX-FileCopyrightText: 2013-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  * SPDX-FileCopyrightText: 2013-2014 by Gowtham Ashok <gwty93 at gmail dot com>
0011  * SPDX-FileCopyrightText: 2021-2022 by Phuoc Khanh Le <phuockhanhnk94 at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "imagequalitytask.h"
0018 
0019 // Local includes
0020 
0021 #include "digikam_debug.h"
0022 #include "dimg.h"
0023 #include "previewloadthread.h"
0024 #include "imagequalitycontainer.h"
0025 #include "imagequalityparser.h"
0026 #include "iteminfo.h"
0027 #include "maintenancedata.h"
0028 
0029 namespace Digikam
0030 {
0031 
0032 class Q_DECL_HIDDEN ImageQualityTask::Private
0033 {
0034 public:
0035 
0036     explicit Private()
0037         : imgqsort(nullptr),
0038           data    (nullptr)
0039     {
0040     }
0041 
0042     ImageQualityContainer quality;
0043     ImageQualityParser*   imgqsort;
0044 
0045     MaintenanceData*      data;
0046 };
0047 
0048 // -------------------------------------------------------
0049 
0050 ImageQualityTask::ImageQualityTask()
0051     : ActionJob(),
0052       d        (new Private)
0053 {
0054 }
0055 
0056 ImageQualityTask::~ImageQualityTask()
0057 {
0058     slotCancel();
0059     cancel();
0060 
0061     delete d;
0062 }
0063 
0064 void ImageQualityTask::setQuality(const ImageQualityContainer& quality)
0065 {
0066     d->quality = quality;
0067 }
0068 
0069 void ImageQualityTask::setMaintenanceData(MaintenanceData* const data)
0070 {
0071     d->data = data;
0072 }
0073 
0074 void ImageQualityTask::slotCancel()
0075 {
0076     if (d->imgqsort)
0077     {
0078         d->imgqsort->cancelAnalyse();
0079     }
0080 }
0081 
0082 void ImageQualityTask::run()
0083 {
0084     // While we have data (using this as check for non-null)
0085 
0086     while (d->data)
0087     {
0088         if (m_cancel)
0089         {
0090             return;
0091         }
0092 
0093         QString path = d->data->getImagePath();
0094 
0095         if (path.isEmpty())
0096         {
0097             break;
0098         }
0099 
0100         // Get item preview to perform quality analysis. No need to load whole image, this will be slower.
0101         // 1024 pixels size image must be enough to get suitable Quality results.
0102 
0103         DImg dimg = PreviewLoadThread::loadFastSynchronously(path, 1024);
0104 
0105         if (!dimg.isNull() && !m_cancel)
0106         {
0107             // Run Quality analysis backend and store Pick Label result to database.
0108             // Backend Input : d->quality as Quality analysis settings,
0109             //                 dimg       as reduced size image data to parse,
0110             //                 path       as file path to patch database properties.
0111             // Result        : Backend must scan Quality of image depending of settings and compute a Quality estimation accordingly.
0112             //                 Finally, using file path, database Pick Label properties is assigned through ItemInfo interface.
0113             // Warning       : All code here will run in a separated thread and must be re-entrant/thread-safe. Only pure computation
0114             //                 must be processed. GUI calls are prohibited. ItemInfo and DImg can be used safety in thread.
0115 
0116             PickLabel pick;
0117             d->imgqsort = new ImageQualityParser(dimg, d->quality, &pick);
0118             d->imgqsort->startAnalyse();
0119 
0120             ItemInfo info = ItemInfo::fromLocalFile(path);
0121             info.setPickLabel(pick);
0122 
0123             // delete image data after to set label
0124 
0125             delete d->imgqsort;
0126             d->imgqsort = nullptr;
0127         }
0128 
0129         // Dispatch progress to Progress Manager
0130 
0131         QImage qimg = dimg.smoothScale(22, 22, Qt::KeepAspectRatio).copyQImage();
0132 
0133         Q_EMIT signalFinished(qimg);
0134     }
0135 
0136     Q_EMIT signalDone();
0137 }
0138 
0139 } // namespace Digikam
0140 
0141 #include "moc_imagequalitytask.cpp"