1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <qglobal.h>
41
42#include "qpixmap.h"
43#include <qpa/qplatformpixmap.h>
44#include "qimagepixmapcleanuphooks_p.h"
45
46#include "qbitmap.h"
47#include "qimage.h"
48#include "qpainter.h"
49#include "qdatastream.h"
50#include "qbuffer.h"
51#include <private/qguiapplication_p.h>
52#include "qevent.h"
53#include "qfile.h"
54#include "qfileinfo.h"
55#include "qpixmapcache.h"
56#include "qdatetime.h"
57#include "qimagereader.h"
58#include "qimagewriter.h"
59#include "qpaintengine.h"
60#include "qscreen.h"
61#include "qthread.h"
62#include "qdebug.h"
63
64#include <qpa/qplatformintegration.h>
65
66#include "qpixmap_raster_p.h"
67#include "private/qhexstring_p.h"
68
69#include <qtgui_tracepoints_p.h>
70
71QT_BEGIN_NAMESPACE
72
73static bool qt_pixmap_thread_test()
74{
75 if (Q_UNLIKELY(!QCoreApplication::instance())) {
76 qFatal("QPixmap: Must construct a QGuiApplication before a QPixmap");
77 return false;
78 }
79
80 if (qApp->thread() != QThread::currentThread()) {
81 bool fail = false;
82 if (!QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
83 printf("Platform 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
94void 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 = nullptr;
100}
101
102/*!
103 Constructs a null pixmap.
104
105 \sa isNull()
106*/
107
108QPixmap::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
129QPixmap::QPixmap(int w, int h)
130 : QPixmap(QSize(w, h))
131{
132}
133
134/*!
135 \overload
136
137 Constructs a pixmap of the given \a size.
138
139 \warning This will create a QPixmap with uninitialized data. Call
140 fill() to fill the pixmap with an appropriate color before drawing
141 onto it with QPainter.
142*/
143
144QPixmap::QPixmap(const QSize &size)
145 : QPixmap(size, QPlatformPixmap::PixmapType)
146{
147}
148
149/*!
150 \internal
151*/
152QPixmap::QPixmap(const QSize &s, int type)
153{
154 if (!qt_pixmap_thread_test())
155 doInit(0, 0, static_cast<QPlatformPixmap::PixelType>(type));
156 else
157 doInit(s.width(), s.height(), static_cast<QPlatformPixmap::PixelType>(type));
158}
159
160/*!
161 \internal
162*/
163QPixmap::QPixmap(QPlatformPixmap *d)
164 : QPaintDevice(), data(d)
165{
166}
167
168/*!
169 Constructs a pixmap from the file with the given \a fileName. If the
170 file does not exist or is of an unknown format, the pixmap becomes a
171 null pixmap.
172
173 The loader attempts to read the pixmap using the specified \a
174 format. If the \a format is not specified (which is the default),
175 the loader probes the file for a header to guess the file format.
176
177 The file name can either refer to an actual file on disk or to
178 one of the application's embedded resources. See the
179 \l{resources.html}{Resource System} overview for details on how
180 to embed images and other resource files in the application's
181 executable.
182
183 If the image needs to be modified to fit in a lower-resolution
184 result (e.g. converting from 32-bit to 8-bit), use the \a
185 flags to control the conversion.
186
187 The \a fileName, \a format and \a flags parameters are
188 passed on to load(). This means that the data in \a fileName is
189 not compiled into the binary. If \a fileName contains a relative
190 path (e.g. the filename only) the relevant file must be found
191 relative to the runtime working directory.
192
193 \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
194 Image Files}
195*/
196
197QPixmap::QPixmap(const QString& fileName, const char *format, Qt::ImageConversionFlags flags)
198 : QPaintDevice()
199{
200 doInit(0, 0, QPlatformPixmap::PixmapType);
201 if (!qt_pixmap_thread_test())
202 return;
203
204 load(fileName, format, flags);
205}
206
207/*!
208 Constructs a pixmap that is a copy of the given \a pixmap.
209
210 \sa copy()
211*/
212
213QPixmap::QPixmap(const QPixmap &pixmap)
214 : QPaintDevice()
215{
216 if (!qt_pixmap_thread_test()) {
217 doInit(0, 0, QPlatformPixmap::PixmapType);
218 return;
219 }
220 if (pixmap.paintingActive()) { // make a deep copy
221 pixmap.copy().swap(*this);
222 } else {
223 data = pixmap.data;
224 }
225}
226
227/*! \fn QPixmap::QPixmap(QPixmap &&other)
228 Move-constructs a QPixmap instance from \a other.
229
230 \sa swap() operator=(QPixmap&&)
231*/
232
233QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QPlatformPixmap)
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 code/src_gui_image_qimage.cpp 2
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
251QPixmap::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(std::move(image));
262 else
263 *this = fromImage(std::move(image));
264 }
265}
266#endif
267
268
269/*!
270 Destroys the pixmap.
271*/
272
273QPixmap::~QPixmap()
274{
275 Q_ASSERT(!data || data->ref.loadRelaxed() >= 1); // Catch if ref-counting changes again
276}
277
278/*!
279 \internal
280*/
281int 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*/
306QPixmap 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 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*/
343void 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
380QPixmap &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 pixmap.copy().swap(*this);
388 } else {
389 data = pixmap.data;
390 }
391 return *this;
392}
393
394/*!
395 \fn QPixmap &QPixmap::operator=(QPixmap &&other)
396
397 Move-assigns \a other to this QPixmap instance.
398
399 \since 5.2
400*/
401
402/*!
403 \fn void QPixmap::swap(QPixmap &other)
404 \since 4.8
405
406 Swaps pixmap \a other with this pixmap. This operation is very
407 fast and never fails.
408*/
409
410/*!
411 Returns the pixmap as a QVariant.
412*/
413QPixmap::operator QVariant() const
414{
415 return QVariant::fromValue(*this);
416}
417
418/*!
419 \fn bool QPixmap::operator!() const
420
421 Returns \c true if this is a null pixmap; otherwise returns \c false.
422
423 \sa isNull()
424*/
425
426/*!
427 Converts the pixmap to a QImage. Returns a null image if the
428 conversion fails.
429
430 If the pixmap has 1-bit depth, the returned image will also be 1
431 bit deep. Images with more bits will be returned in a format
432 closely represents the underlying system. Usually this will be
433 QImage::Format_ARGB32_Premultiplied for pixmaps with an alpha and
434 QImage::Format_RGB32 or QImage::Format_RGB16 for pixmaps without
435 alpha.
436
437 Note that for the moment, alpha masks on monochrome images are
438 ignored.
439
440 \sa fromImage(), {QImage#Image Formats}{Image Formats}
441*/
442QImage QPixmap::toImage() const
443{
444 if (isNull())
445 return QImage();
446
447 return data->toImage();
448}
449
450/*!
451 \fn QTransform QPixmap::trueMatrix(const QTransform &matrix, int width, int height)
452
453 Returns the actual matrix used for transforming a pixmap with the
454 given \a width, \a height and \a matrix.
455
456 When transforming a pixmap using the transformed() function, the
457 transformation matrix is internally adjusted to compensate for
458 unwanted translation, i.e. transformed() returns the smallest
459 pixmap containing all transformed points of the original
460 pixmap. This function returns the modified matrix, which maps
461 points correctly from the original pixmap into the new pixmap.
462
463 \sa transformed(), {QPixmap#Pixmap Transformations}{Pixmap
464 Transformations}
465*/
466QTransform QPixmap::trueMatrix(const QTransform &m, int w, int h)
467{
468 return QImage::trueMatrix(m, w, h);
469}
470
471/*!
472 \fn bool QPixmap::isQBitmap() const
473
474 Returns \c true if this is a QBitmap; otherwise returns \c false.
475*/
476
477bool QPixmap::isQBitmap() const
478{
479 return data && data->type == QPlatformPixmap::BitmapType;
480}
481
482/*!
483 \fn bool QPixmap::isNull() const
484
485 Returns \c true if this is a null pixmap; otherwise returns \c false.
486
487 A null pixmap has zero width, zero height and no contents. You
488 cannot draw in a null pixmap.
489*/
490bool QPixmap::isNull() const
491{
492 return !data || data->isNull();
493}
494
495/*!
496 \fn int QPixmap::width() const
497
498 Returns the width of the pixmap.
499
500 \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
501*/
502int QPixmap::width() const
503{
504 return data ? data->width() : 0;
505}
506
507/*!
508 \fn int QPixmap::height() const
509
510 Returns the height of the pixmap.
511
512 \sa size(), {QPixmap#Pixmap Information}{Pixmap Information}
513*/
514int QPixmap::height() const
515{
516 return data ? data->height() : 0;
517}
518
519/*!
520 \fn QSize QPixmap::size() const
521
522 Returns the size of the pixmap.
523
524 \sa width(), height(), {QPixmap#Pixmap Information}{Pixmap
525 Information}
526*/
527QSize QPixmap::size() const
528{
529 return data ? QSize(data->width(), data->height()) : QSize(0, 0);
530}
531
532/*!
533 \fn QRect QPixmap::rect() const
534
535 Returns the pixmap's enclosing rectangle.
536
537 \sa {QPixmap#Pixmap Information}{Pixmap Information}
538*/
539QRect QPixmap::rect() const
540{
541 return data ? QRect(0, 0, data->width(), data->height()) : QRect();
542}
543
544/*!
545 \fn int QPixmap::depth() const
546
547 Returns the depth of the pixmap.
548
549 The pixmap depth is also called bits per pixel (bpp) or bit planes
550 of a pixmap. A null pixmap has depth 0.
551
552 \sa defaultDepth(), {QPixmap#Pixmap Information}{Pixmap
553 Information}
554*/
555int QPixmap::depth() const
556{
557 return data ? data->depth() : 0;
558}
559
560/*!
561 Sets a mask bitmap.
562
563 This function merges the \a mask with the pixmap's alpha channel. A pixel
564 value of 1 on the mask means the pixmap's pixel is unchanged; a value of 0
565 means the pixel is transparent. The mask must have the same size as this
566 pixmap.
567
568 Setting a null mask resets the mask, leaving the previously transparent
569 pixels black. The effect of this function is undefined when the pixmap is
570 being painted on.
571
572 \warning This is potentially an expensive operation.
573
574 \sa mask(), {QPixmap#Pixmap Transformations}{Pixmap Transformations},
575 QBitmap
576*/
577void QPixmap::setMask(const QBitmap &mask)
578{
579 if (paintingActive()) {
580 qWarning("QPixmap::setMask: Cannot set mask while pixmap is being painted on");
581 return;
582 }
583
584 if (!mask.isNull() && mask.size() != size()) {
585 qWarning("QPixmap::setMask() mask size differs from pixmap size");
586 return;
587 }
588
589 if (isNull())
590 return;
591
592 if (static_cast<const QPixmap &>(mask).data == data) // trying to selfmask
593 return;
594
595 detach();
596 data->setMask(mask);
597}
598
599/*!
600 Returns the device pixel ratio for the pixmap. This is the
601 ratio between \e{device pixels} and \e{device independent pixels}.
602
603 Use this function when calculating layout geometry based on
604 the pixmap size: QSize layoutSize = image.size() / image.devicePixelRatio()
605
606 The default value is 1.0.
607
608 \sa setDevicePixelRatio(), QImageReader
609*/
610qreal QPixmap::devicePixelRatio() const
611{
612 if (!data)
613 return qreal(1.0);
614 return data->devicePixelRatio();
615}
616
617/*!
618 Sets the device pixel ratio for the pixmap. This is the
619 ratio between image pixels and device-independent pixels.
620
621 The default \a scaleFactor is 1.0. Setting it to something else has
622 two effects:
623
624 QPainters that are opened on the pixmap will be scaled. For
625 example, painting on a 200x200 image if with a ratio of 2.0
626 will result in effective (device-independent) painting bounds
627 of 100x100.
628
629 Code paths in Qt that calculate layout geometry based on the
630 pixmap size will take the ratio into account:
631 QSize layoutSize = pixmap.size() / pixmap.devicePixelRatio()
632 The net effect of this is that the pixmap is displayed as
633 high-DPI pixmap rather than a large pixmap
634 (see \l{Drawing High Resolution Versions of Pixmaps and Images}).
635
636 \sa devicePixelRatio(), deviceIndependentSize()
637*/
638void QPixmap::setDevicePixelRatio(qreal scaleFactor)
639{
640 if (isNull())
641 return;
642
643 if (scaleFactor == data->devicePixelRatio())
644 return;
645
646 detach();
647 data->setDevicePixelRatio(scaleFactor);
648}
649
650/*!
651 Returns the size of the pixmap in device independent pixels.
652
653 This value should be used when using the pixmap size in user interface
654 size calculations.
655
656 The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(),
657*/
658QSizeF QPixmap::deviceIndependentSize() const
659{
660 if (!data)
661 return QSizeF(0, 0);
662 return QSizeF(data->width(), data->height()) / data->devicePixelRatio();
663}
664
665#ifndef QT_NO_IMAGE_HEURISTIC_MASK
666/*!
667 Creates and returns a heuristic mask for this pixmap.
668
669 The function works by selecting a color from one of the corners
670 and then chipping away pixels of that color, starting at all the
671 edges. If \a clipTight is true (the default) the mask is just
672 large enough to cover the pixels; otherwise, the mask is larger
673 than the data pixels.
674
675 The mask may not be perfect but it should be reasonable, so you
676 can do things such as the following:
677
678 \snippet code/src_gui_image_qpixmap.cpp 1
679
680 This function is slow because it involves converting to/from a
681 QImage, and non-trivial computations.
682
683 \sa QImage::createHeuristicMask(), createMaskFromColor()
684*/
685QBitmap QPixmap::createHeuristicMask(bool clipTight) const
686{
687 QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
688 return m;
689}
690#endif
691
692/*!
693 Creates and returns a mask for this pixmap based on the given \a
694 maskColor. If the \a mode is Qt::MaskInColor, all pixels matching the
695 maskColor will be transparent. If \a mode is Qt::MaskOutColor, all pixels
696 matching the maskColor will be opaque.
697
698 This function is slow because it involves converting to/from a
699 QImage.
700
701 \sa createHeuristicMask(), QImage::createMaskFromColor()
702*/
703QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const
704{
705 QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
706 return QBitmap::fromImage(std::move(image).createMaskFromColor(maskColor.rgba(), mode));
707}
708
709/*!
710 Loads a pixmap from the file with the given \a fileName. Returns
711 true if the pixmap was successfully loaded; otherwise invalidates
712 the pixmap and returns \c false.
713
714 The loader attempts to read the pixmap using the specified \a
715 format. If the \a format is not specified (which is the default),
716 the loader probes the file for a header to guess the file format.
717
718 The file name can either refer to an actual file on disk or to one
719 of the application's embedded resources. See the
720 \l{resources.html}{Resource System} overview for details on how to
721 embed pixmaps and other resource files in the application's
722 executable.
723
724 If the data needs to be modified to fit in a lower-resolution
725 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
726 control the conversion.
727
728 Note that QPixmaps are automatically added to the QPixmapCache
729 when loaded from a file in main thread; the key used is internal
730 and cannot be acquired.
731
732 \sa loadFromData(), {QPixmap#Reading and Writing Image
733 Files}{Reading and Writing Image Files}
734*/
735
736bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
737{
738 if (!fileName.isEmpty()) {
739
740 QFileInfo info(fileName);
741 // Note: If no extension is provided, we try to match the
742 // file against known plugin extensions
743 if (info.completeSuffix().isEmpty() || info.exists()) {
744 const bool inGuiThread = qApp->thread() == QThread::currentThread();
745
746 QString key = QLatin1String("qt_pixmap")
747 % info.absoluteFilePath()
748 % HexString<uint>(info.lastModified().toSecsSinceEpoch())
749 % HexString<quint64>(info.size())
750 % HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);
751
752 if (inGuiThread && QPixmapCache::find(key, this))
753 return true;
754
755 data = QPlatformPixmap::create(0, 0, data ? data->pixelType() : QPlatformPixmap::PixmapType);
756
757 if (data->fromFile(fileName, format, flags)) {
758 if (inGuiThread)
759 QPixmapCache::insert(key, *this);
760 return true;
761 }
762 }
763 }
764
765 if (!isNull()) {
766 if (isQBitmap())
767 *this = QBitmap();
768 else
769 data.reset();
770 }
771 return false;
772}
773
774/*!
775 \fn bool QPixmap::loadFromData(const uchar *data, uint len, const char *format, Qt::ImageConversionFlags flags)
776
777 Loads a pixmap from the \a len first bytes of the given binary \a
778 data. Returns \c true if the pixmap was loaded successfully;
779 otherwise invalidates the pixmap and returns \c false.
780
781 The loader attempts to read the pixmap using the specified \a
782 format. If the \a format is not specified (which is the default),
783 the loader probes the file for a header to guess the file format.
784
785 If the data needs to be modified to fit in a lower-resolution
786 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
787 control the conversion.
788
789 \sa load(), {QPixmap#Reading and Writing Image Files}{Reading and
790 Writing Image Files}
791*/
792
793bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
794{
795 if (len == 0 || buf == nullptr) {
796 data.reset();
797 return false;
798 }
799
800 data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
801
802 if (data->fromData(buf, len, format, flags))
803 return true;
804
805 data.reset();
806 return false;
807}
808
809/*!
810 \fn bool QPixmap::loadFromData(const QByteArray &data, const char *format, Qt::ImageConversionFlags flags)
811
812 \overload
813
814 Loads a pixmap from the binary \a data using the specified \a
815 format and conversion \a flags.
816*/
817
818
819/*!
820 Saves the pixmap to the file with the given \a fileName using the
821 specified image file \a format and \a quality factor. Returns \c true
822 if successful; otherwise returns \c false.
823
824 The \a quality factor must be in the range [0,100] or -1. Specify
825 0 to obtain small compressed files, 100 for large uncompressed
826 files, and -1 to use the default settings.
827
828 If \a format is \nullptr, an image format will be chosen from
829 \a fileName's suffix.
830
831 \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
832 Image Files}
833*/
834
835bool QPixmap::save(const QString &fileName, const char *format, int quality) const
836{
837 if (isNull())
838 return false; // nothing to save
839 QImageWriter writer(fileName, format);
840 return doImageIO(&writer, quality);
841}
842
843/*!
844 \overload
845
846 This function writes a QPixmap to the given \a device using the
847 specified image file \a format and \a quality factor. This can be
848 used, for example, to save a pixmap directly into a QByteArray:
849
850 \snippet image/image.cpp 1
851*/
852
853bool QPixmap::save(QIODevice* device, const char* format, int quality) const
854{
855 if (isNull())
856 return false; // nothing to save
857 QImageWriter writer(device, format);
858 return doImageIO(&writer, quality);
859}
860
861/*! \internal
862*/
863bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
864{
865 if (quality > 100 || quality < -1)
866 qWarning("QPixmap::save: quality out of range [-1,100]");
867 if (quality >= 0)
868 writer->setQuality(qMin(quality,100));
869 return writer->write(toImage());
870}
871
872
873/*!
874 Fills the pixmap with the given \a color.
875
876 The effect of this function is undefined when the pixmap is
877 being painted on.
878
879 \sa {QPixmap#Pixmap Transformations}{Pixmap Transformations}
880*/
881
882void QPixmap::fill(const QColor &color)
883{
884 if (isNull())
885 return;
886
887 // Some people are probably already calling fill while a painter is active, so to not break
888 // their programs, only print a warning and return when the fill operation could cause a crash.
889 if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
890 qWarning("QPixmap::fill: Cannot fill while pixmap is being painted on");
891 return;
892 }
893
894 if (data->ref.loadRelaxed() == 1) {
895 // detach() will also remove this pixmap from caches, so
896 // it has to be called even when ref == 1.
897 detach();
898 } else {
899 // Don't bother to make a copy of the data object, since
900 // it will be filled with new pixel data anyway.
901 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
902 d->resize(data->width(), data->height());
903 data = d;
904 }
905 data->fill(color);
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*/
915qint64 QPixmap::cacheKey() const
916{
917 if (isNull())
918 return 0;
919
920 Q_ASSERT(data);
921 return data->cacheKey();
922}
923
924#if 0
925static 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
940/*****************************************************************************
941 QPixmap stream functions
942 *****************************************************************************/
943#if !defined(QT_NO_DATASTREAM)
944/*!
945 \relates QPixmap
946
947 Writes the given \a pixmap to the given \a stream as a PNG
948 image. Note that writing the stream to a file will not produce a
949 valid image file.
950
951 \sa QPixmap::save(), {Serializing Qt Data Types}
952*/
953
954QDataStream &operator<<(QDataStream &stream, const QPixmap &pixmap)
955{
956 return stream << pixmap.toImage();
957}
958
959/*!
960 \relates QPixmap
961
962 Reads an image from the given \a stream into the given \a pixmap.
963
964 \sa QPixmap::load(), {Serializing Qt Data Types}
965*/
966
967QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
968{
969 QImage image;
970 stream >> image;
971
972 if (image.isNull()) {
973 pixmap = QPixmap();
974 } else if (image.depth() == 1) {
975 pixmap = QBitmap::fromImage(std::move(image));
976 } else {
977 pixmap = QPixmap::fromImage(std::move(image));
978 }
979 return stream;
980}
981
982#endif // QT_NO_DATASTREAM
983
984/*!
985 \internal
986*/
987
988bool QPixmap::isDetached() const
989{
990 return data && data->ref.loadRelaxed() == 1;
991}
992
993/*!
994 Replaces this pixmap's data with the given \a image using the
995 specified \a flags to control the conversion. The \a flags
996 argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
997 Passing 0 for \a flags sets all the default options. Returns \c true
998 if the result is that this pixmap is not null.
999
1000 Note: this function was part of Qt 3 support in Qt 4.6 and earlier.
1001 It has been promoted to official API status in 4.7 to support updating
1002 the pixmap's image without creating a new QPixmap as fromImage() would.
1003
1004 \sa fromImage()
1005 \since 4.7
1006*/
1007bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags)
1008{
1009 detach();
1010 if (image.isNull() || !data)
1011 *this = QPixmap::fromImage(image, flags);
1012 else
1013 data->fromImage(image, flags);
1014 return !isNull();
1015}
1016
1017/*!
1018 \fn QPixmap QPixmap::scaled(int width, int height,
1019 Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode
1020 transformMode) const
1021
1022 \overload
1023
1024 Returns a copy of the pixmap scaled to a rectangle with the given
1025 \a width and \a height according to the given \a aspectRatioMode and
1026 \a transformMode.
1027
1028 If either the \a width or the \a height is zero or negative, this
1029 function returns a null pixmap.
1030*/
1031
1032/*!
1033 \fn QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode
1034 aspectRatioMode, Qt::TransformationMode transformMode) const
1035
1036 Scales the pixmap to the given \a size, using the aspect ratio and
1037 transformation modes specified by \a aspectRatioMode and \a
1038 transformMode.
1039
1040 \image qimage-scaling.png
1041
1042 \list
1043 \li If \a aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap
1044 is scaled to \a size.
1045 \li If \a aspectRatioMode is Qt::KeepAspectRatio, the pixmap is
1046 scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
1047 \li If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
1048 the pixmap is scaled to a rectangle as small as possible
1049 outside \a size, preserving the aspect ratio.
1050 \endlist
1051
1052 If the given \a size is empty, this function returns a null
1053 pixmap.
1054
1055
1056 In some cases it can be more beneficial to draw the pixmap to a
1057 painter with a scale set rather than scaling the pixmap. This is
1058 the case when the painter is for instance based on OpenGL or when
1059 the scale factor changes rapidly.
1060
1061 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1062 Transformations}
1063
1064*/
1065QPixmap QPixmap::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
1066{
1067 if (isNull()) {
1068 qWarning("QPixmap::scaled: Pixmap is a null pixmap");
1069 return QPixmap();
1070 }
1071 if (s.isEmpty())
1072 return QPixmap();
1073
1074 QSize newSize = size();
1075 newSize.scale(s, aspectMode);
1076 newSize.rwidth() = qMax(newSize.width(), 1);
1077 newSize.rheight() = qMax(newSize.height(), 1);
1078 if (newSize == size())
1079 return *this;
1080
1081 Q_TRACE_SCOPE(QPixmap_scaled, s, aspectMode, mode);
1082
1083 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1084 (qreal)newSize.height() / height());
1085 QPixmap pix = transformed(wm, mode);
1086 return pix;
1087}
1088
1089/*!
1090 \fn QPixmap QPixmap::scaledToWidth(int width, Qt::TransformationMode
1091 mode) const
1092
1093 Returns a scaled copy of the image. The returned image is scaled
1094 to the given \a width using the specified transformation \a mode.
1095 The height of the pixmap is automatically calculated so that the
1096 aspect ratio of the pixmap is preserved.
1097
1098 If \a width is 0 or negative, a null pixmap is returned.
1099
1100 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1101 Transformations}
1102*/
1103QPixmap QPixmap::scaledToWidth(int w, Qt::TransformationMode mode) const
1104{
1105 if (isNull()) {
1106 qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap");
1107 return copy();
1108 }
1109 if (w <= 0)
1110 return QPixmap();
1111
1112 Q_TRACE_SCOPE(QPixmap_scaledToWidth, w, mode);
1113
1114 qreal factor = (qreal) w / width();
1115 QTransform wm = QTransform::fromScale(factor, factor);
1116 return transformed(wm, mode);
1117}
1118
1119/*!
1120 \fn QPixmap QPixmap::scaledToHeight(int height,
1121 Qt::TransformationMode mode) const
1122
1123 Returns a scaled copy of the image. The returned image is scaled
1124 to the given \a height using the specified transformation \a mode.
1125 The width of the pixmap is automatically calculated so that the
1126 aspect ratio of the pixmap is preserved.
1127
1128 If \a height is 0 or negative, a null pixmap is returned.
1129
1130 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1131 Transformations}
1132*/
1133QPixmap QPixmap::scaledToHeight(int h, Qt::TransformationMode mode) const
1134{
1135 if (isNull()) {
1136 qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap");
1137 return copy();
1138 }
1139 if (h <= 0)
1140 return QPixmap();
1141
1142 Q_TRACE_SCOPE(QPixmap_scaledToHeight, h, mode);
1143
1144 qreal factor = (qreal) h / height();
1145 QTransform wm = QTransform::fromScale(factor, factor);
1146 return transformed(wm, mode);
1147}
1148
1149/*!
1150 Returns a copy of the pixmap that is transformed using the given
1151 transformation \a transform and transformation \a mode. The original
1152 pixmap is not changed.
1153
1154 The transformation \a transform is internally adjusted to compensate
1155 for unwanted translation; i.e. the pixmap produced is the smallest
1156 pixmap that contains all the transformed points of the original
1157 pixmap. Use the trueMatrix() function to retrieve the actual
1158 matrix used for transforming the pixmap.
1159
1160 This function is slow because it involves transformation to a
1161 QImage, non-trivial computations and a transformation back to a
1162 QPixmap.
1163
1164 \sa trueMatrix(), {QPixmap#Pixmap Transformations}{Pixmap
1165 Transformations}
1166*/
1167QPixmap QPixmap::transformed(const QTransform &transform,
1168 Qt::TransformationMode mode) const
1169{
1170 if (isNull() || transform.type() <= QTransform::TxTranslate)
1171 return *this;
1172
1173 return data->transformed(transform, mode);
1174}
1175
1176/*!
1177 \class QPixmap
1178 \inmodule QtGui
1179
1180 \brief The QPixmap class is an off-screen image representation
1181 that can be used as a paint device.
1182
1183 \ingroup painting
1184 \ingroup shared
1185
1186
1187 Qt provides four classes for handling image data: QImage, QPixmap,
1188 QBitmap and QPicture. QImage is designed and optimized for I/O,
1189 and for direct pixel access and manipulation, while QPixmap is
1190 designed and optimized for showing images on screen. QBitmap is
1191 only a convenience class that inherits QPixmap, ensuring a depth
1192 of 1. The isQBitmap() function returns \c true if a QPixmap object is
1193 really a bitmap, otherwise returns \c false. Finally, the QPicture class
1194 is a paint device that records and replays QPainter commands.
1195
1196 A QPixmap can easily be displayed on the screen using QLabel or
1197 one of QAbstractButton's subclasses (such as QPushButton and
1198 QToolButton). QLabel has a pixmap property, whereas
1199 QAbstractButton has an icon property.
1200
1201 QPixmap objects can be passed around by value since the QPixmap
1202 class uses implicit data sharing. For more information, see the \l
1203 {Implicit Data Sharing} documentation. QPixmap objects can also be
1204 streamed.
1205
1206 Note that the pixel data in a pixmap is internal and is managed by
1207 the underlying window system. Because QPixmap is a QPaintDevice
1208 subclass, QPainter can be used to draw directly onto pixmaps.
1209 Pixels can only be accessed through QPainter functions or by
1210 converting the QPixmap to a QImage. However, the fill() function
1211 is available for initializing the entire pixmap with a given color.
1212
1213 There are functions to convert between QImage and
1214 QPixmap. Typically, the QImage class is used to load an image
1215 file, optionally manipulating the image data, before the QImage
1216 object is converted into a QPixmap to be shown on
1217 screen. Alternatively, if no manipulation is desired, the image
1218 file can be loaded directly into a QPixmap.
1219
1220 QPixmap provides a collection of functions that can be used to
1221 obtain a variety of information about the pixmap. In addition,
1222 there are several functions that enables transformation of the
1223 pixmap.
1224
1225 \tableofcontents
1226
1227 \section1 Reading and Writing Image Files
1228
1229 QPixmap provides several ways of reading an image file: The file
1230 can be loaded when constructing the QPixmap object, or by using
1231 the load() or loadFromData() functions later on. When loading an
1232 image, the file name can either refer to an actual file on disk or
1233 to one of the application's embedded resources. See \l{The Qt
1234 Resource System} overview for details on how to embed images and
1235 other resource files in the application's executable.
1236
1237 Simply call the save() function to save a QPixmap object.
1238
1239 The complete list of supported file formats are available through
1240 the QImageReader::supportedImageFormats() and
1241 QImageWriter::supportedImageFormats() functions. New file formats
1242 can be added as plugins. By default, Qt supports the following
1243 formats:
1244
1245 \table
1246 \header \li Format \li Description \li Qt's support
1247 \row \li BMP \li Windows Bitmap \li Read/write
1248 \row \li GIF \li Graphic Interchange Format (optional) \li Read
1249 \row \li JPG \li Joint Photographic Experts Group \li Read/write
1250 \row \li JPEG \li Joint Photographic Experts Group \li Read/write
1251 \row \li PNG \li Portable Network Graphics \li Read/write
1252 \row \li PBM \li Portable Bitmap \li Read
1253 \row \li PGM \li Portable Graymap \li Read
1254 \row \li PPM \li Portable Pixmap \li Read/write
1255 \row \li XBM \li X11 Bitmap \li Read/write
1256 \row \li XPM \li X11 Pixmap \li Read/write
1257 \endtable
1258
1259 \section1 Pixmap Information
1260
1261 QPixmap provides a collection of functions that can be used to
1262 obtain a variety of information about the pixmap:
1263
1264 \table
1265 \header
1266 \li \li Available Functions
1267 \row
1268 \li Geometry
1269 \li
1270 The size(), width() and height() functions provide information
1271 about the pixmap's size. The rect() function returns the image's
1272 enclosing rectangle.
1273
1274 \row
1275 \li Alpha component
1276 \li
1277
1278 The hasAlphaChannel() returns \c true if the pixmap has a format that
1279 respects the alpha channel, otherwise returns \c false. The hasAlpha(),
1280 setMask() and mask() functions are legacy and should not be used.
1281 They are potentially very slow.
1282
1283 The createHeuristicMask() function creates and returns a 1-bpp
1284 heuristic mask (i.e. a QBitmap) for this pixmap. It works by
1285 selecting a color from one of the corners and then chipping away
1286 pixels of that color, starting at all the edges. The
1287 createMaskFromColor() function creates and returns a mask (i.e. a
1288 QBitmap) for the pixmap based on a given color.
1289
1290 \row
1291 \li Low-level information
1292 \li
1293
1294 The depth() function returns the depth of the pixmap. The
1295 defaultDepth() function returns the default depth, i.e. the depth
1296 used by the application on the given screen.
1297
1298 The cacheKey() function returns a number that uniquely
1299 identifies the contents of the QPixmap object.
1300
1301 \endtable
1302
1303 \section1 Pixmap Conversion
1304
1305 A QPixmap object can be converted into a QImage using the
1306 toImage() function. Likewise, a QImage can be converted into a
1307 QPixmap using the fromImage(). If this is too expensive an
1308 operation, you can use QBitmap::fromImage() instead.
1309
1310 To convert a QPixmap to and from HICON you can use the QtWinExtras
1311 functions QtWin::toHICON() and QtWin::fromHICON() respectively.
1312
1313 \section1 Pixmap Transformations
1314
1315 QPixmap supports a number of functions for creating a new pixmap
1316 that is a transformed version of the original:
1317
1318 The scaled(), scaledToWidth() and scaledToHeight() functions
1319 return scaled copies of the pixmap, while the copy() function
1320 creates a QPixmap that is a plain copy of the original one.
1321
1322 The transformed() function returns a copy of the pixmap that is
1323 transformed with the given transformation matrix and
1324 transformation mode: Internally, the transformation matrix is
1325 adjusted to compensate for unwanted translation,
1326 i.e. transformed() returns the smallest pixmap containing all
1327 transformed points of the original pixmap. The static trueMatrix()
1328 function returns the actual matrix used for transforming the
1329 pixmap.
1330
1331 \sa QBitmap, QImage, QImageReader, QImageWriter
1332*/
1333
1334
1335/*!
1336 \typedef QPixmap::DataPtr
1337 \internal
1338*/
1339
1340/*!
1341 \fn DataPtr &QPixmap::data_ptr()
1342 \internal
1343*/
1344
1345/*!
1346 Returns \c true if this pixmap has an alpha channel, \e or has a
1347 mask, otherwise returns \c false.
1348
1349 \sa hasAlphaChannel(), mask()
1350*/
1351bool QPixmap::hasAlpha() const
1352{
1353 return data && data->hasAlphaChannel();
1354}
1355
1356/*!
1357 Returns \c true if the pixmap has a format that respects the alpha
1358 channel, otherwise returns \c false.
1359
1360 \sa hasAlpha()
1361*/
1362bool QPixmap::hasAlphaChannel() const
1363{
1364 return data && data->hasAlphaChannel();
1365}
1366
1367/*!
1368 \internal
1369*/
1370int QPixmap::metric(PaintDeviceMetric metric) const
1371{
1372 return data ? data->metric(metric) : 0;
1373}
1374
1375/*!
1376 \internal
1377*/
1378QPaintEngine *QPixmap::paintEngine() const
1379{
1380 return data ? data->paintEngine() : nullptr;
1381}
1382
1383/*!
1384 \fn QBitmap QPixmap::mask() const
1385
1386 Extracts a bitmap mask from the pixmap's alpha channel.
1387
1388 \warning This is potentially an expensive operation. The mask of
1389 the pixmap is extracted dynamically from the pixeldata.
1390
1391 \sa setMask(), {QPixmap#Pixmap Information}{Pixmap Information}
1392*/
1393QBitmap QPixmap::mask() const
1394{
1395 return data ? data->mask() : QBitmap();
1396}
1397
1398/*!
1399 Returns the default pixmap depth used by the application.
1400
1401 On all platforms the depth of the primary screen will be returned.
1402
1403 \note QGuiApplication must be created before calling this function.
1404
1405 \sa depth(), QColormap::depth(), {QPixmap#Pixmap Information}{Pixmap Information}
1406
1407*/
1408int QPixmap::defaultDepth()
1409{
1410 QScreen *primary = QGuiApplication::primaryScreen();
1411 if (Q_LIKELY(primary))
1412 return primary->depth();
1413 qWarning("QPixmap: QGuiApplication must be created before calling defaultDepth().");
1414 return 0;
1415}
1416
1417/*!
1418 Detaches the pixmap from shared pixmap data.
1419
1420 A pixmap is automatically detached by Qt whenever its contents are
1421 about to change. This is done in almost all QPixmap member
1422 functions that modify the pixmap (fill(), fromImage(),
1423 load(), etc.), and in QPainter::begin() on a pixmap.
1424
1425 There are two exceptions in which detach() must be called
1426 explicitly, that is when calling the handle() or the
1427 x11PictureHandle() function (only available on X11). Otherwise,
1428 any modifications done using system calls, will be performed on
1429 the shared data.
1430
1431 The detach() function returns immediately if there is just a
1432 single reference or if the pixmap has not been initialized yet.
1433*/
1434void QPixmap::detach()
1435{
1436 if (!data)
1437 return;
1438
1439 // QPixmap.data member may be QRuntimePlatformPixmap so use handle() function to get
1440 // the actual underlaying runtime pixmap data.
1441 QPlatformPixmap *pd = handle();
1442 QPlatformPixmap::ClassId id = pd->classId();
1443 if (id == QPlatformPixmap::RasterClass) {
1444 QRasterPlatformPixmap *rasterData = static_cast<QRasterPlatformPixmap*>(pd);
1445 rasterData->image.detach();
1446 }
1447
1448 if (data->is_cached && data->ref.loadRelaxed() == 1)
1449 QImagePixmapCleanupHooks::executePlatformPixmapModificationHooks(data.data());
1450
1451 if (data->ref.loadRelaxed() != 1) {
1452 *this = copy();
1453 }
1454 ++data->detach_no;
1455}
1456
1457/*!
1458 \fn QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1459
1460 Converts the given \a image to a pixmap using the specified \a
1461 flags to control the conversion. The \a flags argument is a
1462 bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for \a
1463 flags sets all the default options.
1464
1465 In case of monochrome and 8-bit images, the image is first
1466 converted to a 32-bit pixmap and then filled with the colors in
1467 the color table. If this is too expensive an operation, you can
1468 use QBitmap::fromImage() instead.
1469
1470 \sa fromImageReader(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1471*/
1472QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1473{
1474 if (image.isNull())
1475 return QPixmap();
1476
1477 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1478 qWarning("QPixmap::fromImage: QPixmap cannot be created without a QGuiApplication");
1479 return QPixmap();
1480 }
1481
1482 QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1483 data->fromImage(image, flags);
1484 return QPixmap(data.take());
1485}
1486
1487/*!
1488 \fn QPixmap QPixmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags)
1489 \since 5.3
1490 \overload
1491
1492 Converts the given \a image to a pixmap without copying if possible.
1493*/
1494
1495
1496/*!
1497 \internal
1498*/
1499QPixmap QPixmap::fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags)
1500{
1501 if (image.isNull())
1502 return QPixmap();
1503
1504 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1505 qWarning("QPixmap::fromImageInPlace: QPixmap cannot be created without a QGuiApplication");
1506 return QPixmap();
1507 }
1508
1509 QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1510 data->fromImageInPlace(image, flags);
1511 return QPixmap(data.take());
1512}
1513
1514/*!
1515 \fn QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1516
1517 Create a QPixmap from an image read directly from an \a imageReader.
1518 The \a flags argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
1519 Passing 0 for \a flags sets all the default options.
1520
1521 On some systems, reading an image directly to QPixmap can use less memory than
1522 reading a QImage to convert it to QPixmap.
1523
1524 \sa fromImage(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1525*/
1526QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1527{
1528 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1529 qWarning("QPixmap::fromImageReader: QPixmap cannot be created without a QGuiApplication");
1530 return QPixmap();
1531 }
1532
1533 QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1534 data->fromImageReader(imageReader, flags);
1535 return QPixmap(data.take());
1536}
1537
1538/*!
1539 \internal
1540*/
1541QPlatformPixmap* QPixmap::handle() const
1542{
1543 return data.data();
1544}
1545
1546#ifndef QT_NO_DEBUG_STREAM
1547QDebug operator<<(QDebug dbg, const QPixmap &r)
1548{
1549 QDebugStateSaver saver(dbg);
1550 dbg.resetFormat();
1551 dbg.nospace();
1552 dbg << "QPixmap(";
1553 if (r.isNull()) {
1554 dbg << "null";
1555 } else {
1556 dbg << r.size() << ",depth=" << r.depth()
1557 << ",devicePixelRatio=" << r.devicePixelRatio()
1558 << ",cacheKey=" << Qt::showbase << Qt::hex << r.cacheKey() << Qt::dec << Qt::noshowbase;
1559 }
1560 dbg << ')';
1561 return dbg;
1562}
1563#endif
1564
1565QT_END_NAMESPACE
1566