Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qimage.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QIMAGE_H
5#define QIMAGE_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtGui/qcolor.h>
9#include <QtGui/qrgb.h>
10#include <QtGui/qpaintdevice.h>
11#include <QtGui/qpixelformat.h>
12#include <QtGui/qtransform.h>
13#include <QtCore/qbytearray.h>
14#include <QtCore/qbytearrayview.h>
15#include <QtCore/qrect.h>
16#include <QtCore/qstring.h>
17#include <QtCore/qcontainerfwd.h>
18
19#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
21#endif
22
24
25
26class QColorSpace;
27class QColorTransform;
28class QIODevice;
29class QTransform;
30class QVariant;
31
32struct QImageData;
33
34typedef void (*QImageCleanupFunction)(void*);
35
36class Q_GUI_EXPORT QImage : public QPaintDevice
37{
39public:
40 enum InvertMode { InvertRgb, InvertRgba };
84
85 QImage() noexcept;
86 QImage(const QSize &size, Format format);
87 QImage(int width, int height, Format format);
88 QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
89 QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
90 QImage(uchar *data, int width, int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
91 QImage(const uchar *data, int width, int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
92
93#ifndef QT_NO_IMAGEFORMAT_XPM
94 explicit QImage(const char * const xpm[]);
95#endif
96 explicit QImage(const QString &fileName, const char *format = nullptr);
97
98 QImage(const QImage &);
99 QImage(QImage &&other) noexcept
100 : QPaintDevice(), d(std::exchange(other.d, nullptr))
101 {}
102 ~QImage();
103
104 QImage &operator=(const QImage &);
105 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QImage)
107 { qt_ptr_swap(d, other.d); }
108
109 bool isNull() const;
110
111 int devType() const override;
112
113 bool operator==(const QImage &) const;
114 bool operator!=(const QImage &) const;
115 operator QVariant() const;
116 void detach();
117 bool isDetached() const;
118
119 [[nodiscard]] QImage copy(const QRect &rect = QRect()) const;
120 [[nodiscard]] QImage copy(int x, int y, int w, int h) const
121 { return copy(QRect(x, y, w, h)); }
122
123 Format format() const;
124
125 [[nodiscard]] QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const &
126 { return convertToFormat_helper(f, flags); }
127 [[nodiscard]] QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) &&
128 {
129 if (convertToFormat_inplace(f, flags))
130 return std::move(*this);
131 else
132 return convertToFormat_helper(f, flags);
133 }
134 [[nodiscard]] QImage convertToFormat(Format f, const QList<QRgb> &colorTable,
135 Qt::ImageConversionFlags flags = Qt::AutoColor) const;
136
137 bool reinterpretAsFormat(Format f);
138 [[nodiscard]] QImage convertedTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const &
139 { return convertToFormat(f, flags); }
140 [[nodiscard]] QImage convertedTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) &&
141 { return convertToFormat(f, flags); }
142 void convertTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor);
143
144 int width() const;
145 int height() const;
146 QSize size() const;
147 QRect rect() const;
148
149 int depth() const;
150 int colorCount() const;
151 int bitPlaneCount() const;
152
153 QRgb color(int i) const;
154 void setColor(int i, QRgb c);
155 void setColorCount(int);
156
157 bool allGray() const;
158 bool isGrayscale() const;
159
160 uchar *bits();
161 const uchar *bits() const;
162 const uchar *constBits() const;
163
164 qsizetype sizeInBytes() const;
165
166 uchar *scanLine(int);
167 const uchar *scanLine(int) const;
168 const uchar *constScanLine(int) const;
169 qsizetype bytesPerLine() const;
170
171 bool valid(int x, int y) const;
172 bool valid(const QPoint &pt) const;
173
174 int pixelIndex(int x, int y) const;
175 int pixelIndex(const QPoint &pt) const;
176
177 QRgb pixel(int x, int y) const;
178 QRgb pixel(const QPoint &pt) const;
179
180 void setPixel(int x, int y, uint index_or_rgb);
181 void setPixel(const QPoint &pt, uint index_or_rgb);
182
183 QColor pixelColor(int x, int y) const;
184 QColor pixelColor(const QPoint &pt) const;
185
186 void setPixelColor(int x, int y, const QColor &c);
187 void setPixelColor(const QPoint &pt, const QColor &c);
188
189 QList<QRgb> colorTable() const;
190 void setColorTable(const QList<QRgb> &colors);
191
192 qreal devicePixelRatio() const;
193 void setDevicePixelRatio(qreal scaleFactor);
194 QSizeF deviceIndependentSize() const;
195
196 void fill(uint pixel);
197 void fill(const QColor &color);
199
200
201 bool hasAlphaChannel() const;
202 void setAlphaChannel(const QImage &alphaChannel);
203 [[nodiscard]] QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
204#ifndef QT_NO_IMAGE_HEURISTIC_MASK
205 [[nodiscard]] QImage createHeuristicMask(bool clipTight = true) const;
206#endif
208
209 [[nodiscard]] QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
211 { return scaled(QSize(w, h), aspectMode, mode); }
212 [[nodiscard]] QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
214 [[nodiscard]] QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
217 static QTransform trueMatrix(const QTransform &, int w, int h);
218
219 [[nodiscard]] QImage mirrored(bool horizontally = false, bool vertically = true) const &
220 { return mirrored_helper(horizontally, vertically); }
221 [[nodiscard]] QImage mirrored(bool horizontally = false, bool vertically = true) &&
222 { mirrored_inplace(horizontally, vertically); return std::move(*this); }
223 [[nodiscard]] QImage rgbSwapped() const &
224 { return rgbSwapped_helper(); }
225 [[nodiscard]] QImage rgbSwapped() &&
226 { rgbSwapped_inplace(); return std::move(*this); }
227 void mirror(bool horizontally = false, bool vertically = true)
228 { mirrored_inplace(horizontally, vertically); }
229 void rgbSwap()
230 { rgbSwapped_inplace(); }
231 void invertPixels(InvertMode = InvertRgb);
232
233 QColorSpace colorSpace() const;
234 [[nodiscard]] QImage convertedToColorSpace(const QColorSpace &colorSpace) const;
235 [[nodiscard]] QImage convertedToColorSpace(const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor) const;
236 void convertToColorSpace(const QColorSpace &colorSpace);
237 void convertToColorSpace(const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor);
238 void setColorSpace(const QColorSpace &colorSpace);
239
240 QImage colorTransformed(const QColorTransform &transform) const &;
241 QImage colorTransformed(const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor) const &;
242 QImage colorTransformed(const QColorTransform &transform) &&;
243 QImage colorTransformed(const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor) &&;
244 void applyColorTransform(const QColorTransform &transform);
245 void applyColorTransform(const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags = Qt::AutoColor);
246
247 bool load(QIODevice *device, const char *format);
248 bool load(const QString &fileName, const char *format = nullptr);
249 bool loadFromData(QByteArrayView data, const char *format = nullptr);
250 bool loadFromData(const uchar *buf, int len, const char *format = nullptr); // ### Qt 7: qsizetype
251 bool loadFromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop
252 { return loadFromData(QByteArrayView(data), format); }
253
254 bool save(const QString &fileName, const char *format = nullptr, int quality = -1) const;
255 bool save(QIODevice *device, const char *format = nullptr, int quality = -1) const;
256
257 static QImage fromData(QByteArrayView data, const char *format = nullptr);
258 static QImage fromData(const uchar *data, int size, const char *format = nullptr); // ### Qt 7: qsizetype
259 static QImage fromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop
260 { return fromData(QByteArrayView(data), format); }
261
262 qint64 cacheKey() const;
263
264 QPaintEngine *paintEngine() const override;
265
266 // Auxiliary data
267 int dotsPerMeterX() const;
268 int dotsPerMeterY() const;
269 void setDotsPerMeterX(int);
270 void setDotsPerMeterY(int);
271 QPoint offset() const;
272 void setOffset(const QPoint&);
273
274 QStringList textKeys() const;
275 QString text(const QString &key = QString()) const;
276 void setText(const QString &key, const QString &value);
277
278 QPixelFormat pixelFormat() const noexcept;
279 static QPixelFormat toPixelFormat(QImage::Format format) noexcept;
280 static QImage::Format toImageFormat(QPixelFormat format) noexcept;
281
282 // Platform specific conversion functions
283#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
284 CGImageRef toCGImage() const Q_DECL_CF_RETURNS_RETAINED;
285#endif
286#if defined(Q_OS_WIN) || defined(Q_QDOC)
287 HBITMAP toHBITMAP() const;
288 HICON toHICON(const QImage &mask = {}) const;
289 static QImage fromHBITMAP(HBITMAP hbitmap);
290 static QImage fromHICON(HICON icon);
291#endif
292
293protected:
294 virtual int metric(PaintDeviceMetric metric) const override;
295 QImage mirrored_helper(bool horizontal, bool vertical) const;
297 void mirrored_inplace(bool horizontal, bool vertical);
299 QImage convertToFormat_helper(Format format, Qt::ImageConversionFlags flags) const;
300 bool convertToFormat_inplace(Format format, Qt::ImageConversionFlags flags);
301 QImage smoothScaled(int w, int h) const;
302
303 void detachMetadata(bool invalidateCache = false);
304
305private:
306 QImageData *d;
307
310 friend class QPixmapCacheEntry;
311 friend struct QImageData;
312
313public:
315 inline DataPtr &data_ptr() { return d; }
316};
317
318Q_DECLARE_SHARED(QImage)
319
320// Inline functions...
321
322inline bool QImage::valid(const QPoint &pt) const { return valid(pt.x(), pt.y()); }
323inline int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(pt.x(), pt.y());}
324inline QRgb QImage::pixel(const QPoint &pt) const { return pixel(pt.x(), pt.y()); }
325inline void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(pt.x(), pt.y(), index_or_rgb); }
326inline QColor QImage::pixelColor(const QPoint &pt) const { return pixelColor(pt.x(), pt.y()); }
327inline void QImage::setPixelColor(const QPoint &pt, const QColor &c) { setPixelColor(pt.x(), pt.y(), c); }
328
329// QImage stream functions
330
331#if !defined(QT_NO_DATASTREAM)
332Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
333Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
334#endif
335
336#ifndef QT_NO_DEBUG_STREAM
337Q_GUI_EXPORT QDebug operator<<(QDebug, const QImage &);
338#endif
339
340
342
343#endif // QIMAGE_H
IOBluetoothDevice * device
\inmodule QtCore
Definition qbytearray.h:57
The QColorSpace class provides a color space abstraction.
Definition qcolorspace.h:21
The QColorTransform class is a transformation between color spaces.
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore \reentrant
Definition qiodevice.h:34
\inmodule QtGui
Definition qimage.h:37
QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
Returns a copy of the image scaled to a rectangle defined by the given size according to the given as...
void mirror(bool horizontally=false, bool vertically=true)
Definition qimage.h:227
QImage createAlphaMask(Qt::ImageConversionFlags flags=Qt::AutoColor) const
QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.h:209
void setPixel(int x, int y, uint index_or_rgb)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2588
void setPixelColor(int x, int y, const QColor &c)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2788
QRgb pixel(int x, int y) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2493
QImage transformed(const QTransform &matrix, Qt::TransformationMode mode=Qt::FastTransformation) const
QImage copy(const QRect &rect=QRect()) const
Returns a sub-area of the image as a new image.
DataPtr & data_ptr()
Definition qimage.h:315
bool loadFromData(const QByteArray &data, const char *format=nullptr)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.h:251
void rgbSwap()
Definition qimage.h:229
QImage createHeuristicMask(bool clipTight=true) const
void rgbSwapped_inplace()
QSize size() const
Returns the size of the image, i.e.
int width() const
Returns the width of the image.
int height() const
Returns the height of the image.
void setColorTable(const QList< QRgb > &colors)
QImage createMaskFromColor(QRgb color, Qt::MaskMode mode=Qt::MaskInColor) const
Format
The following image formats are available in Qt.
Definition qimage.h:41
@ Format_Grayscale16
Definition qimage.h:70
@ Format_Alpha8
Definition qimage.h:65
@ Format_CMYK8888
Definition qimage.h:78
@ Format_RGBA8888
Definition qimage.h:59
@ Format_RGB30
Definition qimage.h:63
@ Format_RGB888
Definition qimage.h:55
@ Format_RGBA16FPx4
Definition qimage.h:73
@ Format_RGBA32FPx4_Premultiplied
Definition qimage.h:77
@ Format_RGB32
Definition qimage.h:46
@ Format_Invalid
Definition qimage.h:42
@ Format_RGB666
Definition qimage.h:51
@ Format_RGBX32FPx4
Definition qimage.h:75
@ Format_RGBA64_Premultiplied
Definition qimage.h:69
@ Format_ARGB6666_Premultiplied
Definition qimage.h:52
@ Format_ARGB8555_Premultiplied
Definition qimage.h:54
@ Format_RGB444
Definition qimage.h:56
@ Format_MonoLSB
Definition qimage.h:44
@ Format_RGBA8888_Premultiplied
Definition qimage.h:60
@ Format_ARGB8565_Premultiplied
Definition qimage.h:50
@ Format_RGB555
Definition qimage.h:53
@ Format_RGBA64
Definition qimage.h:68
@ Format_RGBA32FPx4
Definition qimage.h:76
@ Format_Mono
Definition qimage.h:43
@ Format_RGBA16FPx4_Premultiplied
Definition qimage.h:74
@ Format_RGBX64
Definition qimage.h:67
@ Format_A2BGR30_Premultiplied
Definition qimage.h:62
@ Format_RGBX16FPx4
Definition qimage.h:72
@ Format_Indexed8
Definition qimage.h:45
@ Format_BGR30
Definition qimage.h:61
@ Format_ARGB32_Premultiplied
Definition qimage.h:48
@ Format_A2RGB30_Premultiplied
Definition qimage.h:64
@ Format_ARGB4444_Premultiplied
Definition qimage.h:57
@ Format_RGB16
Definition qimage.h:49
@ Format_BGR888
Definition qimage.h:71
@ Format_ARGB32
Definition qimage.h:47
@ Format_RGBX8888
Definition qimage.h:58
@ Format_Grayscale8
Definition qimage.h:66
QImage convertedTo(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) &&
Definition qimage.h:140
QImage mirrored(bool horizontally=false, bool vertically=true) &&
Definition qimage.h:221
QImage mirrored(bool horizontally=false, bool vertically=true) const &
Definition qimage.h:219
QColor pixelColor(int x, int y) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2709
QImage rgbSwapped() const &
Definition qimage.h:223
QImage scaledToHeight(int h, Qt::TransformationMode mode=Qt::FastTransformation) const
Returns a scaled copy of the image.
static QImage fromData(const QByteArray &data, const char *format=nullptr)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.h:259
QImage(QImage &&other) noexcept
Move-constructs a QImage instance, making it point at the same object that other was pointing to.
Definition qimage.h:99
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) &&
Definition qimage.h:127
QRect rect() const
Returns the enclosing rectangle (0, 0, width(), height()) of the image.
QImageData * DataPtr
Definition qimage.h:314
QImage rgbSwapped_helper() const
int colorCount() const
Returns the depth of the image.
QImage rgbSwapped() &&
Definition qimage.h:225
int pixelIndex(int x, int y) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.cpp:2452
QImage convertedTo(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:138
InvertMode
This enum type is used to describe how pixel values should be inverted in the invertPixels() function...
Definition qimage.h:40
QImage copy(int x, int y, int w, int h) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qimage.h:120
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:125
int depth() const
\inmodule QtGui
\inmodule QtGui
\inmodule QtCore\reentrant
Definition qpoint.h:25
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:130
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:135
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:208
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
\inmodule QtCore
Definition qvariant.h:65
Format
Definition ddsheader.h:14
p1 load("image.bmp")
QString text
rect
[4]
Combined button and popup list for selecting options.
@ AutoColor
Definition qnamespace.h:478
TransformationMode
@ FastTransformation
AspectRatioMode
@ IgnoreAspectRatio
GlobalColor
Definition qnamespace.h:27
@ MaskInColor
static jboolean copy(JNIEnv *, jobject)
#define Q_DECL_CF_RETURNS_RETAINED
constexpr bool operator!=(const timespec &t1, const timespec &t2)
#define Q_FORWARD_DECLARE_MUTABLE_CG_TYPE(type)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
void(* QImageCleanupFunction)(void *)
Definition qimage.h:34
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QImage &)
Definition qimage.cpp:3973
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QImage &)
[0]
Definition qimage.cpp:3947
static QByteArray cacheKey(Args &&...args)
GLint GLint GLint GLint GLint x
[0]
GLenum mode
GLuint64 key
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLint GLsizei width
GLuint color
[2]
GLenum GLuint GLenum GLsizei const GLchar * buf
GLbitfield flags
GLenum GLuint GLintptr offset
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint GLsizei GLsizei GLenum format
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLuint GLenum GLenum transform
GLdouble s
[6]
Definition qopenglext.h:235
const GLubyte * c
GLuint GLenum matrix
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
GLenum GLsizei len
static QT_BEGIN_NAMESPACE const QRgb colors[][14]
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
constexpr void qt_ptr_swap(T *&lhs, T *&rhs) noexcept
Definition qswap.h:29
#define Q_ENUM(x)
#define Q_GADGET
unsigned char uchar
Definition qtypes.h:32
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
long long qint64
Definition qtypes.h:60
double qreal
Definition qtypes.h:187
QImage scaled(const QImage &image)
[0]
ba fill(true)
QSharedPointer< T > other(t)
[5]
this swap(other)
myFilter setColor(QColor(128, 0, 0))
insertRed setText("insert red text")