Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • graphics
  • bitmap
bitmapimage.h
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#ifndef BITMAP_IMAGE_H
18#define BITMAP_IMAGE_H
19
20#include <QPainter>
21#include "keyframe.h"
22#include <QtMath>
23#include <QHash>
24
25class TiledBuffer;
26
27class BitmapImage : public KeyFrame
28{
29public:
30 const QRgb transp = qRgba(0, 0, 0, 0);
31 const QRgb blackline = qRgba(1, 1, 1, 255);
32 const QRgb redline = qRgba(254,0,0,255);
33 const QRgb greenline = qRgba(0,254,0,255);
34 const QRgb blueline = qRgba(0,0,254,255);
35
36 BitmapImage();
37 BitmapImage(const BitmapImage&);
38 BitmapImage(const QRect &rectangle, const QColor& color);
39 BitmapImage(const QPoint& topLeft, const QImage& image);
40 BitmapImage(const QPoint& topLeft, const QString& path);
41
42 ~BitmapImage() override;
43 BitmapImage& operator=(const BitmapImage& a);
44
45 BitmapImage* clone() const override;
46 void loadFile() override;
47 void unloadFile() override;
48 bool isLoaded() const override;
49 quint64 memoryUsage() override;
50
51 void paintImage(QPainter& painter);
52 void paintImage(QPainter &painter, QImage &image, QRect sourceRect, QRect destRect);
53
54 QImage* image();
55 void setImage(QImage* pImg);
56
57 BitmapImage copy();
58 BitmapImage copy(QRect rectangle);
59 void paste(BitmapImage*, QPainter::CompositionMode cm = QPainter::CompositionMode_SourceOver);
60 void paste(const TiledBuffer* tiledBuffer, QPainter::CompositionMode cm = QPainter::CompositionMode_SourceOver);
61
62 void moveTopLeft(QPoint point);
63 void moveTopLeft(QPointF point) { moveTopLeft(point.toPoint()); }
64 void transform(QRect rectangle, bool smoothTransform);
65 void transform(QRectF rectangle, bool smoothTransform) { transform(rectangle.toRect(), smoothTransform); }
66 BitmapImage transformed(QRect selection, QTransform transform, bool smoothTransform);
67 BitmapImage transformed(QRect rectangle, bool smoothTransform);
68 BitmapImage transformed(QRectF rectangle, bool smoothTransform) { return transformed(rectangle.toRect(), smoothTransform); }
69
70 bool contains(QPoint P) { return mBounds.contains(P); }
71 bool contains(QPointF P) { return contains(P.toPoint()); }
72 void autoCrop();
73
74 QRgb pixel(int x, int y);
75 QRgb pixel(QPoint p);
76 void setPixel(int x, int y, QRgb color);
77 void setPixel(QPoint p, QRgb color);
78 void fillNonAlphaPixels(const QRgb color);
79
80 QRgb constScanLine(int x, int y) const;
81 void scanLine(int x, int y, QRgb color);
82 void clear();
83 void clear(QRect rectangle);
84 void clear(QRectF rectangle) { clear(rectangle.toRect()); }
85
86 static bool floodFill(BitmapImage** replaceImage, const BitmapImage* targetImage, const QRect& cameraRect, const QPoint& point, const QRgb& fillColor, int tolerance, const int expandValue);
87 static bool* floodFillPoints(const BitmapImage* targetImage,
88 const QRect& searchBounds,
89 QPoint point,
90 const int tolerance,
91 QRect& newBounds);
92 static void expandFill(bool* fillPixels, const QRect& searchBounds, const QRect& maxBounds, int expand);
93
94 void drawLine(QPointF P1, QPointF P2, QPen pen, QPainter::CompositionMode cm, bool antialiasing);
95 void drawRect(QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
96 void drawEllipse(QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
97 void drawPath(QPainterPath path, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
98
99 QPoint topLeft() { autoCrop(); return mBounds.topLeft(); }
100 QPoint topRight() { autoCrop(); return mBounds.topRight(); }
101 QPoint bottomLeft() { autoCrop(); return mBounds.bottomLeft(); }
102 QPoint bottomRight() { autoCrop(); return mBounds.bottomRight(); }
103 int left() { autoCrop(); return mBounds.left(); }
104 int right() { autoCrop(); return mBounds.right(); }
105 int top() { autoCrop(); return mBounds.top(); }
106 int bottom() { autoCrop(); return mBounds.bottom(); }
107 int width() { autoCrop(); return mBounds.width(); }
108 int height() { autoCrop(); return mBounds.height(); }
109 QSize size() { autoCrop(); return mBounds.size(); }
110
111 BitmapImage* scanToTransparent(BitmapImage* img, int threshold, bool redEnabled, bool greenEnabled, bool blueEnabled);
112
113 QRect& bounds() { autoCrop(); return mBounds; }
114
125 bool isMinimallyBounded() const { return mMinBound; }
126 void enableAutoCrop(bool b) { mEnableAutoCrop = b; }
127 void setOpacity(qreal opacity) { mOpacity = opacity; }
128 qreal getOpacity() const { return mOpacity; }
129
130 Status writeFile(const QString& filename);
131
146 static inline bool compareColor(QRgb newColor, QRgb oldColor, int tolerance, QHash<QRgb, bool> *cache)
147 {
148 // Handle trivial case
149 if (newColor == oldColor) return true;
150
151 if(cache && cache->contains(newColor)) return cache->value(newColor);
152
153 // Get Eulcidian distance between colors
154 // Not an accurate representation of human perception,
155 // but it's the best any image editing program ever does
156 int diffRed = static_cast<int>(qPow(qRed(oldColor) - qRed(newColor), 2));
157 int diffGreen = static_cast<int>(qPow(qGreen(oldColor) - qGreen(newColor), 2));
158 int diffBlue = static_cast<int>(qPow(qBlue(oldColor) - qBlue(newColor), 2));
159 // This may not be the best way to handle alpha since the other channels become less relevant as
160 // the alpha is reduces (ex. QColor(0,0,0,0) is the same as QColor(255,255,255,0))
161 int diffAlpha = static_cast<int>(qPow(qAlpha(oldColor) - qAlpha(newColor), 2));
162
163 bool isSimilar = (diffRed + diffGreen + diffBlue + diffAlpha) <= tolerance;
164
165 if(cache)
166 {
167 Q_ASSERT(cache->contains(isSimilar) ? isSimilar == (*cache)[newColor] : true);
168 (*cache)[newColor] = isSimilar;
169 }
170
171 return isSimilar;
172 }
173
174protected:
175 void updateBounds(QRect rectangle);
176 void extend(const QPoint& p);
177 void extend(QRect rectangle);
178
179 void setCompositionModeBounds(BitmapImage *source, QPainter::CompositionMode cm);
180 void setCompositionModeBounds(QRect sourceBounds, bool isSourceMinBounds, QPainter::CompositionMode cm);
181
182private:
183 QImage mImage;
184 QRect mBounds{0, 0, 0, 0};
185
187 bool mMinBound = true;
188 bool mEnableAutoCrop = false;
189
190 const int LOW_THRESHOLD = 30; // threshold for images to be given transparency
191 const int COLORDIFF = 5; // difference in color values to decide color
192 const int GRAYSCALEDIFF = 15; // difference in grasycale values to decide color
193
194 qreal mOpacity = 1.0;
195};
196
197#endif
BitmapImage
Definition: bitmapimage.h:28
BitmapImage::updateBounds
void updateBounds(QRect rectangle)
Update image bounds.
Definition: bitmapimage.cpp:298
BitmapImage::mMinBound
bool mMinBound
Definition: bitmapimage.h:187
BitmapImage::setCompositionModeBounds
void setCompositionModeBounds(BitmapImage *source, QPainter::CompositionMode cm)
Updates the bounds after a drawImage operation with the composition mode cm.
Definition: bitmapimage.cpp:359
BitmapImage::expandFill
static void expandFill(bool *fillPixels, const QRect &searchBounds, const QRect &maxBounds, int expand)
Finds all pixels closest to the input color and applies the input color to the image.
Definition: bitmapimage.cpp:1045
BitmapImage::autoCrop
void autoCrop()
Removes any transparent borders by reducing the boundaries.
Definition: bitmapimage.cpp:431
BitmapImage::compareColor
static bool compareColor(QRgb newColor, QRgb oldColor, int tolerance, QHash< QRgb, bool > *cache)
Compare colors for the purposes of flood filling.
Definition: bitmapimage.h:146
BitmapImage::isMinimallyBounded
bool isMinimallyBounded() const
Determines if the BitmapImage is minimally bounded.
Definition: bitmapimage.h:125
KeyFrame
Definition: keyframe.h:30
Status
Definition: pencilerror.h:40
TiledBuffer
Definition: tiledbuffer.h:49
QBrush
QColor
QHash
QHash::contains
bool contains(const Key &key) const const
QHash::value
const T value(const Key &key) const const
QImage
QPainter
QPainter::CompositionMode
CompositionMode
QPainterPath
QPen
QPoint
QPointF
QPointF::toPoint
QPoint toPoint() const const
QRect
QRect::bottom
int bottom() const const
QRect::bottomLeft
QPoint bottomLeft() const const
QRect::bottomRight
QPoint bottomRight() const const
QRect::contains
bool contains(const QRect &rectangle, bool proper) const const
QRect::height
int height() const const
QRect::left
int left() const const
QRect::right
int right() const const
QRect::size
QSize size() const const
QRect::top
int top() const const
QRect::topLeft
QPoint topLeft() const const
QRect::topRight
QPoint topRight() const const
QRect::width
int width() const const
QRectF
QRectF::toRect
QRect toRect() const const
QSize
QString
QTransform
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39