1 /****************************************************************************
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Copyright (C) 2016 Ivailo Monev
6 ** This file is part of the QtGui module of the Katie Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
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.
20 ****************************************************************************/
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>
38 template <class T> class QVector;
41 class Q_GUI_EXPORT QImage : public QPaintDevice
44 enum InvertMode { InvertRgb, InvertRgba };
51 Format_ARGB32_Premultiplied,
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);
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);
69 QImage(const QImage &);
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); }
81 bool operator==(const QImage &) const;
82 bool operator!=(const QImage &) const;
83 operator QVariant() const;
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)); }
89 Format format() const;
90 static Format systemFormat();
92 QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const Q_REQUIRED_RESULT;
100 int bitPlaneCount() const;
102 QRgb color(int i) const;
103 void setColor(int i, QRgb c);
105 bool allGray() const;
106 bool isGrayscale() const;
109 const uchar *bits() const;
110 const uchar *constBits() const;
111 int byteCount() const;
113 uchar *scanLine(int);
114 const uchar *scanLine(int) const;
115 const uchar *constScanLine(int) const;
116 int bytesPerLine() const;
118 bool valid(int x, int y) const;
119 inline bool valid(const QPoint &pt) const
120 { return valid(pt.x(), pt.y()); }
122 QRgb pixel(int x, int y) const;
123 inline QRgb pixel(const QPoint &pt) const
124 { return pixel(pt.x(), pt.y()); }
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); }
130 QVector<QRgb> colorTable() const;
131 void setColorTable(const QVector<QRgb> &colors);
133 void fill(uint pixel);
134 void fill(const QColor &color);
135 void fill(Qt::GlobalColor color);
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;
145 QImage createMaskFromColor(QRgb color, Qt::MaskMode mode = Qt::MaskInColor) const;
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);
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); }
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;
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); }
176 qint64 cacheKey() const;
178 QPaintEngine *paintEngine() const;
181 int dotsPerMeterX() const;
182 int dotsPerMeterY() const;
183 void setDotsPerMeterX(int);
184 void setDotsPerMeterY(int);
187 virtual int metric(PaintDeviceMetric metric) const;
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 &);
207 Q_DECLARE_TYPEINFO(QImage, Q_MOVABLE_TYPE);
209 // QImage stream functions
211 #if !defined(QT_NO_DATASTREAM)
212 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
213 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);