8#include <qpa/qplatformpixmap.h>
16#include <private/qguiapplication_p.h>
29#include <qpa/qplatformintegration.h>
32#include "private/qhexstring_p.h"
34#include <qtgui_tracepoints_p.h>
40using namespace Qt::StringLiterals;
42Q_TRACE_PARAM_REPLACE(Qt::AspectRatioMode,
int);
43Q_TRACE_PARAM_REPLACE(Qt::TransformationMode,
int);
48QT_WARNING_DISABLE_MSVC(4723)
52 if (!QCoreApplication::instanceExists()) {
53 qFatal(
"QPixmap: Must construct a QGuiApplication before a QPixmap");
56 if (QGuiApplicationPrivate::instance()
57 && !QThread::isMainThread()
58 && Q_LIKELY(QGuiApplicationPrivate::platformIntegration())
59 && !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
60 qWarning(
"QPixmap: It is not safe to use pixmaps outside the GUI thread on this platform");
66void QPixmap::doInit(
int w,
int h,
int type)
68 if ((w > 0 && h > 0) || type == QPlatformPixmap::BitmapType)
69 data = QPlatformPixmap::create(w, h, (QPlatformPixmap::PixelType) type);
75
76
77
78
83 (
void) qt_pixmap_thread_test();
84 doInit(0, 0, QPlatformPixmap::PixmapType);
88
89
90
91
92
93
94
95
96
97
98
99
101QPixmap::QPixmap(
int w,
int h)
102 : QPixmap(QSize(w, h))
107
108
109
110
111
112
113
114
116QPixmap::QPixmap(
const QSize &size)
117 : QPixmap(size, QPlatformPixmap::PixmapType)
122
123
124QPixmap::QPixmap(
const QSize &s,
int type)
126 if (!qt_pixmap_thread_test())
127 doInit(0, 0,
static_cast<QPlatformPixmap::PixelType>(type));
129 doInit(s.width(), s.height(),
static_cast<QPlatformPixmap::PixelType>(type));
133
134
135QPixmap::QPixmap(QPlatformPixmap *d)
136 : QPaintDevice(), data(d)
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
171QPixmap::QPixmap(
const QString& fileName,
const char *format, Qt::ImageConversionFlags flags)
174 doInit(0, 0, QPlatformPixmap::PixmapType);
175 if (!qt_pixmap_thread_test())
178 load(fileName, format, flags);
182
183
184
185
187QPixmap::QPixmap(
const QPixmap &pixmap)
190 if (!qt_pixmap_thread_test()) {
191 doInit(0, 0, QPlatformPixmap::PixmapType);
194 if (pixmap.paintingActive()) {
195 pixmap.copy().swap(*
this);
202
203
204
205
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224#ifndef QT_NO_IMAGEFORMAT_XPM
225QPixmap::QPixmap(
const char *
const xpm[])
228 doInit(0, 0, QPlatformPixmap::PixmapType);
233 if (!image.isNull()) {
234 if (data && data->pixelType() == QPlatformPixmap::BitmapType)
235 *
this = QBitmap::fromImage(std::move(image));
237 *
this = fromImage(std::move(image));
244
245
249 Q_ASSERT(!data || data->ref.loadRelaxed() >= 1);
253
254
255int QPixmap::devType()
const
257 return QInternal::Pixmap;
261
262
263
264
265
266
269
270
271
272
273
274
275
276
277
278
279
280QPixmap QPixmap::copy(
const QRect &rect)
const
285 QRect r(0, 0, width(), height());
287 r = r.intersected(rect);
289 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
290 d->copy(data.data(), r);
295
296
297
298
299
300
301
304
305
306
307
308
309
310
311
312
313
314void QPixmap::scroll(
int dx,
int dy,
const QRect &rect, QRegion *exposed)
316 if (isNull() || (dx == 0 && dy == 0))
318 QRect dest = rect &
this->rect();
319 QRect src = dest.translated(-dx, -dy) & dest;
328 if (!data->scroll(dx, dy, src)) {
331 QPainter painter(&pix);
332 painter.setCompositionMode(QPainter::CompositionMode_Source);
333 painter.drawPixmap(src.translated(dx, dy), *
this, src);
340 *exposed -= src.translated(dx, dy);
345
346
347
348
349
351QPixmap &QPixmap::operator=(
const QPixmap &pixmap)
353 if (paintingActive()) {
354 qWarning(
"QPixmap::operator=: Cannot assign to pixmap during painting");
357 if (pixmap.paintingActive()) {
358 pixmap.copy().swap(*
this);
366
367
368
369
370
371
374
375
376
379
380
381QPixmap::operator QVariant()
const
383 return QVariant::fromValue(*
this);
387
388
389
390
391
392
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410QImage QPixmap::toImage()
const
415 return data->toImage();
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434QTransform QPixmap::trueMatrix(
const QTransform &m,
int w,
int h)
436 return QImage::trueMatrix(m, w, h);
440
441
442
443
445bool QPixmap::isQBitmap()
const
447 return data && data->type == QPlatformPixmap::BitmapType;
451
452
453
454
455
456
457
458bool QPixmap::isNull()
const
460 return !data || data->isNull();
464
465
466
467
468
469
470int QPixmap::width()
const
472 return data ? data->width() : 0;
476
477
478
479
480
481
482int QPixmap::height()
const
484 return data ? data->height() : 0;
488
489
490
491
492
493
494
495QSize QPixmap::size()
const
497 return data ? QSize(data->width(), data->height()) : QSize(0, 0);
501
502
503
504
505
506
507QRect QPixmap::rect()
const
509 return data ? QRect(0, 0, data->width(), data->height()) : QRect();
513
514
515
516
517
518
519
520
521
522
523int QPixmap::depth()
const
525 return data ? data->depth() : 0;
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545void QPixmap::setMask(
const QBitmap &mask)
547 if (paintingActive()) {
548 qWarning(
"QPixmap::setMask: Cannot set mask while pixmap is being painted on");
552 if (!mask.isNull() && mask.size() != size()) {
553 qWarning(
"QPixmap::setMask() mask size differs from pixmap size");
560 if (
static_cast<
const QPixmap &>(mask).data == data)
568
569
570
571
572
573
574
575
576
577
578qreal QPixmap::devicePixelRatio()
const
582 return data->devicePixelRatio();
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606void QPixmap::setDevicePixelRatio(qreal scaleFactor)
611 if (scaleFactor == data->devicePixelRatio())
615 data->setDevicePixelRatio(scaleFactor);
619
620
621
622
623
624
625
626
627
628QSizeF QPixmap::deviceIndependentSize()
const
632 return QSizeF(data->width(), data->height()) / data->devicePixelRatio();
635#ifndef QT_NO_IMAGE_HEURISTIC_MASK
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655QBitmap QPixmap::createHeuristicMask(
bool clipTight)
const
657 QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
663
664
665
666
667
668
669
670
671
672
673QBitmap QPixmap::createMaskFromColor(
const QColor &maskColor, Qt::MaskMode mode)
const
675 QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
676 return QBitmap::fromImage(std::move(image).createMaskFromColor(maskColor.rgba(), mode));
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
708bool QPixmap::load(
const QString &fileName,
const char *format, Qt::ImageConversionFlags flags)
710 if (!fileName.isEmpty()) {
712 QFileInfo info(fileName);
715 if (info.completeSuffix().isEmpty() || info.exists()) {
716 const bool inGuiThread = QThread::isMainThread();
718 QString key =
"qt_pixmap"_L1
719 % info.absoluteFilePath()
720 % HexString<uint>(info.lastModified(QTimeZone::UTC).toSecsSinceEpoch())
721 % HexString<quint64>(info.size())
722 % HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);
724 if (inGuiThread && QPixmapCache::find(key,
this))
727 data = QPlatformPixmap::create(0, 0, data ? data->pixelType() : QPlatformPixmap::PixmapType);
729 if (data->fromFile(fileName, format, flags)) {
731 QPixmapCache::insert(key, *
this);
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
767bool QPixmap::loadFromData(
const uchar *buf, uint len,
const char *format, Qt::ImageConversionFlags flags)
769 if (len == 0 || buf ==
nullptr) {
774 data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
776 if (data->fromData(buf, len, format, flags))
784
785
786
787
788
789
790
794
795
796
797
798
799
800
801
802
803
804
805
806
807
809bool QPixmap::save(
const QString &fileName,
const char *format,
int quality)
const
813 QImageWriter writer(fileName, format);
814 return doImageIO(&writer, quality);
818
819
820
821
822
823
824
825
827bool QPixmap::save(QIODevice* device,
const char* format,
int quality)
const
831 QImageWriter writer(device, format);
832 return doImageIO(&writer, quality);
836
837bool QPixmap::doImageIO(QImageWriter *writer,
int quality)
const
839 if (quality > 100 || quality < -1)
840 qWarning(
"QPixmap::save: quality out of range [-1,100]");
842 writer->setQuality(qMin(quality,100));
843 return writer->write(toImage());
848
849
850
851
852
853
854
856void QPixmap::fill(
const QColor &color)
863 if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
864 qWarning(
"QPixmap::fill: Cannot fill while pixmap is being painted on");
868 if (data->ref.loadRelaxed() == 1) {
875 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
876 d->resize(data->width(), data->height());
877 d->setDevicePixelRatio(data->devicePixelRatio());
884
885
886
887
888
889
890qint64 QPixmap::cacheKey()
const
896 return data->cacheKey();
900static void sendResizeEvents(QWidget *target)
902 QResizeEvent e(target->size(), QSize());
903 QApplication::sendEvent(target, &e);
905 const QObjectList children = target->children();
906 for (
int i = 0; i < children.size(); ++i) {
907 QWidget *child =
static_cast<QWidget*>(children.at(i));
908 if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
909 sendResizeEvents(child);
916
917
918#if !defined(QT_NO_DATASTREAM)
920
921
922
923
924
925
926
927
929QDataStream &operator<<(QDataStream &stream,
const QPixmap &pixmap)
931 return stream << pixmap.toImage();
935
936
937
938
939
940
942QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
947 if (image.isNull()) {
949 }
else if (image.depth() == 1) {
950 pixmap = QBitmap::fromImage(std::move(image));
952 pixmap = QPixmap::fromImage(std::move(image));
960
961
963bool QPixmap::isDetached()
const
965 return data && data->ref.loadRelaxed() == 1;
969
970
971
972
973
974
975
976
977bool QPixmap::convertFromImage(
const QImage &image, Qt::ImageConversionFlags flags)
980 if (image.isNull() || !data)
981 *
this = QPixmap::fromImage(image, flags);
983 data->fromImage(image, flags);
988
989
990
991
992
993
994
995
996
997
998
999
1000
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaled(
const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode)
const
1042 qWarning(
"QPixmap::scaled: Pixmap is a null pixmap");
1048 QSize newSize = size();
1049 if (aspectMode == Qt::KeepAspectRatioByExpanding) {
1050 const QSizeF fs = QSizeF(newSize).scaled(s, aspectMode);
1051 if (std::max(fs.width(), fs.height()) > INT_MAX)
1054 newSize.scale(s, aspectMode);
1055 newSize.rwidth() = qMax(newSize.width(), 1);
1056 newSize.rheight() = qMax(newSize.height(), 1);
1057 if (newSize == size())
1060 Q_TRACE_SCOPE(QPixmap_scaled, s, aspectMode, mode);
1062 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1063 (qreal)newSize.height() / height());
1064 QPixmap pix = transformed(wm, mode);
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToWidth(
int w, Qt::TransformationMode mode)
const
1088 qWarning(
"QPixmap::scaleWidth: Pixmap is a null pixmap");
1094 Q_TRACE_SCOPE(QPixmap_scaledToWidth, w, mode);
1096 qreal factor = (qreal) w / width();
1097 QTransform wm = QTransform::fromScale(factor, factor);
1098 return transformed(wm, mode);
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToHeight(
int h, Qt::TransformationMode mode)
const
1121 qWarning(
"QPixmap::scaleHeight: Pixmap is a null pixmap");
1127 Q_TRACE_SCOPE(QPixmap_scaledToHeight, h, mode);
1129 qreal factor = (qreal) h / height();
1130 QTransform wm = QTransform::fromScale(factor, factor);
1131 return transformed(wm, mode);
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152QPixmap QPixmap::transformed(
const QTransform &transform,
1153 Qt::TransformationMode mode)
const
1155 if (isNull() || transform.type() <= QTransform::TxTranslate)
1158 return data->transformed(transform, mode);
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1327
1328
1329
1332
1333
1334
1337
1338
1339
1340
1341
1342bool QPixmap::hasAlpha()
const
1344 return data && data->hasAlphaChannel();
1348
1349
1350
1351
1352
1353bool QPixmap::hasAlphaChannel()
const
1355 return data && data->hasAlphaChannel();
1359
1360
1361int QPixmap::metric(PaintDeviceMetric metric)
const
1363 return data ? data->metric(metric) : 0;
1367
1368
1369QPaintEngine *QPixmap::paintEngine()
const
1371 return data ? data->paintEngine() :
nullptr;
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384QBitmap QPixmap::mask()
const
1386 return data ? data->mask() : QBitmap();
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399int QPixmap::defaultDepth()
1401 QScreen *primary = QGuiApplication::primaryScreen();
1402 if (Q_LIKELY(primary))
1403 return primary->depth();
1404 qWarning(
"QPixmap: QGuiApplication must be created before calling defaultDepth().");
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425void QPixmap::detach()
1432 QPlatformPixmap *pd = handle();
1433 QPlatformPixmap::ClassId id = pd->classId();
1434 if (id == QPlatformPixmap::RasterClass) {
1435 QRasterPlatformPixmap *rasterData =
static_cast<QRasterPlatformPixmap*>(pd);
1436 rasterData->image.detach();
1439 if (data->is_cached && data->ref.loadRelaxed() == 1)
1440 QImagePixmapCleanupHooks::executePlatformPixmapModificationHooks(data.data());
1442 if (data->ref.loadRelaxed() != 1) {
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463QPixmap QPixmap::fromImage(
const QImage &image, Qt::ImageConversionFlags flags)
1468 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1469 qWarning(
"QPixmap::fromImage: QPixmap cannot be created without a QGuiApplication");
1473 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1474 data->fromImage(image, flags);
1475 return QPixmap(data.release());
1479
1480
1481
1482
1483
1484
1488
1489
1490QPixmap QPixmap::fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags)
1495 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1496 qWarning(
"QPixmap::fromImageInPlace: QPixmap cannot be created without a QGuiApplication");
1500 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1501 data->fromImageInPlace(image, flags);
1502 return QPixmap(data.release());
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1519 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1520 qWarning(
"QPixmap::fromImageReader: QPixmap cannot be created without a QGuiApplication");
1524 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1525 data->fromImageReader(imageReader, flags);
1526 return QPixmap(data.release());
1530
1531
1532QPlatformPixmap* QPixmap::handle()
const
1537#ifndef QT_NO_DEBUG_STREAM
1538QDebug operator<<(QDebug dbg,
const QPixmap &r)
1540 QDebugStateSaver saver(dbg);
1547 dbg << r.size() <<
",depth=" << r.depth()
1548 <<
",devicePixelRatio=" << r.devicePixelRatio()
1549 <<
",cacheKey=" << Qt::showbase << Qt::hex << r.cacheKey() << Qt::dec << Qt::noshowbase;
Combined button and popup list for selecting options.
static bool qt_pixmap_thread_test()