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
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 25/08/2013
 * Description : Image Quality Calculor
 *
 * SPDX-FileCopyrightText: 2013-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2021-2022 by Phuoc Khanh Le <phuockhanhnk94 at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "imagequalitycalculator.h"

// Qt includes

#include <QRecursiveMutex>

namespace Digikam
{

class Q_DECL_HIDDEN ImageQualityCalculator::Private
{
public:

    Private() = default;

public:

    float                  threshold_punish = 0.9F;
    float                  weight_punish    = 20.0F;

    QRecursiveMutex        mutex;

    QList<ResultDetection> detectionResults;
};

ImageQualityCalculator::ImageQualityCalculator()
    : d(new Private)
{
    d->detectionResults = QList<ResultDetection>();
}

ImageQualityCalculator::~ImageQualityCalculator()
{
    delete d;
}

void ImageQualityCalculator::addDetectionResult(const QString& name,
                                                const float score,
                                                const float weight) const
{
    ResultDetection result;

    result.detetionType = name;
    result.weight       = weight;
    result.score        = score;

    QMutexLocker locker(&d->mutex);

    d->detectionResults.append(result);
}

void ImageQualityCalculator::normalizeWeight() const
{
    float sum = 0.0F;

    for (const auto& result : std::as_const(d->detectionResults))
    {
        sum += result.weight;<--- Consider using std::accumulate algorithm instead of a raw loop.
    }

    for (auto& result : d->detectionResults)
    {
        if (sum != 0.0F)
        {
            result.weight /= sum;
        }
    }
}

float ImageQualityCalculator::calculateQuality() const
{
    QMutexLocker locker(&d->mutex);

    if (!numberDetectors())
    {
        return (-1.0F);
    }

    adjustWeightByQualityLevel();

    normalizeWeight();

    float damage = 0.0F;

    for (const auto& result : std::as_const(d->detectionResults))
    {
        damage += result.score * result.weight;<--- Consider using std::accumulate algorithm instead of a raw loop.
    }

    return ((1.0F - damage) * 100.0F);
}

int ImageQualityCalculator::numberDetectors() const
{
    return d->detectionResults.count();
}

void ImageQualityCalculator::adjustWeightByQualityLevel() const
{
    for (auto& result : d->detectionResults)
    {
        if (result.score > d->threshold_punish)
        {
            result.weight *= d->weight_punish;
        }
    }
}

} // namespace Digikam