Qt 6.x
The Qt SDK
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 };
41 enum Format {
78#ifndef Q_QDOC
79 NImageFormats
80#endif
81 };
83
84 QImage() noexcept;
85 QImage(const QSize &size, Format format);
86 QImage(int width, int height, Format format);
87 QImage(uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
88 QImage(const uchar *data, int width, int height, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
89 QImage(uchar *data, int width, int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
90 QImage(const uchar *data, int width, int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction = nullptr, void *cleanupInfo = nullptr);
91
92#ifndef QT_NO_IMAGEFORMAT_XPM
93 explicit QImage(const char * const xpm[]);
94#endif
95 explicit QImage(const QString &fileName, const char *format = nullptr);
96
97 QImage(const QImage &);
98 QImage(QImage &&other) noexcept
99 : QPaintDevice(), d(std::exchange(other.d, nullptr))
100 {}
101 ~QImage();
102
103 QImage &operator=(const QImage &);
104 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QImage)
105 void swap(QImage &other) noexcept
106 { qt_ptr_swap(d, other.d); }
107
108 bool isNull() const;
109
110 int devType() const override;
111
112 bool operator==(const QImage &) const;
113 bool operator!=(const QImage &) const;
114 operator QVariant() const;
115 void detach();
116 bool isDetached() const;
117
118 [[nodiscard]] QImage copy(const QRect &rect = QRect()) const;
119 [[nodiscard]] QImage copy(int x, int y, int w, int h) const
120 { return copy(QRect(x, y, w, h)); }
121
122 Format format() const;
123
124 [[nodiscard]] QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const &
125 { return convertToFormat_helper(f, flags); }
126 [[nodiscard]] QImage convertToFormat(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) &&
127 {
128 if (convertToFormat_inplace(f, flags))
129 return std::move(*this);
130 else
131 return convertToFormat_helper(f, flags);
132 }
133 [[nodiscard]] QImage convertToFormat(Format f, const QList<QRgb> &colorTable,
134 Qt::ImageConversionFlags flags = Qt::AutoColor) const;
135
136 bool reinterpretAsFormat(Format f);
137 [[nodiscard]] QImage convertedTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) const &
138 { return convertToFormat(f, flags); }
139 [[nodiscard]] QImage convertedTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor) &&
140 { return convertToFormat(f, flags); }
141 void convertTo(Format f, Qt::ImageConversionFlags flags = Qt::AutoColor);
142
143 int width() const;
144 int height() const;
145 QSize size() const;
146 QRect rect() const;
147
148 int depth() const;
149 int colorCount() const;
150 int bitPlaneCount() const;
151
152 QRgb color(int i) const;
153 void setColor(int i, QRgb c);
154 void setColorCount(int);
155
156 bool allGray() const;
157 bool isGrayscale() const;
158
159 uchar *bits();
160 const uchar *bits() const;
161 const uchar *constBits() const;
162
163 qsizetype sizeInBytes() const;
164
165 uchar *scanLine(int);
166 const uchar *scanLine(int) const;
167 const uchar *constScanLine(int) const;
168 qsizetype bytesPerLine() const;
169
170 bool valid(int x, int y) const;
171 bool valid(const QPoint &pt) const;
172
173 int pixelIndex(int x, int y) const;
174 int pixelIndex(const QPoint &pt) const;
175
176 QRgb pixel(int x, int y) const;
177 QRgb pixel(const QPoint &pt) const;
178
179 void setPixel(int x, int y, uint index_or_rgb);
180 void setPixel(const QPoint &pt, uint index_or_rgb);
181
182 QColor pixelColor(int x, int y) const;
183 QColor pixelColor(const QPoint &pt) const;
184
185 void setPixelColor(int x, int y, const QColor &c);
186 void setPixelColor(const QPoint &pt, const QColor &c);
187
188 QList<QRgb> colorTable() const;
190
191 qreal devicePixelRatio() const;
192 void setDevicePixelRatio(qreal scaleFactor);
193 QSizeF deviceIndependentSize() const;
194
195 void fill(uint pixel);
196 void fill(const QColor &color);
198
199
200 bool hasAlphaChannel() const;
201 void setAlphaChannel(const QImage &alphaChannel);
202 [[nodiscard]] QImage createAlphaMask(Qt::ImageConversionFlags flags = Qt::AutoColor) const;
203#ifndef QT_NO_IMAGE_HEURISTIC_MASK
204 [[nodiscard]] QImage createHeuristicMask(bool clipTight = true) const;
205#endif
207
208 [[nodiscard]] QImage scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
210 { return scaled(QSize(w, h), aspectMode, mode); }
211 [[nodiscard]] QImage scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
213 [[nodiscard]] QImage scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
216 static QTransform trueMatrix(const QTransform &, int w, int h);
217
218 [[nodiscard]] QImage mirrored(bool horizontally = false, bool vertically = true) const &
219 { return mirrored_helper(horizontally, vertically); }
220 [[nodiscard]] QImage mirrored(bool horizontally = false, bool vertically = true) &&
221 { mirrored_inplace(horizontally, vertically); return std::move(*this); }
222 [[nodiscard]] QImage rgbSwapped() const &
223 { return rgbSwapped_helper(); }
224 [[nodiscard]] QImage rgbSwapped() &&
225 { rgbSwapped_inplace(); return std::move(*this); }
226 void mirror(bool horizontally = false, bool vertically = true)
227 { mirrored_inplace(horizontally, vertically); }
228 void rgbSwap()
229 { rgbSwapped_inplace(); }
230 void invertPixels(InvertMode = InvertRgb);
231
232 QColorSpace colorSpace() const;
233 [[nodiscard]] QImage convertedToColorSpace(const QColorSpace &) const;
234 void convertToColorSpace(const QColorSpace &);
235 void setColorSpace(const QColorSpace &);
236
237 QImage colorTransformed(const QColorTransform &transform) const &;
238 QImage colorTransformed(const QColorTransform &transform) &&;
239 void applyColorTransform(const QColorTransform &transform);
240
241 bool load(QIODevice *device, const char *format);
242 bool load(const QString &fileName, const char *format = nullptr);
243 bool loadFromData(QByteArrayView data, const char *format = nullptr);
244 bool loadFromData(const uchar *buf, int len, const char *format = nullptr); // ### Qt 7: qsizetype
245 bool loadFromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop
246 { return loadFromData(QByteArrayView(data), format); }
247
248 bool save(const QString &fileName, const char *format = nullptr, int quality = -1) const;
249 bool save(QIODevice *device, const char *format = nullptr, int quality = -1) const;
250
251 static QImage fromData(QByteArrayView data, const char *format = nullptr);
252 static QImage fromData(const uchar *data, int size, const char *format = nullptr); // ### Qt 7: qsizetype
253 static QImage fromData(const QByteArray &data, const char *format = nullptr) // ### Qt 7: drop
254 { return fromData(QByteArrayView(data), format); }
255
256 qint64 cacheKey() const;
257
258 QPaintEngine *paintEngine() const override;
259
260 // Auxiliary data
261 int dotsPerMeterX() const;
262 int dotsPerMeterY() const;
263 void setDotsPerMeterX(int);
264 void setDotsPerMeterY(int);
265 QPoint offset() const;
266 void setOffset(const QPoint&);
267
268 QStringList textKeys() const;
269 QString text(const QString &key = QString()) const;
270 void setText(const QString &key, const QString &value);
271
272 QPixelFormat pixelFormat() const noexcept;
273 static QPixelFormat toPixelFormat(QImage::Format format) noexcept;
274 static QImage::Format toImageFormat(QPixelFormat format) noexcept;
275
276 // Platform specific conversion functions
277#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
278 CGImageRef toCGImage() const Q_DECL_CF_RETURNS_RETAINED;
279#endif
280#if defined(Q_OS_WIN) || defined(Q_QDOC)
281 HBITMAP toHBITMAP() const;
282 HICON toHICON(const QImage &mask = {}) const;
283 static QImage fromHBITMAP(HBITMAP hbitmap);
284 static QImage fromHICON(HICON icon);
285#endif
286
287protected:
288 virtual int metric(PaintDeviceMetric metric) const override;
289 QImage mirrored_helper(bool horizontal, bool vertical) const;
291 void mirrored_inplace(bool horizontal, bool vertical);
293 QImage convertToFormat_helper(Format format, Qt::ImageConversionFlags flags) const;
294 bool convertToFormat_inplace(Format format, Qt::ImageConversionFlags flags);
295 QImage smoothScaled(int w, int h) const;
296
297 void detachMetadata(bool invalidateCache = false);
298
299private:
300 QImageData *d;
301
304 friend class QPixmapCacheEntry;
305 friend struct QImageData;
306
307public:
309 inline DataPtr &data_ptr() { return d; }
310};
311
312Q_DECLARE_SHARED(QImage)
313
314// Inline functions...
315
316inline bool QImage::valid(const QPoint &pt) const { return valid(pt.x(), pt.y()); }
317inline int QImage::pixelIndex(const QPoint &pt) const { return pixelIndex(pt.x(), pt.y());}
318inline QRgb QImage::pixel(const QPoint &pt) const { return pixel(pt.x(), pt.y()); }
319inline void QImage::setPixel(const QPoint &pt, uint index_or_rgb) { setPixel(pt.x(), pt.y(), index_or_rgb); }
320inline QColor QImage::pixelColor(const QPoint &pt) const { return pixelColor(pt.x(), pt.y()); }
321inline void QImage::setPixelColor(const QPoint &pt, const QColor &c) { setPixelColor(pt.x(), pt.y(), c); }
322
323// QImage stream functions
324
325#if !defined(QT_NO_DATASTREAM)
326Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QImage &);
327Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QImage &);
328#endif
329
330#ifndef QT_NO_DEBUG_STREAM
331Q_GUI_EXPORT QDebug operator<<(QDebug, const QImage &);
332#endif
333
334
336
337#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:30
\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:226
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:208
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:2573
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:2770
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:2478
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:309
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:245
void rgbSwap()
Definition qimage.h:228
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_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:139
QImage mirrored(bool horizontally=false, bool vertically=true) &&
Definition qimage.h:220
QImage mirrored(bool horizontally=false, bool vertically=true) const &
Definition qimage.h:218
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:2691
QImage rgbSwapped() const &
Definition qimage.h:222
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:253
QImage(QImage &&other) noexcept
Move-constructs a QImage instance, making it point at the same object that other was pointing to.
Definition qimage.h:98
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) &&
Definition qimage.h:126
QRect rect() const
Returns the enclosing rectangle (0, 0, width(), height()) of the image.
QImageData * DataPtr
Definition qimage.h:308
QImage rgbSwapped_helper() const
int colorCount() const
Returns the depth of the image.
QImage rgbSwapped() &&
Definition qimage.h:224
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:2437
QImage convertedTo(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:137
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:119
QImage convertToFormat(Format f, Qt::ImageConversionFlags flags=Qt::AutoColor) const &
Definition qimage.h:124
int depth() const
Definition qlist.h:74
QPaintDevice() noexcept
qreal devicePixelRatio() const
virtual int devType() const
virtual int metric(PaintDeviceMetric metric) const
virtual QPaintEngine * paintEngine() const =0
\inmodule QtGui
\inmodule QtGui
\inmodule QtCore\reentrant
Definition qpoint.h:23
constexpr int x() const noexcept
Returns the x coordinate of this point.
Definition qpoint.h:127
constexpr int y() const noexcept
Returns the y coordinate of this point.
Definition qpoint.h:132
\inmodule QtCore\reentrant
Definition qrect.h:30
\inmodule QtCore
Definition qsize.h:207
\inmodule QtCore
Definition qsize.h:25
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
The QTransform class specifies 2D transformations of a coordinate system.
Definition qtransform.h:20
\inmodule QtCore
Definition qvariant.h:64
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:477
TransformationMode
@ FastTransformation
AspectRatioMode
@ IgnoreAspectRatio
GlobalColor
Definition qnamespace.h:26
@ 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:3955
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QImage &)
[0]
Definition qimage.cpp:3929
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]
GLfloat GLfloat f
GLint GLsizei width
GLenum GLuint GLenum GLsizei const GLchar * buf
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
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
const GLubyte * c
GLenum GLsizei len
GLuint GLenum matrix
GLenum GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const void * bits
GLdouble s
[6]
Definition qopenglext.h:235
static QT_BEGIN_NAMESPACE const QRgb colors[][14]
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1219
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
constexpr void qt_ptr_swap(T *&lhs, T *&rhs) noexcept
Definition qswap.h:43
#define Q_ENUM(x)
#define Q_GADGET
unsigned char uchar
Definition qtypes.h:27
ptrdiff_t qsizetype
Definition qtypes.h:70
unsigned int uint
Definition qtypes.h:29
long long qint64
Definition qtypes.h:55
double qreal
Definition qtypes.h:92
QImage scaled(const QImage &image)
[0]
ba fill(true)
QSharedPointer< T > other(t)
[5]
myFilter setColor(QColor(128, 0, 0))
insertRed setText("insert red text")