Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_image_patch.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2010 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_image_patch.h"
8
9#include <QPainter>
10#include "kis_debug.h"
11
12#include <math.h>
13
14/****** Some helper functions *******/
15
16inline void scaleRect(QRectF &rc, qreal scaleX, qreal scaleY)
17{
18 qreal x, y, w, h;
19 rc.getRect(&x, &y, &w, &h);
20
21 x *= scaleX;
22 y *= scaleY;
23 w *= scaleX;
24 h *= scaleY;
25
26 rc.setRect(x, y, w, h);
27}
28
29inline void scaleRect(QRect &rc, qreal scaleX, qreal scaleY)
30{
31 qint32 x, y, w, h;
32 rc.getRect(&x, &y, &w, &h);
33
34 x *= scaleX;
35 y *= scaleY;
36 w *= scaleX;
37 h *= scaleY;
38
39 rc.setRect(x, y, w, h);
40}
41
42/*********** KisImagePatch ************/
43
47
48KisImagePatch::KisImagePatch(QRect imageRect, qint32 borderWidth,
49 qreal scaleX, qreal scaleY)
50 : m_scaleX(scaleX)
51 , m_scaleY(scaleY)
52 , m_isScaled(false)
53{
54 // First we get unscaled rects
55 m_interestRect = QRectF(borderWidth, borderWidth,
56 imageRect.width(), imageRect.height());
57 m_patchRect = imageRect.adjusted(-borderWidth, -borderWidth,
58 borderWidth, borderWidth);
59 // And then we scale them
60 scaleRect(m_interestRect, scaleX, scaleY);
61 scaleRect(m_patchRect, scaleX, scaleY);
62
63 dbgRender << "A new patch has been created:";
64 dbgRender << ppVar(scaleX) << ppVar(scaleY);
67}
68
69void KisImagePatch::setImage(QImage image)
70{
71 m_image = image;
72 m_isScaled = false;
73}
74
75void KisImagePatch::preScale(const QRectF &dstRect)
76{
77 if (m_isScaled) return;
78
79 qreal scaleX = dstRect.width() / m_interestRect.width();
80 qreal scaleY = dstRect.height() / m_interestRect.height();
81
82 QSize newImageSize = QSize(ceil(m_image.width() * scaleX),
83 ceil(m_image.height() * scaleY));
84 // Calculating new _aligned_ scale
85 scaleX = qreal(newImageSize.width()) / m_image.width();
86 scaleY = qreal(newImageSize.height()) / m_image.height();
87
88 m_scaleX *= scaleX;
89 m_scaleY *= scaleY;
90
91 scaleRect(m_interestRect, scaleX, scaleY);
92
93 m_image = m_image.scaled(newImageSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
94
95 m_isScaled = true;
96
97}
98
100{
101 return m_patchRect;
102}
103
105{
106 return !m_image.isNull();
107}
108
109void KisImagePatch::drawMe(QPainter &gc,
110 const QRectF &dstRect,
111 QPainter::RenderHints renderHints)
112{
113 gc.save();
114 gc.setCompositionMode(QPainter::CompositionMode_Source);
115 gc.setRenderHints(renderHints, true);
116 gc.drawImage(dstRect, m_image, m_interestRect);
117 gc.restore();
118
119#if 0
123 qreal scaleX = dstRect.width() / m_interestRect.width();
124 qreal scaleY = dstRect.height() / m_interestRect.height();
125 dbgRender << "## PATCH.DRAWME #####################";
126 dbgRender << ppVar(scaleX) << ppVar(scaleY);
129 dbgRender << ppVar(dstRect);
130 dbgRender << "## EODM #############################";
131#endif
132}
void preScale(const QRectF &dstRect)
void drawMe(QPainter &gc, const QRectF &dstRect, QPainter::RenderHints renderHints)
void setImage(QImage image)
#define ppVar(var)
Definition kis_debug.h:155
#define dbgRender
Definition kis_debug.h:55
void scaleRect(QRectF &rc, qreal scaleX, qreal scaleY)