All Classes Namespaces Functions Variables Enumerations Properties Pages
bitmapimage.h
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU 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 
23 
24 class BitmapImage : public KeyFrame
25 {
26 public:
27  BitmapImage();
28  BitmapImage(const BitmapImage&);
29  BitmapImage(const QRect &rectangle, const QColor& color);
30  BitmapImage(const QPoint& topLeft, const QImage& image);
31  BitmapImage(const QPoint& topLeft, const QString& path);
32 
33  ~BitmapImage() override;
34  BitmapImage& operator=(const BitmapImage& a);
35 
36  BitmapImage* clone() const override;
37  void loadFile() override;
38  void unloadFile() override;
39  bool isLoaded() const override;
40  quint64 memoryUsage() override;
41 
42  void paintImage(QPainter& painter);
43  void paintImage(QPainter &painter, QImage &image, QRect sourceRect, QRect destRect);
44 
45  QImage* image();
46  void setImage(QImage* pImg);
47 
48  BitmapImage copy();
49  BitmapImage copy(QRect rectangle);
51 
52  void moveTopLeft(QPoint point);
53  void moveTopLeft(QPointF point) { moveTopLeft(point.toPoint()); }
54  void transform(QRect rectangle, bool smoothTransform);
55  void transform(QRectF rectangle, bool smoothTransform) { transform(rectangle.toRect(), smoothTransform); }
56  BitmapImage transformed(QRect selection, QTransform transform, bool smoothTransform);
57  BitmapImage transformed(QRect rectangle, bool smoothTransform);
58  BitmapImage transformed(QRectF rectangle, bool smoothTransform) { return transformed(rectangle.toRect(), smoothTransform); }
59 
60  bool contains(QPoint P) { return mBounds.contains(P); }
61  bool contains(QPointF P) { return contains(P.toPoint()); }
62  void autoCrop();
63 
64  QRgb pixel(int x, int y);
65  QRgb pixel(QPoint p);
66  void setPixel(int x, int y, QRgb color);
67  void setPixel(QPoint p, QRgb color);
68  void fillNonAlphaPixels(const QRgb color);
69 
70  QRgb constScanLine(int x, int y) const;
71  void scanLine(int x, int y, QRgb color);
72  void clear();
73  void clear(QRect rectangle);
74  void clear(QRectF rectangle) { clear(rectangle.toRect()); }
75 
76  static inline bool compareColor(QRgb newColor, QRgb oldColor, int tolerance, QHash<QRgb, bool> *cache);
77  static bool floodFill(BitmapImage** replaceImage, const BitmapImage* targetImage, const QRect& cameraRect, const QPoint& point, const QRgb& fillColor, int tolerance, const int expandValue);
78  static bool* floodFillPoints(const BitmapImage* targetImage,
79  QRect searchBounds, const QRect& maxBounds,
80  QPoint point,
81  const int tolerance,
82  QRect& newBounds,
83  bool &fillBorder);
84  static void expandFill(bool* fillPixels, const QRect& searchBounds, const QRect& maxBounds, int expand);
85 
86  void drawLine(QPointF P1, QPointF P2, QPen pen, QPainter::CompositionMode cm, bool antialiasing);
87  void drawRect(QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
88  void drawEllipse(QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
89  void drawPath(QPainterPath path, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
90 
91  QPoint topLeft() { autoCrop(); return mBounds.topLeft(); }
92  QPoint topRight() { autoCrop(); return mBounds.topRight(); }
93  QPoint bottomLeft() { autoCrop(); return mBounds.bottomLeft(); }
94  QPoint bottomRight() { autoCrop(); return mBounds.bottomRight(); }
95  int left() { autoCrop(); return mBounds.left(); }
96  int right() { autoCrop(); return mBounds.right(); }
97  int top() { autoCrop(); return mBounds.top(); }
98  int bottom() { autoCrop(); return mBounds.bottom(); }
99  int width() { autoCrop(); return mBounds.width(); }
100  int height() { autoCrop(); return mBounds.height(); }
101  QSize size() { autoCrop(); return mBounds.size(); }
102 
103 
104  QRect& bounds() { autoCrop(); return mBounds; }
105 
116  bool isMinimallyBounded() const { return mMinBound; }
117  void enableAutoCrop(bool b) { mEnableAutoCrop = b; }
118  void setOpacity(qreal opacity) { mOpacity = opacity; }
119  qreal getOpacity() const { return mOpacity; }
120 
121  Status writeFile(const QString& filename);
122 
123 protected:
124  void updateBounds(QRect rectangle);
125  void extend(const QPoint& p);
126  void extend(QRect rectangle);
127 
129  void setCompositionModeBounds(QRect sourceBounds, bool isSourceMinBounds, QPainter::CompositionMode cm);
130 
131 private:
132  QImage mImage;
133  QRect mBounds{0, 0, 0, 0};
134 
136  bool mMinBound = true;
137  bool mEnableAutoCrop = false;
138  qreal mOpacity = 1.0;
139 };
140 
141 #endif
QSize size() const const
QPoint topRight() const const
QRect toRect() const const
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.
int right() const const
QPoint bottomRight() const const
QPoint bottomLeft() const const
int height() const const
void setCompositionModeBounds(BitmapImage *source, QPainter::CompositionMode cm)
Updates the bounds after a drawImage operation with the composition mode cm.
int top() const const
int left() const const
void autoCrop()
Removes any transparent borders by reducing the boundaries.
bool contains(const QRect &rectangle, bool proper) const const
int width() const const
QPoint toPoint() const const
int bottom() const const
QPoint topLeft() const const
QRect bool mMinBound
Definition: bitmapimage.h:136
void updateBounds(QRect rectangle)
Update image bounds.
static bool compareColor(QRgb newColor, QRgb oldColor, int tolerance, QHash< QRgb, bool > *cache)
Compare colors for the purposes of flood filling.
bool isMinimallyBounded() const
Determines if the BitmapImage is minimally bounded.
Definition: bitmapimage.h:116