7#include <qpa/qplatformpixmap.h>
15#include <private/qguiapplication_p.h>
28#include <qpa/qplatformintegration.h>
31#include "private/qhexstring_p.h"
33#include <qtgui_tracepoints_p.h>
39using namespace Qt::StringLiterals;
41Q_TRACE_PARAM_REPLACE(Qt::AspectRatioMode,
int);
42Q_TRACE_PARAM_REPLACE(Qt::TransformationMode,
int);
47QT_WARNING_DISABLE_MSVC(4723)
51 if (!QCoreApplication::instanceExists()) {
52 qFatal(
"QPixmap: Must construct a QGuiApplication before a QPixmap");
55 if (QGuiApplicationPrivate::instance()
56 && !QThread::isMainThread()
57 && !QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedPixmaps)) {
58 qWarning(
"QPixmap: It is not safe to use pixmaps outside the GUI thread on this platform");
64void QPixmap::doInit(
int w,
int h,
int type)
66 if ((w > 0 && h > 0) || type == QPlatformPixmap::BitmapType)
67 data = QPlatformPixmap::create(w, h, (QPlatformPixmap::PixelType) type);
73
74
75
76
81 (
void) qt_pixmap_thread_test();
82 doInit(0, 0, QPlatformPixmap::PixmapType);
86
87
88
89
90
91
92
93
94
95
96
97
99QPixmap::QPixmap(
int w,
int h)
100 : QPixmap(QSize(w, h))
105
106
107
108
109
110
111
112
114QPixmap::QPixmap(
const QSize &size)
115 : QPixmap(size, QPlatformPixmap::PixmapType)
120
121
122QPixmap::QPixmap(
const QSize &s,
int type)
124 if (!qt_pixmap_thread_test())
125 doInit(0, 0,
static_cast<QPlatformPixmap::PixelType>(type));
127 doInit(s.width(), s.height(),
static_cast<QPlatformPixmap::PixelType>(type));
131
132
133QPixmap::QPixmap(QPlatformPixmap *d)
134 : QPaintDevice(), data(d)
139
140
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
167QPixmap::QPixmap(
const QString& fileName,
const char *format, Qt::ImageConversionFlags flags)
170 doInit(0, 0, QPlatformPixmap::PixmapType);
171 if (!qt_pixmap_thread_test())
174 load(fileName, format, flags);
178
179
180
181
183QPixmap::QPixmap(
const QPixmap &pixmap)
186 if (!qt_pixmap_thread_test()) {
187 doInit(0, 0, QPlatformPixmap::PixmapType);
190 if (pixmap.paintingActive()) {
191 pixmap.copy().swap(*
this);
198
199
200
201
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220#ifndef QT_NO_IMAGEFORMAT_XPM
221QPixmap::QPixmap(
const char *
const xpm[])
224 doInit(0, 0, QPlatformPixmap::PixmapType);
229 if (!image.isNull()) {
230 if (data && data->pixelType() == QPlatformPixmap::BitmapType)
231 *
this = QBitmap::fromImage(std::move(image));
233 *
this = fromImage(std::move(image));
240
241
245 Q_ASSERT(!data || data->ref.loadRelaxed() >= 1);
249
250
251int QPixmap::devType()
const
253 return QInternal::Pixmap;
257
258
259
260
261
262
265
266
267
268
269
270
271
272
273
274
275
276QPixmap QPixmap::copy(
const QRect &rect)
const
281 QRect r(0, 0, width(), height());
283 r = r.intersected(rect);
285 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
286 d->copy(data.data(), r);
291
292
293
294
295
296
297
300
301
302
303
304
305
306
307
308
309
310void QPixmap::scroll(
int dx,
int dy,
const QRect &rect, QRegion *exposed)
312 if (isNull() || (dx == 0 && dy == 0))
314 QRect dest = rect &
this->rect();
315 QRect src = dest.translated(-dx, -dy) & dest;
324 if (!data->scroll(dx, dy, src)) {
327 QPainter painter(&pix);
328 painter.setCompositionMode(QPainter::CompositionMode_Source);
329 painter.drawPixmap(src.translated(dx, dy), *
this, src);
336 *exposed -= src.translated(dx, dy);
341
342
343
344
345
347QPixmap &QPixmap::operator=(
const QPixmap &pixmap)
349 if (paintingActive()) {
350 qWarning(
"QPixmap::operator=: Cannot assign to pixmap during painting");
353 if (pixmap.paintingActive()) {
354 pixmap.copy().swap(*
this);
362
363
364
365
366
367
370
371
372
375
376
377QPixmap::operator QVariant()
const
379 return QVariant::fromValue(*
this);
383
384
385
386
387
388
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406QImage QPixmap::toImage()
const
411 return data->toImage();
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430QTransform QPixmap::trueMatrix(
const QTransform &m,
int w,
int h)
432 return QImage::trueMatrix(m, w, h);
436
437
438
439
441bool QPixmap::isQBitmap()
const
443 return data && data->type == QPlatformPixmap::BitmapType;
447
448
449
450
451
452
453
454bool QPixmap::isNull()
const
456 return !data || data->isNull();
460
461
462
463
464
465
466int QPixmap::width()
const
468 return data ? data->width() : 0;
472
473
474
475
476
477
478int QPixmap::height()
const
480 return data ? data->height() : 0;
484
485
486
487
488
489
490
491QSize QPixmap::size()
const
493 return data ? QSize(data->width(), data->height()) : QSize(0, 0);
497
498
499
500
501
502
503QRect QPixmap::rect()
const
505 return data ? QRect(0, 0, data->width(), data->height()) : QRect();
509
510
511
512
513
514
515
516
517
518
519int QPixmap::depth()
const
521 return data ? data->depth() : 0;
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541void QPixmap::setMask(
const QBitmap &mask)
543 if (paintingActive()) {
544 qWarning(
"QPixmap::setMask: Cannot set mask while pixmap is being painted on");
548 if (!mask.isNull() && mask.size() != size()) {
549 qWarning(
"QPixmap::setMask() mask size differs from pixmap size");
556 if (
static_cast<
const QPixmap &>(mask).data == data)
564
565
566
567
568
569
570
571
572
573
574qreal QPixmap::devicePixelRatio()
const
578 return data->devicePixelRatio();
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602void QPixmap::setDevicePixelRatio(qreal scaleFactor)
607 if (scaleFactor == data->devicePixelRatio())
611 data->setDevicePixelRatio(scaleFactor);
615
616
617
618
619
620
621
622
623
624QSizeF QPixmap::deviceIndependentSize()
const
628 return QSizeF(data->width(), data->height()) / data->devicePixelRatio();
631#ifndef QT_NO_IMAGE_HEURISTIC_MASK
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651QBitmap QPixmap::createHeuristicMask(
bool clipTight)
const
653 QBitmap m = QBitmap::fromImage(toImage().createHeuristicMask(clipTight));
659
660
661
662
663
664
665
666
667
668
669QBitmap QPixmap::createMaskFromColor(
const QColor &maskColor, Qt::MaskMode mode)
const
671 QImage image = toImage().convertToFormat(QImage::Format_ARGB32);
672 return QBitmap::fromImage(std::move(image).createMaskFromColor(maskColor.rgba(), mode));
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
702bool QPixmap::load(
const QString &fileName,
const char *format, Qt::ImageConversionFlags flags)
704 if (!fileName.isEmpty()) {
706 QFileInfo info(fileName);
709 if (info.completeSuffix().isEmpty() || info.exists()) {
710 const bool inGuiThread = qApp->thread() == QThread::currentThread();
712 QString key =
"qt_pixmap"_L1
713 % info.absoluteFilePath()
714 % HexString<uint>(info.lastModified(QTimeZone::UTC).toSecsSinceEpoch())
715 % HexString<quint64>(info.size())
716 % HexString<uint>(data ? data->pixelType() : QPlatformPixmap::PixmapType);
718 if (inGuiThread && QPixmapCache::find(key,
this))
721 data = QPlatformPixmap::create(0, 0, data ? data->pixelType() : QPlatformPixmap::PixmapType);
723 if (data->fromFile(fileName, format, flags)) {
725 QPixmapCache::insert(key, *
this);
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
759bool QPixmap::loadFromData(
const uchar *buf, uint len,
const char *format, Qt::ImageConversionFlags flags)
761 if (len == 0 || buf ==
nullptr) {
766 data = QPlatformPixmap::create(0, 0, QPlatformPixmap::PixmapType);
768 if (data->fromData(buf, len, format, flags))
776
777
778
779
780
781
782
786
787
788
789
790
791
792
793
794
795
796
797
798
799
801bool QPixmap::save(
const QString &fileName,
const char *format,
int quality)
const
805 QImageWriter writer(fileName, format);
806 return doImageIO(&writer, quality);
810
811
812
813
814
815
816
817
819bool QPixmap::save(QIODevice* device,
const char* format,
int quality)
const
823 QImageWriter writer(device, format);
824 return doImageIO(&writer, quality);
828
829bool QPixmap::doImageIO(QImageWriter *writer,
int quality)
const
831 if (quality > 100 || quality < -1)
832 qWarning(
"QPixmap::save: quality out of range [-1,100]");
834 writer->setQuality(qMin(quality,100));
835 return writer->write(toImage());
840
841
842
843
844
845
846
848void QPixmap::fill(
const QColor &color)
855 if (paintingActive() && (color.alpha() != 255) && !hasAlphaChannel()) {
856 qWarning(
"QPixmap::fill: Cannot fill while pixmap is being painted on");
860 if (data->ref.loadRelaxed() == 1) {
867 QPlatformPixmap *d = data->createCompatiblePlatformPixmap();
868 d->resize(data->width(), data->height());
869 d->setDevicePixelRatio(data->devicePixelRatio());
876
877
878
879
880
881
882qint64 QPixmap::cacheKey()
const
888 return data->cacheKey();
892static void sendResizeEvents(QWidget *target)
894 QResizeEvent e(target->size(), QSize());
895 QApplication::sendEvent(target, &e);
897 const QObjectList children = target->children();
898 for (
int i = 0; i < children.size(); ++i) {
899 QWidget *child =
static_cast<QWidget*>(children.at(i));
900 if (child->isWidgetType() && !child->isWindow() && child->testAttribute(Qt::WA_PendingResizeEvent))
901 sendResizeEvents(child);
908
909
910#if !defined(QT_NO_DATASTREAM)
912
913
914
915
916
917
918
919
921QDataStream &operator<<(QDataStream &stream,
const QPixmap &pixmap)
923 return stream << pixmap.toImage();
927
928
929
930
931
932
934QDataStream &operator>>(QDataStream &stream, QPixmap &pixmap)
939 if (image.isNull()) {
941 }
else if (image.depth() == 1) {
942 pixmap = QBitmap::fromImage(std::move(image));
944 pixmap = QPixmap::fromImage(std::move(image));
952
953
955bool QPixmap::isDetached()
const
957 return data && data->ref.loadRelaxed() == 1;
961
962
963
964
965
966
967
968
969bool QPixmap::convertFromImage(
const QImage &image, Qt::ImageConversionFlags flags)
972 if (image.isNull() || !data)
973 *
this = QPixmap::fromImage(image, flags);
975 data->fromImage(image, flags);
980
981
982
983
984
985
986
987
988
989
990
991
992
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaled(
const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode)
const
1030 qWarning(
"QPixmap::scaled: Pixmap is a null pixmap");
1036 QSize newSize = size();
1037 newSize.scale(s, aspectMode);
1038 newSize.rwidth() = qMax(newSize.width(), 1);
1039 newSize.rheight() = qMax(newSize.height(), 1);
1040 if (newSize == size())
1043 Q_TRACE_SCOPE(QPixmap_scaled, s, aspectMode, mode);
1045 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(),
1046 (qreal)newSize.height() / height());
1047 QPixmap pix = transformed(wm, mode);
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToWidth(
int w, Qt::TransformationMode mode)
const
1068 qWarning(
"QPixmap::scaleWidth: Pixmap is a null pixmap");
1074 Q_TRACE_SCOPE(QPixmap_scaledToWidth, w, mode);
1076 qreal factor = (qreal) w / width();
1077 QTransform wm = QTransform::fromScale(factor, factor);
1078 return transformed(wm, mode);
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095QPixmap Q_TRACE_INSTRUMENT(qtgui) QPixmap::scaledToHeight(
int h, Qt::TransformationMode mode)
const
1098 qWarning(
"QPixmap::scaleHeight: Pixmap is a null pixmap");
1104 Q_TRACE_SCOPE(QPixmap_scaledToHeight, h, mode);
1106 qreal factor = (qreal) h / height();
1107 QTransform wm = QTransform::fromScale(factor, factor);
1108 return transformed(wm, mode);
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129QPixmap QPixmap::transformed(
const QTransform &transform,
1130 Qt::TransformationMode mode)
const
1132 if (isNull() || transform.type() <= QTransform::TxTranslate)
1135 return data->transformed(transform, mode);
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
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
1304
1305
1306
1309
1310
1311
1314
1315
1316
1317
1318
1319bool QPixmap::hasAlpha()
const
1321 return data && data->hasAlphaChannel();
1325
1326
1327
1328
1329
1330bool QPixmap::hasAlphaChannel()
const
1332 return data && data->hasAlphaChannel();
1336
1337
1338int QPixmap::metric(PaintDeviceMetric metric)
const
1340 return data ? data->metric(metric) : 0;
1344
1345
1346QPaintEngine *QPixmap::paintEngine()
const
1348 return data ? data->paintEngine() :
nullptr;
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361QBitmap QPixmap::mask()
const
1363 return data ? data->mask() : QBitmap();
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376int QPixmap::defaultDepth()
1378 QScreen *primary = QGuiApplication::primaryScreen();
1379 if (Q_LIKELY(primary))
1380 return primary->depth();
1381 qWarning(
"QPixmap: QGuiApplication must be created before calling defaultDepth().");
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402void QPixmap::detach()
1409 QPlatformPixmap *pd = handle();
1410 QPlatformPixmap::ClassId id = pd->classId();
1411 if (id == QPlatformPixmap::RasterClass) {
1412 QRasterPlatformPixmap *rasterData =
static_cast<QRasterPlatformPixmap*>(pd);
1413 rasterData->image.detach();
1416 if (data->is_cached && data->ref.loadRelaxed() == 1)
1417 QImagePixmapCleanupHooks::executePlatformPixmapModificationHooks(data.data());
1419 if (data->ref.loadRelaxed() != 1) {
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440QPixmap QPixmap::fromImage(
const QImage &image, Qt::ImageConversionFlags flags)
1445 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1446 qWarning(
"QPixmap::fromImage: QPixmap cannot be created without a QGuiApplication");
1450 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1451 data->fromImage(image, flags);
1452 return QPixmap(data.release());
1456
1457
1458
1459
1460
1461
1465
1466
1467QPixmap QPixmap::fromImageInPlace(QImage &image, Qt::ImageConversionFlags flags)
1472 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1473 qWarning(
"QPixmap::fromImageInPlace: QPixmap cannot be created without a QGuiApplication");
1477 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1478 data->fromImageInPlace(image, flags);
1479 return QPixmap(data.release());
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494QPixmap QPixmap::fromImageReader(QImageReader *imageReader, Qt::ImageConversionFlags flags)
1496 if (Q_UNLIKELY(!qobject_cast<QGuiApplication *>(QCoreApplication::instance()))) {
1497 qWarning(
"QPixmap::fromImageReader: QPixmap cannot be created without a QGuiApplication");
1501 std::unique_ptr<QPlatformPixmap> data(QGuiApplicationPrivate::platformIntegration()->createPlatformPixmap(QPlatformPixmap::PixmapType));
1502 data->fromImageReader(imageReader, flags);
1503 return QPixmap(data.release());
1507
1508
1509QPlatformPixmap* QPixmap::handle()
const
1514#ifndef QT_NO_DEBUG_STREAM
1515QDebug operator<<(QDebug dbg,
const QPixmap &r)
1517 QDebugStateSaver saver(dbg);
1524 dbg << r.size() <<
",depth=" << r.depth()
1525 <<
",devicePixelRatio=" << r.devicePixelRatio()
1526 <<
",cacheKey=" << Qt::showbase << Qt::hex << r.cacheKey() << Qt::dec << Qt::noshowbase;
static bool qt_pixmap_thread_test()