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 <memory>
21 #include <QPainter>
22 #include "keyframe.h"
23 
24 
25 class BitmapImage : public KeyFrame
26 {
27 public:
28  BitmapImage();
29  BitmapImage(const BitmapImage&);
30  BitmapImage(const QRect &rectangle, const QColor& color);
31  BitmapImage(const QPoint& topLeft, const QImage& image);
32  BitmapImage(const QPoint& topLeft, const QString& path);
33 
34  ~BitmapImage();
35  BitmapImage& operator=(const BitmapImage& a);
36 
37  BitmapImage* clone() override;
38  void loadFile() override;
39  void unloadFile() override;
40  bool isLoaded() override;
41  quint64 memoryUsage() override;
42 
43  void paintImage(QPainter& painter);
44  void paintImage(QPainter &painter, QImage &image, QRect sourceRect, QRect destRect);
45 
46  QImage* image();
47  void setImage(QImage* pImg);
48 
49  BitmapImage copy();
50  BitmapImage copy(QRect rectangle);
52 
53  void moveTopLeft(QPoint point);
54  void moveTopLeft(QPointF point) { moveTopLeft(point.toPoint()); }
55  void transform(QRect rectangle, bool smoothTransform);
56  void transform(QRectF rectangle, bool smoothTransform) { transform(rectangle.toRect(), smoothTransform); }
57  BitmapImage transformed(QRect selection, QTransform transform, bool smoothTransform);
58  BitmapImage transformed(QRect rectangle, bool smoothTransform);
59  BitmapImage transformed(QRectF rectangle, bool smoothTransform) { return transformed(rectangle.toRect(), smoothTransform); }
60 
61  bool contains(QPoint P) { return mBounds.contains(P); }
62  bool contains(QPointF P) { return contains(P.toPoint()); }
63  void autoCrop();
64 
65  QRgb pixel(int x, int y);
66  QRgb pixel(QPoint p);
67  void setPixel(int x, int y, QRgb color);
68  void setPixel(QPoint p, QRgb color);
69  void fillNonAlphaPixels(const QRgb color);
70 
71  QRgb constScanLine(int x, int y) const;
72  void scanLine(int x, int y, QRgb color);
73  void clear();
74  void clear(QRect rectangle);
75  void clear(QRectF rectangle) { clear(rectangle.toRect()); }
76 
77  static inline bool compareColor(QRgb newColor, QRgb oldColor, int tolerance, QHash<QRgb, bool> *cache);
78  static void floodFill(BitmapImage* targetImage, QRect cameraRect, QPoint point, QRgb newColor, int tolerance);
79 
80  void drawLine(QPointF P1, QPointF P2, QPen pen, QPainter::CompositionMode cm, bool antialiasing);
81  void drawRect(QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
82  void drawEllipse(QRectF rectangle, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
83  void drawPath(QPainterPath path, QPen pen, QBrush brush, QPainter::CompositionMode cm, bool antialiasing);
84 
85  QPoint topLeft() { autoCrop(); return mBounds.topLeft(); }
86  QPoint topRight() { autoCrop(); return mBounds.topRight(); }
87  QPoint bottomLeft() { autoCrop(); return mBounds.bottomLeft(); }
88  QPoint bottomRight() { autoCrop(); return mBounds.bottomRight(); }
89  int left() { autoCrop(); return mBounds.left(); }
90  int right() { autoCrop(); return mBounds.right(); }
91  int top() { autoCrop(); return mBounds.top(); }
92  int bottom() { autoCrop(); return mBounds.bottom(); }
93  int width() { autoCrop(); return mBounds.width(); }
94  int height() { autoCrop(); return mBounds.height(); }
95  QSize size() { autoCrop(); return mBounds.size(); }
96 
97  // peg bar alignment
98  PegbarResult findLeft(QRectF rect, int grayValue);
99  PegbarResult findTop(QRectF rect, int grayValue);
100 
101 
102  QRect& bounds() { autoCrop(); return mBounds; }
103 
114  bool isMinimallyBounded() const { return mMinBound; }
115  void enableAutoCrop(bool b) { mEnableAutoCrop = b; }
116 
117  Status writeFile(const QString& filename);
118 
119 protected:
120  void updateBounds(QRect rectangle);
121  void extend(const QPoint& p);
122  void extend(QRect rectangle);
123 
125  void setCompositionModeBounds(QRect sourceBounds, bool isSourceMinBounds, QPainter::CompositionMode cm);
126 
127 private:
128  std::unique_ptr<QImage> mImage;
129  QRect mBounds;
130 
132  bool mMinBound = true;
133  bool mEnableAutoCrop = false;
134 };
135 
136 #endif
QSize size() const const
QPoint topRight() const const
QRect toRect() const const
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.
bool mMinBound
Definition: bitmapimage.h:132
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
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:114