source: trunk/src/gui/image/qpixmap.cpp@ 603

Last change on this file since 603 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 63.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qglobal.h>
43
44#include "qpixmap.h"
45#include "qpixmapdata_p.h"
46#include "qimagepixmapcleanuphooks_p.h"
47
48#include "qbitmap.h"
49#include "qcolormap.h"
50#include "qimage.h"
51#include "qwidget.h"
52#include "qpainter.h"
53#include "qdatastream.h"
54#include "qbuffer.h"
55#include "qapplication.h"
56#include <private/qapplication_p.h>
57#include <private/qgraphicssystem_p.h>
58#include <private/qwidget_p.h>
59#include "qevent.h"
60#include "qfile.h"
61#include "qfileinfo.h"
62#include "qpixmapcache.h"
63#include "qdatetime.h"
64#include "qimagereader.h"
65#include "qimagewriter.h"
66#include "qpaintengine.h"
67#include "qthread.h"
68
69#ifdef Q_WS_MAC
70# include "private/qt_mac_p.h"
71# include "private/qpixmap_mac_p.h"
72#endif
73
74#if defined(Q_WS_X11)
75# include "qx11info_x11.h"
76# include <private/qt_x11_p.h>
77# include <private/qpixmap_x11_p.h>
78#endif
79
80#if defined(Q_OS_SYMBIAN)
81# include <private/qt_s60_p.h>
82#endif
83
84#include "qpixmap_raster_p.h"
85
86QT_BEGIN_NAMESPACE
87
88// ### Qt 5: remove
89Q_GUI_EXPORT qint64 qt_pixmap_id(const QPixmap &pixmap)
90{
91 return pixmap.cacheKey();
92}
93
94static bool qt_pixmap_thread_test()
95{
96 if (!qApp) {
97 qFatal("QPixmap: Must construct a QApplication before a QPaintDevice");
98 return false;
99 }
100#ifndef Q_WS_WIN
101 if (qApp->thread() != QThread::currentThread()) {
102 qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread");
103 return false;
104 }
105#endif
106 return true;
107}
108
109void QPixmap::init(int w, int h, Type type)
110{
111 init(w, h, int(type));
112}
113
114void QPixmap::init(int w, int h, int type)
115{
116 if ((w > 0 && h > 0) || type == QPixmapData::BitmapType)
117 data = QPixmapData::create(w, h, (QPixmapData::PixelType) type);
118 else
119 data = 0;
120}
121
122/*!
123 \enum QPixmap::ColorMode
124
125 \compat
126
127 This enum type defines the color modes that exist for converting
128 QImage objects to QPixmap. It is provided here for compatibility
129 with earlier versions of Qt.
130
131 Use Qt::ImageConversionFlags instead.
132
133 \value Auto Select \c Color or \c Mono on a case-by-case basis.
134 \value Color Always create colored pixmaps.
135 \value Mono Always create bitmaps.
136*/
137
138/*!
139 Constructs a null pixmap.
140
141 \sa isNull()
142*/
143
144QPixmap::QPixmap()
145 : QPaintDevice()
146{
147 (void) qt_pixmap_thread_test();
148 init(0, 0, QPixmapData::PixmapType);
149}
150
151/*!
152 \fn QPixmap::QPixmap(int width, int height)
153
154 Constructs a pixmap with the given \a width and \a height. If
155 either \a width or \a height is zero, a null pixmap is
156 constructed.
157
158 \warning This will create a QPixmap with uninitialized data. Call
159 fill() to fill the pixmap with an appropriate color before drawing
160 onto it with QPainter.
161
162 \sa isNull()
163*/
164
165QPixmap::QPixmap(int w, int h)
166 : QPaintDevice()
167{
168 if (!qt_pixmap_thread_test())
169 init(0, 0, QPixmapData::PixmapType);
170 else
171 init(w, h, QPixmapData::PixmapType);
172}
173
174/*!
175 \overload
176
177 Constructs a pixmap of the given \a size.
178
179 \warning This will create a QPixmap with uninitialized data. Call
180 fill() to fill the pixmap with an appropriate color before drawing
181 onto it with QPainter.
182*/
183
184QPixmap::QPixmap(const QSize &size)
185 : QPaintDevice()
186{
187 if (!qt_pixmap_thread_test())
188 init(0, 0, QPixmapData::PixmapType);
189 else
190 init(size.width(), size.height(), QPixmapData::PixmapType);
191}
192
193/*!
194 \internal
195*/
196QPixmap::QPixmap(const QSize &s, Type type)
197{
198 if (!qt_pixmap_thread_test())
199 init(0, 0, type);
200 else
201 init(s.width(), s.height(), type);
202}
203
204/*!
205 \internal
206*/
207QPixmap::QPixmap(const QSize &s, int type)
208{
209 if (!qt_pixmap_thread_test())
210 init(0, 0, static_cast<QPixmapData::PixelType>(type));
211 else
212 init(s.width(), s.height(), static_cast<QPixmapData::PixelType>(type));
213}
214
215/*!
216 \internal
217*/
218QPixmap::QPixmap(QPixmapData *d)
219 : QPaintDevice(), data(d)
220{
221}
222
223/*!
224 Constructs a pixmap from the file with the given \a fileName. If the
225 file does not exist or is of an unknown format, the pixmap becomes a
226 null pixmap.
227
228 The loader attempts to read the pixmap using the specified \a
229 format. If the \a format is not specified (which is the default),
230 the loader probes the file for a header to guess the file format.
231
232 The file name can either refer to an actual file on disk or to
233 one of the application's embedded resources. See the
234 \l{resources.html}{Resource System} overview for details on how
235 to embed images and other resource files in the application's
236 executable.
237
238 If the image needs to be modified to fit in a lower-resolution
239 result (e.g. converting from 32-bit to 8-bit), use the \a
240 flags to control the conversion.
241
242 The \a fileName, \a format and \a flags parameters are
243 passed on to load(). This means that the data in \a fileName is
244 not compiled into the binary. If \a fileName contains a relative
245 path (e.g. the filename only) the relevant file must be found
246 relative to the runtime working directory.
247
248 \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
249 Image Files}
250*/
251
252QPixmap::QPixmap(const QString& fileName, const char *format, Qt::ImageConversionFlags flags)
253 : QPaintDevice()
254{
255 init(0, 0, QPixmapData::PixmapType);
256 if (!qt_pixmap_thread_test())
257 return;
258
259 load(fileName, format, flags);
260}
261
262/*!
263 Constructs a pixmap that is a copy of the given \a pixmap.
264
265 \sa copy()
266*/
267
268QPixmap::QPixmap(const QPixmap &pixmap)
269 : QPaintDevice()
270{
271 if (!qt_pixmap_thread_test()) {
272 init(0, 0, QPixmapData::PixmapType);
273 return;
274 }
275 if (pixmap.paintingActive()) { // make a deep copy
276 operator=(pixmap.copy());
277 } else {
278 data = pixmap.data;
279 }
280}
281
282/*!
283 Constructs a pixmap from the given \a xpm data, which must be a
284 valid XPM image.
285
286 Errors are silently ignored.
287
288 Note that it's possible to squeeze the XPM variable a little bit
289 by using an unusual declaration:
290
291 \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 0
292
293 The extra \c const makes the entire definition read-only, which is
294 slightly more efficient (for example, when the code is in a shared
295 library) and ROMable when the application is to be stored in ROM.
296*/
297#ifndef QT_NO_IMAGEFORMAT_XPM
298QPixmap::QPixmap(const char * const xpm[])
299 : QPaintDevice()
300{
301 init(0, 0, QPixmapData::PixmapType);
302 if (!xpm)
303 return;
304
305 QImage image(xpm);
306 if (!image.isNull()) {
307 if (data && data->pixelType() == QPixmapData::BitmapType)
308 *this = QBitmap::fromImage(image);
309 else
310 *this = fromImage(image);
311 }
312}
313#endif
314
315
316/*!
317 Destroys the pixmap.
318*/
319
320QPixmap::~QPixmap()
321{
322 Q_ASSERT(!data || data->ref >= 1); // Catch if ref-counting changes again
323 if (data && data->is_cached && data->ref == 1) // ref will be decrememnted after destructor returns
324 QImagePixmapCleanupHooks::executePixmapDestructionHooks(this);
325}
326
327/*!
328 \internal
329*/
330int QPixmap::devType() const
331{
332 return QInternal::Pixmap;
333}
334
335/*!
336 \fn QPixmap QPixmap::copy(int x, int y, int width, int height) const
337 \overload
338
339 Returns a deep copy of the subset of the pixmap that is specified
340 by the rectangle QRect( \a x, \a y, \a width, \a height).
341*/
342
343/*!
344 \fn QPixmap QPixmap::copy(const QRect &rectangle) const
345
346 Returns a deep copy of the subset of the pixmap that is specified
347 by the given \a rectangle. For more information on deep copies,
348 see the \l {Implicit Data Sharing} documentation.
349
350 If the given \a rectangle is empty, the whole image is copied.
351
352 \sa operator=(), QPixmap(), {QPixmap#Pixmap
353 Transformations}{Pixmap Transformations}
354*/
355QPixmap QPixmap::copy(const QRect &rect) const
356{
357 if (isNull())
358 return QPixmap();
359
360 QRect r(0, 0, width(), height());
361 if (!rect.isEmpty())
362 r = r.intersected(rect);
363
364 QPixmapData *d = data->createCompatiblePixmapData();
365 d->copy(data.data(), r);
366 return QPixmap(d);
367}
368
369/*!
370 \fn QPixmap::scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed)
371 \since 4.6
372
373 This convenience function is equivalent to calling QPixmap::scroll(\a dx,
374 \a dy, QRect(\a x, \a y, \a width, \a height), \a exposed).
375
376 \sa QWidget::scroll(), QGraphicsItem::scroll()
377*/
378
379/*!
380 \since 4.6
381
382 Scrolls the area \a rect of this pixmap by (\a dx, \a dy). The exposed
383 region is left unchanged. You can optionally pass a pointer to an empty
384 QRegion to get the region that is \a exposed by the scroll operation.
385
386 \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 2
387
388 You cannot scroll while there is an active painter on the pixmap.
389
390 \sa QWidget::scroll(), QGraphicsItem::scroll()
391*/
392void QPixmap::scroll(int dx, int dy, const QRect &rect, QRegion *exposed)
393{
394 if (isNull() || (dx == 0 && dy == 0))
395 return;
396 QRect dest = rect & this->rect();
397 QRect src = dest.translated(-dx, -dy) & dest;
398 if (src.isEmpty()) {
399 if (exposed)
400 *exposed += dest;
401 return;
402 }
403
404 detach();
405
406 if (!data->scroll(dx, dy, src)) {
407 // Fallback
408 QPixmap pix = *this;
409 QPainter painter(&pix);
410 painter.setCompositionMode(QPainter::CompositionMode_Source);
411 painter.drawPixmap(src.translated(dx, dy), *this, src);
412 painter.end();
413 *this = pix;
414 }
415
416 if (exposed) {
417 *exposed += dest;
418 *exposed -= src.translated(dx, dy);
419 }
420}
421
422/*!
423 Assigns the given \a pixmap to this pixmap and returns a reference
424 to this pixmap.
425
426 \sa copy(), QPixmap()
427*/
428
429QPixmap &QPixmap::operator=(const QPixmap &pixmap)
430{
431 if (paintingActive()) {
432 qWarning("QPixmap::operator=: Cannot assign to pixmap during painting");
433 return *this;
434 }
435 if (pixmap.paintingActive()) { // make a deep copy
436 *this = pixmap.copy();
437 } else {
438 data = pixmap.data;
439 }
440 return *this;
441}
442
443/*!
444 Returns the pixmap as a QVariant.
445*/
446QPixmap::operator QVariant() const
447{
448 return QVariant(QVariant::Pixmap, this);
449}
450
451/*!
452 \fn bool QPixmap::operator!() const
453
454 Returns true if this is a null pixmap; otherwise returns false.
455
456 \sa isNull()
457*/
458
459/*!
460 \fn QPixmap::operator QImage() const
461
462 Returns the pixmap as a QImage.
463
464 Use the toImage() function instead.
465*/
466
467/*!
468 Converts the pixmap to a QImage. Returns a null image if the
469 conversion fails.
470
471 If the pixmap has 1-bit depth, the returned image will also be 1
472 bit deep. Images with more bits will be returned in a format
473 closely represents the underlying system. Usually this will be
474 QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and
475 QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without
476 alpha.
477
478 Note that for the moment, alpha masks on monochrome images are
479 ignored.
480
481 \sa fromImage(), {QImage#Image Formats}{Image Formats}
482*/
483QImage QPixmap::toImage() const
484{
485 if (isNull())
486 return QImage();
487
488 return data->toImage();
489}
490
491/*!
492 \fn QMatrix QPixmap::trueMatrix(const QTransform &matrix, int width, int height)
493
494 Returns the actual matrix used for transforming a pixmap with the
495 given \a width, \a height and \a matrix.
496
497 When transforming a pixmap using the transformed() function, the
498 transformation matrix is internally adjusted to compensate for
499 unwanted translation, i.e. transformed() returns the smallest
500 pixmap containing all transformed points of the original
501 pixmap. This function returns the modified matrix, which maps
502 points correctly from the original pixmap into the new pixmap.
503
504 \sa transformed(), {QPixmap#Pixmap Transformations}{Pixmap
505 Transformations}
506*/
507QTransform QPixmap::trueMatrix(const QTransform &m, int w, int h)
508{
509 return QImage::trueMatrix(m, w, h);
510}
511
512/*!
513 \overload
514
515 This convenience function loads the matrix \a m into a
516 QTransform and calls the overloaded function with the
517 QTransform and the width \a w and the height \a h.
518 */
519QMatrix QPixmap::trueMatrix(const QMatrix &m, int w, int h)
520{
521 return trueMatrix(QTransform(m), w, h).toAffine();
522}
523
524
525/*!
526 \fn bool QPixmap::isQBitmap() const
527
528 Returns true if this is a QBitmap; otherwise returns false.
529*/
530
531bool QPixmap::isQBitmap() const
532{
533 return data->type == QPixmapData::BitmapType;
534}
535
536/*!
537 \fn bool QPixmap::isNull() const
538
539 Returns true if this is a null pixmap; otherwise returns false.
540
541 A null pixmap has zero width, zero height and no contents. You
542 cannot draw in a null pixmap.
543*/
544bool QPixmap::isNull() const
545{
546 return !data || data->isNull();
547}
548
549/*!
550 \fn int QPixmap::width() const
551
552 Returns the width of the pixmap.
553
554 \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
555*/
556int QPixmap::width() const
557{
558 return data ? data->width() : 0;
559}
560
561/*!
562 \fn int QPixmap::height() const
563
564 Returns the height of the pixmap.
565
566 \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
567*/
568int QPixmap::height() const
569{
570 return data ? data->height() : 0;
571}
572
573/*!
574 \fn QSize QPixmap::size() const
575
576 Returns the size of the pixmap.
577
578 \sa width(), height(), {QPixmap#Pixmap Information}{Pixmap
579 Information}
580*/
581QSize QPixmap::size() const
582{
583 return data ? QSize(data->width(), data->height()) : QSize();
584}
585
586/*!
587 \fn QRect QPixmap::rect() const
588
589 Returns the pixmap's enclosing rectangle.
590
591 \sa {QPixmap#Pixmap Information}{Pixmap Information}
592*/
593QRect QPixmap::rect() const
594{
595 return data ? QRect(0, 0, data->width(), data->height()) : QRect();
596}
597
598/*!
599 \fn int QPixmap::depth() const
600
601 Returns the depth of the pixmap.
602
603 The pixmap depth is also called bits per pixel (bpp) or bit planes
604 of a pixmap. A null pixmap has depth 0.
605
606 \sa defaultDepth(), {QPixmap#Pixmap Information}{Pixmap
607 Information}
608*/
609int QPixmap::depth() const
610{
611 return data ? data->depth() : 0;
612}
613
614/*!
615 \fn void QPixmap::resize(const QSize &size)
616 \overload
617 \compat
618
619 Use QPixmap::copy() instead to get the pixmap with the new size.
620
621 \oldcode
622 pixmap.resize(size);
623 \newcode
624 pixmap = pixmap.copy(QRect(QPoint(0, 0), size));
625 \endcode
626*/
627#ifdef QT3_SUPPORT
628void QPixmap::resize_helper(const QSize &s)
629{
630 int w = s.width();
631 int h = s.height();
632 if (w < 1 || h < 1) {
633 *this = QPixmap();
634 return;
635 }
636
637 if (size() == s)
638 return;
639
640 // Create new pixmap
641 QPixmap pm(QSize(w, h), data ? data->type : QPixmapData::PixmapType);
642 bool uninit = false;
643#if defined(Q_WS_X11)
644 QX11PixmapData *x11Data = data && data->classId() == QPixmapData::X11Class ? static_cast<QX11PixmapData*>(data.data()) : 0;
645 if (x11Data) {
646 pm.x11SetScreen(x11Data->xinfo.screen());
647 uninit = x11Data->flags & QX11PixmapData::Uninitialized;
648 }
649#elif defined(Q_WS_MAC)
650 QMacPixmapData *macData = data && data->classId() == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data.data()) : 0;
651 if (macData)
652 uninit = macData->uninit;
653#endif
654 if (!uninit && !isNull()) {
655 // Copy old pixmap
656 if (hasAlphaChannel())
657 pm.fill(Qt::transparent);
658 QPainter p(&pm);
659 p.drawPixmap(0, 0, *this, 0, 0, qMin(width(), w), qMin(height(), h));
660 }
661
662#if defined(Q_WS_X11)
663 if (x11Data && x11Data->x11_mask) {
664 QX11PixmapData *pmData = static_cast<QX11PixmapData*>(pm.data.data());
665 pmData->x11_mask = (Qt::HANDLE)XCreatePixmap(X11->display,
666 RootWindow(x11Data->xinfo.display(),
667 x11Data->xinfo.screen()),
668 w, h, 1);
669 GC gc = XCreateGC(X11->display, pmData->x11_mask, 0, 0);
670 XCopyArea(X11->display, x11Data->x11_mask, pmData->x11_mask, gc, 0, 0, qMin(width(), w), qMin(height(), h), 0, 0);
671 XFreeGC(X11->display, gc);
672 }
673#endif
674 *this = pm;
675}
676#endif
677
678/*!
679 \fn void QPixmap::resize(int width, int height)
680 \compat
681
682 Use QPixmap::copy() instead to get the pixmap with the new size.
683
684 \oldcode
685 pixmap.resize(10, 20);
686 \newcode
687 pixmap = pixmap.copy(0, 0, 10, 20);
688 \endcode
689*/
690
691/*!
692 \fn bool QPixmap::selfMask() const
693 \compat
694
695 Returns whether the pixmap is its own mask or not.
696
697 This function is no longer relevant since the concept of self
698 masking doesn't exists anymore.
699*/
700
701/*!
702 Sets a mask bitmap.
703
704 This function merges the \a mask with the pixmap's alpha channel. A pixel
705 value of 1 on the mask means the pixmap's pixel is unchanged; a value of 0
706 means the pixel is transparent. The mask must have the same size as this
707 pixmap.
708
709 Setting a null mask resets the mask, leaving the previously transparent
710 pixels black. The effect of this function is undefined when the pixmap is
711 being painted on.
712
713 \warning This is potentially an expensive operation.
714
715 \sa mask(), {QPixmap#Pixmap Transformations}{Pixmap Transformations},
716 QBitmap
717*/
718void QPixmap::setMask(const QBitmap &mask)
719{
720 if (paintingActive()) {
721 qWarning("QPixmap::setMask: Cannot set mask while pixmap is being painted on");
722 return;
723 }
724
725 if (!mask.isNull() && mask.size() != size()) {
726 qWarning("QPixmap::setMask() mask size differs from pixmap size");
727 return;
728 }
729
730 if (isNull())
731 return;
732
733 if (static_cast<const QPixmap &>(mask).data == data) // trying to selfmask
734 return;
735
736 detach();
737 data->setMask(mask);
738}
739
740#ifndef QT_NO_IMAGE_HEURISTIC_MASK
741/*!
742 Creates and returns a heuristic mask for this pixmap.
743
744 The function works by selecting a color from one of the corners
745 and then chipping away pixels of that color, starting at all the
746 edges. If \a clipTight is true (the default) the mask is just
747 large enough to cover the pixels; otherwise, the mask is larger
748 than the data pixels.
749
750 The mask may not be perfect but it should be reasonable, so you
751 can do things such as the following:
752
753 \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 1
754
755 This function is slow because it involves converting to/from a
756 QImage, and non-trivial computations.
757
758 \sa QImage::createHeuristicMask(), createMaskFromColor()
759*/
760QBitmap QPixmap::createHeuristicMask(bool clipTight) const
761{
762 QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
763 return m;
764}
765#endif
766
767/*!
768 Creates and returns a mask for this pixmap based on the given \a
769 maskColor. If the \a mode is Qt::MaskInColor, all pixels matching the
770 maskColor will be opaque. If \a mode is Qt::MaskOutColor, all pixels
771 matching the maskColor will be transparent.
772
773 This function is slow because it involves converting to/from a
774 QImage.
775
776 \sa createHeuristicMask(), QImage::createMaskFromColor()
777*/
778QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const
779{
780 QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
781 return QBitmap::fromImage(image.createMaskFromColor(maskColor.rgba(), mode));
782}
783
784/*! \overload
785
786 Creates and returns a mask for this pixmap based on the given \a
787 maskColor. Same as calling createMaskFromColor(maskColor,
788 Qt::MaskInColor)
789
790 \sa createHeuristicMask(), QImage::createMaskFromColor()
791*/
792QBitmap QPixmap::createMaskFromColor(const QColor &maskColor) const
793{
794 return createMaskFromColor(maskColor, Qt::MaskInColor);
795}
796
797/*!
798 Loads a pixmap from the file with the given \a fileName. Returns
799 true if the pixmap was successfully loaded; otherwise returns
800 false.
801
802 The loader attempts to read the pixmap using the specified \a
803 format. If the \a format is not specified (which is the default),
804 the loader probes the file for a header to guess the file format.
805
806 The file name can either refer to an actual file on disk or to one
807 of the application's embedded resources. See the
808 \l{resources.html}{Resource System} overview for details on how to
809 embed pixmaps and other resource files in the application's
810 executable.
811
812 If the data needs to be modified to fit in a lower-resolution
813 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
814 control the conversion.
815
816 Note that QPixmaps are automatically added to the QPixmapCache
817 when loaded from a file; the key used is internal and can not
818 be acquired.
819
820 \sa loadFromData(), {QPixmap#Reading and Writing Image
821 Files}{Reading and Writing Image Files}
822*/
823
824bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
825{
826 if (fileName.isEmpty())
827 return false;
828
829 QFileInfo info(fileName);
830 QString key = QLatin1String("qt_pixmap_") + info.absoluteFilePath() + QLatin1Char('_') + QString::number(info.lastModified().toTime_t()) + QLatin1Char('_') +
831 QString::number(info.size()) + QLatin1Char('_') + QString::number(data ? data->pixelType() : QPixmapData::PixmapType);
832
833 if (QPixmapCache::find(key, *this))
834 return true;
835
836 QPixmapData *tmp = QPixmapData::create(0, 0, QPixmapData::PixmapType);
837 if (tmp->fromFile(fileName, format, flags)) {
838 data = tmp;
839 QPixmapCache::insert(key, *this);
840 return true;
841 }
842 delete tmp;
843 return false;
844}
845
846/*!
847 \fn bool QPixmap::loadFromData(const uchar *data, uint len, const char *format, Qt::ImageConversionFlags flags)
848
849 Loads a pixmap from the \a len first bytes of the given binary \a
850 data. Returns true if the pixmap was loaded successfully;
851 otherwise returns false.
852
853 The loader attempts to read the pixmap using the specified \a
854 format. If the \a format is not specified (which is the default),
855 the loader probes the file for a header to guess the file format.
856
857 If the data needs to be modified to fit in a lower-resolution
858 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
859 control the conversion.
860
861 \sa load(), {QPixmap#Reading and Writing Image Files}{Reading and
862 Writing Image Files}
863*/
864
865bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
866{
867 if (len == 0 || buf == 0)
868 return false;
869
870 if (!data)
871 data = QPixmapData::create(0, 0, QPixmapData::PixmapType);
872
873 return data->fromData(buf, len, format, flags);
874}
875
876/*!
877 \fn bool QPixmap::loadFromData(const QByteArray &data, const char *format, Qt::ImageConversionFlags flags)
878
879 \overload
880
881 Loads a pixmap from the binary \a data using the specified \a
882 format and conversion \a flags.
883*/
884
885
886/*!
887 Saves the pixmap to the file with the given \a fileName using the
888 specified image file \a format and \a quality factor. Returns true
889 if successful; otherwise returns false.
890
891 The \a quality factor must be in the range [0,100] or -1. Specify
892 0 to obtain small compressed files, 100 for large uncompressed
893 files, and -1 to use the default settings.
894
895 If \a format is 0, an image format will be chosen from \a fileName's
896 suffix.
897
898 \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
899 Image Files}
900*/
901
902bool QPixmap::save(const QString &fileName, const char *format, int quality) const
903{
904 if (isNull())
905 return false; // nothing to save
906 QImageWriter writer(fileName, format);
907 return doImageIO(&writer, quality);
908}
909
910/*!
911 \overload
912
913 This function writes a QPixmap to the given \a device using the
914 specified image file \a format and \a quality factor. This can be
915 used, for example, to save a pixmap directly into a QByteArray:
916
917 \snippet doc/src/snippets/image/image.cpp 1
918*/
919
920bool QPixmap::save(QIODevice* device, const char* format, int quality) const
921{
922 if (isNull())
923 return false; // nothing to save
924 QImageWriter writer(device, format);
925 return doImageIO(&writer, quality);
926}
927
928/*! \internal
929*/
930bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
931{
932 if (quality > 100 || quality < -1)
933 qWarning("QPixmap::save: quality out of range [-1,100]");
934 if (quality >= 0)
935 writer->setQuality(qMin(quality,100));
936 return writer->write(toImage());
937}
938
939
940// The implementation (and documentation) of
941// QPixmap::fill(const QWidget *, const QPoint &)
942// is in qwidget.cpp
943
944/*!
945 \fn void QPixmap::fill(const QWidget *widget, int x, int y)
946 \overload
947
948 Fills the pixmap with the \a widget's background color or pixmap.
949 The given point, (\a x, \a y), defines an offset in widget
950 coordinates to which the pixmap's top-left pixel will be mapped
951 to.
952*/
953
954/*!
955 Fills the pixmap with the given \a color.
956
957 The effect of this function is undefined when the pixmap is
958 being painted on.
959
960 \sa {QPixmap#Pixmap Transformations}{Pixmap Transformations}
961*/
962
963void QPixmap::fill(const QColor &color)
964{
965 if (isNull())
966 return;
967
968 // Some people are probably already calling fill while a painter is active, so to not break
969 // their programs, only print a warning and return when the fill operation could cause a crash.
970 if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
971 qWarning("QPixmap::fill: Cannot fill while pixmap is being painted on");
972 return;
973 }
974
975 if (data->ref == 1) {
976 // detach() will also remove this pixmap from caches, so
977 // it has to be called even when ref == 1.
978 detach();
979 } else {
980 // Don't bother to make a copy of the data object, since
981 // it will be filled with new pixel data anyway.
982 QPixmapData *d = data->createCompatiblePixmapData();
983 d->resize(data->width(), data->height());
984 data = d;
985 }
986 data->fill(color);
987}
988
989/*! \obsolete
990 Returns a number that identifies the contents of this QPixmap
991 object. Distinct QPixmap objects can only have the same serial
992 number if they refer to the same contents (but they don't have
993 to).
994
995 Use cacheKey() instead.
996
997 \warning The serial number doesn't necessarily change when
998 the pixmap is altered. This means that it may be dangerous to use
999 it as a cache key. For caching pixmaps, we recommend using the
1000 QPixmapCache class whenever possible.
1001*/
1002int QPixmap::serialNumber() const
1003{
1004 if (isNull())
1005 return 0;
1006 return data->serialNumber();
1007}
1008
1009/*!
1010 Returns a number that identifies this QPixmap. Distinct QPixmap
1011 objects can only have the same cache key if they refer to the same
1012 contents.
1013
1014 The cacheKey() will change when the pixmap is altered.
1015*/
1016qint64 QPixmap::cacheKey() const
1017{
1018 if (isNull())
1019 return 0;
1020
1021 int classKey = data->classId();
1022 if (classKey >= 1024)
1023 classKey = -(classKey >> 10);
1024 return ((((qint64) classKey) << 56)
1025 | (((qint64) data->serialNumber()) << 32)
1026 | ((qint64) (data->detach_no)));
1027}
1028
1029static void sendResizeEvents(QWidget *target)
1030{
1031 QResizeEvent e(target->size(), QSize());
1032 QApplication::sendEvent(target, &e);
1033
1034 const QObjectList children = target->children();
1035 for (int i = 0; i < children.size(); ++i) {
1036 QWidget *child = static_cast<QWidget*>(children.at(i));
1037 if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
1038 sendResizeEvents(child);
1039 }
1040}
1041
1042/*!
1043 \fn QPixmap QPixmap::grabWidget(QWidget * widget, const QRect &rectangle)
1044
1045 Creates a pixmap and paints the given \a widget, restricted by the
1046 given \a rectangle, in it. If the \a widget has any children, then
1047 they are also painted in the appropriate positions.
1048
1049 If no rectangle is specified (the default) the entire widget is
1050 painted.
1051
1052 If \a widget is 0, the specified rectangle doesn't overlap the
1053 widget's rectangle, or an error occurs, the function will return a
1054 null QPixmap. If the rectangle is a superset of the given \a
1055 widget, the areas outside the \a widget are covered with the
1056 widget's background.
1057
1058 This function actually asks \a widget to paint itself (and its
1059 children to paint themselves) by calling paintEvent() with painter
1060 redirection turned on. But QPixmap also provides the grabWindow()
1061 function which is a bit faster by grabbing pixels directly off the
1062 screen. In addition, if there are overlaying windows,
1063 grabWindow(), unlike grabWidget(), will see them.
1064
1065 \warning Do not grab a widget from its QWidget::paintEvent().
1066 However, it is safe to grab a widget from another widget's
1067 \l {QWidget::}{paintEvent()}.
1068
1069 \sa grabWindow()
1070*/
1071
1072QPixmap QPixmap::grabWidget(QWidget * widget, const QRect &rect)
1073{
1074 if (!widget)
1075 return QPixmap();
1076
1077 if (widget->testAttribute(Qt::WA_PendingResizeEvent) || !widget->testAttribute(Qt::WA_WState_Created))
1078 sendResizeEvents(widget);
1079
1080 QRect r(rect);
1081 if (r.width() < 0)
1082 r.setWidth(widget->width() - rect.x());
1083 if (r.height() < 0)
1084 r.setHeight(widget->height() - rect.y());
1085
1086 if (!r.intersects(widget->rect()))
1087 return QPixmap();
1088
1089 QPixmap res(r.size());
1090 widget->render(&res, QPoint(), r,
1091 QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask);
1092 return res;
1093}
1094
1095/*!
1096 \fn QPixmap QPixmap::grabWidget(QWidget *widget, int x, int y, int
1097 width, int height)
1098
1099 \overload
1100
1101 Creates a pixmap and paints the given \a widget, restricted by
1102 QRect(\a x, \a y, \a width, \a height), in it.
1103
1104 \warning Do not grab a widget from its QWidget::paintEvent().
1105 However, it is safe to grab a widget from another widget's
1106 \l {QWidget::}{paintEvent()}.
1107*/
1108
1109
1110/*!
1111 \since 4.5
1112
1113 \enum QPixmap::ShareMode
1114
1115 This enum type defines the share modes that are available when
1116 creating a QPixmap object from a raw X11 Pixmap handle.
1117
1118 \value ImplicitlyShared This mode will cause the QPixmap object to
1119 create a copy of the internal data before it is modified, thus
1120 keeping the original X11 pixmap intact.
1121
1122 \value ExplicitlyShared In this mode, the pixmap data will \e not be
1123 copied before it is modified, which in effect will change the
1124 original X11 pixmap.
1125
1126 \warning This enum is only used for X11 specific functions; using
1127 it is non-portable.
1128
1129 \sa QPixmap::fromX11Pixmap()
1130*/
1131
1132/*!
1133 \since 4.5
1134
1135 \fn QPixmap QPixmap::fromX11Pixmap(Qt::HANDLE pixmap, QPixmap::ShareMode mode)
1136
1137 Creates a QPixmap from the native X11 Pixmap handle \a pixmap,
1138 using \a mode as the share mode. The default share mode is
1139 QPixmap::ImplicitlyShared, which means that a copy of the pixmap is
1140 made if someone tries to modify it by e.g. drawing onto it.
1141
1142 QPixmap does \e not take ownership of the \a pixmap handle, and
1143 have to be deleted by the user.
1144
1145 \warning This function is X11 specific; using it is non-portable.
1146
1147 \sa QPixmap::ShareMode
1148*/
1149
1150
1151#if defined(Q_WS_X11) || defined(Q_WS_QWS)
1152
1153/*!
1154 Returns the pixmap's handle to the device context.
1155
1156 Note that, since QPixmap make use of \l {Implicit Data
1157 Sharing}{implicit data sharing}, the detach() function must be
1158 called explicitly to ensure that only \e this pixmap's data is
1159 modified if the pixmap data is shared.
1160
1161 \warning This function is X11 specific; using it is non-portable.
1162
1163 \sa detach()
1164*/
1165
1166Qt::HANDLE QPixmap::handle() const
1167{
1168#if defined(Q_WS_X11)
1169 if (data && data->classId() == QPixmapData::X11Class)
1170 return static_cast<const QX11PixmapData*>(data.constData())->handle();
1171#endif
1172 return 0;
1173}
1174#endif
1175
1176
1177#ifdef QT3_SUPPORT
1178static Qt::ImageConversionFlags colorModeToFlags(QPixmap::ColorMode mode)
1179{
1180 Qt::ImageConversionFlags flags = Qt::AutoColor;
1181 switch (mode) {
1182 case QPixmap::Color:
1183 flags |= Qt::ColorOnly;
1184 break;
1185 case QPixmap::Mono:
1186 flags |= Qt::MonoOnly;
1187 break;
1188 default:
1189 break;// Nothing.
1190 }
1191 return flags;
1192}
1193
1194/*!
1195 Use the constructor that takes a Qt::ImageConversionFlag instead.
1196*/
1197
1198QPixmap::QPixmap(const QString& fileName, const char *format, ColorMode mode)
1199 : QPaintDevice()
1200{
1201 init(0, 0, QPixmapData::PixmapType);
1202 if (!qt_pixmap_thread_test())
1203 return;
1204
1205 load(fileName, format, colorModeToFlags(mode));
1206}
1207
1208/*!
1209 Constructs a pixmap from the QImage \a image.
1210
1211 Use the static fromImage() function instead.
1212*/
1213QPixmap::QPixmap(const QImage& image)
1214 : QPaintDevice()
1215{
1216 init(0, 0, QPixmapData::PixmapType);
1217 if (!qt_pixmap_thread_test())
1218 return;
1219
1220 if (data && data->pixelType() == QPixmapData::BitmapType)
1221 *this = QBitmap::fromImage(image);
1222 else
1223 *this = fromImage(image);
1224}
1225
1226/*!
1227 \overload
1228
1229 Converts the given \a image to a pixmap that is assigned to this
1230 pixmap.
1231
1232 Use the static fromImage() function instead.
1233*/
1234
1235QPixmap &QPixmap::operator=(const QImage &image)
1236{
1237 if (data && data->pixelType() == QPixmapData::BitmapType)
1238 *this = QBitmap::fromImage(image);
1239 else
1240 *this = fromImage(image);
1241 return *this;
1242}
1243
1244/*!
1245 Use the load() function that takes a Qt::ImageConversionFlag instead.
1246*/
1247
1248bool QPixmap::load(const QString &fileName, const char *format, ColorMode mode)
1249{
1250 return load(fileName, format, colorModeToFlags(mode));
1251}
1252
1253/*!
1254 Use the loadFromData() function that takes a Qt::ImageConversionFlag instead.
1255*/
1256
1257bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, ColorMode mode)
1258{
1259 return loadFromData(buf, len, format, colorModeToFlags(mode));
1260}
1261
1262/*!
1263 Use the static fromImage() function instead.
1264*/
1265bool QPixmap::convertFromImage(const QImage &image, ColorMode mode)
1266{
1267 if (data && data->pixelType() == QPixmapData::BitmapType)
1268 *this = QBitmap::fromImage(image, colorModeToFlags(mode));
1269 else
1270 *this = fromImage(image, colorModeToFlags(mode));
1271 return !isNull();
1272}
1273
1274#endif
1275
1276/*****************************************************************************
1277 QPixmap stream functions
1278 *****************************************************************************/
1279#if !defined(QT_NO_DATASTREAM)
1280/*!
1281 \relates QPixmap
1282
1283 Writes the given \a pixmap to the given \a stream as a PNG
1284 image. Note that writing the stream to a file will not produce a
1285 valid image file.
1286
1287 \sa QPixmap::save(), {Format of the QDataStream Operators}
1288*/
1289
1290QDataStream &operator<<(QDataStream &stream, const QPixmap &pixmap)
1291{
1292 return stream << pixmap.toImage();
1293}
1294
1295/*!
1296 \relates QPixmap
1297
1298 Reads an image from the given \a stream into the given \a pixmap.
1299
1300 \sa QPixmap::load(), {Format of the QDataStream Operators}
1301*/
1302
1303QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
1304{
1305 QImage image;
1306 stream >> image;
1307
1308 if (image.isNull()) {
1309 pixmap = QPixmap();
1310 } else if (image.depth() == 1) {
1311 pixmap = QBitmap::fromImage(image);
1312 } else {
1313 pixmap = QPixmap::fromImage(image);
1314 }
1315 return stream;
1316}
1317
1318#endif // QT_NO_DATASTREAM
1319
1320#ifdef QT3_SUPPORT
1321Q_GUI_EXPORT void copyBlt(QPixmap *dst, int dx, int dy,
1322 const QPixmap *src, int sx, int sy, int sw, int sh)
1323{
1324 Q_ASSERT_X(dst, "::copyBlt", "Destination pixmap must be non-null");
1325 Q_ASSERT_X(src, "::copyBlt", "Source pixmap must be non-null");
1326
1327 if (src->hasAlphaChannel()) {
1328 if (dst->paintEngine()->hasFeature(QPaintEngine::PorterDuff)) {
1329 QPainter p(dst);
1330 p.setCompositionMode(QPainter::CompositionMode_Source);
1331 p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
1332 } else {
1333 QImage image = dst->toImage().convertToFormat(QImage::Format_ARGB32_Premultiplied);
1334 QPainter p(&image);
1335 p.setCompositionMode(QPainter::CompositionMode_Source);
1336 p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
1337 p.end();
1338 *dst = QPixmap::fromImage(image);
1339 }
1340 } else {
1341 QPainter p(dst);
1342 p.drawPixmap(dx, dy, *src, sx, sy, sw, sh);
1343 }
1344
1345}
1346#endif
1347
1348/*!
1349 \internal
1350*/
1351
1352bool QPixmap::isDetached() const
1353{
1354 return data && data->ref == 1;
1355}
1356
1357/*! \internal
1358 ### Qt5 - remove me.
1359*/
1360void QPixmap::deref()
1361{
1362 Q_ASSERT_X(false, "QPixmap::deref()", "Do not call this function anymore!");
1363}
1364
1365/*!
1366 \fn QImage QPixmap::convertToImage() const
1367
1368 Use the toImage() function instead.
1369*/
1370
1371/*!
1372 \fn bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags)
1373
1374 Use the static fromImage() function instead.
1375*/
1376
1377/*!
1378 \fn QPixmap QPixmap::xForm(const QMatrix &matrix) const
1379
1380 Use transformed() instead.
1381*/
1382
1383/*!
1384 \fn QPixmap QPixmap::scaled(int width, int height,
1385 Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode
1386 transformMode) const
1387
1388 \overload
1389
1390 Returns a copy of the pixmap scaled to a rectangle with the given
1391 \a width and \a height according to the given \a aspectRatioMode and
1392 \a transformMode.
1393
1394 If either the \a width or the \a height is zero or negative, this
1395 function returns a null pixmap.
1396*/
1397
1398/*!
1399 \fn QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode
1400 aspectRatioMode, Qt::TransformationMode transformMode) const
1401
1402 Scales the pixmap to the given \a size, using the aspect ratio and
1403 transformation modes specified by \a aspectRatioMode and \a
1404 transformMode.
1405
1406 \image qimage-scaling.png
1407
1408 \list
1409 \i If \a aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap
1410 is scaled to \a size.
1411 \i If \a aspectRatioMode is Qt::KeepAspectRatio, the pixmap is
1412 scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
1413 \i If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
1414 the pixmap is scaled to a rectangle as small as possible
1415 outside \a size, preserving the aspect ratio.
1416 \endlist
1417
1418 If the given \a size is empty, this function returns a null
1419 pixmap.
1420
1421
1422 In some cases it can be more beneficial to draw the pixmap to a
1423 painter with a scale set rather than scaling the pixmap. This is
1424 the case when the painter is for instance based on OpenGL or when
1425 the scale factor changes rapidly.
1426
1427 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1428 Transformations}
1429
1430*/
1431QPixmap QPixmap::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
1432{
1433 if (isNull()) {
1434 qWarning("QPixmap::scaled: Pixmap is a null pixmap");
1435 return QPixmap();
1436 }
1437 if (s.isEmpty())
1438 return QPixmap();
1439
1440 QSize newSize = size();
1441 newSize.scale(s, aspectMode);
1442 if (newSize == size())
1443 return *this;
1444
1445 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1446 (qreal)newSize.height() / height());
1447 QPixmap pix = transformed(wm, mode);
1448 return pix;
1449}
1450
1451/*!
1452 \fn QPixmap QPixmap::scaledToWidth(int width, Qt::TransformationMode
1453 mode) const
1454
1455 Returns a scaled copy of the image. The returned image is scaled
1456 to the given \a width using the specified transformation \a mode.
1457 The height of the pixmap is automatically calculated so that the
1458 aspect ratio of the pixmap is preserved.
1459
1460 If \a width is 0 or negative, a null pixmap is returned.
1461
1462 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1463 Transformations}
1464*/
1465QPixmap QPixmap::scaledToWidth(int w, Qt::TransformationMode mode) const
1466{
1467 if (isNull()) {
1468 qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap");
1469 return copy();
1470 }
1471 if (w <= 0)
1472 return QPixmap();
1473
1474 qreal factor = (qreal) w / width();
1475 QTransform wm = QTransform::fromScale(factor, factor);
1476 return transformed(wm, mode);
1477}
1478
1479/*!
1480 \fn QPixmap QPixmap::scaledToHeight(int height,
1481 Qt::TransformationMode mode) const
1482
1483 Returns a scaled copy of the image. The returned image is scaled
1484 to the given \a height using the specified transformation \a mode.
1485 The width of the pixmap is automatically calculated so that the
1486 aspect ratio of the pixmap is preserved.
1487
1488 If \a height is 0 or negative, a null pixmap is returned.
1489
1490 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1491 Transformations}
1492*/
1493QPixmap QPixmap::scaledToHeight(int h, Qt::TransformationMode mode) const
1494{
1495 if (isNull()) {
1496 qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap");
1497 return copy();
1498 }
1499 if (h <= 0)
1500 return QPixmap();
1501
1502 qreal factor = (qreal) h / height();
1503 QTransform wm = QTransform::fromScale(factor, factor);
1504 return transformed(wm, mode);
1505}
1506
1507/*!
1508 Returns a copy of the pixmap that is transformed using the given
1509 transformation \a transform and transformation \a mode. The original
1510 pixmap is not changed.
1511
1512 The transformation \a transform is internally adjusted to compensate
1513 for unwanted translation; i.e. the pixmap produced is the smallest
1514 pixmap that contains all the transformed points of the original
1515 pixmap. Use the trueMatrix() function to retrieve the actual
1516 matrix used for transforming the pixmap.
1517
1518 This function is slow because it involves transformation to a
1519 QImage, non-trivial computations and a transformation back to a
1520 QPixmap.
1521
1522 \sa trueMatrix(), {QPixmap#Pixmap Transformations}{Pixmap
1523 Transformations}
1524*/
1525QPixmap QPixmap::transformed(const QTransform &transform,
1526 Qt::TransformationMode mode) const
1527{
1528 if (isNull() || transform.type() <= QTransform::TxTranslate)
1529 return *this;
1530
1531 return data->transformed(transform, mode);
1532}
1533
1534/*!
1535 \overload
1536
1537 This convenience function loads the \a matrix into a
1538 QTransform and calls the overloaded function.
1539 */
1540QPixmap QPixmap::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
1541{
1542 return transformed(QTransform(matrix), mode);
1543}
1544
1545
1546
1547
1548
1549
1550
1551
1552/*!
1553 \class QPixmap
1554
1555 \brief The QPixmap class is an off-screen image representation
1556 that can be used as a paint device.
1557
1558 \ingroup painting
1559 \ingroup shared
1560
1561
1562 Qt provides four classes for handling image data: QImage, QPixmap,
1563 QBitmap and QPicture. QImage is designed and optimized for I/O,
1564 and for direct pixel access and manipulation, while QPixmap is
1565 designed and optimized for showing images on screen. QBitmap is
1566 only a convenience class that inherits QPixmap, ensuring a depth
1567 of 1. The isQBitmap() function returns true if a QPixmap object is
1568 really a bitmap, otherwise returns false. Finally, the QPicture class
1569 is a paint device that records and replays QPainter commands.
1570
1571 A QPixmap can easily be displayed on the screen using QLabel or
1572 one of QAbstractButton's subclasses (such as QPushButton and
1573 QToolButton). QLabel has a pixmap property, whereas
1574 QAbstractButton has an icon property.
1575
1576 In addition to the ordinary constructors, a QPixmap can be
1577 constructed using the static grabWidget() and grabWindow()
1578 functions which creates a QPixmap and paints the given widget, or
1579 window, into it.
1580
1581 QPixmap objects can be passed around by value since the QPixmap
1582 class uses implicit data sharing. For more information, see the \l
1583 {Implicit Data Sharing} documentation. QPixmap objects can also be
1584 streamed.
1585
1586 Depending on the system, QPixmap is stored using a RGB32 or a
1587 premultiplied alpha format. If the image has an alpha channel, and
1588 if the system allows, the preferred format is premultiplied alpha.
1589 Note also that QPixmap, unlike QImage, may be hardware dependent.
1590 On X11, Mac and Symbian, a QPixmap is stored on the server side while
1591 a QImage is stored on the client side (on Windows, these two classes
1592 have an equivalent internal representation, i.e. both QImage and
1593 QPixmap are stored on the client side and don't use any GDI
1594 resources).
1595
1596 Note that the pixel data in a pixmap is internal and is managed by
1597 the underlying window system. Because QPixmap is a QPaintDevice
1598 subclass, QPainter can be used to draw directly onto pixmaps.
1599 Pixels can only be accessed through QPainter functions or by
1600 converting the QPixmap to a QImage. However, the fill() function
1601 is available for initializing the entire pixmap with a given color.
1602
1603 There are functions to convert between QImage and
1604 QPixmap. Typically, the QImage class is used to load an image
1605 file, optionally manipulating the image data, before the QImage
1606 object is converted into a QPixmap to be shown on
1607 screen. Alternatively, if no manipulation is desired, the image
1608 file can be loaded directly into a QPixmap. On Windows, the
1609 QPixmap class also supports conversion between \c HBITMAP and
1610 QPixmap. On Symbian, the QPixmap class also supports conversion
1611 between CFbsBitmap and QPixmap.
1612
1613 QPixmap provides a collection of functions that can be used to
1614 obtain a variety of information about the pixmap. In addition,
1615 there are several functions that enables transformation of the
1616 pixmap.
1617
1618 \tableofcontents
1619
1620 \section1 Reading and Writing Image Files
1621
1622 QPixmap provides several ways of reading an image file: The file
1623 can be loaded when constructing the QPixmap object, or by using
1624 the load() or loadFromData() functions later on. When loading an
1625 image, the file name can either refer to an actual file on disk or
1626 to one of the application's embedded resources. See \l{The Qt
1627 Resource System} overview for details on how to embed images and
1628 other resource files in the application's executable.
1629
1630 Simply call the save() function to save a QPixmap object.
1631
1632 The complete list of supported file formats are available through
1633 the QImageReader::supportedImageFormats() and
1634 QImageWriter::supportedImageFormats() functions. New file formats
1635 can be added as plugins. By default, Qt supports the following
1636 formats:
1637
1638 \table
1639 \header \o Format \o Description \o Qt's support
1640 \row \o BMP \o Windows Bitmap \o Read/write
1641 \row \o GIF \o Graphic Interchange Format (optional) \o Read
1642 \row \o JPG \o Joint Photographic Experts Group \o Read/write
1643 \row \o JPEG \o Joint Photographic Experts Group \o Read/write
1644 \row \o PNG \o Portable Network Graphics \o Read/write
1645 \row \o PBM \o Portable Bitmap \o Read
1646 \row \o PGM \o Portable Graymap \o Read
1647 \row \o PPM \o Portable Pixmap \o Read/write
1648 \row \o XBM \o X11 Bitmap \o Read/write
1649 \row \o XPM \o X11 Pixmap \o Read/write
1650 \endtable
1651
1652 \section1 Pixmap Information
1653
1654 QPixmap provides a collection of functions that can be used to
1655 obtain a variety of information about the pixmap:
1656
1657 \table
1658 \header
1659 \o \o Available Functions
1660 \row
1661 \o Geometry
1662 \o
1663 The size(), width() and height() functions provide information
1664 about the pixmap's size. The rect() function returns the image's
1665 enclosing rectangle.
1666
1667 \row
1668 \o Alpha component
1669 \o
1670
1671 The hasAlphaChannel() returns true if the pixmap has a format that
1672 respects the alpha channel, otherwise returns false, while the
1673 hasAlpha() function returns true if the pixmap has an alpha
1674 channel \e or a mask (otherwise false). The mask() function returns
1675 the mask as a QBitmap object, which can be set using setMask().
1676
1677 The createHeuristicMask() function creates and returns a 1-bpp
1678 heuristic mask (i.e. a QBitmap) for this pixmap. It works by
1679 selecting a color from one of the corners and then chipping away
1680 pixels of that color, starting at all the edges. The
1681 createMaskFromColor() function creates and returns a mask (i.e. a
1682 QBitmap) for the pixmap based on a given color.
1683
1684 \row
1685 \o Low-level information
1686 \o
1687
1688 The depth() function returns the depth of the pixmap. The
1689 defaultDepth() function returns the default depth, i.e. the depth
1690 used by the application on the given screen.
1691
1692 The cacheKey() function returns a number that uniquely
1693 identifies the contents of the QPixmap object.
1694
1695 The x11Info() function returns information about the configuration
1696 of the X display used by the screen to which the pixmap currently
1697 belongs. The x11PictureHandle() function returns the X11 Picture
1698 handle of the pixmap for XRender support. Note that the two latter
1699 functions are only available on x11.
1700
1701 \endtable
1702
1703 \section1 Pixmap Conversion
1704
1705 A QPixmap object can be converted into a QImage using the
1706 toImage() function. Likewise, a QImage can be converted into a
1707 QPixmap using the fromImage(). If this is too expensive an
1708 operation, you can use QBitmap::fromImage() instead.
1709
1710 In addition, on Windows, the QPixmap class supports conversion to
1711 and from HBITMAP: the toWinHBITMAP() function creates a HBITMAP
1712 equivalent to the QPixmap, based on the given HBitmapFormat, and
1713 returns the HBITMAP handle. The fromWinHBITMAP() function returns
1714 a QPixmap that is equivalent to the given bitmap which has the
1715 specified format. The QPixmap class also supports conversion to
1716 and from HICON: the toWinHICON() function creates a HICON equivalent
1717 to the QPixmap, and returns the HICON handle. The fromWinHICON()
1718 function returns a QPixmap that is equivalent to the given icon.
1719
1720 In addition, on Symbian, the QPixmap class supports conversion to
1721 and from CFbsBitmap: the toSymbianCFbsBitmap() function creates
1722 CFbsBitmap equivalent to the QPixmap, based on given mode and returns
1723 a CFbsBitmap object. The fromSymbianCFbsBitmap() function returns a
1724 QPixmap that is equivalent to the given bitmap and given mode.
1725
1726 \section1 Pixmap Transformations
1727
1728 QPixmap supports a number of functions for creating a new pixmap
1729 that is a transformed version of the original:
1730
1731 The scaled(), scaledToWidth() and scaledToHeight() functions
1732 return scaled copies of the pixmap, while the copy() function
1733 creates a QPixmap that is a plain copy of the original one.
1734
1735 The transformed() function returns a copy of the pixmap that is
1736 transformed with the given transformation matrix and
1737 transformation mode: Internally, the transformation matrix is
1738 adjusted to compensate for unwanted translation,
1739 i.e. transformed() returns the smallest pixmap containing all
1740 transformed points of the original pixmap. The static trueMatrix()
1741 function returns the actual matrix used for transforming the
1742 pixmap.
1743
1744 \sa QBitmap, QImage, QImageReader, QImageWriter
1745*/
1746
1747
1748/*!
1749 \typedef QPixmap::DataPtr
1750 \internal
1751*/
1752
1753/*!
1754 \fn DataPtr &QPixmap::data_ptr()
1755 \internal
1756*/
1757
1758/*!
1759 Returns true if this pixmap has an alpha channel, \e or has a
1760 mask, otherwise returns false.
1761
1762 \sa hasAlphaChannel(), mask()
1763*/
1764bool QPixmap::hasAlpha() const
1765{
1766 return data && (data->hasAlphaChannel() || !data->mask().isNull());
1767}
1768
1769/*!
1770 Returns true if the pixmap has a format that respects the alpha
1771 channel, otherwise returns false.
1772
1773 \sa hasAlpha()
1774*/
1775bool QPixmap::hasAlphaChannel() const
1776{
1777 return data && data->hasAlphaChannel();
1778}
1779
1780/*!
1781 \internal
1782*/
1783int QPixmap::metric(PaintDeviceMetric metric) const
1784{
1785 return data ? data->metric(metric) : 0;
1786}
1787
1788/*!
1789 \fn void QPixmap::setAlphaChannel(const QPixmap &alphaChannel)
1790 \obsolete
1791
1792 Sets the alpha channel of this pixmap to the given \a alphaChannel
1793 by converting the \a alphaChannel into 32 bit and using the
1794 intensity of the RGB pixel values.
1795
1796 The effect of this function is undefined when the pixmap is being
1797 painted on.
1798
1799 \warning This is potentially an expensive operation. Most usecases
1800 for this function are covered by QPainter and compositionModes
1801 which will normally execute faster.
1802
1803 \sa alphaChannel(), {QPixmap#Pixmap Transformations}{Pixmap
1804 Transformations}
1805 */
1806void QPixmap::setAlphaChannel(const QPixmap &alphaChannel)
1807{
1808 if (alphaChannel.isNull())
1809 return;
1810
1811 if (paintingActive()) {
1812 qWarning("QPixmap::setAlphaChannel: "
1813 "Cannot set alpha channel while pixmap is being painted on");
1814 return;
1815 }
1816
1817 if (width() != alphaChannel.width() && height() != alphaChannel.height()) {
1818 qWarning("QPixmap::setAlphaChannel: "
1819 "The pixmap and the alpha channel pixmap must have the same size");
1820 return;
1821 }
1822
1823 detach();
1824 data->setAlphaChannel(alphaChannel);
1825}
1826
1827/*!
1828 \obsolete
1829
1830 Returns the alpha channel of the pixmap as a new grayscale QPixmap in which
1831 each pixel's red, green, and blue values are given the alpha value of the
1832 original pixmap. The color depth of the returned pixmap is the system depth
1833 on X11 and 8-bit on Windows and Mac OS X.
1834
1835 You can use this function while debugging
1836 to get a visible image of the alpha channel. If the pixmap doesn't have an
1837 alpha channel, i.e., the alpha channel's value for all pixels equals
1838 0xff), a null pixmap is returned. You can check this with the \c isNull()
1839 function.
1840
1841 We show an example:
1842
1843 \snippet doc/src/snippets/alphachannel.cpp 0
1844
1845 \image alphachannelimage.png The pixmap and channelImage QPixmaps
1846
1847 \warning This is an expensive operation. The alpha channel of the
1848 pixmap is extracted dynamically from the pixeldata. Most usecases of this
1849 function are covered by QPainter and compositionModes which will normally
1850 execute faster.
1851
1852 \sa setAlphaChannel(), {QPixmap#Pixmap Information}{Pixmap
1853 Information}
1854*/
1855QPixmap QPixmap::alphaChannel() const
1856{
1857 return data ? data->alphaChannel() : QPixmap();
1858}
1859
1860/*!
1861 \internal
1862*/
1863QPaintEngine *QPixmap::paintEngine() const
1864{
1865 return data ? data->paintEngine() : 0;
1866}
1867
1868/*!
1869 \fn QBitmap QPixmap::mask() const
1870
1871 Extracts a bitmap mask from the pixmap's alpha channel.
1872
1873 \warning This is potentially an expensive operation. The mask of
1874 the pixmap is extracted dynamically from the pixeldata.
1875
1876 \sa setMask(), {QPixmap#Pixmap Information}{Pixmap Information}
1877*/
1878QBitmap QPixmap::mask() const
1879{
1880 return data ? data->mask() : QBitmap();
1881}
1882
1883/*!
1884 Returns the default pixmap depth used by the application.
1885
1886 On Windows and Mac, the default depth is always 32. On X11 and
1887 embedded, the depth of the screen will be returned by this
1888 function.
1889
1890 \sa depth(), QColormap::depth(), {QPixmap#Pixmap Information}{Pixmap Information}
1891
1892*/
1893int QPixmap::defaultDepth()
1894{
1895#if defined(Q_WS_QWS)
1896 return QScreen::instance()->depth();
1897#elif defined(Q_WS_X11)
1898 return QX11Info::appDepth();
1899#elif defined(Q_WS_WINCE)
1900 return QColormap::instance().depth();
1901#elif defined(Q_WS_WIN)
1902 return 32; // XXX
1903#elif defined(Q_WS_PM)
1904 return 32; // @todo check this
1905#elif defined(Q_WS_MAC)
1906 return 32;
1907#elif defined(Q_OS_SYMBIAN)
1908 return S60->screenDepth;
1909#endif
1910}
1911
1912/*!
1913 Detaches the pixmap from shared pixmap data.
1914
1915 A pixmap is automatically detached by Qt whenever its contents are
1916 about to change. This is done in almost all QPixmap member
1917 functions that modify the pixmap (fill(), fromImage(),
1918 load(), etc.), and in QPainter::begin() on a pixmap.
1919
1920 There are two exceptions in which detach() must be called
1921 explicitly, that is when calling the handle() or the
1922 x11PictureHandle() function (only available on X11). Otherwise,
1923 any modifications done using system calls, will be performed on
1924 the shared data.
1925
1926 The detach() function returns immediately if there is just a
1927 single reference or if the pixmap has not been initialized yet.
1928*/
1929void QPixmap::detach()
1930{
1931 if (!data)
1932 return;
1933
1934 QPixmapData::ClassId id = data->classId();
1935 if (id == QPixmapData::RasterClass) {
1936 QRasterPixmapData *rasterData = static_cast<QRasterPixmapData*>(data.data());
1937 rasterData->image.detach();
1938 }
1939
1940 if (data->is_cached && data->ref == 1)
1941 QImagePixmapCleanupHooks::executePixmapModificationHooks(this);
1942
1943#if defined(Q_WS_MAC)
1944 QMacPixmapData *macData = id == QPixmapData::MacClass ? static_cast<QMacPixmapData*>(data.data()) : 0;
1945 if (macData) {
1946 if (macData->cg_mask) {
1947 CGImageRelease(macData->cg_mask);
1948 macData->cg_mask = 0;
1949 }
1950 }
1951#endif
1952
1953 if (data->ref != 1) {
1954 *this = copy();
1955 }
1956 ++data->detach_no;
1957
1958#if defined(Q_WS_X11)
1959 if (data->classId() == QPixmapData::X11Class) {
1960 QX11PixmapData *d = static_cast<QX11PixmapData*>(data.data());
1961 d->flags &= ~QX11PixmapData::Uninitialized;
1962
1963 // reset the cache data
1964 if (d->hd2) {
1965 XFreePixmap(X11->display, d->hd2);
1966 d->hd2 = 0;
1967 }
1968 }
1969#elif defined(Q_WS_MAC)
1970 if (macData) {
1971 macData->macReleaseCGImageRef();
1972 macData->uninit = false;
1973 }
1974#endif
1975}
1976
1977/*!
1978 \fn QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1979
1980 Converts the given \a image to a pixmap using the specified \a
1981 flags to control the conversion. The \a flags argument is a
1982 bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for \a
1983 flags sets all the default options.
1984
1985 In case of monochrome and 8-bit images, the image is first
1986 converted to a 32-bit pixmap and then filled with the colors in
1987 the color table. If this is too expensive an operation, you can
1988 use QBitmap::fromImage() instead.
1989
1990 \sa toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1991*/
1992QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1993{
1994 if (image.isNull())
1995 return QPixmap();
1996
1997 QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem();
1998 QScopedPointer<QPixmapData> data(gs ? gs->createPixmapData(QPixmapData::PixmapType)
1999 : QGraphicsSystem::createDefaultPixmapData(QPixmapData::PixmapType));
2000 data->fromImage(image, flags);
2001 return QPixmap(data.take());
2002}
2003
2004/*!
2005 \fn QPixmap QPixmap::grabWindow(WId window, int x, int y, int
2006 width, int height)
2007
2008 Creates and returns a pixmap constructed by grabbing the contents
2009 of the given \a window restricted by QRect(\a x, \a y, \a width,
2010 \a height).
2011
2012 The arguments (\a{x}, \a{y}) specify the offset in the window,
2013 whereas (\a{width}, \a{height}) specify the area to be copied. If
2014 \a width is negative, the function copies everything to the right
2015 border of the window. If \a height is negative, the function
2016 copies everything to the bottom of the window.
2017
2018 The window system identifier (\c WId) can be retrieved using the
2019 QWidget::winId() function. The rationale for using a window
2020 identifier and not a QWidget, is to enable grabbing of windows
2021 that are not part of the application, window system frames, and so
2022 on.
2023
2024 The grabWindow() function grabs pixels from the screen, not from
2025 the window, i.e. if there is another window partially or entirely
2026 over the one you grab, you get pixels from the overlying window,
2027 too. The mouse cursor is generally not grabbed.
2028
2029 Note on X11that if the given \a window doesn't have the same depth
2030 as the root window, and another window partially or entirely
2031 obscures the one you grab, you will \e not get pixels from the
2032 overlying window. The contents of the obscured areas in the
2033 pixmap will be undefined and uninitialized.
2034
2035 \warning In general, grabbing an area outside the screen is not
2036 safe. This depends on the underlying window system.
2037
2038 \sa grabWidget(), {Screenshot Example}
2039*/
2040
2041/*!
2042 \internal
2043*/
2044QPixmapData* QPixmap::pixmapData() const
2045{
2046 return data.data();
2047}
2048
2049/*!
2050 \enum QPixmap::HBitmapFormat
2051
2052 \bold{Win32 only:} This enum defines how the conversion between \c
2053 HBITMAP and QPixmap is performed.
2054
2055 \warning This enum is only available on Windows.
2056
2057 \value NoAlpha The alpha channel is ignored and always treated as
2058 being set to fully opaque. This is preferred if the \c HBITMAP is
2059 used with standard GDI calls, such as \c BitBlt().
2060
2061 \value PremultipliedAlpha The \c HBITMAP is treated as having an
2062 alpha channel and premultiplied colors. This is preferred if the
2063 \c HBITMAP is accessed through the \c AlphaBlend() GDI function.
2064
2065 \value Alpha The \c HBITMAP is treated as having a plain alpha
2066 channel. This is the preferred format if the \c HBITMAP is going
2067 to be used as an application icon or systray icon.
2068
2069 \sa fromWinHBITMAP(), toWinHBITMAP()
2070*/
2071
2072/*! \fn HBITMAP QPixmap::toWinHBITMAP(HBitmapFormat format) const
2073 \bold{Win32 only:} Creates a \c HBITMAP equivalent to the QPixmap,
2074 based on the given \a format. Returns the \c HBITMAP handle.
2075
2076 It is the caller's responsibility to free the \c HBITMAP data
2077 after use.
2078
2079 \warning This function is only available on Windows.
2080
2081 \sa fromWinHBITMAP(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
2082*/
2083
2084/*! \fn QPixmap QPixmap::fromWinHBITMAP(HBITMAP bitmap, HBitmapFormat format)
2085 \bold{Win32 only:} Returns a QPixmap that is equivalent to the
2086 given \a bitmap. The conversion is based on the specified \a
2087 format.
2088
2089 \warning This function is only available on Windows.
2090
2091 \sa toWinHBITMAP(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
2092
2093*/
2094
2095/*! \fn HICON QPixmap::toWinHICON() const
2096 \since 4.6
2097
2098 \bold{Win32 only:} Creates a \c HICON equivalent to the QPixmap.
2099 Returns the \c HICON handle.
2100
2101 It is the caller's responsibility to free the \c HICON data after use.
2102
2103 \warning This function is only available on Windows.
2104
2105 \sa fromWinHICON(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
2106*/
2107
2108/*! \fn QPixmap QPixmap::fromWinHICON(HICON icon)
2109 \since 4.6
2110
2111 \bold{Win32 only:} Returns a QPixmap that is equivalent to the given
2112 \a icon.
2113
2114 \warning This function is only available on Windows.
2115
2116 \sa toWinHICON(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
2117
2118*/
2119
2120/*! \fn const QX11Info &QPixmap::x11Info() const
2121 \bold{X11 only:} Returns information about the configuration of
2122 the X display used by the screen to which the pixmap currently belongs.
2123
2124 \warning This function is only available on X11.
2125
2126 \sa {QPixmap#Pixmap Information}{Pixmap Information}
2127*/
2128
2129/*! \fn Qt::HANDLE QPixmap::x11PictureHandle() const
2130 \bold{X11 only:} Returns the X11 Picture handle of the pixmap for
2131 XRender support.
2132
2133 This function will return 0 if XRender support is not compiled
2134 into Qt, if the XRender extension is not supported on the X11
2135 display, or if the handle could not be created. Use of this
2136 function is not portable.
2137
2138 \warning This function is only available on X11.
2139
2140 \sa {QPixmap#Pixmap Information}{Pixmap Information}
2141*/
2142
2143/*! \fn int QPixmap::x11SetDefaultScreen(int screen)
2144 \internal
2145*/
2146
2147/*! \fn void QPixmap::x11SetScreen(int screen)
2148 \internal
2149*/
2150
2151/*! \fn QRgb* QPixmap::clut() const
2152 \internal
2153*/
2154
2155/*! \fn int QPixmap::numCols() const
2156 \obsolete
2157 \internal
2158 \sa colorCount()
2159*/
2160
2161/*! \fn int QPixmap::colorCount() const
2162 \since 4.6
2163 \internal
2164*/
2165
2166/*! \fn const uchar* QPixmap::qwsBits() const
2167 \internal
2168 \since 4.1
2169*/
2170
2171/*! \fn int QPixmap::qwsBytesPerLine() const
2172 \internal
2173 \since 4.1
2174*/
2175
2176QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.