Remove the usage of deprecated qdoc macros.
[profile/ivi/qtbase.git] / src / gui / image / qpixmap.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <qglobal.h>
43
44 #include "qpixmap.h"
45 #include "qplatformpixmap_qpa.h"
46 #include "qimagepixmapcleanuphooks_p.h"
47
48 #include "qbitmap.h"
49 #include "qimage.h"
50 #include "qpainter.h"
51 #include "qdatastream.h"
52 #include "qbuffer.h"
53 #include <private/qguiapplication_p.h>
54 #include "qevent.h"
55 #include "qfile.h"
56 #include "qfileinfo.h"
57 #include "qpixmapcache.h"
58 #include "qdatetime.h"
59 #include "qimagereader.h"
60 #include "qimagewriter.h"
61 #include "qpaintengine.h"
62 #include "qscreen.h"
63 #include "qthread.h"
64 #include "qdebug.h"
65
66 # include "qplatformintegration_qpa.h"
67
68 #include "qpixmap_raster_p.h"
69 #include "private/qhexstring_p.h"
70
71 QT_BEGIN_NAMESPACE
72
73 static bool qt_pixmap_thread_test()
74 {
75     if (!QCoreApplication::instance()) {
76         qFatal("QPixmap: Must construct a QApplication before a QPaintDevice");
77         return false;
78     }
79
80     if (qApp->thread() != QThread::currentThread()) {
81         bool fail = false;
82         if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
83             printf("Lighthouse plugin does not support threaded pixmaps!\n");
84             fail = true;
85         }
86         if (fail) {
87             qWarning("QPixmap: It is not safe to use pixmaps outside the GUI thread");
88             return false;
89         }
90     }
91     return true;
92 }
93
94 void QPixmap::doInit(int w, int h, int type)
95 {
96     if ((w > 0 && h > 0) || type == QPlatformPixmap::BitmapType)
97         data = QPlatformPixmap::create(w, h, (QPlatformPixmap::PixelType) type);
98     else
99         data = 0;
100 }
101
102 /*!
103     Constructs a null pixmap.
104
105     \sa isNull()
106 */
107
108 QPixmap::QPixmap()
109     : QPaintDevice()
110 {
111     (void) qt_pixmap_thread_test();
112     doInit(0, 0, QPlatformPixmap::PixmapType);
113 }
114
115 /*!
116     \fn QPixmap::QPixmap(int width, int height)
117
118     Constructs a pixmap with the given \a width and \a height. If
119     either \a width or \a height is zero, a null pixmap is
120     constructed.
121
122     \warning This will create a QPixmap with uninitialized data. Call
123     fill() to fill the pixmap with an appropriate color before drawing
124     onto it with QPainter.
125
126     \sa isNull()
127 */
128
129 QPixmap::QPixmap(int w, int h)
130     : QPaintDevice()
131 {
132     if (!qt_pixmap_thread_test())
133         doInit(0, 0, QPlatformPixmap::PixmapType);
134     else
135         doInit(w, h, QPlatformPixmap::PixmapType);
136 }
137
138 /*!
139     \overload
140
141     Constructs a pixmap of the given \a size.
142
143     \warning This will create a QPixmap with uninitialized data. Call
144     fill() to fill the pixmap with an appropriate color before drawing
145     onto it with QPainter.
146 */
147
148 QPixmap::QPixmap(const QSize &size)
149     : QPaintDevice()
150 {
151     if (!qt_pixmap_thread_test())
152         doInit(0, 0, QPlatformPixmap::PixmapType);
153     else
154         doInit(size.width(), size.height(), QPlatformPixmap::PixmapType);
155 }
156
157 /*!
158   \internal
159 */
160 QPixmap::QPixmap(const QSize &s, int type)
161 {
162     if (!qt_pixmap_thread_test())
163         doInit(0, 0, static_cast<QPlatformPixmap::PixelType>(type));
164     else
165         doInit(s.width(), s.height(), static_cast<QPlatformPixmap::PixelType>(type));
166 }
167
168 /*!
169     \internal
170 */
171 QPixmap::QPixmap(QPlatformPixmap *d)
172     : QPaintDevice(), data(d)
173 {
174 }
175
176 /*!
177     Constructs a pixmap from the file with the given \a fileName. If the
178     file does not exist or is of an unknown format, the pixmap becomes a
179     null pixmap.
180
181     The loader attempts to read the pixmap using the specified \a
182     format. If the \a format is not specified (which is the default),
183     the loader probes the file for a header to guess the file format.
184
185     The file name can either refer to an actual file on disk or to
186     one of the application's embedded resources. See the
187     \l{resources.html}{Resource System} overview for details on how
188     to embed images and other resource files in the application's
189     executable.
190
191     If the image needs to be modified to fit in a lower-resolution
192     result (e.g. converting from 32-bit to 8-bit), use the \a
193     flags to control the conversion.
194
195     The \a fileName, \a format and \a flags parameters are
196     passed on to load(). This means that the data in \a fileName is
197     not compiled into the binary. If \a fileName contains a relative
198     path (e.g. the filename only) the relevant file must be found
199     relative to the runtime working directory.
200
201     \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
202     Image Files}
203 */
204
205 QPixmap::QPixmap(const QString& fileName, const char *format, Qt::ImageConversionFlags flags)
206     : QPaintDevice()
207 {
208     doInit(0, 0, QPlatformPixmap::PixmapType);
209     if (!qt_pixmap_thread_test())
210         return;
211
212     load(fileName, format, flags);
213 }
214
215 /*!
216     Constructs a pixmap that is a copy of the given \a pixmap.
217
218     \sa copy()
219 */
220
221 QPixmap::QPixmap(const QPixmap &pixmap)
222     : QPaintDevice()
223 {
224     if (!qt_pixmap_thread_test()) {
225         doInit(0, 0, QPlatformPixmap::PixmapType);
226         return;
227     }
228     if (pixmap.paintingActive()) {                // make a deep copy
229         operator=(pixmap.copy());
230     } else {
231         data = pixmap.data;
232     }
233 }
234
235 /*!
236     Constructs a pixmap from the given \a xpm data, which must be a
237     valid XPM image.
238
239     Errors are silently ignored.
240
241     Note that it's possible to squeeze the XPM variable a little bit
242     by using an unusual declaration:
243
244     \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 0
245
246     The extra \c const makes the entire definition read-only, which is
247     slightly more efficient (for example, when the code is in a shared
248     library) and ROMable when the application is to be stored in ROM.
249 */
250 #ifndef QT_NO_IMAGEFORMAT_XPM
251 QPixmap::QPixmap(const char * const xpm[])
252     : QPaintDevice()
253 {
254     doInit(0, 0, QPlatformPixmap::PixmapType);
255     if (!xpm)
256         return;
257
258     QImage image(xpm);
259     if (!image.isNull()) {
260         if (data && data->pixelType() == QPlatformPixmap::BitmapType)
261             *this = QBitmap::fromImage(image);
262         else
263             *this = fromImage(image);
264     }
265 }
266 #endif
267
268
269 /*!
270     Destroys the pixmap.
271 */
272
273 QPixmap::~QPixmap()
274 {
275     Q_ASSERT(!data || data->ref.load() >= 1); // Catch if ref-counting changes again
276 }
277
278 /*!
279   \internal
280 */
281 int QPixmap::devType() const
282 {
283     return QInternal::Pixmap;
284 }
285
286 /*!
287     \fn QPixmap QPixmap::copy(int x, int y, int width, int height) const
288     \overload
289
290     Returns a deep copy of the subset of the pixmap that is specified
291     by the rectangle QRect( \a x, \a y, \a width, \a height).
292 */
293
294 /*!
295     \fn QPixmap QPixmap::copy(const QRect &rectangle) const
296
297     Returns a deep copy of the subset of the pixmap that is specified
298     by the given \a rectangle. For more information on deep copies,
299     see the \l {Implicit Data Sharing} documentation.
300
301     If the given \a rectangle is empty, the whole image is copied.
302
303     \sa operator=(), QPixmap(), {QPixmap#Pixmap
304     Transformations}{Pixmap Transformations}
305 */
306 QPixmap QPixmap::copy(const QRect &rect) const
307 {
308     if (isNull())
309         return QPixmap();
310
311     QRect r(0, 0, width(), height());
312     if (!rect.isEmpty())
313         r = r.intersected(rect);
314
315     QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
316     d->copy(data.data(), r);
317     return QPixmap(d);
318 }
319
320 /*!
321     \fn QPixmap::scroll(int dx, int dy, int x, int y, int width, int height, QRegion *exposed)
322     \since 4.6
323
324     This convenience function is equivalent to calling QPixmap::scroll(\a dx,
325     \a dy, QRect(\a x, \a y, \a width, \a height), \a exposed).
326
327     \sa QWidget::scroll(), QGraphicsItem::scroll()
328 */
329
330 /*!
331     \since 4.6
332
333     Scrolls the area \a rect of this pixmap by (\a dx, \a dy). The exposed
334     region is left unchanged. You can optionally pass a pointer to an empty
335     QRegion to get the region that is \a exposed by the scroll operation.
336
337     \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 2
338
339     You cannot scroll while there is an active painter on the pixmap.
340
341     \sa QWidget::scroll(), QGraphicsItem::scroll()
342 */
343 void QPixmap::scroll(int dx, int dy, const QRect &rect, QRegion *exposed)
344 {
345     if (isNull() || (dx == 0 && dy == 0))
346         return;
347     QRect dest = rect & this->rect();
348     QRect src = dest.translated(-dx, -dy) & dest;
349     if (src.isEmpty()) {
350         if (exposed)
351             *exposed += dest;
352         return;
353     }
354
355     detach();
356
357     if (!data->scroll(dx, dy, src)) {
358         // Fallback
359         QPixmap pix = *this;
360         QPainter painter(&pix);
361         painter.setCompositionMode(QPainter::CompositionMode_Source);
362         painter.drawPixmap(src.translated(dx, dy), *this, src);
363         painter.end();
364         *this = pix;
365     }
366
367     if (exposed) {
368         *exposed += dest;
369         *exposed -= src.translated(dx, dy);
370     }
371 }
372
373 /*!
374     Assigns the given \a pixmap to this pixmap and returns a reference
375     to this pixmap.
376
377     \sa copy(), QPixmap()
378 */
379
380 QPixmap &QPixmap::operator=(const QPixmap &pixmap)
381 {
382     if (paintingActive()) {
383         qWarning("QPixmap::operator=: Cannot assign to pixmap during painting");
384         return *this;
385     }
386     if (pixmap.paintingActive()) {                // make a deep copy
387         *this = pixmap.copy();
388     } else {
389         data = pixmap.data;
390     }
391     return *this;
392 }
393
394 /*!
395     \fn void QPixmap::swap(QPixmap &other)
396     \since 4.8
397
398     Swaps pixmap \a other with this pixmap. This operation is very
399     fast and never fails.
400 */
401
402 /*!
403    Returns the pixmap as a QVariant.
404 */
405 QPixmap::operator QVariant() const
406 {
407     return QVariant(QVariant::Pixmap, this);
408 }
409
410 /*!
411     \fn bool QPixmap::operator!() const
412
413     Returns true if this is a null pixmap; otherwise returns false.
414
415     \sa isNull()
416 */
417
418 /*!
419     Converts the pixmap to a QImage. Returns a null image if the
420     conversion fails.
421
422     If the pixmap has 1-bit depth, the returned image will also be 1
423     bit deep. Images with more bits will be returned in a format
424     closely represents the underlying system. Usually this will be
425     QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and
426     QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without
427     alpha.
428
429     Note that for the moment, alpha masks on monochrome images are
430     ignored.
431
432     \sa fromImage(), {QImage#Image Formats}{Image Formats}
433 */
434 QImage QPixmap::toImage() const
435 {
436     if (isNull())
437         return QImage();
438
439     return data->toImage();
440 }
441
442 /*!
443     \fn QMatrix QPixmap::trueMatrix(const QTransform &matrix, int width, int height)
444
445     Returns the actual matrix used for transforming a pixmap with the
446     given \a width, \a height and \a matrix.
447
448     When transforming a pixmap using the transformed() function, the
449     transformation matrix is internally adjusted to compensate for
450     unwanted translation, i.e. transformed() returns the smallest
451     pixmap containing all transformed points of the original
452     pixmap. This function returns the modified matrix, which maps
453     points correctly from the original pixmap into the new pixmap.
454
455     \sa transformed(), {QPixmap#Pixmap Transformations}{Pixmap
456     Transformations}
457 */
458 QTransform QPixmap::trueMatrix(const QTransform &m, int w, int h)
459 {
460     return QImage::trueMatrix(m, w, h);
461 }
462
463 /*!
464   \overload
465
466   This convenience function loads the matrix \a m into a
467   QTransform and calls the overloaded function with the
468   QTransform and the width \a w and the height \a h.
469  */
470 QMatrix QPixmap::trueMatrix(const QMatrix &m, int w, int h)
471 {
472     return trueMatrix(QTransform(m), w, h).toAffine();
473 }
474
475
476 /*!
477     \fn bool QPixmap::isQBitmap() const
478
479     Returns true if this is a QBitmap; otherwise returns false.
480 */
481
482 bool QPixmap::isQBitmap() const
483 {
484     return data->type == QPlatformPixmap::BitmapType;
485 }
486
487 /*!
488     \fn bool QPixmap::isNull() const
489
490     Returns true if this is a null pixmap; otherwise returns false.
491
492     A null pixmap has zero width, zero height and no contents. You
493     cannot draw in a null pixmap.
494 */
495 bool QPixmap::isNull() const
496 {
497     return !data || data->isNull();
498 }
499
500 /*!
501     \fn int QPixmap::width() const
502
503     Returns the width of the pixmap.
504
505     \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
506 */
507 int QPixmap::width() const
508 {
509     return data ? data->width() : 0;
510 }
511
512 /*!
513     \fn int QPixmap::height() const
514
515     Returns the height of the pixmap.
516
517     \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
518 */
519 int QPixmap::height() const
520 {
521     return data ? data->height() : 0;
522 }
523
524 /*!
525     \fn QSize QPixmap::size() const
526
527     Returns the size of the pixmap.
528
529     \sa width(), height(), {QPixmap#Pixmap Information}{Pixmap
530     Information}
531 */
532 QSize QPixmap::size() const
533 {
534     return data ? QSize(data->width(), data->height()) : QSize(0, 0);
535 }
536
537 /*!
538     \fn QRect QPixmap::rect() const
539
540     Returns the pixmap's enclosing rectangle.
541
542     \sa {QPixmap#Pixmap Information}{Pixmap Information}
543 */
544 QRect QPixmap::rect() const
545 {
546     return data ? QRect(0, 0, data->width(), data->height()) : QRect();
547 }
548
549 /*!
550     \fn int QPixmap::depth() const
551
552     Returns the depth of the pixmap.
553
554     The pixmap depth is also called bits per pixel (bpp) or bit planes
555     of a pixmap. A null pixmap has depth 0.
556
557     \sa defaultDepth(), {QPixmap#Pixmap Information}{Pixmap
558     Information}
559 */
560 int QPixmap::depth() const
561 {
562     return data ? data->depth() : 0;
563 }
564
565 /*!
566     Sets a mask bitmap.
567
568     This function merges the \a mask with the pixmap's alpha channel. A pixel
569     value of 1 on the mask means the pixmap's pixel is unchanged; a value of 0
570     means the pixel is transparent. The mask must have the same size as this
571     pixmap.
572
573     Setting a null mask resets the mask, leaving the previously transparent
574     pixels black. The effect of this function is undefined when the pixmap is
575     being painted on.
576
577     \warning This is potentially an expensive operation.
578
579     \sa mask(), {QPixmap#Pixmap Transformations}{Pixmap Transformations},
580     QBitmap
581 */
582 void QPixmap::setMask(const QBitmap &mask)
583 {
584     if (paintingActive()) {
585         qWarning("QPixmap::setMask: Cannot set mask while pixmap is being painted on");
586         return;
587     }
588
589     if (!mask.isNull() && mask.size() != size()) {
590         qWarning("QPixmap::setMask() mask size differs from pixmap size");
591         return;
592     }
593
594     if (isNull())
595         return;
596
597     if (static_cast<const QPixmap &>(mask).data == data) // trying to selfmask
598        return;
599
600     detach();
601
602     QImage image = data->toImage();
603     if (mask.size().isEmpty()) {
604         if (image.depth() != 1) { // hw: ????
605             image = image.convertToFormat(QImage::Format_RGB32);
606         }
607     } else {
608         const int w = image.width();
609         const int h = image.height();
610
611         switch (image.depth()) {
612         case 1: {
613             const QImage imageMask = mask.toImage().convertToFormat(image.format());
614             for (int y = 0; y < h; ++y) {
615                 const uchar *mscan = imageMask.scanLine(y);
616                 uchar *tscan = image.scanLine(y);
617                 int bytesPerLine = image.bytesPerLine();
618                 for (int i = 0; i < bytesPerLine; ++i)
619                     tscan[i] &= mscan[i];
620             }
621             break;
622         }
623         default: {
624             const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
625             image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
626             for (int y = 0; y < h; ++y) {
627                 const uchar *mscan = imageMask.scanLine(y);
628                 QRgb *tscan = (QRgb *)image.scanLine(y);
629                 for (int x = 0; x < w; ++x) {
630                     if (!(mscan[x>>3] & (1 << (x&7))))
631                         tscan[x] = 0;
632                 }
633             }
634             break;
635         }
636         }
637     }
638     data->fromImage(image, Qt::AutoColor);
639 }
640
641 #ifndef QT_NO_IMAGE_HEURISTIC_MASK
642 /*!
643     Creates and returns a heuristic mask for this pixmap.
644
645     The function works by selecting a color from one of the corners
646     and then chipping away pixels of that color, starting at all the
647     edges.  If \a clipTight is true (the default) the mask is just
648     large enough to cover the pixels; otherwise, the mask is larger
649     than the data pixels.
650
651     The mask may not be perfect but it should be reasonable, so you
652     can do things such as the following:
653
654     \snippet doc/src/snippets/code/src_gui_image_qpixmap.cpp 1
655
656     This function is slow because it involves converting to/from a
657     QImage, and non-trivial computations.
658
659     \sa QImage::createHeuristicMask(), createMaskFromColor()
660 */
661 QBitmap QPixmap::createHeuristicMask(bool clipTight) const
662 {
663     QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
664     return m;
665 }
666 #endif
667
668 /*!
669     Creates and returns a mask for this pixmap based on the given \a
670     maskColor. If the \a mode is Qt::MaskInColor, all pixels matching the
671     maskColor will be transparent. If \a mode is Qt::MaskOutColor, all pixels
672     matching the maskColor will be opaque.
673
674     This function is slow because it involves converting to/from a
675     QImage.
676
677     \sa createHeuristicMask(), QImage::createMaskFromColor()
678 */
679 QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const
680 {
681     QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
682     return QBitmap::fromImage(image.createMaskFromColor(maskColor.rgba(), mode));
683 }
684
685 /*!
686     Loads a pixmap from the file with the given \a fileName. Returns
687     true if the pixmap was successfully loaded; otherwise returns
688     false.
689
690     The loader attempts to read the pixmap using the specified \a
691     format. If the \a format is not specified (which is the default),
692     the loader probes the file for a header to guess the file format.
693
694     The file name can either refer to an actual file on disk or to one
695     of the application's embedded resources. See the
696     \l{resources.html}{Resource System} overview for details on how to
697     embed pixmaps and other resource files in the application's
698     executable.
699
700     If the data needs to be modified to fit in a lower-resolution
701     result (e.g. converting from 32-bit to 8-bit), use the \a flags to
702     control the conversion.
703
704     Note that QPixmaps are automatically added to the QPixmapCache
705     when loaded from a file; the key used is internal and can not
706     be acquired.
707
708     \sa loadFromData(), {QPixmap#Reading and Writing Image
709     Files}{Reading and Writing Image Files}
710 */
711
712 bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
713 {
714     if (fileName.isEmpty())
715         return false;
716
717     QFileInfo info(fileName);
718     QString key = QLatin1Literal("qt_pixmap")
719                   % info.absoluteFilePath()
720                   % HexString<uint>(info.lastModified().toTime_t())
721                   % HexString<quint64>(info.size())
722                   % HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);
723
724     // Note: If no extension is provided, we try to match the
725     // file against known plugin extensions
726     if (!info.completeSuffix().isEmpty() && !info.exists())
727         return false;
728
729     if (QPixmapCache::find(key, *this))
730         return true;
731
732     QScopedPointer<QPlatformPixmap> tmp(QPlatformPixmap::create(0, 0, data ? data->type : QPlatformPixmap::PixmapType));
733     if (tmp->fromFile(fileName, format, flags)) {
734         data = tmp.take();
735         QPixmapCache::insert(key, *this);
736         return true;
737     }
738
739     return false;
740 }
741
742 /*!
743     \fn bool QPixmap::loadFromData(const uchar *data, uint len, const char *format, Qt::ImageConversionFlags flags)
744
745     Loads a pixmap from the \a len first bytes of the given binary \a
746     data.  Returns true if the pixmap was loaded successfully;
747     otherwise returns false.
748
749     The loader attempts to read the pixmap using the specified \a
750     format. If the \a format is not specified (which is the default),
751     the loader probes the file for a header to guess the file format.
752
753     If the data needs to be modified to fit in a lower-resolution
754     result (e.g. converting from 32-bit to 8-bit), use the \a flags to
755     control the conversion.
756
757     \sa load(), {QPixmap#Reading and Writing Image Files}{Reading and
758     Writing Image Files}
759 */
760
761 bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
762 {
763     if (len == 0 || buf == 0)
764         return false;
765
766     if (!data)
767         data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
768
769     return data->fromData(buf, len, format, flags);
770 }
771
772 /*!
773     \fn bool QPixmap::loadFromData(const QByteArray &data, const char *format, Qt::ImageConversionFlags flags)
774
775     \overload
776
777     Loads a pixmap from the binary \a data using the specified \a
778     format and conversion \a flags.
779 */
780
781
782 /*!
783     Saves the pixmap to the file with the given \a fileName using the
784     specified image file \a format and \a quality factor. Returns true
785     if successful; otherwise returns false.
786
787     The \a quality factor must be in the range [0,100] or -1. Specify
788     0 to obtain small compressed files, 100 for large uncompressed
789     files, and -1 to use the default settings.
790
791     If \a format is 0, an image format will be chosen from \a fileName's
792     suffix.
793
794     \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
795     Image Files}
796 */
797
798 bool QPixmap::save(const QString &fileName, const char *format, int quality) const
799 {
800     if (isNull())
801         return false;                                // nothing to save
802     QImageWriter writer(fileName, format);
803     return doImageIO(&writer, quality);
804 }
805
806 /*!
807     \overload
808
809     This function writes a QPixmap to the given \a device using the
810     specified image file \a format and \a quality factor. This can be
811     used, for example, to save a pixmap directly into a QByteArray:
812
813     \snippet doc/src/snippets/image/image.cpp 1
814 */
815
816 bool QPixmap::save(QIODevice* device, const char* format, int quality) const
817 {
818     if (isNull())
819         return false;                                // nothing to save
820     QImageWriter writer(device, format);
821     return doImageIO(&writer, quality);
822 }
823
824 /*! \internal
825 */
826 bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
827 {
828     if (quality > 100  || quality < -1)
829         qWarning("QPixmap::save: quality out of range [-1,100]");
830     if (quality >= 0)
831         writer->setQuality(qMin(quality,100));
832     return writer->write(toImage());
833 }
834
835 /*!
836     \fn void QPixmap::fill(const QPaintDevice *device, int x, int y)
837     \overload
838
839     \obsolete
840
841     Fills the pixmap with the \a device's background color or pixmap.
842     The given point, (\a x, \a y), defines an offset in widget
843     coordinates to which the pixmap's top-left pixel will be mapped
844     to.
845 */
846
847 void QPixmap::fill(const QPaintDevice *, const QPoint &)
848 {
849     qWarning() << "QPixmap::fill(const QPaintDevice *device, const QPoint &offset) is deprecated, ignored";
850 }
851
852
853 /*!
854     Fills the pixmap with the given \a color.
855
856     The effect of this function is undefined when the pixmap is
857     being painted on.
858
859     \sa {QPixmap#Pixmap Transformations}{Pixmap Transformations}
860 */
861
862 void QPixmap::fill(const QColor &color)
863 {
864     if (isNull())
865         return;
866
867     // Some people are probably already calling fill while a painter is active, so to not break
868     // their programs, only print a warning and return when the fill operation could cause a crash.
869     if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
870         qWarning("QPixmap::fill: Cannot fill while pixmap is being painted on");
871         return;
872     }
873
874     if (data->ref.load() == 1) {
875         // detach() will also remove this pixmap from caches, so
876         // it has to be called even when ref == 1.
877         detach();
878     } else {
879         // Don't bother to make a copy of the data object, since
880         // it will be filled with new pixel data anyway.
881         QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
882         d->resize(data->width(), data->height());
883         data = d;
884     }
885     data->fill(color);
886 }
887
888 /*! \obsolete
889     Returns a number that identifies the contents of this QPixmap
890     object. Distinct QPixmap objects can only have the same serial
891     number if they refer to the same contents (but they don't have
892     to).
893
894     Use cacheKey() instead.
895
896     \warning The serial number doesn't necessarily change when
897     the pixmap is altered. This means that it may be dangerous to use
898     it as a cache key. For caching pixmaps, we recommend using the
899     QPixmapCache class whenever possible.
900 */
901 int QPixmap::serialNumber() const
902 {
903     if (isNull())
904         return 0;
905     return data->serialNumber();
906 }
907
908 /*!
909     Returns a number that identifies this QPixmap. Distinct QPixmap
910     objects can only have the same cache key if they refer to the same
911     contents.
912
913     The cacheKey() will change when the pixmap is altered.
914 */
915 qint64 QPixmap::cacheKey() const
916 {
917     if (isNull())
918         return 0;
919
920     Q_ASSERT(data);
921     return data->cacheKey();
922 }
923
924 #if 0
925 static void sendResizeEvents(QWidget *target)
926 {
927     QResizeEvent e(target->size(), QSize());
928     QApplication::sendEvent(target, &e);
929
930     const QObjectList children = target->children();
931     for (int i = 0; i < children.size(); ++i) {
932         QWidget *child = static_cast<QWidget*>(children.at(i));
933         if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
934             sendResizeEvents(child);
935     }
936 }
937 #endif
938
939 QPixmap QPixmap::grabWidget(QObject *widget, const QRect &rectangle)
940 {
941     QPixmap pixmap;
942     // ### Qt5: should we keep or remove this method?
943     // SC solution would be to install a callback form QtWidgets, but ugly.
944     qWarning("QPixmap::grabWidget is deprecated, use QWidget::grab() instead");
945     if (!widget)
946         return pixmap;
947     QMetaObject::invokeMethod(widget, "grab", Qt::DirectConnection,
948                               Q_RETURN_ARG(QPixmap, pixmap),
949                               Q_ARG(QRect, rectangle));
950     return pixmap;
951 }
952
953 /*****************************************************************************
954   QPixmap stream functions
955  *****************************************************************************/
956 #if !defined(QT_NO_DATASTREAM)
957 /*!
958     \relates QPixmap
959
960     Writes the given \a pixmap to the given \a stream as a PNG
961     image. Note that writing the stream to a file will not produce a
962     valid image file.
963
964     \sa QPixmap::save(), {Serializing Qt Data Types}
965 */
966
967 QDataStream &operator<<(QDataStream &stream, const QPixmap &pixmap)
968 {
969     return stream << pixmap.toImage();
970 }
971
972 /*!
973     \relates QPixmap
974
975     Reads an image from the given \a stream into the given \a pixmap.
976
977     \sa QPixmap::load(), {Serializing Qt Data Types}
978 */
979
980 QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
981 {
982     QImage image;
983     stream >> image;
984
985     if (image.isNull()) {
986         pixmap = QPixmap();
987     } else if (image.depth() == 1) {
988         pixmap = QBitmap::fromImage(image);
989     } else {
990         pixmap = QPixmap::fromImage(image);
991     }
992     return stream;
993 }
994
995 #endif // QT_NO_DATASTREAM
996
997 /*!
998     \internal
999 */
1000
1001 bool QPixmap::isDetached() const
1002 {
1003     return data && data->ref.load() == 1;
1004 }
1005
1006 /*!
1007     Replaces this pixmap's data with the given \a image using the
1008     specified \a flags to control the conversion.  The \a flags
1009     argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
1010     Passing 0 for \a flags sets all the default options. Returns true
1011     if the result is that this pixmap is not null.
1012
1013     Note: this function was part of Qt 3 support in Qt 4.6 and earlier.
1014     It has been promoted to official API status in 4.7 to support updating
1015     the pixmap's image without creating a new QPixmap as fromImage() would.
1016
1017     \sa fromImage()
1018     \since 4.7
1019 */
1020 bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags)
1021 {
1022     if (image.isNull() || !data)
1023         *this = QPixmap::fromImage(image, flags);
1024     else
1025         data->fromImage(image, flags);
1026     return !isNull();
1027 }
1028
1029 /*!
1030     \fn QPixmap QPixmap::scaled(int width, int height,
1031     Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode
1032     transformMode) const
1033
1034     \overload
1035
1036     Returns a copy of the pixmap scaled to a rectangle with the given
1037     \a width and \a height according to the given \a aspectRatioMode and
1038     \a transformMode.
1039
1040     If either the \a width or the \a height is zero or negative, this
1041     function returns a null pixmap.
1042 */
1043
1044 /*!
1045     \fn QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode
1046     aspectRatioMode, Qt::TransformationMode transformMode) const
1047
1048     Scales the pixmap to the given \a size, using the aspect ratio and
1049     transformation modes specified by \a aspectRatioMode and \a
1050     transformMode.
1051
1052     \image qimage-scaling.png
1053
1054     \list
1055     \li If \a aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap
1056        is scaled to \a size.
1057     \li If \a aspectRatioMode is Qt::KeepAspectRatio, the pixmap is
1058        scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
1059     \li If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
1060        the pixmap is scaled to a rectangle as small as possible
1061        outside \a size, preserving the aspect ratio.
1062     \endlist
1063
1064     If the given \a size is empty, this function returns a null
1065     pixmap.
1066
1067
1068     In some cases it can be more beneficial to draw the pixmap to a
1069     painter with a scale set rather than scaling the pixmap. This is
1070     the case when the painter is for instance based on OpenGL or when
1071     the scale factor changes rapidly.
1072
1073     \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1074     Transformations}
1075
1076 */
1077 QPixmap QPixmap::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
1078 {
1079     if (isNull()) {
1080         qWarning("QPixmap::scaled: Pixmap is a null pixmap");
1081         return QPixmap();
1082     }
1083     if (s.isEmpty())
1084         return QPixmap();
1085
1086     QSize newSize = size();
1087     newSize.scale(s, aspectMode);
1088     newSize.rwidth() = qMax(newSize.width(), 1);
1089     newSize.rheight() = qMax(newSize.height(), 1);
1090     if (newSize == size())
1091         return *this;
1092
1093     QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1094                                           (qreal)newSize.height() / height());
1095     QPixmap pix = transformed(wm, mode);
1096     return pix;
1097 }
1098
1099 /*!
1100     \fn QPixmap QPixmap::scaledToWidth(int width, Qt::TransformationMode
1101     mode) const
1102
1103     Returns a scaled copy of the image. The returned image is scaled
1104     to the given \a width using the specified transformation \a mode.
1105     The height of the pixmap is automatically calculated so that the
1106     aspect ratio of the pixmap is preserved.
1107
1108     If \a width is 0 or negative, a null pixmap is returned.
1109
1110     \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1111     Transformations}
1112 */
1113 QPixmap QPixmap::scaledToWidth(int w, Qt::TransformationMode mode) const
1114 {
1115     if (isNull()) {
1116         qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap");
1117         return copy();
1118     }
1119     if (w <= 0)
1120         return QPixmap();
1121
1122     qreal factor = (qreal) w / width();
1123     QTransform wm = QTransform::fromScale(factor, factor);
1124     return transformed(wm, mode);
1125 }
1126
1127 /*!
1128     \fn QPixmap QPixmap::scaledToHeight(int height,
1129     Qt::TransformationMode mode) const
1130
1131     Returns a scaled copy of the image. The returned image is scaled
1132     to the given \a height using the specified transformation \a mode.
1133     The width of the pixmap is automatically calculated so that the
1134     aspect ratio of the pixmap is preserved.
1135
1136     If \a height is 0 or negative, a null pixmap is returned.
1137
1138     \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1139     Transformations}
1140 */
1141 QPixmap QPixmap::scaledToHeight(int h, Qt::TransformationMode mode) const
1142 {
1143     if (isNull()) {
1144         qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap");
1145         return copy();
1146     }
1147     if (h <= 0)
1148         return QPixmap();
1149
1150     qreal factor = (qreal) h / height();
1151     QTransform wm = QTransform::fromScale(factor, factor);
1152     return transformed(wm, mode);
1153 }
1154
1155 /*!
1156     Returns a copy of the pixmap that is transformed using the given
1157     transformation \a transform and transformation \a mode. The original
1158     pixmap is not changed.
1159
1160     The transformation \a transform is internally adjusted to compensate
1161     for unwanted translation; i.e. the pixmap produced is the smallest
1162     pixmap that contains all the transformed points of the original
1163     pixmap. Use the trueMatrix() function to retrieve the actual
1164     matrix used for transforming the pixmap.
1165
1166     This function is slow because it involves transformation to a
1167     QImage, non-trivial computations and a transformation back to a
1168     QPixmap.
1169
1170     \sa trueMatrix(), {QPixmap#Pixmap Transformations}{Pixmap
1171     Transformations}
1172 */
1173 QPixmap QPixmap::transformed(const QTransform &transform,
1174                              Qt::TransformationMode mode) const
1175 {
1176     if (isNull() || transform.type() <= QTransform::TxTranslate)
1177         return *this;
1178
1179     return data->transformed(transform, mode);
1180 }
1181
1182 /*!
1183   \overload
1184
1185   This convenience function loads the \a matrix into a
1186   QTransform and calls the overloaded function.
1187  */
1188 QPixmap QPixmap::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
1189 {
1190     return transformed(QTransform(matrix), mode);
1191 }
1192
1193
1194
1195
1196
1197
1198
1199
1200 /*!
1201     \class QPixmap
1202
1203     \brief The QPixmap class is an off-screen image representation
1204     that can be used as a paint device.
1205
1206     \ingroup painting
1207     \ingroup shared
1208
1209
1210     Qt provides four classes for handling image data: QImage, QPixmap,
1211     QBitmap and QPicture. QImage is designed and optimized for I/O,
1212     and for direct pixel access and manipulation, while QPixmap is
1213     designed and optimized for showing images on screen. QBitmap is
1214     only a convenience class that inherits QPixmap, ensuring a depth
1215     of 1. The isQBitmap() function returns true if a QPixmap object is
1216     really a bitmap, otherwise returns false. Finally, the QPicture class
1217     is a paint device that records and replays QPainter commands.
1218
1219     A QPixmap can easily be displayed on the screen using QLabel or
1220     one of QAbstractButton's subclasses (such as QPushButton and
1221     QToolButton). QLabel has a pixmap property, whereas
1222     QAbstractButton has an icon property.
1223
1224     In addition to the ordinary constructors, a QPixmap can be
1225     constructed using the static grabWidget() and grabWindow()
1226     functions which creates a QPixmap and paints the given widget, or
1227     window, into it.
1228
1229     QPixmap objects can be passed around by value since the QPixmap
1230     class uses implicit data sharing. For more information, see the \l
1231     {Implicit Data Sharing} documentation. QPixmap objects can also be
1232     streamed.
1233
1234     Note that the pixel data in a pixmap is internal and is managed by
1235     the underlying window system. Because QPixmap is a QPaintDevice
1236     subclass, QPainter can be used to draw directly onto pixmaps.
1237     Pixels can only be accessed through QPainter functions or by
1238     converting the QPixmap to a QImage. However, the fill() function
1239     is available for initializing the entire pixmap with a given color.
1240
1241     There are functions to convert between QImage and
1242     QPixmap. Typically, the QImage class is used to load an image
1243     file, optionally manipulating the image data, before the QImage
1244     object is converted into a QPixmap to be shown on
1245     screen. Alternatively, if no manipulation is desired, the image
1246     file can be loaded directly into a QPixmap. On Windows, the
1247     QPixmap class also supports conversion between \c HBITMAP and
1248     QPixmap.
1249
1250     QPixmap provides a collection of functions that can be used to
1251     obtain a variety of information about the pixmap. In addition,
1252     there are several functions that enables transformation of the
1253     pixmap.
1254
1255     \tableofcontents
1256
1257     \section1 Reading and Writing Image Files
1258
1259     QPixmap provides several ways of reading an image file: The file
1260     can be loaded when constructing the QPixmap object, or by using
1261     the load() or loadFromData() functions later on. When loading an
1262     image, the file name can either refer to an actual file on disk or
1263     to one of the application's embedded resources. See \l{The Qt
1264     Resource System} overview for details on how to embed images and
1265     other resource files in the application's executable.
1266
1267     Simply call the save() function to save a QPixmap object.
1268
1269     The complete list of supported file formats are available through
1270     the QImageReader::supportedImageFormats() and
1271     QImageWriter::supportedImageFormats() functions. New file formats
1272     can be added as plugins. By default, Qt supports the following
1273     formats:
1274
1275     \table
1276     \header \li Format \li Description                      \li Qt's support
1277     \row    \li BMP    \li Windows Bitmap                   \li Read/write
1278     \row    \li GIF    \li Graphic Interchange Format (optional) \li Read
1279     \row    \li JPG    \li Joint Photographic Experts Group \li Read/write
1280     \row    \li JPEG   \li Joint Photographic Experts Group \li Read/write
1281     \row    \li PNG    \li Portable Network Graphics        \li Read/write
1282     \row    \li PBM    \li Portable Bitmap                  \li Read
1283     \row    \li PGM    \li Portable Graymap                 \li Read
1284     \row    \li PPM    \li Portable Pixmap                  \li Read/write
1285     \row    \li XBM    \li X11 Bitmap                       \li Read/write
1286     \row    \li XPM    \li X11 Pixmap                       \li Read/write
1287     \endtable
1288
1289     \section1 Pixmap Information
1290
1291     QPixmap provides a collection of functions that can be used to
1292     obtain a variety of information about the pixmap:
1293
1294     \table
1295     \header
1296     \li \li Available Functions
1297     \row
1298     \li Geometry
1299     \li
1300     The size(), width() and height() functions provide information
1301     about the pixmap's size. The rect() function returns the image's
1302     enclosing rectangle.
1303
1304     \row
1305     \li Alpha component
1306     \li
1307
1308     The hasAlphaChannel() returns true if the pixmap has a format that
1309     respects the alpha channel, otherwise returns false. The hasAlpha(),
1310     setMask() and mask() functions are legacy and should not be used.
1311     They are potentially very slow.
1312
1313     The createHeuristicMask() function creates and returns a 1-bpp
1314     heuristic mask (i.e. a QBitmap) for this pixmap. It works by
1315     selecting a color from one of the corners and then chipping away
1316     pixels of that color, starting at all the edges. The
1317     createMaskFromColor() function creates and returns a mask (i.e. a
1318     QBitmap) for the pixmap based on a given color.
1319
1320     \row
1321     \li Low-level information
1322     \li
1323
1324     The depth() function returns the depth of the pixmap. The
1325     defaultDepth() function returns the default depth, i.e. the depth
1326     used by the application on the given screen.
1327
1328     The cacheKey() function returns a number that uniquely
1329     identifies the contents of the QPixmap object.
1330
1331     The x11Info() function returns information about the configuration
1332     of the X display used by the screen to which the pixmap currently
1333     belongs. The x11PictureHandle() function returns the X11 Picture
1334     handle of the pixmap for XRender support. Note that the two latter
1335     functions are only available on x11.
1336
1337     \endtable
1338
1339     \section1 Pixmap Conversion
1340
1341     A QPixmap object can be converted into a QImage using the
1342     toImage() function. Likewise, a QImage can be converted into a
1343     QPixmap using the fromImage(). If this is too expensive an
1344     operation, you can use QBitmap::fromImage() instead.
1345
1346     In addition, on Windows, the QPixmap class supports conversion to
1347     and from HBITMAP: the toWinHBITMAP() function creates a HBITMAP
1348     equivalent to the QPixmap, based on the given HBitmapFormat, and
1349     returns the HBITMAP handle. The fromWinHBITMAP() function returns
1350     a QPixmap that is equivalent to the given bitmap which has the
1351     specified format. The QPixmap class also supports conversion to
1352     and from HICON: the toWinHICON() function creates a HICON equivalent
1353     to the QPixmap, and returns the HICON handle. The fromWinHICON()
1354     function returns a QPixmap that is equivalent to the given icon.
1355
1356     \section1 Pixmap Transformations
1357
1358     QPixmap supports a number of functions for creating a new pixmap
1359     that is a transformed version of the original:
1360
1361     The scaled(), scaledToWidth() and scaledToHeight() functions
1362     return scaled copies of the pixmap, while the copy() function
1363     creates a QPixmap that is a plain copy of the original one.
1364
1365     The transformed() function returns a copy of the pixmap that is
1366     transformed with the given transformation matrix and
1367     transformation mode: Internally, the transformation matrix is
1368     adjusted to compensate for unwanted translation,
1369     i.e. transformed() returns the smallest pixmap containing all
1370     transformed points of the original pixmap. The static trueMatrix()
1371     function returns the actual matrix used for transforming the
1372     pixmap.
1373
1374     \note When using the native X11 graphics system, the pixmap
1375     becomes invalid when the QApplication instance is destroyed.
1376
1377     \sa QBitmap, QImage, QImageReader, QImageWriter
1378 */
1379
1380
1381 /*!
1382     \typedef QPixmap::DataPtr
1383     \internal
1384 */
1385
1386 /*!
1387     \fn DataPtr &QPixmap::data_ptr()
1388     \internal
1389 */
1390
1391 /*!
1392     Returns true if this pixmap has an alpha channel, \e or has a
1393     mask, otherwise returns false.
1394
1395     \sa hasAlphaChannel(), mask()
1396 */
1397 bool QPixmap::hasAlpha() const
1398 {
1399     return data && data->hasAlphaChannel();
1400 }
1401
1402 /*!
1403     Returns true if the pixmap has a format that respects the alpha
1404     channel, otherwise returns false.
1405
1406     \sa hasAlpha()
1407 */
1408 bool QPixmap::hasAlphaChannel() const
1409 {
1410     return data && data->hasAlphaChannel();
1411 }
1412
1413 /*!
1414     \internal
1415 */
1416 int QPixmap::metric(PaintDeviceMetric metric) const
1417 {
1418     return data ? data->metric(metric) : 0;
1419 }
1420
1421 /*!
1422     \internal
1423 */
1424 QPaintEngine *QPixmap::paintEngine() const
1425 {
1426     return data ? data->paintEngine() : 0;
1427 }
1428
1429 /*!
1430     \fn QBitmap QPixmap::mask() const
1431
1432     Extracts a bitmap mask from the pixmap's alpha channel.
1433
1434     \warning This is potentially an expensive operation. The mask of
1435     the pixmap is extracted dynamically from the pixeldata.
1436
1437     \sa setMask(), {QPixmap#Pixmap Information}{Pixmap Information}
1438 */
1439 QBitmap QPixmap::mask() const
1440 {
1441     if (!data || !hasAlphaChannel())
1442         return QBitmap();
1443
1444     const QImage img = toImage();
1445     const QImage image = (img.depth() < 32 ? img.convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
1446     const int w = image.width();
1447     const int h = image.height();
1448
1449     QImage mask(w, h, QImage::Format_MonoLSB);
1450     if (mask.isNull()) // allocation failed
1451         return QBitmap();
1452
1453     mask.setColorCount(2);
1454     mask.setColor(0, QColor(Qt::color0).rgba());
1455     mask.setColor(1, QColor(Qt::color1).rgba());
1456
1457     const int bpl = mask.bytesPerLine();
1458
1459     for (int y = 0; y < h; ++y) {
1460         const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
1461         uchar *dest = mask.scanLine(y);
1462         memset(dest, 0, bpl);
1463         for (int x = 0; x < w; ++x) {
1464             if (qAlpha(*src) > 0)
1465                 dest[x >> 3] |= (1 << (x & 7));
1466             ++src;
1467         }
1468     }
1469
1470     return QBitmap::fromImage(mask);
1471 }
1472
1473 /*!
1474     Returns the default pixmap depth used by the application.
1475
1476     On all platforms the depth of the primary screen will be returned.
1477
1478     \sa depth(), QColormap::depth(), {QPixmap#Pixmap Information}{Pixmap Information}
1479
1480 */
1481 int QPixmap::defaultDepth()
1482 {
1483     return QGuiApplication::primaryScreen()->depth();
1484 }
1485
1486 /*!
1487     Detaches the pixmap from shared pixmap data.
1488
1489     A pixmap is automatically detached by Qt whenever its contents are
1490     about to change. This is done in almost all QPixmap member
1491     functions that modify the pixmap (fill(), fromImage(),
1492     load(), etc.), and in QPainter::begin() on a pixmap.
1493
1494     There are two exceptions in which detach() must be called
1495     explicitly, that is when calling the handle() or the
1496     x11PictureHandle() function (only available on X11). Otherwise,
1497     any modifications done using system calls, will be performed on
1498     the shared data.
1499
1500     The detach() function returns immediately if there is just a
1501     single reference or if the pixmap has not been initialized yet.
1502 */
1503 void QPixmap::detach()
1504 {
1505     if (!data)
1506         return;
1507
1508     // QPixmap.data member may be QRuntimePlatformPixmap so use handle() function to get
1509     // the actual underlaying runtime pixmap data.
1510     QPlatformPixmap *pd = handle();
1511     QPlatformPixmap::ClassId id = pd->classId();
1512     if (id == QPlatformPixmap::RasterClass) {
1513         QRasterPlatformPixmap *rasterData = static_cast<QRasterPlatformPixmap*>(pd);
1514         rasterData->image.detach();
1515     }
1516
1517     if (data->is_cached && data->ref.load() == 1)
1518         QImagePixmapCleanupHooks::executePlatformPixmapModificationHooks(data.data());
1519
1520     if (data->ref.load() != 1) {
1521         *this = copy();
1522     }
1523     ++data->detach_no;
1524 }
1525
1526 /*!
1527     \fn QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1528
1529     Converts the given \a image to a pixmap using the specified \a
1530     flags to control the conversion.  The \a flags argument is a
1531     bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for \a
1532     flags sets all the default options.
1533
1534     In case of monochrome and 8-bit images, the image is first
1535     converted to a 32-bit pixmap and then filled with the colors in
1536     the color table. If this is too expensive an operation, you can
1537     use QBitmap::fromImage() instead.
1538
1539     \sa fromImageReader(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1540 */
1541 QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1542 {
1543     if (image.isNull())
1544         return QPixmap();
1545
1546     QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1547     data->fromImage(image, flags);
1548     return QPixmap(data.take());
1549 }
1550
1551 /*!
1552     \fn QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1553
1554     Create a QPixmap from an image read directly from an \a imageReader.
1555     The \a flags argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
1556     Passing 0 for \a flags sets all the default options.
1557
1558     On some systems, reading an image directly to QPixmap can use less memory than
1559     reading a QImage to convert it to QPixmap.
1560
1561     \sa fromImage(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1562 */
1563 QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1564 {
1565     QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1566     data->fromImageReader(imageReader, flags);
1567     return QPixmap(data.take());
1568 }
1569
1570 /*!
1571     \fn QPixmap QPixmap::grabWindow(WId window, int x, int y, int
1572     width, int height)
1573
1574     Creates and returns a pixmap constructed by grabbing the contents
1575     of the given \a window restricted by QRect(\a x, \a y, \a width,
1576     \a height).
1577
1578     The arguments (\a{x}, \a{y}) specify the offset in the window,
1579     whereas (\a{width}, \a{height}) specify the area to be copied.  If
1580     \a width is negative, the function copies everything to the right
1581     border of the window. If \a height is negative, the function
1582     copies everything to the bottom of the window.
1583
1584     The window system identifier (\c WId) can be retrieved using the
1585     QWidget::winId() function. The rationale for using a window
1586     identifier and not a QWidget, is to enable grabbing of windows
1587     that are not part of the application, window system frames, and so
1588     on.
1589
1590     The grabWindow() function grabs pixels from the screen, not from
1591     the window, i.e. if there is another window partially or entirely
1592     over the one you grab, you get pixels from the overlying window,
1593     too. The mouse cursor is generally not grabbed.
1594
1595     Note on X11 that if the given \a window doesn't have the same depth
1596     as the root window, and another window partially or entirely
1597     obscures the one you grab, you will \e not get pixels from the
1598     overlying window.  The contents of the obscured areas in the
1599     pixmap will be undefined and uninitialized.
1600
1601     On Windows Vista and above grabbing a layered window, which is
1602     created by setting the Qt::WA_TranslucentBackground attribute, will
1603     not work. Instead grabbing the desktop widget should work.
1604
1605     \warning In general, grabbing an area outside the screen is not
1606     safe. This depends on the underlying window system.
1607
1608     \warning The function is deprecated in Qt 5.0 since there might be
1609     platform plugins in which window system identifiers (\c WId)
1610     are local to a screen. Use QScreen::grabWindow() instead.
1611
1612     \sa grabWidget(), {Screenshot Example}
1613     \sa QScreen
1614     \deprecated
1615 */
1616
1617 QPixmap QPixmap::grabWindow(WId window, int x, int y, int w, int h)
1618 {
1619     qWarning("%s is deprecated, use QScreen::grabWindow() instead."
1620              " Defaulting to primary screen.", Q_FUNC_INFO);
1621     return QGuiApplication::primaryScreen()->grabWindow(window, x, y, w, h);
1622 }
1623
1624 /*!
1625   \internal
1626 */
1627 QPlatformPixmap* QPixmap::handle() const
1628 {
1629     return data.data();
1630 }
1631
1632 #ifndef QT_NO_DEBUG_STREAM
1633 QDebug operator<<(QDebug dbg, const QPixmap &r)
1634 {
1635     dbg.nospace() << "QPixmap(" << r.size() << ')';
1636     return dbg.space();
1637 }
1638 #endif
1639
1640 QT_END_NAMESPACE