QtBase  v6.3.1
qpixmap.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QPIXMAP_H
41 #define QPIXMAP_H
42 
43 #include <QtGui/qtguiglobal.h>
44 #include <QtGui/qpaintdevice.h>
45 #include <QtGui/qcolor.h>
46 #include <QtCore/qnamespace.h>
47 #include <QtCore/qstring.h> // char*->QString conversion
48 #include <QtCore/qsharedpointer.h>
49 #include <QtGui/qimage.h>
50 #include <QtGui/qtransform.h>
51 
53 
54 
55 class QImageWriter;
56 class QImageReader;
57 class QColor;
58 class QVariant;
59 class QPlatformPixmap;
61 
62 class Q_GUI_EXPORT QPixmap : public QPaintDevice
63 {
64 public:
65  QPixmap();
66  explicit QPixmap(QPlatformPixmap *data);
67  QPixmap(int w, int h);
68  explicit QPixmap(const QSize &);
69  QPixmap(const QString& fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
70 #ifndef QT_NO_IMAGEFORMAT_XPM
71  explicit QPixmap(const char * const xpm[]);
72 #endif
73  QPixmap(const QPixmap &);
74  QPixmap(QPixmap &&other) noexcept : QPaintDevice(), data(std::move(other.data)) {}
75  ~QPixmap();
76 
77  QPixmap &operator=(const QPixmap &);
79  inline void swap(QPixmap &other) noexcept
80  { data.swap(other.data); }
81  bool operator==(const QPixmap &) const = delete;
82  bool operator!=(const QPixmap &) const = delete;
83 
84  operator QVariant() const;
85 
86  bool isNull() const;
87  int devType() const override;
88 
89  int width() const;
90  int height() const;
91  QSize size() const;
92  QRect rect() const;
93  int depth() const;
94 
95  static int defaultDepth();
96 
97  void fill(const QColor &fillColor = Qt::white);
98 
99  QBitmap mask() const;
100  void setMask(const QBitmap &);
101 
102  qreal devicePixelRatio() const;
103  void setDevicePixelRatio(qreal scaleFactor);
104  QSizeF deviceIndependentSize() const;
105 
106  bool hasAlpha() const;
107  bool hasAlphaChannel() const;
108 
109 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
110  QBitmap createHeuristicMask(bool clipTight = true) const;
111 #endif
112  QBitmap createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode = Qt::MaskInColor) const;
113 
116  { return scaled(QSize(w, h), aspectMode, mode); }
119  QPixmap scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
120  QPixmap scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
122  static QTransform trueMatrix(const QTransform &m, int w, int h);
123 
124  QImage toImage() const;
125  static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
126  static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags = Qt::AutoColor);
127  static QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags = Qt::AutoColor)
128  {
129  return fromImageInPlace(image, flags);
130  }
131 
132  bool load(const QString& fileName, const char *format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
133  bool loadFromData(const uchar *buf, uint len, const char* format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
134  inline bool loadFromData(const QByteArray &data, const char* format = nullptr, Qt::ImageConversionFlags flags = Qt::AutoColor);
135  bool save(const QString& fileName, const char* format = nullptr, int quality = -1) const;
136  bool save(QIODevice* device, const char* format = nullptr, int quality = -1) const;
137 
138  bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags = Qt::AutoColor);
139 
140  inline QPixmap copy(int x, int y, int width, int height) const;
141  QPixmap copy(const QRect &rect = QRect()) const;
142 
143  inline void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed = nullptr);
144  void scroll(int dx, int dy, const QRect &rect, QRegion *exposed = nullptr);
145 
146  qint64 cacheKey() const;
147 
148  bool isDetached() const;
149  void detach();
150 
151  bool isQBitmap() const;
152 
153  QPaintEngine *paintEngine() const override;
154 
155  inline bool operator!() const { return isNull(); }
156 
157 protected:
158  int metric(PaintDeviceMetric) const override;
159  static QPixmap fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
160 
161 private:
163 
164  bool doImageIO(QImageWriter *io, int quality) const;
165 
166  QPixmap(const QSize &s, int type);
167  void doInit(int, int, int);
168  Q_DUMMY_COMPARISON_OPERATOR(QPixmap)
169  friend class QPlatformPixmap;
170  friend class QBitmap;
171  friend class QPaintDevice;
172  friend class QPainter;
173  friend class QOpenGLWidget;
174  friend class QWidgetPrivate;
175  friend class QRasterBuffer;
176 #if !defined(QT_NO_DATASTREAM)
177  friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
178 #endif
179 
180 public:
181  QPlatformPixmap* handle() const;
182 
183 public:
185  inline DataPtr &data_ptr() { return data; }
186 };
187 
189 
190 inline QPixmap QPixmap::copy(int ax, int ay, int awidth, int aheight) const
191 {
192  return copy(QRect(ax, ay, awidth, aheight));
193 }
194 
195 inline void QPixmap::scroll(int dx, int dy, int ax, int ay, int awidth, int aheight, QRegion *exposed)
196 {
197  scroll(dx, dy, QRect(ax, ay, awidth, aheight), exposed);
198 }
199 
200 inline bool QPixmap::loadFromData(const QByteArray &buf, const char *format,
201  Qt::ImageConversionFlags flags)
202 {
203  return loadFromData(reinterpret_cast<const uchar *>(buf.constData()), buf.size(), format, flags);
204 }
205 
206 
207 /*****************************************************************************
208  QPixmap stream functions
209 *****************************************************************************/
210 
211 #if !defined(QT_NO_DATASTREAM)
212 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPixmap &);
214 #endif
215 
216 #ifndef QT_NO_DEBUG_STREAM
217 Q_GUI_EXPORT QDebug operator<<(QDebug, const QPixmap &);
218 #endif
219 
221 
222 #endif // QPIXMAP_H
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:52
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
char * data()
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:70
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:73
The QImageReader class provides a format independent interface for reading images from files or other...
Definition: qimagereader.h:59
The QImageWriter class provides a format independent interface for writing images to files or other d...
Definition: qimagewriter.h:57
The QOpenGLWidget class is a widget for rendering OpenGL graphics.
Definition: qopenglwidget.h:54
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
Definition: qpaintengine.h:87
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:82
The QPixmap class is an off-screen image representation that can be used as a paint device.
Definition: qpixmap.h:63
QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition: qpixmap.h:114
bool operator!=(const QPixmap &) const =delete
static QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition: qpixmap.h:127
void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed=nullptr)
Definition: qpixmap.h:195
bool operator==(const QPixmap &) const =delete
bool loadFromData(const uchar *buf, uint len, const char *format=nullptr, Qt::ImageConversionFlags flags=Qt::AutoColor)
Definition: qpixmap.cpp:794
QExplicitlySharedDataPointer< QPlatformPixmap > DataPtr
Definition: qpixmap.h:184
QPixmap(QPixmap &&other) noexcept
Definition: qpixmap.h:74
DataPtr & data_ptr()
Definition: qpixmap.h:185
bool operator!() const
Definition: qpixmap.h:155
qint64 cacheKey() const
virtual void setMask(const QBitmap &mask)
virtual int metric(QPaintDevice::PaintDeviceMetric metric) const =0
virtual bool scroll(int dx, int dy, const QRect &rect)
virtual void fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
virtual void fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags)
virtual QImage toImage() const =0
friend class QPixmap
virtual qreal devicePixelRatio() const =0
virtual bool hasAlphaChannel() const =0
virtual void setDevicePixelRatio(qreal scaleFactor)=0
bool isNull() const
virtual void copy(const QPlatformPixmap *data, const QRect &rect)
virtual QPixmap transformed(const QTransform &matrix, Qt::TransformationMode mode) const
virtual void fromImage(const QImage &image, Qt::ImageConversionFlags flags)=0
virtual QPaintEngine * paintEngine() const =0
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:59
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:63
The QSizeF class defines the size of a two-dimensional object using floating point precision.
Definition: qsize.h:235
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:55
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:56
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
p1 load("image.bmp")
rect
[4]
int quality
Definition: jpeglib.h:933
#define inline
Definition: md4c.c:45
@ AutoColor
Definition: qnamespace.h:503
TransformationMode
Definition: qnamespace.h:1349
@ FastTransformation
Definition: qnamespace.h:1350
AspectRatioMode
Definition: qnamespace.h:1211
@ IgnoreAspectRatio
Definition: qnamespace.h:1212
@ white
Definition: qnamespace.h:62
MaskMode
Definition: qnamespace.h:1325
@ MaskInColor
Definition: qnamespace.h:1326
Definition: image.cpp:51
QT_BEGIN_INCLUDE_NAMESPACE typedef unsigned char uchar
Definition: qglobal.h:332
QT_END_INCLUDE_NAMESPACE typedef double qreal
Definition: qglobal.h:341
unsigned int uint
Definition: qglobal.h:334
long long qint64
Definition: qglobal.h:298
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(Class)
Definition: qglobal.h:556
GLenum type
Definition: qopengl.h:270
GLuint64 GLenum void * handle
GLint GLint GLint GLint GLint x
[0]
GLint GLenum GLsizei GLsizei GLsizei depth
GLenum mode
const GLfloat * m
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei height
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLint GLsizei width
GLenum GLuint GLenum GLsizei const GLchar * buf
GLbitfield flags
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
GLint GLsizei GLsizei GLenum format
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLint void * img
Definition: qopenglext.h:233
GLenum GLsizei len
Definition: qopenglext.h:3292
GLdouble s
[6]
Definition: qopenglext.h:235
#define QT_DECLARE_QESDP_SPECIALIZATION_DTOR_WITH_EXPORT(Class, ExportMacro)
Definition: qshareddata.h:315
#define Q_DECLARE_SHARED(TYPE)
Definition: qtypeinfo.h:197
QImage scaled(const QImage &image)
[0]
ba fill(true)
QSharedPointer< T > other(t)
[5]
this swap(other)