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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-09-03
0007  * Description : Integrated, multithread face detection / recognition
0008  *
0009  * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "facepreviewloader.h"
0016 
0017 // Local includes
0018 
0019 #include "digikam_debug.h"
0020 
0021 namespace Digikam
0022 {
0023 
0024 FacePreviewLoader::FacePreviewLoader(FacePipeline::Private* const dd)
0025     : d(dd)
0026 {
0027     // upper limit for memory cost
0028 
0029     maximumSentOutPackages = qMin(QThread::idealThreadCount(), 4);
0030 
0031     // this is crucial! Per default, only the last added image will be loaded
0032 
0033     setLoadingPolicy(PreviewLoadThread::LoadingPolicySimpleAppend);
0034 
0035     connect(this, SIGNAL(signalImageLoaded(LoadingDescription,DImg)),
0036             this, SLOT(slotImageLoaded(LoadingDescription,DImg)));
0037 }
0038 
0039 FacePreviewLoader::~FacePreviewLoader()
0040 {
0041 }
0042 
0043 void FacePreviewLoader::cancel()
0044 {
0045     stopAllTasks();
0046     scheduledPackages.clear();
0047 }
0048 
0049 void FacePreviewLoader::process(const FacePipelineExtendedPackage::Ptr& package)
0050 {
0051     if (!package->image.isNull())
0052     {
0053         Q_EMIT processed(package);
0054         return;
0055     }
0056 
0057     scheduledPackages << package;
0058     loadHighQuality(package->filePath, PreviewSettings::RawPreviewFromRawHalfSize);
0059 /*
0060     load(package->filePath, 800, MetaEngineSettings::instance()->settings().exifRotate);
0061     loadHighQuality(package->filePath, MetaEngineSettings::instance()->settings().exifRotate);
0062 */
0063     checkRestart();
0064 }
0065 
0066 void FacePreviewLoader::slotImageLoaded(const LoadingDescription& loadingDescription, const DImg& img)
0067 {
0068     FacePipelineExtendedPackage::Ptr package = scheduledPackages.take(loadingDescription);
0069 
0070     if (!package)
0071     {
0072         return;
0073     }
0074 
0075     // Avoid congestion before detection or recognition by pausing the thread.
0076     // We are throwing around serious amounts of memory!
0077 /*
0078     //qCDebug(DIGIKAM_GENERAL_LOG) << "sent out packages:"
0079                                    << d->packagesOnTheRoad - scheduledPackages.size()
0080                                    << "scheduled:" << scheduledPackages.size();
0081 */
0082     if (sentOutLimitReached() && !scheduledPackages.isEmpty())
0083     {
0084         stop();
0085         wait();
0086     }
0087 
0088     if (qMax(img.width(), img.height()) > 2000)
0089     {
0090         package->image     = img.smoothScale(2000,
0091                                              2000,
0092                                              Qt::KeepAspectRatio);
0093     }
0094     else
0095     {
0096         package->image     = img;
0097     }
0098 
0099     package->processFlags |= FacePipelinePackage::PreviewImageLoaded;
0100 
0101     Q_EMIT processed(package);
0102 }
0103 
0104 bool FacePreviewLoader::sentOutLimitReached() const
0105 {
0106     int packagesInTheFollowingPipeline = d->packagesOnTheRoad - scheduledPackages.size();
0107 
0108     return (packagesInTheFollowingPipeline > maximumSentOutPackages);
0109 }
0110 
0111 void FacePreviewLoader::checkRestart()
0112 {
0113     if (!sentOutLimitReached() && !scheduledPackages.isEmpty())
0114     {
0115         start();
0116     }
0117 }
0118 
0119 } // namespace Digikam
0120 
0121 #include "moc_facepreviewloader.cpp"