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

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  *               Copying operations.
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 DImg DImg::copy() const
0023 {
0024     DImg img(*this);
0025     img.detach();
0026 
0027     return img;
0028 }
0029 
0030 DImg DImg::copyImageData() const
0031 {
0032     DImg img(width(), height(), sixteenBit(), hasAlpha(), bits(), true);
0033 
0034     return img;
0035 }
0036 
0037 DImg DImg::copyMetaData() const
0038 {
0039     DImg img;
0040 
0041     // copy width, height, alpha, sixteenBit, null
0042 
0043     img.copyImageData(m_priv);
0044 
0045     // deeply copy metadata
0046 
0047     img.copyMetaData(m_priv);
0048 
0049     // set image to null
0050 
0051     img.m_priv->null = true;
0052 
0053     return img;
0054 }
0055 
0056 DImg DImg::copy(const QRect& rect) const
0057 {
0058     return copy(rect.x(),
0059                 rect.y(),
0060                 rect.width(),
0061                 rect.height());
0062 }
0063 
0064 DImg DImg::copy(const QRectF& rel) const
0065 {
0066     if (isNull() || !rel.isValid())
0067     {
0068         return DImg();
0069     }
0070 
0071     return copy(QRectF(rel.x()      * m_priv->width,
0072                        rel.y()      * m_priv->height,
0073                        rel.width()  * m_priv->width,
0074                        rel.height() * m_priv->height)
0075                 .toRect());
0076 }
0077 
0078 DImg DImg::copy(int x, int y, int w, int h) const
0079 {
0080     if (isNull() || (w <= 0) || (h <= 0))
0081     {
0082         qCDebug(DIGIKAM_DIMG_LOG) << " : return null image! ("
0083                                   << isNull() << ", " << w
0084                                   << ", " << h << ")";
0085         return DImg();
0086     }
0087 
0088     if (!clipped(x, y, w, h, m_priv->width, m_priv->height))
0089     {
0090         return DImg();
0091     }
0092 
0093     DImg image(*this, w, h);
0094     image.bitBltImage(this, x, y, w, h, 0, 0);
0095 
0096     return image;
0097 }
0098 
0099 /**
0100  * x,y, w x h is a section of the image. The image size is width x height.
0101  * Clips the section to the bounds of the image.
0102  * Returns if the (clipped) section is a valid rectangle.
0103  */
0104 bool DImg::clipped(int& x, int& y, int& w, int& h, uint width, uint height) const
0105 {
0106     QRect inner(x, y, w, h);
0107     QRect outer(0, 0, width, height);
0108 
0109     if (!outer.contains(inner))
0110     {
0111         QRect pt = inner.intersected(outer);
0112         x        = pt.x();
0113         y        = pt.y();
0114         w        = pt.width();
0115         h        = pt.height();
0116 
0117         return pt.isValid();
0118     }
0119 
0120     return inner.isValid();
0121 }
0122 
0123 } // namespace Digikam