Cppcheck - HTML report - digiKam-master-rev-70b072e127

# Cppcheck report - digiKam-master-rev-70b072e127: ../../core/libs/dimg/filters/hotpixels/blackframeparser.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
    /* ============================================================
     *
     * This file is a part of digiKam project
     * https://www.digikam.org
     *
     * Date        : 2005-03-27
     * Description : black frames parser
     *
     * SPDX-FileCopyrightText: 2005-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
     * SPDX-FileCopyrightText: 2005-2006 by Unai Garro <ugarro at users dot sourceforge dot net>
     * SPDX-FileCopyrightText: 2015      by Mohamed_Anwer <d->dot_anwer at gmx dot com>
     *
     * Part of the algorithm for finding the hot pixels was based on
     * the code of jpegpixi, which was released under the GPL license,
     * written by Martin Dickopp
     *
     * SPDX-License-Identifier: GPL-2.0-or-later
     *
     * ============================================================ */

    #include "blackframeparser.h"

    // Qt includes

    #include <QStringList>
    #include <QApplication>

    // Local includes

    #include "digikam_debug.h"
    #include "dcolor.h"
    #include "dimg.h"
    #include "loadsavethread.h"

    namespace Digikam
    {

    /// Square root of denominator for relative quantities.
    static const int DENOM_SQRT = 10000;

    /// Denominator for relative quantities.
    #define DENOM (DENOM_SQRT * DENOM_SQRT)

    /// Convert relative to absolute numbers. Care must be taken not to overflow integers.
    #define REL_TO_ABS(n,m) \
        ((((n) / DENOM_SQRT) * (m) + ((n) % DENOM_SQRT) * (m) / DENOM_SQRT) / DENOM_SQRT)

    class Q_DECL_HIDDEN BlackFrameParser::Private
    {
    public:

        Private() = default;

    public:

        DImg            image;

        LoadSaveThread* imageLoaderThread = nullptr;
    };

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

    BlackFrameParser::~BlackFrameParser()
    {
        delete d->imageLoaderThread;
        delete d;
    }

    void BlackFrameParser::parseHotPixels(const QString& file)
    {
        parseBlackFrame(QUrl::fromLocalFile(file));
    }

    void BlackFrameParser::parseBlackFrame(const QUrl& url)
    {
        if (url.isEmpty())
        {
            return;
        }

        QString localFile = url.toLocalFile();

        if (!d->imageLoaderThread)
        {
            d->imageLoaderThread = new LoadSaveThread();

            connect(d->imageLoaderThread, SIGNAL(signalLoadingProgress(LoadingDescription,float)),
                    this, SLOT(slotLoadingProgress(LoadingDescription,float)));

            connect(d->imageLoaderThread, SIGNAL(signalImageLoaded(LoadingDescription,DImg)),
                    this, SLOT(slotLoadImageFromUrlComplete(LoadingDescription,DImg)));
        }

        LoadingDescription desc = LoadingDescription(localFile, DRawDecoding());
        d->imageLoaderThread->load(desc);
    }

    void BlackFrameParser::slotLoadingProgress(const LoadingDescription&, float v)
    {
        Q_EMIT signalLoadingProgress(v);
    }

    void BlackFrameParser::slotLoadImageFromUrlComplete(const LoadingDescription&, const DImg& img)
    {
        parseBlackFrame(img);

        Q_EMIT signalLoadingComplete();
    }

    void BlackFrameParser::parseBlackFrame(const DImg& img)
    {
        d->image = img.copy();
        blackFrameParsing();
    }

    DImg BlackFrameParser::image() const
    {
        return d->image;
    }

    /**
     * Parses black frames
     */
    void BlackFrameParser::blackFrameParsing()
    {
        // Now find the hot pixels and store them in a list

        QList<HotPixelProps> hpList;

        // If you accidentally open a normal image for a black frame, the tool and host application will
        // freeze due to heavy calculation.
        // We should stop at a certain amount of hot pixels, to avoid the freeze.
        // 1000 of total hot pixels should be good enough for a trigger. Images with such an amount of hot pixels should
        // be considered as messed up anyway.

        const int maxHotPixels = 1000;

        for (int y = 0 ; y < (int)d->image.height() ; ++y)
        {
            for (int x = 0 ; x < (int)d->image.width() ; ++x)
            {
                // Get each point in the image

                DColor pixrgb = d->image.getPixelColor(x, y);
                QColor color;
                color.setRgb(pixrgb.getQColor().rgb());

                // Find maximum component value.

                int       maxValue;
                int       threshold       = DENOM / 10;
                const int threshold_value = REL_TO_ABS(threshold, 255);
                maxValue                  = (color.red() > color.blue()) ? color.red() : color.blue();

                if (color.green() > maxValue)
                {
                    maxValue = color.green();
                }

                // If the component is bigger than the threshold, add the point

                if (maxValue > threshold_value)
                {
                    HotPixelProps point;
                    point.rect       = QRect (x, y, 1, 1);

                    // TODO: check this

                    point.luminosity = ((2 * DENOM) / 255 ) * maxValue / 2;

                    hpList.append(point);
                }
            }

            if (hpList.count() > maxHotPixels)
            {
                break;
            }
        }

        // Now join points together into groups

        consolidatePixels(hpList);

        // And notify

        Q_EMIT signalHotPixelsParsed(hpList);
    }

    /**
     * Consolidate adjacent points into larger points.
     */
    void BlackFrameParser::consolidatePixels(QList<HotPixelProps>& list)
    {
        if (list.isEmpty())
        {
            return;
        }

        qCDebug(DIGIKAM_DIMG_LOG) << "Consolidate hp list of" << list.size() << "items";

        // Consolidate horizontally.

        QList<HotPixelProps>::iterator it = list.begin();

        ++it;

        HotPixelProps tmp;
        HotPixelProps point;
        HotPixelProps point_below;

        for ( ; it != list.end() ; ++it)
        {
            // cppcheck-suppress knownConditionTrueFalse
            while (it != list.end())
            {
                point = (*it);
                tmp   = point;

                QList<HotPixelProps>::iterator point_below_it = list.end();

                // find any intersecting hot pixels below tmp

                int i = list.indexOf(tmp);

                qCDebug(DIGIKAM_DIMG_LOG) << "Processing hp index" << i;

                if (i != -1)
                {
                    point_below_it = list.begin() + i;

                    if (point_below_it != list.end())
                    {
                        point_below =* point_below_it;
                        validateAndConsolidate(&point, &point_below);

                        point.rect.setX(qMin(point.x(), point_below.x()));
                        point.rect.setWidth(qMax(point.x() + point.width(),
                                                 point_below.x() + point_below.width()) - point.x());
                        point.rect.setHeight(qMax(point.y() + point.height(),
                                              point_below.y() + point_below.height()) - point.y());
                        (*it)       = point;
                        list.erase(point_below_it); // TODO: Check! this could remove it++?
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }

    void BlackFrameParser::validateAndConsolidate(HotPixelProps* const a, HotPixelProps* const b)<--- Either there is a missing override/final keyword, or the parameter 'b' can be declared as pointer to const
    {
        a->luminosity = qMax(a->luminosity, b->luminosity);
    }

    } // namespace Digikam

    #include "moc_blackframeparser.cpp"