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()
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#ifndef QT_NO_IMAGE_HEURISTIC_MASK
651/*!
652 Creates and returns a heuristic mask for this pixmap.
653
654 The function works by selecting a color from one of the corners
655 and then chipping away pixels of that color, starting at all the
656 edges. If \a clipTight is true (the default) the mask is just
657 large enough to cover the pixels; otherwise, the mask is larger
658 than the data pixels.
659
660 The mask may not be perfect but it should be reasonable, so you
661 can do things such as the following:
662
663 \snippet code/src_gui_image_qpixmap.cpp 1
664
665 This function is slow because it involves converting to/from a
666 QImage, and non-trivial computations.
667
668 \sa QImage::createHeuristicMask(), createMaskFromColor()
669*/
670QBitmap QPixmap::createHeuristicMask(bool clipTight) const
671{
672 QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
673 return m;
674}
675#endif
676
677/*!
678 Creates and returns a mask for this pixmap based on the given \a
679 maskColor. If the \a mode is Qt::MaskInColor, all pixels matching the
680 maskColor will be transparent. If \a mode is Qt::MaskOutColor, all pixels
681 matching the maskColor will be opaque.
682
683 This function is slow because it involves converting to/from a
684 QImage.
685
686 \sa createHeuristicMask(), QImage::createMaskFromColor()
687*/
688QBitmap QPixmap::createMaskFromColor(const QColor &maskColor, Qt::MaskMode mode) const
689{
690 QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
691 return QBitmap::fromImage(std::move(image).createMaskFromColor(maskColor.rgba(), mode));
692}
693
694/*!
695 Loads a pixmap from the file with the given \a fileName. Returns
696 true if the pixmap was successfully loaded; otherwise invalidates
697 the pixmap and returns \c false.
698
699 The loader attempts to read the pixmap using the specified \a
700 format. If the \a format is not specified (which is the default),
701 the loader probes the file for a header to guess the file format.
702
703 The file name can either refer to an actual file on disk or to one
704 of the application's embedded resources. See the
705 \l{resources.html}{Resource System} overview for details on how to
706 embed pixmaps and other resource files in the application's
707 executable.
708
709 If the data needs to be modified to fit in a lower-resolution
710 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
711 control the conversion.
712
713 Note that QPixmaps are automatically added to the QPixmapCache
714 when loaded from a file in main thread; the key used is internal
715 and cannot be acquired.
716
717 \sa loadFromData(), {QPixmap#Reading and Writing Image
718 Files}{Reading and Writing Image Files}
719*/
720
721bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConversionFlags flags)
722{
723 if (!fileName.isEmpty()) {
724
725 QFileInfo info(fileName);
726 // Note: If no extension is provided, we try to match the
727 // file against known plugin extensions
728 if (info.completeSuffix().isEmpty() || info.exists()) {
729 const bool inGuiThread = qApp->thread() == QThread::currentThread();
730
731 QString key = QLatin1String("qt_pixmap")
732 % info.absoluteFilePath()
733 % HexString<uint>(info.lastModified().toSecsSinceEpoch())
734 % HexString<quint64>(info.size())
735 % HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);
736
737 if (inGuiThread && QPixmapCache::find(key, this))
738 return true;
739
740 data = QPlatformPixmap::create(0, 0, data ? data->pixelType() : QPlatformPixmap::PixmapType);
741
742 if (data->fromFile(fileName, format, flags)) {
743 if (inGuiThread)
744 QPixmapCache::insert(key, *this);
745 return true;
746 }
747 }
748 }
749
750 if (!isNull()) {
751 if (isQBitmap())
752 *this = QBitmap();
753 else
754 data.reset();
755 }
756 return false;
757}
758
759/*!
760 \fn bool QPixmap::loadFromData(const uchar *data, uint len, const char *format, Qt::ImageConversionFlags flags)
761
762 Loads a pixmap from the \a len first bytes of the given binary \a
763 data. Returns \c true if the pixmap was loaded successfully;
764 otherwise invalidates the pixmap and returns \c false.
765
766 The loader attempts to read the pixmap using the specified \a
767 format. If the \a format is not specified (which is the default),
768 the loader probes the file for a header to guess the file format.
769
770 If the data needs to be modified to fit in a lower-resolution
771 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
772 control the conversion.
773
774 \sa load(), {QPixmap#Reading and Writing Image Files}{Reading and
775 Writing Image Files}
776*/
777
778bool QPixmap::loadFromData(const uchar *buf, uint len, const char *format, Qt::ImageConversionFlags flags)
779{
780 if (len == 0 || buf == nullptr) {
781 data.reset();
782 return false;
783 }
784
785 data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
786
787 if (data->fromData(buf, len, format, flags))
788 return true;
789
790 data.reset();
791 return false;
792}
793
794/*!
795 \fn bool QPixmap::loadFromData(const QByteArray &data, const char *format, Qt::ImageConversionFlags flags)
796
797 \overload
798
799 Loads a pixmap from the binary \a data using the specified \a
800 format and conversion \a flags.
801*/
802
803
804/*!
805 Saves the pixmap to the file with the given \a fileName using the
806 specified image file \a format and \a quality factor. Returns \c true
807 if successful; otherwise returns \c false.
808
809 The \a quality factor must be in the range [0,100] or -1. Specify
810 0 to obtain small compressed files, 100 for large uncompressed
811 files, and -1 to use the default settings.
812
813 If \a format is \nullptr, an image format will be chosen from
814 \a fileName's suffix.
815
816 \sa {QPixmap#Reading and Writing Image Files}{Reading and Writing
817 Image Files}
818*/
819
820bool QPixmap::save(const QString &fileName, const char *format, int quality) const
821{
822 if (isNull())
823 return false; // nothing to save
824 QImageWriter writer(fileName, format);
825 return doImageIO(&writer, quality);
826}
827
828/*!
829 \overload
830
831 This function writes a QPixmap to the given \a device using the
832 specified image file \a format and \a quality factor. This can be
833 used, for example, to save a pixmap directly into a QByteArray:
834
835 \snippet image/image.cpp 1
836*/
837
838bool QPixmap::save(QIODevice* device, const char* format, int quality) const
839{
840 if (isNull())
841 return false; // nothing to save
842 QImageWriter writer(device, format);
843 return doImageIO(&writer, quality);
844}
845
846/*! \internal
847*/
848bool QPixmap::doImageIO(QImageWriter *writer, int quality) const
849{
850 if (quality > 100 || quality < -1)
851 qWarning("QPixmap::save: quality out of range [-1,100]");
852 if (quality >= 0)
853 writer->setQuality(qMin(quality,100));
854 return writer->write(toImage());
855}
856
857
858/*!
859 Fills the pixmap with the given \a color.
860
861 The effect of this function is undefined when the pixmap is
862 being painted on.
863
864 \sa {QPixmap#Pixmap Transformations}{Pixmap Transformations}
865*/
866
867void QPixmap::fill(const QColor &color)
868{
869 if (isNull())
870 return;
871
872 // Some people are probably already calling fill while a painter is active, so to not break
873 // their programs, only print a warning and return when the fill operation could cause a crash.
874 if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
875 qWarning("QPixmap::fill: Cannot fill while pixmap is being painted on");
876 return;
877 }
878
879 if (data->ref.loadRelaxed() == 1) {
880 // detach() will also remove this pixmap from caches, so
881 // it has to be called even when ref == 1.
882 detach();
883 } else {
884 // Don't bother to make a copy of the data object, since
885 // it will be filled with new pixel data anyway.
886 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
887 d->resize(data->width(), data->height());
888 data = d;
889 }
890 data->fill(color);
891}
892
893/*!
894 Returns a number that identifies this QPixmap. Distinct QPixmap
895 objects can only have the same cache key if they refer to the same
896 contents.
897
898 The cacheKey() will change when the pixmap is altered.
899*/
900qint64 QPixmap::cacheKey() const
901{
902 if (isNull())
903 return 0;
904
905 Q_ASSERT(data);
906 return data->cacheKey();
907}
908
909#if 0
910static void sendResizeEvents(QWidget *target)
911{
912 QResizeEvent e(target->size(), QSize());
913 QApplication::sendEvent(target, &e);
914
915 const QObjectList children = target->children();
916 for (int i = 0; i < children.size(); ++i) {
917 QWidget *child = static_cast<QWidget*>(children.at(i));
918 if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
919 sendResizeEvents(child);
920 }
921}
922#endif
923
924
925/*****************************************************************************
926 QPixmap stream functions
927 *****************************************************************************/
928#if !defined(QT_NO_DATASTREAM)
929/*!
930 \relates QPixmap
931
932 Writes the given \a pixmap to the given \a stream as a PNG
933 image. Note that writing the stream to a file will not produce a
934 valid image file.
935
936 \sa QPixmap::save(), {Serializing Qt Data Types}
937*/
938
939QDataStream &operator<<(QDataStream &stream, const QPixmap &pixmap)
940{
941 return stream << pixmap.toImage();
942}
943
944/*!
945 \relates QPixmap
946
947 Reads an image from the given \a stream into the given \a pixmap.
948
949 \sa QPixmap::load(), {Serializing Qt Data Types}
950*/
951
952QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
953{
954 QImage image;
955 stream >> image;
956
957 if (image.isNull()) {
958 pixmap = QPixmap();
959 } else if (image.depth() == 1) {
960 pixmap = QBitmap::fromImage(std::move(image));
961 } else {
962 pixmap = QPixmap::fromImage(std::move(image));
963 }
964 return stream;
965}
966
967#endif // QT_NO_DATASTREAM
968
969/*!
970 \internal
971*/
972
973bool QPixmap::isDetached() const
974{
975 return data && data->ref.loadRelaxed() == 1;
976}
977
978/*!
979 Replaces this pixmap's data with the given \a image using the
980 specified \a flags to control the conversion. The \a flags
981 argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
982 Passing 0 for \a flags sets all the default options. Returns \c true
983 if the result is that this pixmap is not null.
984
985 Note: this function was part of Qt 3 support in Qt 4.6 and earlier.
986 It has been promoted to official API status in 4.7 to support updating
987 the pixmap's image without creating a new QPixmap as fromImage() would.
988
989 \sa fromImage()
990 \since 4.7
991*/
992bool QPixmap::convertFromImage(const QImage &image, Qt::ImageConversionFlags flags)
993{
994 detach();
995 if (image.isNull() || !data)
996 *this = QPixmap::fromImage(image, flags);
997 else
998 data->fromImage(image, flags);
999 return !isNull();
1000}
1001
1002/*!
1003 \fn QPixmap QPixmap::scaled(int width, int height,
1004 Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode
1005 transformMode) const
1006
1007 \overload
1008
1009 Returns a copy of the pixmap scaled to a rectangle with the given
1010 \a width and \a height according to the given \a aspectRatioMode and
1011 \a transformMode.
1012
1013 If either the \a width or the \a height is zero or negative, this
1014 function returns a null pixmap.
1015*/
1016
1017/*!
1018 \fn QPixmap QPixmap::scaled(const QSize &size, Qt::AspectRatioMode
1019 aspectRatioMode, Qt::TransformationMode transformMode) const
1020
1021 Scales the pixmap to the given \a size, using the aspect ratio and
1022 transformation modes specified by \a aspectRatioMode and \a
1023 transformMode.
1024
1025 \image qimage-scaling.png
1026
1027 \list
1028 \li If \a aspectRatioMode is Qt::IgnoreAspectRatio, the pixmap
1029 is scaled to \a size.
1030 \li If \a aspectRatioMode is Qt::KeepAspectRatio, the pixmap is
1031 scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
1032 \li If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
1033 the pixmap is scaled to a rectangle as small as possible
1034 outside \a size, preserving the aspect ratio.
1035 \endlist
1036
1037 If the given \a size is empty, this function returns a null
1038 pixmap.
1039
1040
1041 In some cases it can be more beneficial to draw the pixmap to a
1042 painter with a scale set rather than scaling the pixmap. This is
1043 the case when the painter is for instance based on OpenGL or when
1044 the scale factor changes rapidly.
1045
1046 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1047 Transformations}
1048
1049*/
1050QPixmap QPixmap::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
1051{
1052 if (isNull()) {
1053 qWarning("QPixmap::scaled: Pixmap is a null pixmap");
1054 return QPixmap();
1055 }
1056 if (s.isEmpty())
1057 return QPixmap();
1058
1059 QSize newSize = size();
1060 newSize.scale(s, aspectMode);
1061 newSize.rwidth() = qMax(newSize.width(), 1);
1062 newSize.rheight() = qMax(newSize.height(), 1);
1063 if (newSize == size())
1064 return *this;
1065
1066 Q_TRACE_SCOPE(QPixmap_scaled, s, aspectMode, mode);
1067
1068 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1069 (qreal)newSize.height() / height());
1070 QPixmap pix = transformed(wm, mode);
1071 return pix;
1072}
1073
1074/*!
1075 \fn QPixmap QPixmap::scaledToWidth(int width, Qt::TransformationMode
1076 mode) const
1077
1078 Returns a scaled copy of the image. The returned image is scaled
1079 to the given \a width using the specified transformation \a mode.
1080 The height of the pixmap is automatically calculated so that the
1081 aspect ratio of the pixmap is preserved.
1082
1083 If \a width is 0 or negative, a null pixmap is returned.
1084
1085 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1086 Transformations}
1087*/
1088QPixmap QPixmap::scaledToWidth(int w, Qt::TransformationMode mode) const
1089{
1090 if (isNull()) {
1091 qWarning("QPixmap::scaleWidth: Pixmap is a null pixmap");
1092 return copy();
1093 }
1094 if (w <= 0)
1095 return QPixmap();
1096
1097 Q_TRACE_SCOPE(QPixmap_scaledToWidth, w, mode);
1098
1099 qreal factor = (qreal) w / width();
1100 QTransform wm = QTransform::fromScale(factor, factor);
1101 return transformed(wm, mode);
1102}
1103
1104/*!
1105 \fn QPixmap QPixmap::scaledToHeight(int height,
1106 Qt::TransformationMode mode) const
1107
1108 Returns a scaled copy of the image. The returned image is scaled
1109 to the given \a height using the specified transformation \a mode.
1110 The width of the pixmap is automatically calculated so that the
1111 aspect ratio of the pixmap is preserved.
1112
1113 If \a height is 0 or negative, a null pixmap is returned.
1114
1115 \sa isNull(), {QPixmap#Pixmap Transformations}{Pixmap
1116 Transformations}
1117*/
1118QPixmap QPixmap::scaledToHeight(int h, Qt::TransformationMode mode) const
1119{
1120 if (isNull()) {
1121 qWarning("QPixmap::scaleHeight: Pixmap is a null pixmap");
1122 return copy();
1123 }
1124 if (h <= 0)
1125 return QPixmap();
1126
1127 Q_TRACE_SCOPE(QPixmap_scaledToHeight, h, mode);
1128
1129 qreal factor = (qreal) h / height();
1130 QTransform wm = QTransform::fromScale(factor, factor);
1131 return transformed(wm, mode);
1132}
1133
1134/*!
1135 Returns a copy of the pixmap that is transformed using the given
1136 transformation \a transform and transformation \a mode. The original
1137 pixmap is not changed.
1138
1139 The transformation \a transform is internally adjusted to compensate
1140 for unwanted translation; i.e. the pixmap produced is the smallest
1141 pixmap that contains all the transformed points of the original
1142 pixmap. Use the trueMatrix() function to retrieve the actual
1143 matrix used for transforming the pixmap.
1144
1145 This function is slow because it involves transformation to a
1146 QImage, non-trivial computations and a transformation back to a
1147 QPixmap.
1148
1149 \sa trueMatrix(), {QPixmap#Pixmap Transformations}{Pixmap
1150 Transformations}
1151*/
1152QPixmap QPixmap::transformed(const QTransform &transform,
1153 Qt::TransformationMode mode) const
1154{
1155 if (isNull() || transform.type() <= QTransform::TxTranslate)
1156 return *this;
1157
1158 return data->transformed(transform, mode);
1159}
1160
1161/*!
1162 \class QPixmap
1163 \inmodule QtGui
1164
1165 \brief The QPixmap class is an off-screen image representation
1166 that can be used as a paint device.
1167
1168 \ingroup painting
1169 \ingroup shared
1170
1171
1172 Qt provides four classes for handling image data: QImage, QPixmap,
1173 QBitmap and QPicture. QImage is designed and optimized for I/O,
1174 and for direct pixel access and manipulation, while QPixmap is
1175 designed and optimized for showing images on screen. QBitmap is
1176 only a convenience class that inherits QPixmap, ensuring a depth
1177 of 1. The isQBitmap() function returns \c true if a QPixmap object is
1178 really a bitmap, otherwise returns \c false. Finally, the QPicture class
1179 is a paint device that records and replays QPainter commands.
1180
1181 A QPixmap can easily be displayed on the screen using QLabel or
1182 one of QAbstractButton's subclasses (such as QPushButton and
1183 QToolButton). QLabel has a pixmap property, whereas
1184 QAbstractButton has an icon property.
1185
1186 QPixmap objects can be passed around by value since the QPixmap
1187 class uses implicit data sharing. For more information, see the \l
1188 {Implicit Data Sharing} documentation. QPixmap objects can also be
1189 streamed.
1190
1191 Note that the pixel data in a pixmap is internal and is managed by
1192 the underlying window system. Because QPixmap is a QPaintDevice
1193 subclass, QPainter can be used to draw directly onto pixmaps.
1194 Pixels can only be accessed through QPainter functions or by
1195 converting the QPixmap to a QImage. However, the fill() function
1196 is available for initializing the entire pixmap with a given color.
1197
1198 There are functions to convert between QImage and
1199 QPixmap. Typically, the QImage class is used to load an image
1200 file, optionally manipulating the image data, before the QImage
1201 object is converted into a QPixmap to be shown on
1202 screen. Alternatively, if no manipulation is desired, the image
1203 file can be loaded directly into a QPixmap.
1204
1205 QPixmap provides a collection of functions that can be used to
1206 obtain a variety of information about the pixmap. In addition,
1207 there are several functions that enables transformation of the
1208 pixmap.
1209
1210 \tableofcontents
1211
1212 \section1 Reading and Writing Image Files
1213
1214 QPixmap provides several ways of reading an image file: The file
1215 can be loaded when constructing the QPixmap object, or by using
1216 the load() or loadFromData() functions later on. When loading an
1217 image, the file name can either refer to an actual file on disk or
1218 to one of the application's embedded resources. See \l{The Qt
1219 Resource System} overview for details on how to embed images and
1220 other resource files in the application's executable.
1221
1222 Simply call the save() function to save a QPixmap object.
1223
1224 The complete list of supported file formats are available through
1225 the QImageReader::supportedImageFormats() and
1226 QImageWriter::supportedImageFormats() functions. New file formats
1227 can be added as plugins. By default, Qt supports the following
1228 formats:
1229
1230 \table
1231 \header \li Format \li Description \li Qt's support
1232 \row \li BMP \li Windows Bitmap \li Read/write
1233 \row \li GIF \li Graphic Interchange Format (optional) \li Read
1234 \row \li JPG \li Joint Photographic Experts Group \li Read/write
1235 \row \li JPEG \li Joint Photographic Experts Group \li Read/write
1236 \row \li PNG \li Portable Network Graphics \li Read/write
1237 \row \li PBM \li Portable Bitmap \li Read
1238 \row \li PGM \li Portable Graymap \li Read
1239 \row \li PPM \li Portable Pixmap \li Read/write
1240 \row \li XBM \li X11 Bitmap \li Read/write
1241 \row \li XPM \li X11 Pixmap \li Read/write
1242 \endtable
1243
1244 \section1 Pixmap Information
1245
1246 QPixmap provides a collection of functions that can be used to
1247 obtain a variety of information about the pixmap:
1248
1249 \table
1250 \header
1251 \li \li Available Functions
1252 \row
1253 \li Geometry
1254 \li
1255 The size(), width() and height() functions provide information
1256 about the pixmap's size. The rect() function returns the image's
1257 enclosing rectangle.
1258
1259 \row
1260 \li Alpha component
1261 \li
1262
1263 The hasAlphaChannel() returns \c true if the pixmap has a format that
1264 respects the alpha channel, otherwise returns \c false. The hasAlpha(),
1265 setMask() and mask() functions are legacy and should not be used.
1266 They are potentially very slow.
1267
1268 The createHeuristicMask() function creates and returns a 1-bpp
1269 heuristic mask (i.e. a QBitmap) for this pixmap. It works by
1270 selecting a color from one of the corners and then chipping away
1271 pixels of that color, starting at all the edges. The
1272 createMaskFromColor() function creates and returns a mask (i.e. a
1273 QBitmap) for the pixmap based on a given color.
1274
1275 \row
1276 \li Low-level information
1277 \li
1278
1279 The depth() function returns the depth of the pixmap. The
1280 defaultDepth() function returns the default depth, i.e. the depth
1281 used by the application on the given screen.
1282
1283 The cacheKey() function returns a number that uniquely
1284 identifies the contents of the QPixmap object.
1285
1286 \endtable
1287
1288 \section1 Pixmap Conversion
1289
1290 A QPixmap object can be converted into a QImage using the
1291 toImage() function. Likewise, a QImage can be converted into a
1292 QPixmap using the fromImage(). If this is too expensive an
1293 operation, you can use QBitmap::fromImage() instead.
1294
1295 To convert a QPixmap to and from HICON you can use the QtWinExtras
1296 functions QtWin::toHICON() and QtWin::fromHICON() respectively.
1297
1298 \section1 Pixmap Transformations
1299
1300 QPixmap supports a number of functions for creating a new pixmap
1301 that is a transformed version of the original:
1302
1303 The scaled(), scaledToWidth() and scaledToHeight() functions
1304 return scaled copies of the pixmap, while the copy() function
1305 creates a QPixmap that is a plain copy of the original one.
1306
1307 The transformed() function returns a copy of the pixmap that is
1308 transformed with the given transformation matrix and
1309 transformation mode: Internally, the transformation matrix is
1310 adjusted to compensate for unwanted translation,
1311 i.e. transformed() returns the smallest pixmap containing all
1312 transformed points of the original pixmap. The static trueMatrix()
1313 function returns the actual matrix used for transforming the
1314 pixmap.
1315
1316 \sa QBitmap, QImage, QImageReader, QImageWriter
1317*/
1318
1319
1320/*!
1321 \typedef QPixmap::DataPtr
1322 \internal
1323*/
1324
1325/*!
1326 \fn DataPtr &QPixmap::data_ptr()
1327 \internal
1328*/
1329
1330/*!
1331 Returns \c true if this pixmap has an alpha channel, \e or has a
1332 mask, otherwise returns \c false.
1333
1334 \sa hasAlphaChannel(), mask()
1335*/
1336bool QPixmap::hasAlpha() const
1337{
1338 return data && data->hasAlphaChannel();
1339}
1340
1341/*!
1342 Returns \c true if the pixmap has a format that respects the alpha
1343 channel, otherwise returns \c false.
1344
1345 \sa hasAlpha()
1346*/
1347bool QPixmap::hasAlphaChannel() const
1348{
1349 return data && data->hasAlphaChannel();
1350}
1351
1352/*!
1353 \internal
1354*/
1355int QPixmap::metric(PaintDeviceMetric metric) const
1356{
1357 return data ? data->metric(metric) : 0;
1358}
1359
1360/*!
1361 \internal
1362*/
1363QPaintEngine *QPixmap::paintEngine() const
1364{
1365 return data ? data->paintEngine() : nullptr;
1366}
1367
1368/*!
1369 \fn QBitmap QPixmap::mask() const
1370
1371 Extracts a bitmap mask from the pixmap's alpha channel.
1372
1373 \warning This is potentially an expensive operation. The mask of
1374 the pixmap is extracted dynamically from the pixeldata.
1375
1376 \sa setMask(), {QPixmap#Pixmap Information}{Pixmap Information}
1377*/
1378QBitmap QPixmap::mask() const
1379{
1380 return data ? data->mask() : QBitmap();
1381}
1382
1383/*!
1384 Returns the default pixmap depth used by the application.
1385
1386 On all platforms the depth of the primary screen will be returned.
1387
1388 \note QGuiApplication must be created before calling this function.
1389
1390 \sa depth(), QColormap::depth(), {QPixmap#Pixmap Information}{Pixmap Information}
1391
1392*/
1393int QPixmap::defaultDepth()
1394{
1395 QScreen *primary = QGuiApplication::primaryScreen();
1396 if (Q_LIKELY(primary))
1397 return primary->depth();
1398 qWarning("QPixmap: QGuiApplication must be created before calling defaultDepth().");
1399 return 0;
1400}
1401
1402/*!
1403 Detaches the pixmap from shared pixmap data.
1404
1405 A pixmap is automatically detached by Qt whenever its contents are
1406 about to change. This is done in almost all QPixmap member
1407 functions that modify the pixmap (fill(), fromImage(),
1408 load(), etc.), and in QPainter::begin() on a pixmap.
1409
1410 There are two exceptions in which detach() must be called
1411 explicitly, that is when calling the handle() or the
1412 x11PictureHandle() function (only available on X11). Otherwise,
1413 any modifications done using system calls, will be performed on
1414 the shared data.
1415
1416 The detach() function returns immediately if there is just a
1417 single reference or if the pixmap has not been initialized yet.
1418*/
1419void QPixmap::detach()
1420{
1421 if (!data)
1422 return;
1423
1424 // QPixmap.data member may be QRuntimePlatformPixmap so use handle() function to get
1425 // the actual underlaying runtime pixmap data.
1426 QPlatformPixmap *pd = handle();
1427 QPlatformPixmap::ClassId id = pd->classId();
1428 if (id == QPlatformPixmap::RasterClass) {
1429 QRasterPlatformPixmap *rasterData = static_cast<QRasterPlatformPixmap*>(pd);
1430 rasterData->image.detach();
1431 }
1432
1433 if (data->is_cached && data->ref.loadRelaxed() == 1)
1434 QImagePixmapCleanupHooks::executePlatformPixmapModificationHooks(data.data());
1435
1436 if (data->ref.loadRelaxed() != 1) {
1437 *this = copy();
1438 }
1439 ++data->detach_no;
1440}
1441
1442/*!
1443 \fn QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1444
1445 Converts the given \a image to a pixmap using the specified \a
1446 flags to control the conversion. The \a flags argument is a
1447 bitwise-OR of the \l{Qt::ImageConversionFlags}. Passing 0 for \a
1448 flags sets all the default options.
1449
1450 In case of monochrome and 8-bit images, the image is first
1451 converted to a 32-bit pixmap and then filled with the colors in
1452 the color table. If this is too expensive an operation, you can
1453 use QBitmap::fromImage() instead.
1454
1455 \sa fromImageReader(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1456*/
1457QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags)
1458{
1459 if (image.isNull())
1460 return QPixmap();
1461
1462 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1463 qWarning("QPixmap::fromImage: QPixmap cannot be created without a QGuiApplication");
1464 return QPixmap();
1465 }
1466
1467 QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1468 data->fromImage(image, flags);
1469 return QPixmap(data.take());
1470}
1471
1472/*!
1473 \fn QPixmap QPixmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags)
1474 \since 5.3
1475 \overload
1476
1477 Converts the given \a image to a pixmap without copying if possible.
1478*/
1479
1480
1481/*!
1482 \internal
1483*/
1484QPixmap QPixmap::fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags)
1485{
1486 if (image.isNull())
1487 return QPixmap();
1488
1489 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1490 qWarning("QPixmap::fromImageInPlace: QPixmap cannot be created without a QGuiApplication");
1491 return QPixmap();
1492 }
1493
1494 QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1495 data->fromImageInPlace(image, flags);
1496 return QPixmap(data.take());
1497}
1498
1499/*!
1500 \fn QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1501
1502 Create a QPixmap from an image read directly from an \a imageReader.
1503 The \a flags argument is a bitwise-OR of the \l{Qt::ImageConversionFlags}.
1504 Passing 0 for \a flags sets all the default options.
1505
1506 On some systems, reading an image directly to QPixmap can use less memory than
1507 reading a QImage to convert it to QPixmap.
1508
1509 \sa fromImage(), toImage(), {QPixmap#Pixmap Conversion}{Pixmap Conversion}
1510*/
1511QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1512{
1513 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1514 qWarning("QPixmap::fromImageReader: QPixmap cannot be created without a QGuiApplication");
1515 return QPixmap();
1516 }
1517
1518 QScopedPointer<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1519 data->fromImageReader(imageReader, flags);
1520 return QPixmap(data.take());
1521}
1522
1523/*!
1524 \internal
1525*/
1526QPlatformPixmap* QPixmap::handle() const
1527{
1528 return data.data();
1529}
1530
1531#ifndef QT_NO_DEBUG_STREAM
1532QDebug operator<<(QDebug dbg, const QPixmap &r)
1533{
1534 QDebugStateSaver saver(dbg);
1535 dbg.resetFormat();
1536 dbg.nospace();
1537 dbg << "QPixmap(";
1538 if (r.isNull()) {
1539 dbg << "null";
1540 } else {
1541 dbg << r.size() << ",depth=" << r.depth()
1542 << ",devicePixelRatio=" << r.devicePixelRatio()
1543 << ",cacheKey=" << Qt::showbase << Qt::hex << r.cacheKey() << Qt::dec << Qt::noshowbase;
1544 }
1545 dbg << ')';
1546 return dbg;
1547}
1548#endif
1549
1550QT_END_NAMESPACE
1551