OSDN Git Service

zero-copy QX11Data::copyXImageToQImage() for some cases
[kde/Katie.git] / src / gui / image / qimage.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Copyright (C) 2016 Ivailo Monev
5 **
6 ** This file is part of the QtGui module of the Katie Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 **
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser
12 ** General Public License version 2.1 as published by the Free Software
13 ** Foundation and appearing in the file LICENSE.LGPL included in the
14 ** packaging of this file.  Please review the following information to
15 ** ensure the GNU Lesser General Public License version 2.1 requirements
16 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** $QT_END_LICENSE$
19 **
20 ****************************************************************************/
21
22 #ifndef QIMAGE_H
23 #define QIMAGE_H
24
25 #include <QtCore/qbytearray.h>
26 #include <QtCore/qrect.h>
27 #include <QtCore/qstring.h>
28 #include <QtGui/qtransform.h>
29 #include <QtGui/qpaintdevice.h>
30 #include <QtGui/qrgb.h>
31
32
33 QT_BEGIN_NAMESPACE
34
35 class QColor;
36 class QIODevice;
37 class QVariant;
38 template <class T> class QVector;
39 struct QImageData;
40
41 class Q_GUI_EXPORT QImage : public QPaintDevice
42 {
43 public:
44     enum InvertMode { InvertRgb, InvertRgba };
45     enum Format {
46         Format_Invalid,
47         Format_Mono,
48         Format_MonoLSB,
49         Format_RGB32,
50         Format_ARGB32,
51         Format_ARGB32_Premultiplied,
52         Format_RGB16,
53         NImageFormats
54     };
55
56     QImage();
57     QImage(const QSize &size, Format format);
58     QImage(int width, int height, Format format);
59     QImage(uchar *data, int width, int height, Format format);
60     QImage(const uchar *data, int width, int height, Format format);
61     QImage(uchar *data, int width, int height, int bytesPerLine, Format format);
62     QImage(const uchar *data, int width, int height, int bytesPerLine, Format format);
63
64     explicit QImage(const QString &fileName, const char *format = nullptr);
65 #ifndef QT_NO_CAST_FROM_ASCII
66     explicit QImage(const char *fileName, const char *format = nullptr);
67 #endif
68
69     QImage(const QImage &);
70     ~QImage();
71
72     QImage &operator=(const QImage &);
73     inline QImage &operator=(QImage &&other)
74     { qSwap(d, other.d); return *this; }
75     inline void swap(QImage &other) { qSwap(d, other.d); }
76
77     bool isNull() const;
78
79     int devType() const;
80
81     bool operator==(const QImage &) const;
82     bool operator!=(const QImage &) const;
83     operator QVariant() const;
84
85     QImage copy(const QRect &rect = QRect()) const;
86     inline QImage copy(int x, int y, int w, int h) const
87         { return copy(QRect(x, y, w, h)); }
88
89     Format format() const;
90     static Format systemFormat();
91
92     QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;
93
94     int width() const;
95     int height() const;
96     QSize size() const;
97     QRect rect() const;
98
99     int depth() const;
100     int bitPlaneCount() const;
101
102     QRgb color(int i) const;
103     void setColor(int i, QRgb c);
104
105     bool allGray() const;
106     bool isGrayscale() const;
107
108     uchar *bits();
109     const uchar *bits() const;
110     const uchar *constBits() const;
111     int byteCount() const;
112
113     uchar *scanLine(int);
114     const uchar *scanLine(int) const;
115     const uchar *constScanLine(int) const;
116     int bytesPerLine() const;
117
118     bool valid(int x, int y) const;
119     inline bool valid(const QPoint &pt) const
120         { return valid(pt.x(), pt.y()); }
121
122     QRgb pixel(int x, int y) const;
123     inline QRgb pixel(const QPoint &pt) const
124         { return pixel(pt.x(), pt.y()); }
125
126     void setPixel(int x, int y, QRgb rgb);
127     inline void setPixel(const QPoint &pt, QRgb rgb)
128         { setPixel(pt.x(), pt.y(), rgb); }
129
130     QVector<QRgb> colorTable() const;
131     void setColorTable(const QVector<QRgb> &colors);
132
133     void fill(uint pixel);
134     void fill(const QColor &color);
135     void fill(Qt::GlobalColor color);
136
137
138     bool hasAlphaChannel() const;
139     void setAlphaChannel(const QImage &alphaChannel);
140     QImage alphaChannel() const;
141     QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
142 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
143     QImage createHeuristicMask(bool clipTight = true) const;
144 #endif
145     QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;
146
147     inline QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
148                         Qt::TransformationMode mode = Qt::FastTransformation) const
149         { return scaled(QSize(w, h), aspectMode, mode); }
150     QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
151                  Qt::TransformationMode mode = Qt::FastTransformation) const;
152     QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
153     QImage scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
154     QImage transformed(const QMatrix &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
155     static QMatrix trueMatrix(const QMatrix &, int w, int h);
156     QImage transformed(const QTransform &matrix, Qt::TransformationMode mode = Qt::FastTransformation) const;
157     static QTransform trueMatrix(const QTransform &, int w, int h);
158     QImage mirrored(bool horizontally = false, bool vertically = true) const;
159     QImage rgbSwapped() const;
160     void invertPixels(InvertMode = InvertRgb);
161
162
163     bool load(QIODevice *device, const char* format);
164     bool load(const QString &fileName, const char* format = nullptr);
165     bool loadFromData(const char *buf, int len, const char *format = nullptr);
166     inline bool loadFromData(const QByteArray &data, const char* aformat = nullptr)
167         { return loadFromData(data.constData(), data.size(), aformat); }
168
169     bool save(const QString &fileName, const char* format = nullptr, int quality=-1) const;
170     bool save(QIODevice *device, const char* format = nullptr, int quality=-1) const;
171
172     static QImage fromData(const char *data, int size, const char *format = nullptr);
173     inline static QImage fromData(const QByteArray &data, const char *format = nullptr)
174         { return fromData(data.constData(), data.size(), format); }
175
176     qint64 cacheKey() const;
177
178     QPaintEngine *paintEngine() const;
179
180     // Auxiliary data
181     int dotsPerMeterX() const;
182     int dotsPerMeterY() const;
183     void setDotsPerMeterX(int);
184     void setDotsPerMeterY(int);
185
186 protected:
187     virtual int metric(PaintDeviceMetric metric) const;
188
189 private:
190     void detach();
191
192     QImageData *d;
193
194     friend class QPixmap;
195     friend class QPainter;
196     friend class QPixmapData;
197     friend class QSpanData;
198     friend class QPngHandler;
199     friend class QKatHandler;
200     friend class QX11Data;
201 #if !defined(QT_NO_DATASTREAM)
202     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
203     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
204 #endif
205 };
206
207 Q_DECLARE_TYPEINFO(QImage, Q_MOVABLE_TYPE);
208
209 // QImage stream functions
210
211 #if !defined(QT_NO_DATASTREAM)
212 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
213 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
214 #endif
215
216 QT_END_NAMESPACE
217
218
219 #endif // QIMAGE_H