File indexing completed on 2025-02-23 04:09:00

0001 /*
0002  *  SPDX-FileCopyrightText: 2010 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_image_patch.h"
0008 
0009 #include <QPainter>
0010 #include "kis_debug.h"
0011 
0012 #include <math.h>
0013 
0014 /****** Some helper functions *******/
0015 
0016 inline void scaleRect(QRectF &rc, qreal scaleX, qreal scaleY)
0017 {
0018     qreal x, y, w, h;
0019     rc.getRect(&x, &y, &w, &h);
0020 
0021     x *= scaleX;
0022     y *= scaleY;
0023     w *= scaleX;
0024     h *= scaleY;
0025 
0026     rc.setRect(x, y, w, h);
0027 }
0028 
0029 inline void scaleRect(QRect &rc, qreal scaleX, qreal scaleY)
0030 {
0031     qint32 x, y, w, h;
0032     rc.getRect(&x, &y, &w, &h);
0033 
0034     x *= scaleX;
0035     y *= scaleY;
0036     w *= scaleX;
0037     h *= scaleY;
0038 
0039     rc.setRect(x, y, w, h);
0040 }
0041 
0042 /*********** KisImagePatch ************/
0043 
0044 KisImagePatch::KisImagePatch()
0045 {
0046 }
0047 
0048 KisImagePatch::KisImagePatch(QRect imageRect, qint32 borderWidth,
0049                              qreal scaleX, qreal scaleY)
0050     : m_scaleX(scaleX)
0051     , m_scaleY(scaleY)
0052     , m_isScaled(false)
0053 {
0054     // First we get unscaled rects
0055     m_interestRect = QRectF(borderWidth, borderWidth,
0056                             imageRect.width(), imageRect.height());
0057     m_patchRect = imageRect.adjusted(-borderWidth, -borderWidth,
0058                                      borderWidth, borderWidth);
0059     // And then we scale them
0060     scaleRect(m_interestRect, scaleX, scaleY);
0061     scaleRect(m_patchRect, scaleX, scaleY);
0062 
0063     dbgRender << "A new patch has been created:";
0064     dbgRender << ppVar(scaleX) << ppVar(scaleY);
0065     dbgRender << ppVar(m_interestRect);
0066     dbgRender << ppVar(m_patchRect);
0067 }
0068 
0069 void KisImagePatch::setImage(QImage image)
0070 {
0071     m_image = image;
0072     m_isScaled = false;
0073 }
0074 
0075 void KisImagePatch::preScale(const QRectF &dstRect)
0076 {
0077     if (m_isScaled) return;
0078 
0079     qreal scaleX = dstRect.width() / m_interestRect.width();
0080     qreal scaleY = dstRect.height() / m_interestRect.height();
0081 
0082     QSize newImageSize = QSize(ceil(m_image.width() * scaleX),
0083                                    ceil(m_image.height() * scaleY));
0084     // Calculating new _aligned_ scale
0085     scaleX = qreal(newImageSize.width()) / m_image.width();
0086     scaleY = qreal(newImageSize.height()) / m_image.height();
0087 
0088     m_scaleX *= scaleX;
0089     m_scaleY *= scaleY;
0090 
0091     scaleRect(m_interestRect, scaleX, scaleY);
0092 
0093     m_image = m_image.scaled(newImageSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
0094 
0095     m_isScaled = true;
0096 
0097 }
0098 
0099 QRect KisImagePatch::patchRect()
0100 {
0101     return m_patchRect;
0102 }
0103 
0104 bool KisImagePatch::isValid()
0105 {
0106     return !m_image.isNull();
0107 }
0108 
0109 void KisImagePatch::drawMe(QPainter &gc,
0110                            const QRectF &dstRect,
0111                            QPainter::RenderHints renderHints)
0112 {
0113     gc.save();
0114     gc.setCompositionMode(QPainter::CompositionMode_Source);
0115     gc.setRenderHints(renderHints, true);
0116     gc.drawImage(dstRect, m_image, m_interestRect);
0117     gc.restore();
0118 
0119 #if 0
0120     /**
0121      * Just for debugging purposes
0122      */
0123     qreal scaleX = dstRect.width() / m_interestRect.width();
0124     qreal scaleY = dstRect.height() / m_interestRect.height();
0125     dbgRender << "## PATCH.DRAWME #####################";
0126     dbgRender << ppVar(scaleX) << ppVar(scaleY);
0127     dbgRender << ppVar(m_patchRect);
0128     dbgRender << ppVar(m_interestRect);
0129     dbgRender << ppVar(dstRect);
0130     dbgRender << "## EODM #############################";
0131 #endif
0132 }