Cppcheck - HTML report - digiKam-master-rev-960d3ce42d

# Cppcheck report - digiKam-master-rev-960d3ce42d: ../../core/libs/threadimageio/fileio/loadsavethread.cpp

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    /* ============================================================
     *
     * This file is a part of digiKam project
     * https://www.digikam.org
     *
     * Date        : 2005-12-17
     * Description : image file IO threaded interface.
     *
     * SPDX-FileCopyrightText: 2005-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
     * SPDX-FileCopyrightText: 2005-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
     *
     * SPDX-License-Identifier: GPL-2.0-or-later
     *
     * ============================================================ */

    #include "loadsavethread.h"

    // Local includes

    #include "metaengine_rotation.h"
    #include "dmetadata.h"
    #include "managedloadsavethread.h"
    #include "sharedloadsavethread.h"
    #include "loadsavetask.h"

    namespace Digikam
    {

    class Q_DECL_HIDDEN LoadSaveThread::Private
    {
    public:

        Private() = default;

    public:

        bool                             running            = true;
        bool                             blockNotification  = false;

        QTime                            notificationTime;

        LoadSaveTask*                    lastTask           = nullptr;

        static LoadSaveFileInfoProvider* infoProvider;
    };

    LoadSaveFileInfoProvider* LoadSaveThread::Private::infoProvider = nullptr;

    //---------------------------------------------------------------------------------------------------

    LoadSaveThread::LoadSaveThread(QObject* const parent)
        : DynamicThread(parent),
          d            (new Private)
    {
    }

    LoadSaveThread::~LoadSaveThread()
    {
        shutDown();
        delete d;
    }

    void LoadSaveThread::setInfoProvider(LoadSaveFileInfoProvider* const infoProvider)<--- Shadow argument
    {
        Private::infoProvider = infoProvider;
    }

    LoadSaveFileInfoProvider* LoadSaveThread::infoProvider()
    {
        return Private::infoProvider;
    }

    void LoadSaveThread::load(const LoadingDescription& description)
    {
        QMutexLocker lock(threadMutex());
        m_todo << new LoadingTask(this, description);
        start(lock);
    }

    void LoadSaveThread::save(const DImg& image, const QString& filePath, const QString& format)
    {
        QMutexLocker lock(threadMutex());
        m_todo << new SavingTask(this, image, filePath, format);
        start(lock);
    }

    void LoadSaveThread::run()
    {
        while (runningFlag())
        {
            {
                QMutexLocker lock(threadMutex());

                delete d->lastTask;
                d->lastTask   = nullptr;
                delete m_currentTask;
                m_currentTask = nullptr;

                if (!m_todo.isEmpty())
                {
                    m_currentTask = m_todo.takeFirst();

                    if (m_notificationPolicy == NotificationPolicyTimeLimited)
                    {
                        // set timing values so that first event is sent only
                        // after an initial time span.

                        d->notificationTime  = QTime::currentTime();
                        d->blockNotification = true;
                    }
                }
                else
                {
                    stop(lock);
                }
            }

            if (m_currentTask)
            {
                m_currentTask->execute();
            }
        }
    }

    void LoadSaveThread::taskHasFinished()
    {
        // This function is called by the tasks _before_ they send their _final_ message.
        // This is to guarantee the user of the API that at least the final message
        // is sent after load() has been called.
        // We set m_currentTask to 0 here. If a new task is appended, base classes usually check
        // that m_currentTask is not currently loading the same task.
        // Now it might happen that m_currentTask has already emitted its final signal,
        // but the new task is rejected afterwards when m_currentTask is still the task
        // that has actually already finished (execute() in the loop above is of course not under mutex).
        // So we set m_currentTask to 0 immediately before the final message is emitted,
        // so that anyone who finds this task running as m_current task will get a message.

        QMutexLocker lock(threadMutex());
        d->lastTask   = m_currentTask;
        m_currentTask = nullptr;
    }

    void LoadSaveThread::imageStartedLoading(const LoadingDescription& loadingDescription)
    {
        notificationReceived();

        Q_EMIT signalImageStartedLoading(loadingDescription);
    }

    void LoadSaveThread::loadingProgress(const LoadingDescription& loadingDescription, float progress)
    {
        notificationReceived();

        Q_EMIT signalLoadingProgress(loadingDescription, progress);
    }

    void LoadSaveThread::imageLoaded(const LoadingDescription& loadingDescription, const DImg& img)
    {
        notificationReceived();

        Q_EMIT signalImageLoaded(loadingDescription, img);
    }

    void LoadSaveThread::moreCompleteLoadingAvailable(const LoadingDescription& oldLoadingDescription,
                                                      const LoadingDescription& newLoadingDescription)
    {
        notificationReceived();

        Q_EMIT signalMoreCompleteLoadingAvailable(oldLoadingDescription, newLoadingDescription);
    }

    void LoadSaveThread::imageStartedSaving(const QString& filePath)
    {
        notificationReceived();

        Q_EMIT signalImageStartedSaving(filePath);
    }

    void LoadSaveThread::savingProgress(const QString& filePath, float progress)
    {
        notificationReceived();

        Q_EMIT signalSavingProgress(filePath, progress);
    }

    void LoadSaveThread::imageSaved(const QString& filePath, bool success)
    {
        notificationReceived();

        Q_EMIT signalImageSaved(filePath, success);
    }

    void LoadSaveThread::thumbnailLoaded(const LoadingDescription& loadingDescription, const QImage& img)
    {
        notificationReceived();

        Q_EMIT signalQImageThumbnailLoaded(loadingDescription, img);
    }

    void LoadSaveThread::notificationReceived()
    {
        switch (m_notificationPolicy)
        {
            case NotificationPolicyDirect:
            {
                d->blockNotification = false;
                break;
            }

            case NotificationPolicyTimeLimited:
            {
                break;
            }
        }
    }

    void LoadSaveThread::setNotificationPolicy(NotificationPolicy notificationPolicy)
    {
        m_notificationPolicy = notificationPolicy;
        d->blockNotification = false;
    }

    bool LoadSaveThread::querySendNotifyEvent() const
    {
        // This function is called from the thread to ask for permission to send a notify event.

        switch (m_notificationPolicy)
        {
            case NotificationPolicyDirect:
            {
                // Note that m_blockNotification is not protected by a mutex. However, if there is a
                // race condition, the worst case is that one event is not sent, which is no problem.

                if (d->blockNotification)
                {
                    return false;
                }
                else
                {
                    d->blockNotification = true;

                    return true;
                }

                break;
            }

            case NotificationPolicyTimeLimited:
            {
                // Current default time value: 100 millisecs.

                if (d->blockNotification)
                {
                    d->blockNotification = d->notificationTime.msecsTo(QTime::currentTime()) < 100;
                }

                if (d->blockNotification)
                {
                    return false;
                }
                else
                {
                    d->notificationTime  = QTime::currentTime();
                    d->blockNotification = true;

                    return true;
                }

                break;
            }
        }

        return false;
    }

    int LoadSaveThread::exifOrientation(const QString& filePath,
                                        const DMetadata& metadata,
                                        bool isRaw,
                                        bool fromRawEmbeddedPreview)
    {
        int dbOrientation = MetaEngine::ORIENTATION_UNSPECIFIED;

        if (infoProvider())
        {
            dbOrientation = infoProvider()->orientationHint(filePath);
        }

        int exifOrientation = metadata.getItemOrientation();

        // Raw files are already rotated properly by Raw engine. Only perform auto-rotation with JPEG/PNG/TIFF file.
        // We don't have a feedback from Raw engine about auto-rotated RAW file during decoding.

        if (isRaw && !fromRawEmbeddedPreview)
        {
            // Did the user apply any additional rotation over the metadata flag?

            if ((dbOrientation == MetaEngine::ORIENTATION_UNSPECIFIED) || (dbOrientation == exifOrientation))
            {
                return MetaEngine::ORIENTATION_NORMAL;
            }

            // Assume A is the orientation as from metadata, B is an additional operation applied by the user,
            // C is the current orientation in the database.
            // A*B = C and B = A_inv * C

            QTransform A     = MetaEngineRotation::toTransform((MetaEngine::ImageOrientation)exifOrientation);
            QTransform C     = MetaEngineRotation::toTransform((MetaEngine::ImageOrientation)dbOrientation);
            QTransform A_inv = A.inverted();
            QTransform B     = A_inv * C;
            MetaEngineRotation m(B.m11(), B.m12(), B.m21(), B.m22());

            return m.exifOrientation();
        }

        if (dbOrientation != MetaEngine::ORIENTATION_UNSPECIFIED)
        {
            return dbOrientation;
        }

        return exifOrientation;
    }

    } // namespace Digikam

    #include "moc_loadsavethread.cpp"