Qt 4.8
qpixmap.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QPIXMAP_H
43 #define QPIXMAP_H
44 
45 #include <QtGui/qpaintdevice.h>
46 #include <QtGui/qcolor.h>
47 #include <QtCore/qnamespace.h>
48 #include <QtCore/qstring.h> // char*->QString conversion
49 #include <QtCore/qsharedpointer.h>
50 #include <QtGui/qimage.h>
51 #include <QtGui/qtransform.h>
52 
54 
55 #if defined(Q_OS_SYMBIAN)
56 class CFbsBitmap;
57 class RSgImage;
58 #endif
59 
61 
62 QT_MODULE(Gui)
63 
64 class QImageWriter;
65 class QImageReader;
66 class QColor;
67 class QVariant;
68 class QX11Info;
69 class QPixmapData;
70 
72 {
73 public:
74  QPixmap();
75  explicit QPixmap(QPixmapData *data);
76  QPixmap(int w, int h);
77  QPixmap(const QSize &);
78  QPixmap(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
79 #ifndef QT_NO_IMAGEFORMAT_XPM
80  QPixmap(const char * const xpm[]);
81 #endif
82  QPixmap(const QPixmap &);
83  ~QPixmap();
84 
85  QPixmap &operator=(const QPixmap &);
86 #ifdef Q_COMPILER_RVALUE_REFS
87  inline QPixmap &operator=(QPixmap &&other)
88  { qSwap(data, other.data); return *this; }
89 #endif
90  inline void swap(QPixmap &other) { qSwap(data, other.data); }
91 
92  operator QVariant() const;
93 
94  bool isNull() const; // ### Qt 5: make inline
95  int devType() const;
96 
97  int width() const; // ### Qt 5: make inline
98  int height() const; // ### Qt 5: make inline
99  QSize size() const;
100  QRect rect() const;
101  int depth() const;
102 
103  static int defaultDepth();
104 
105  void fill(const QColor &fillColor = Qt::white);
106  void fill(const QWidget *widget, const QPoint &ofs);
107  inline void fill(const QWidget *widget, int xofs, int yofs) { fill(widget, QPoint(xofs, yofs)); }
108 
109  QBitmap mask() const;
110  void setMask(const QBitmap &);
111 
112 #ifdef QT_DEPRECATED
113  QT_DEPRECATED QPixmap alphaChannel() const;
114  QT_DEPRECATED void setAlphaChannel(const QPixmap &);
115 #endif
116 
117  bool hasAlpha() const;
118  bool hasAlphaChannel() const;
119 
120 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
121  QBitmap createHeuristicMask(bool clipTight = true) const;
122 #endif
123  QBitmap createMaskFromColor(const QColor &maskColor) const; // ### Qt 5: remove
124  QBitmap createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const;
125 
126  static QPixmap grabWindow(WId, int x=0, int y=0, int w=-1, int h=-1);
127  static QPixmap grabWidget(QWidget *widget, const QRect &rect);
128  static inline QPixmap grabWidget(QWidget *widget, int x=0, int y=0, int w=-1, int h=-1)
129  { return grabWidget(widget, QRect(x, y, w, h)); }
130 
131 
132  inline QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
134  { return scaled(QSize(w, h), aspectMode, mode); }
135  QPixmap scaled(const QSize &s, Qt::AspectRatioMode aspectMode = Qt::IgnoreAspectRatio,
137  QPixmap scaledToWidth(int w, Qt::TransformationMode mode = Qt::FastTransformation) const;
138  QPixmap scaledToHeight(int h, Qt::TransformationMode mode = Qt::FastTransformation) const;
139  QPixmap transformed(const QMatrix &, Qt::TransformationMode mode = Qt::FastTransformation) const;
140  static QMatrix trueMatrix(const QMatrix &m, int w, int h);
141  QPixmap transformed(const QTransform &, Qt::TransformationMode mode = Qt::FastTransformation) const;
142  static QTransform trueMatrix(const QTransform &m, int w, int h);
143 
144  QImage toImage() const;
145  static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags = Qt::AutoColor);
146  static QPixmap fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags = Qt::AutoColor);
147 
148  bool load(const QString& fileName, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
149  bool loadFromData(const uchar *buf, uint len, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
150  inline bool loadFromData(const QByteArray &data, const char* format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
151  bool save(const QString& fileName, const char* format = 0, int quality = -1) const;
152  bool save(QIODevice* device, const char* format = 0, int quality = -1) const;
153 
154  bool convertFromImage(const QImage &img, Qt::ImageConversionFlags flags = Qt::AutoColor);
155 
156 #if defined(Q_WS_WIN)
160  Alpha
161  };
162 
163  HBITMAP toWinHBITMAP(HBitmapFormat format = NoAlpha) const;
164  HICON toWinHICON() const;
165 
166  static QPixmap fromWinHBITMAP(HBITMAP hbitmap, HBitmapFormat format = NoAlpha);
167  static QPixmap fromWinHICON(HICON hicon);
168 #endif
169 
170 #if defined(Q_WS_MAC)
171  CGImageRef toMacCGImageRef() const;
172  static QPixmap fromMacCGImageRef(CGImageRef image);
173 #endif
174 
175 #if defined(Q_OS_SYMBIAN)
176  CFbsBitmap *toSymbianCFbsBitmap() const;
177  static QPixmap fromSymbianCFbsBitmap(CFbsBitmap *bitmap);
178  RSgImage* toSymbianRSgImage() const;
179  static QPixmap fromSymbianRSgImage(RSgImage *sgImage);
180 #endif
181 
182  inline QPixmap copy(int x, int y, int width, int height) const;
183  QPixmap copy(const QRect &rect = QRect()) const;
184 
185  inline void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed = 0);
186  void scroll(int dx, int dy, const QRect &rect, QRegion *exposed = 0);
187 
188 #ifdef QT_DEPRECATED
189  QT_DEPRECATED int serialNumber() const;
190 #endif
191  qint64 cacheKey() const;
192 
193  bool isDetached() const;
194  void detach();
195 
196  bool isQBitmap() const;
197 
198 #if defined(Q_WS_QWS)
199  const uchar *qwsBits() const;
200  int qwsBytesPerLine() const;
201  QRgb *clut() const;
202 #ifdef QT_DEPRECATED
203  QT_DEPRECATED int numCols() const;
204 #endif
205  int colorCount() const;
206 #elif defined(Q_WS_MAC)
207  Qt::HANDLE macQDHandle() const;
208  Qt::HANDLE macQDAlphaHandle() const;
209  Qt::HANDLE macCGHandle() const;
210 #elif defined(Q_WS_X11)
211  enum ShareMode { ImplicitlyShared, ExplicitlyShared };
212 
213  static QPixmap fromX11Pixmap(Qt::HANDLE pixmap, ShareMode mode = ImplicitlyShared);
214  static int x11SetDefaultScreen(int screen);
215  void x11SetScreen(int screen);
216  const QX11Info &x11Info() const;
217  Qt::HANDLE x11PictureHandle() const;
218 #endif
219 
220 #if defined(Q_WS_X11) || defined(Q_WS_QWS)
221  Qt::HANDLE handle() const;
222 #endif
223 
224  QPaintEngine *paintEngine() const;
225 
226  inline bool operator!() const { return isNull(); }
227 
228 protected:
229  int metric(PaintDeviceMetric) const;
230 
231 public:
232 #ifdef QT3_SUPPORT
233  enum ColorMode { Auto, Color, Mono };
234  QT3_SUPPORT_CONSTRUCTOR QPixmap(const QString& fileName, const char *format, ColorMode mode);
235  QT3_SUPPORT bool load(const QString& fileName, const char *format, ColorMode mode);
236  QT3_SUPPORT bool loadFromData(const uchar *buf, uint len, const char* format, ColorMode mode);
237  QT3_SUPPORT_CONSTRUCTOR QPixmap(const QImage& image);
238  QT3_SUPPORT QPixmap &operator=(const QImage &);
239  inline QT3_SUPPORT QImage convertToImage() const { return toImage(); }
240  QT3_SUPPORT bool convertFromImage(const QImage &, ColorMode mode);
241  inline QT3_SUPPORT operator QImage() const { return toImage(); }
242  inline QT3_SUPPORT QPixmap xForm(const QMatrix &matrix) const { return transformed(QTransform(matrix)); }
243  inline QT3_SUPPORT bool selfMask() const { return false; }
244 private:
245  void resize_helper(const QSize &s);
246 public:
247  inline QT3_SUPPORT void resize(const QSize &s) { resize_helper(s); }
248  inline QT3_SUPPORT void resize(int width, int height) { resize_helper(QSize(width, height)); }
249 #endif
250 
251 private:
253 
254  bool doImageIO(QImageWriter *io, int quality) const;
255 
256  // ### Qt5: remove the following three lines
257  enum Type { PixmapType, BitmapType }; // must match QPixmapData::PixelType
258  QPixmap(const QSize &s, Type);
259  void init(int, int, Type = PixmapType);
260 
261  QPixmap(const QSize &s, int type);
262  void init(int, int, int);
263  void deref();
264 #if defined(Q_WS_WIN)
265  void initAlphaPixmap(uchar *bytes, int length, struct tagBITMAPINFO *bmi);
266 #endif
268 #ifdef Q_WS_MAC
270  friend CGImageRef qt_mac_create_imagemask(const QPixmap&, const QRectF&);
271  friend IconRef qt_mac_create_iconref(const QPixmap&);
272  friend quint32 *qt_mac_pixmap_get_base(const QPixmap*);
273  friend int qt_mac_pixmap_get_bytes_per_line(const QPixmap*);
274 #endif
275  friend class QPixmapData;
276  friend class QX11PixmapData;
277  friend class QMacPixmapData;
278  friend class QSymbianRasterPixmapData;
279  friend class QBitmap;
280  friend class QPaintDevice;
281  friend class QPainter;
282  friend class QGLWidget;
283  friend class QX11PaintEngine;
285  friend class QWidgetPrivate;
286  friend class QRasterBuffer;
287 #if !defined(QT_NO_DATASTREAM)
288  friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPixmap &);
289 #endif
290  friend Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap);
291 
292 public:
293  QPixmapData* pixmapData() const;
294 
295 public:
297  inline DataPtr &data_ptr() { return data; }
298 };
299 
301 
302 inline QPixmap QPixmap::copy(int ax, int ay, int awidth, int aheight) const
303 {
304  return copy(QRect(ax, ay, awidth, aheight));
305 }
306 
307 inline void QPixmap::scroll(int dx, int dy, int ax, int ay, int awidth, int aheight, QRegion *exposed)
308 {
309  scroll(dx, dy, QRect(ax, ay, awidth, aheight), exposed);
310 }
311 
312 inline bool QPixmap::loadFromData(const QByteArray &buf, const char *format,
313  Qt::ImageConversionFlags flags)
314 {
315  return loadFromData(reinterpret_cast<const uchar *>(buf.constData()), buf.size(), format, flags);
316 }
317 
318 /*****************************************************************************
319  QPixmap stream functions
320 *****************************************************************************/
321 
322 #if !defined(QT_NO_DATASTREAM)
325 #endif
326 
327 /*****************************************************************************
328  QPixmap (and QImage) helper functions
329 *****************************************************************************/
330 #ifdef QT3_SUPPORT
331 QT3_SUPPORT Q_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy, const QPixmap *src,
332  int sx=0, int sy=0, int sw=-1, int sh=-1);
333 #endif // QT3_SUPPORT
334 
336 
338 
339 #endif // QPIXMAP_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
CGImageRef qt_mac_create_imagemask(const QPixmap &pixmap, const QRectF &sr)
DataPtr & data_ptr()
Definition: qpixmap.h:297
unsigned int QRgb
Definition: qrgb.h:53
unsigned long WId
Definition: qwindowdefs.h:119
HBitmapFormat
HBITMAP and QPixmap is performed.
Definition: qpixmap.h:157
int type
Definition: qmetatype.cpp:239
#define Q_DUMMY_COMPARISON_OPERATOR(C)
Definition: qglobal.h:2104
Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap)
Definition: qpixmap.cpp:94
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QPointer< QWidget > widget
Q_GUI_EXPORT int qt_mac_pixmap_get_bytes_per_line(const QPixmap *pix)
Definition: qpixmap_mac.cpp:83
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
The QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
MaskMode
Definition: qnamespace.h:1490
IconRef qt_mac_create_iconref(const QPixmap &px)
QExplicitlySharedDataPointer< QPixmapData > data
Definition: qpixmap.h:252
static QPixmap grabWidget(QWidget *widget, int x=0, int y=0, int w=-1, int h=-1)
Definition: qpixmap.h:128
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
int height() const
Definition: qpaintdevice.h:92
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void fill(const QWidget *widget, int xofs, int yofs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: qpixmap.h:107
bool operator!() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.h:226
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
QPixmap scaled(int w, int h, Qt::AspectRatioMode aspectMode=Qt::IgnoreAspectRatio, Qt::TransformationMode mode=Qt::FastTransformation) const
Definition: qpixmap.h:132
struct CGImage * CGImageRef
static int defaultDepth()
Definition: qbbscreen.cpp:164
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual int devType() const
Definition: qpaintdevice.h:167
QExplicitlySharedDataPointer< QPixmapData > DataPtr
Definition: qpixmap.h:296
int colorCount() const
Definition: qpaintdevice.h:102
#define Q_DECLARE_SHARED(TYPE)
Definition: qglobal.h:2214
unsigned char uchar
Definition: qglobal.h:994
The QBitmap class provides monochrome (1-bit depth) pixmaps.
Definition: qbitmap.h:55
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
int width() const
Definition: qpaintdevice.h:91
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
The QImageWriter class provides a format independent interface for writing images to files or other d...
Definition: qimagewriter.h:59
static bool init
CGContextRef qt_mac_cg_context(const QPaintDevice *pdev)
Returns the CoreGraphics CGContextRef of the paint device.
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QPixmap &)
void scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed=0)
Definition: qpixmap.h:307
TransformationMode
Definition: qnamespace.h:1510
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
__int64 qint64
Definition: qglobal.h:942
void * HANDLE
Definition: qnamespace.h:1671
bool loadFromData(const uchar *buf, uint len, const char *format=0, Qt::ImageConversionFlags flags=Qt::AutoColor)
Loads a pixmap from the len first bytes of the given binary data.
Definition: qpixmap.cpp:979
The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a g...
Definition: qpaintengine.h:90
The QImageReader class provides a format independent interface for reading images from files or other...
Definition: qimagereader.h:62
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
virtual QPaintEngine * paintEngine() const =0
The QGLWidget class is a widget for rendering OpenGL graphics.
Definition: qgl.h:474
The QX11Info class provides information about the X display configuration.
Definition: qx11info_x11.h:63
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QPixmap &)
#define load(x)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
unsigned int quint32
Definition: qglobal.h:938
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
virtual int metric(PaintDeviceMetric metric) const
Q_GUI_EXPORT quint32 * qt_mac_pixmap_get_base(const QPixmap *pix)
Definition: qpixmap_mac.cpp:75
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
int depth() const
Definition: qpaintdevice.h:103
#define QT_DEPRECATED
Definition: qglobal.h:1094
void swap(QPixmap &other)
Swaps pixmap other with this pixmap.
Definition: qpixmap.h:90
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
struct CGContext * CGContextRef
The QIODevice class is the base interface class of all I/O devices in Qt.
Definition: qiodevice.h:66
#define QT_END_HEADER
Definition: qglobal.h:137
static QString fileName(const QString &fileUrl)
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
struct OpaqueIconRef * IconRef
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
AspectRatioMode
Definition: qnamespace.h:1317