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
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2005-06-14
 * Description : digiKam 8/16 bits image management API.
 *               QImage accessors.
 *
 * SPDX-FileCopyrightText: 2005-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "dimg_p.h"

namespace Digikam
{

QImage DImg::copyQImage() const
{
    if (isNull())
    {
        return QImage();
    }

    QImage::Format format = sixteenBit() ? QImage::Format_RGBA64<--- Shadow variable
                                         : QImage::Format_ARGB32;

    QImage img(width(), height(), format);

    if (img.isNull())
    {
        qCDebug(DIGIKAM_DIMG_LOG) << "Failed to allocate memory to copy DImg of size"
                                  << size() << "to QImage";

        return QImage();
    }

    if (!sixteenBit())
    {
        uchar* sptr = bits();
        QRgb*  dptr = reinterpret_cast<QRgb*>(img.bits());

        for (uint i = 0 ; i < width() * height() ; ++i)
        {
            *dptr++ = qRgba(sptr[2], sptr[1], sptr[0], sptr[3]);
            sptr   += 4;
        }
    }
    else
    {
        ushort*  sptr = reinterpret_cast<ushort*>(bits());
        QRgba64* dptr = reinterpret_cast<QRgba64*>(img.bits());

        for (uint i = 0 ; i < width() * height() ; ++i)
        {
            *dptr++ = qRgba64(sptr[2], sptr[1], sptr[0], sptr[3]);
            sptr   += 4;
        }
    }

    QByteArray iccProfile = getIccProfile().data();

    if (!iccProfile.isEmpty())
    {
        img.setColorSpace(QColorSpace::fromIccProfile(iccProfile));
    }

    return img;
}

QImage DImg::copyQImage32() const
{
    if (isNull())
    {
        return QImage();
    }

    if (!sixteenBit())
    {
        return copyQImage();
    }

    DImg img(*this);
    img.detach();
    img.convertDepth(32);

    return img.copyQImage();
}

QImage DImg::copyQImage(const QRect& rect) const
{
    return (copyQImage(rect.x(), rect.y(), rect.width(), rect.height()));
}

QImage DImg::copyQImage(const QRectF& rel) const
{
    if (isNull() || !rel.isValid())
    {
        return QImage();
    }

    return copyQImage(QRectF(rel.x()      * m_priv->width,
                             rel.y()      * m_priv->height,
                             rel.width()  * m_priv->width,
                             rel.height() * m_priv->height)
                      .toRect());
}

QImage DImg::copyQImage(int x, int y, int w, int h) const
{
    if (isNull())
    {
        return QImage();
    }

    DImg img = copy(x, y, w, h);

    return img.copyQImage();
}

} // namespace Digikam