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 #ifndef KIS_IMAGE_PATCH_H_
0008 #define KIS_IMAGE_PATCH_H_
0009 
0010 #include <QPainter>
0011 #include <QImage>
0012 #include <kis_types.h>
0013 
0014 #define BORDER_SIZE(scale) (ceil(0.5/scale))
0015 
0016 
0017 class KisImagePatch
0018 {
0019 public:
0020     /**
0021      * A default constructor initializing invalid patch
0022      */
0023     KisImagePatch();
0024 
0025     /**
0026      * Initializes a new patch with given values.
0027      * Be careful, because the constructor does not fill
0028      * QImage of the patch, as the patch rect is not known yet
0029      *
0030      * \see setImage
0031      */
0032     KisImagePatch(QRect imageRect, qint32 borderWidth,
0033                   qreal scaleX, qreal scaleY);
0034 
0035     /**
0036      * Sets the image of the patch
0037      * Should be called right after the constructor
0038      * to finish initializing the object
0039      */
0040     void setImage(QImage image);
0041 
0042     /**
0043      * prescale the patch image. Call after setImage().
0044      * This ensures that we use the QImage smoothscale method, not the QPainter scaling,
0045      * which is far inferior.
0046      */
0047     void preScale(const QRectF &dstRect);
0048 
0049     /**
0050      * Returns the rect of KisImage covered by the image
0051      * of the patch (in KisImage pixels)
0052      *
0053      * \see m_patchRect
0054      */
0055     QRect patchRect();
0056 
0057     /**
0058      * Draws an m_interestRect of the patch onto @p gc
0059      * By the way it fits this rect into @p dstRect
0060      * @p renderHints are directly transmitted to QPainter
0061      */
0062     void drawMe(QPainter &gc,
0063                 const QRectF &dstRect,
0064                 QPainter::RenderHints renderHints);
0065 
0066     /**
0067      * Checks whether the patch can be used for drawing the image
0068      */
0069     bool isValid();
0070 
0071 private:
0072     /**
0073      * The scale of the image stored in the patch
0074      */
0075     qreal m_scaleX {0.0};
0076     qreal m_scaleY {0.0};
0077 
0078     /**
0079      * The rect of KisImage covered by the image
0080      * of the patch (in KisImage pixels)
0081      */
0082     QRect m_patchRect;
0083 
0084     /**
0085      * The rect that was requested during creation
0086      * of the patch. It equals to patchRect without
0087      * borders
0088      * These borders are introduced for more accurate
0089      * smooth scaling to reduce border effects
0090      * (IN m_image PIXELS, relative to m_image's topLeft);
0091 
0092      */
0093     QRectF m_interestRect;
0094 
0095     QImage m_image;
0096     bool m_isScaled {false};
0097 };
0098 
0099 #endif /* KIS_IMAGE_PATCH_H_ */