File indexing completed on 2025-01-05 03:55:19

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2005-06-14
0007  * Description : digiKam 8/16 bits image management API.
0008  *               QImage accessors.
0009  *
0010  * SPDX-FileCopyrightText: 2005-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "dimg_p.h"
0018 
0019 namespace Digikam
0020 {
0021 
0022 QImage DImg::copyQImage() const
0023 {
0024     if (isNull())
0025     {
0026         return QImage();
0027     }
0028 
0029     QImage::Format format = sixteenBit() ? QImage::Format_RGBA64
0030                                          : QImage::Format_ARGB32;
0031 
0032     QImage img(width(), height(), format);
0033 
0034     if (img.isNull())
0035     {
0036         qCDebug(DIGIKAM_DIMG_LOG) << "Failed to allocate memory to copy DImg of size" << size() << "to QImage";
0037 
0038         return QImage();
0039     }
0040 
0041     if (!sixteenBit())
0042     {
0043         uchar* sptr = bits();
0044         QRgb*  dptr = reinterpret_cast<QRgb*>(img.bits());
0045 
0046         for (uint i = 0 ; i < width() * height() ; ++i)
0047         {
0048             *dptr++ = qRgba(sptr[2], sptr[1], sptr[0], sptr[3]);
0049             sptr   += 4;
0050         }
0051     }
0052     else
0053     {
0054         ushort*  sptr = reinterpret_cast<ushort*>(bits());
0055         QRgba64* dptr = reinterpret_cast<QRgba64*>(img.bits());
0056 
0057         for (uint i = 0 ; i < width() * height() ; ++i)
0058         {
0059             *dptr++ = qRgba64(sptr[2], sptr[1], sptr[0], sptr[3]);
0060             sptr   += 4;
0061         }
0062     }
0063 
0064     // NOTE: Qt4 do not provide anymore QImage::setAlphaChannel() because
0065     // alpha channel is auto-detected during QImage->QPixmap conversion
0066 
0067     return img;
0068 }
0069 
0070 QImage DImg::copyQImage(const QRect& rect) const
0071 {
0072     return (copyQImage(rect.x(), rect.y(), rect.width(), rect.height()));
0073 }
0074 
0075 QImage DImg::copyQImage(const QRectF& rel) const
0076 {
0077     if (isNull() || !rel.isValid())
0078     {
0079         return QImage();
0080     }
0081 
0082     return copyQImage(QRectF(rel.x()      * m_priv->width,
0083                              rel.y()      * m_priv->height,
0084                              rel.width()  * m_priv->width,
0085                              rel.height() * m_priv->height)
0086                       .toRect());
0087 }
0088 
0089 QImage DImg::copyQImage(int x, int y, int w, int h) const
0090 {
0091     if (isNull())
0092     {
0093         return QImage();
0094     }
0095 
0096     DImg img = copy(x, y, w, h);
0097 
0098     return img.copyQImage();
0099 }
0100 
0101 } // namespace Digikam