19#include <qpa/qplatformintegration.h>
20#include <private/qguiapplication_p.h>
24#include <qpa/qplatformpixmap.h>
26#include <private/qcolorspace_p.h>
27#include <private/qcolortransform_p.h>
28#include <private/qmemrotate_p.h>
29#include <private/qimagescale_p.h>
30#include <private/qpixellayout_p.h>
31#include <private/qsimd_p.h>
35#include <private/qpaintengine_raster_p.h>
37#include <private/qimage_p.h>
38#include <private/qfont_p.h>
40#if QT_CONFIG(qtgui_threadpool)
41#include <private/qlatch_p.h>
42#include <qthreadpool.h>
43#include <private/qthreadpool_p.h>
46#include <qtgui_tracepoints_p.h>
50#define QT_XFORM_TYPE_MSBFIRST 0
51#define QT_XFORM_TYPE_LSBFIRST 1
56using namespace Qt::StringLiterals;
61QT_WARNING_DISABLE_MSVC(4723)
63#if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0
>= 50190001
)
64#pragma message disable narrowptr
68#define QIMAGE_SANITYCHECK_MEMORY(image)
69 if ((image).isNull()) {
70 qWarning("QImage: out of memory, returning null image");
75 "#include <qimagereader.h>"
79"ENUM { } QImage::Format;" \
80"FLAGS { } Qt::ImageConversionFlags;"
83Q_TRACE_PARAM_REPLACE(Qt::AspectRatioMode,
int);
84Q_TRACE_PARAM_REPLACE(Qt::TransformationMode,
int);
86static QImage
rotated90(
const QImage &src);
92 Q_CONSTINIT
static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(0);
93 return 1 + serial.fetchAndAddRelaxed(1);
96QImageData::QImageData()
97 : ref(0), width(0), height(0), depth(0), nbytes(0), devicePixelRatio(1.0), data(
nullptr),
98 format(QImage::Format_ARGB32), bytes_per_line(0),
99 ser_no(next_qimage_serial_number()),
101 offset(0, 0), own_data(
true), ro_data(
false), has_alpha_clut(
false),
102 is_cached(
false), cleanupFunction(
nullptr), cleanupInfo(
nullptr),
105 QPoint dpis = qt_defaultDpis();
106 dpmx = dpis.x() * 100 / qreal(2.54);
107 dpmy = dpis.y() * 100 / qreal(2.54);
111
112
113
114
115
116
117QImageData * Q_TRACE_INSTRUMENT(qtgui) QImageData::create(
const QSize &size, QImage::Format format)
119 if (size.isEmpty() || format <= QImage::Format_Invalid || format >= QImage::NImageFormats)
122 Q_TRACE_SCOPE(QImageData_create, size, format);
124 int width = size.width();
125 int height = size.height();
126 int depth = qt_depthForFormat(format);
127 auto params = calculateImageParameters(width, height, depth);
128 if (!params.isValid())
131 auto d = std::make_unique<QImageData>();
134 case QImage::Format_Mono:
135 case QImage::Format_MonoLSB:
136 d->colortable.resize(2);
137 d->colortable[0] = QColor(Qt::black).rgba();
138 d->colortable[1] = QColor(Qt::white).rgba();
148 d->has_alpha_clut =
false;
149 d->is_cached =
false;
151 d->bytes_per_line = params.bytesPerLine;
152 d->nbytes = params.totalSize;
153 d->data = (uchar *)malloc(d->nbytes);
162QImageData::~QImageData()
165 cleanupFunction(cleanupInfo);
167 QImagePixmapCleanupHooks::executeImageHooks((((qint64) ser_no) << 32) | ((qint64) detach_no));
169 if (data && own_data)
170 QtPrivate::sizedFree(data, nbytes);
174#if defined(_M_ARM) && defined(_MSC_VER)
175#pragma optimize("", off)
178bool QImageData::checkForAlphaPixels()
const
180 bool has_alpha_pixels =
false;
184 case QImage::Format_Mono:
185 case QImage::Format_MonoLSB:
186 case QImage::Format_Indexed8:
187 has_alpha_pixels = has_alpha_clut;
189 case QImage::Format_Alpha8:
190 has_alpha_pixels =
true;
192 case QImage::Format_ARGB32:
193 case QImage::Format_ARGB32_Premultiplied: {
194 const uchar *bits = data;
195 for (
int y=0; y<height && !has_alpha_pixels; ++y) {
196 uint alphaAnd = 0xff000000;
197 for (
int x=0; x<width; ++x)
198 alphaAnd &=
reinterpret_cast<
const uint*>(bits)[x];
199 has_alpha_pixels = (alphaAnd != 0xff000000);
200 bits += bytes_per_line;
204 case QImage::Format_RGBA8888:
205 case QImage::Format_RGBA8888_Premultiplied: {
206 const uchar *bits = data;
207 for (
int y=0; y<height && !has_alpha_pixels; ++y) {
208 uchar alphaAnd = 0xff;
209 for (
int x=0; x<width; ++x)
210 alphaAnd &= bits[x * 4+ 3];
211 has_alpha_pixels = (alphaAnd != 0xff);
212 bits += bytes_per_line;
216 case QImage::Format_A2BGR30_Premultiplied:
217 case QImage::Format_A2RGB30_Premultiplied: {
218 const uchar *bits = data;
219 for (
int y=0; y<height && !has_alpha_pixels; ++y) {
220 uint alphaAnd = 0xc0000000;
221 for (
int x=0; x<width; ++x)
222 alphaAnd &=
reinterpret_cast<
const uint*>(bits)[x];
223 has_alpha_pixels = (alphaAnd != 0xc0000000);
224 bits += bytes_per_line;
228 case QImage::Format_ARGB8555_Premultiplied:
229 case QImage::Format_ARGB8565_Premultiplied: {
230 const uchar *bits = data;
231 const uchar *end_bits = data + bytes_per_line;
233 for (
int y=0; y<height && !has_alpha_pixels; ++y) {
234 uchar alphaAnd = 0xff;
235 while (bits < end_bits) {
239 has_alpha_pixels = (alphaAnd != 0xff);
241 end_bits += bytes_per_line;
245 case QImage::Format_ARGB6666_Premultiplied: {
246 const uchar *bits = data;
247 const uchar *end_bits = data + bytes_per_line;
249 for (
int y=0; y<height && !has_alpha_pixels; ++y) {
250 uchar alphaAnd = 0xfc;
251 while (bits < end_bits) {
255 has_alpha_pixels = (alphaAnd != 0xfc);
257 end_bits += bytes_per_line;
261 case QImage::Format_ARGB4444_Premultiplied: {
262 const uchar *bits = data;
263 for (
int y=0; y<height && !has_alpha_pixels; ++y) {
264 ushort alphaAnd = 0xf000;
265 for (
int x=0; x<width; ++x)
266 alphaAnd &=
reinterpret_cast<
const ushort*>(bits)[x];
267 has_alpha_pixels = (alphaAnd != 0xf000);
268 bits += bytes_per_line;
271 case QImage::Format_RGBA64:
272 case QImage::Format_RGBA64_Premultiplied: {
274 for (
int y=0; y<height && !has_alpha_pixels; ++y) {
275 for (
int x=0; x<width; ++x) {
276 has_alpha_pixels |= !(((QRgba64 *)bits)[x].isOpaque());
278 bits += bytes_per_line;
281 case QImage::Format_RGBA16FPx4:
282 case QImage::Format_RGBA16FPx4_Premultiplied: {
284 for (
int y = 0; y < height && !has_alpha_pixels; ++y) {
285 for (
int x = 0; x < width; ++x)
286 has_alpha_pixels |= ((qfloat16 *)bits)[x * 4 + 3] < 1.0f;
287 bits += bytes_per_line;
290 case QImage::Format_RGBA32FPx4:
291 case QImage::Format_RGBA32FPx4_Premultiplied: {
293 for (
int y = 0; y < height && !has_alpha_pixels; ++y) {
294 for (
int x = 0; x < width; ++x)
295 has_alpha_pixels |= ((
float *)bits)[x * 4 + 3] < 1.0f;
296 bits += bytes_per_line;
300 case QImage::Format_RGB32:
301 case QImage::Format_RGB16:
302 case QImage::Format_RGB444:
303 case QImage::Format_RGB555:
304 case QImage::Format_RGB666:
305 case QImage::Format_RGB888:
306 case QImage::Format_BGR888:
307 case QImage::Format_RGBX8888:
308 case QImage::Format_BGR30:
309 case QImage::Format_RGB30:
310 case QImage::Format_Grayscale8:
311 case QImage::Format_Grayscale16:
312 case QImage::Format_RGBX64:
313 case QImage::Format_RGBX16FPx4:
314 case QImage::Format_RGBX32FPx4:
315 case QImage::Format_CMYK8888:
317 case QImage::Format_Invalid:
318 case QImage::NImageFormats:
323 return has_alpha_pixels;
325#if defined(_M_ARM) && defined(_MSC_VER)
326#pragma optimize("", on)
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
633
634
635
636
637
638
639
642
643
644
645
646
647
651
652
653
654
655
656
657
658
659
660
663
664
665
666
667
668
669
670
671
672
673
674
677
678
679
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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
791
792
795
796
797
798
800QImage::QImage()
noexcept
807
808
809
810
811
812
813
814
815
816QImage::QImage(
int width,
int height, Format format)
817 : QImage(QSize(width, height), format)
822
823
824
825
826
827
828
829
830QImage::QImage(
const QSize &size, Format format)
833 d = QImageData::create(size, format);
838QImageData *QImageData::create(uchar *data,
int width,
int height, qsizetype bpl, QImage::Format format,
bool readOnly, QImageCleanupFunction cleanupFunction,
void *cleanupInfo)
840 if (width <= 0 || height <= 0 || !data || format <= QImage::Format_Invalid || format >= QImage::NImageFormats)
843 const int depth = qt_depthForFormat(format);
844 auto params = calculateImageParameters(width, height, depth);
845 if (!params.isValid())
850 const qsizetype min_bytes_per_line = (qsizetype(width) * depth + 7)/8;
851 if (bpl < min_bytes_per_line)
855 params.bytesPerLine = bpl;
856 if (qMulOverflow<qsizetype>(bpl, height, ¶ms.totalSize))
860 QImageData *d =
new QImageData;
864 d->ro_data = readOnly;
871 d->bytes_per_line = params.bytesPerLine;
872 d->nbytes = params.totalSize;
874 d->cleanupFunction = cleanupFunction;
875 d->cleanupInfo = cleanupInfo;
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897QImage::QImage(uchar* data,
int width,
int height, Format format, QImageCleanupFunction cleanupFunction,
void *cleanupInfo)
900 d = QImageData::create(data, width, height, 0, format,
false, cleanupFunction, cleanupInfo);
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928QImage::QImage(
const uchar* data,
int width,
int height, Format format, QImageCleanupFunction cleanupFunction,
void *cleanupInfo)
931 d = QImageData::create(
const_cast<uchar*>(data), width, height, 0, format,
true, cleanupFunction, cleanupInfo);
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
952QImage::QImage(uchar *data,
int width,
int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction,
void *cleanupInfo)
955 d = QImageData::create(data, width, height, bytesPerLine, format,
false, cleanupFunction, cleanupInfo);
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
983QImage::QImage(
const uchar *data,
int width,
int height, qsizetype bytesPerLine, Format format, QImageCleanupFunction cleanupFunction,
void *cleanupInfo)
986 d = QImageData::create(
const_cast<uchar*>(data), width, height, bytesPerLine, format,
true, cleanupFunction, cleanupInfo);
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1010QImage::QImage(
const QString &fileName,
const char *format)
1014 load(fileName, format);
1017#ifndef QT_NO_IMAGEFORMAT_XPM
1018extern bool qt_read_xpm_image_or_array(QIODevice *device,
const char *
const *source, QImage &image);
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1036QImage::QImage(
const char *
const xpm[])
1042 if (!qt_read_xpm_image_or_array(
nullptr, xpm, *
this))
1044 qWarning(
"QImage::QImage(), XPM is not supported");
1049
1050
1051
1052
1053
1054
1055
1057QImage::QImage(
const QImage &image)
1060 if (image.paintingActive()) {
1062 image.copy().swap(*
this);
1071
1072
1076 if (d && !d->ref.deref())
1081
1082
1083
1084
1085
1086
1087
1088
1090QImage &QImage::operator=(
const QImage &image)
1092 if (image.paintingActive()) {
1093 operator=(image.copy());
1097 if (d && !d->ref.deref())
1105
1106
1107
1110
1111
1112int QImage::devType()
const
1114 return QInternal::Image;
1118
1119
1120QImage::operator QVariant()
const
1122 return QVariant::fromValue(*
this);
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136void QImage::detach()
1139 if (d->is_cached && d->ref.loadRelaxed() == 1)
1140 QImagePixmapCleanupHooks::executeImageHooks(cacheKey());
1142 if (d->ref.loadRelaxed() != 1 || d->ro_data)
1152
1153
1154
1155
1156
1157
1158
1159void QImage::detachMetadata(
bool invalidateCache)
1162 if (d->is_cached && d->ref.loadRelaxed() == 1)
1163 QImagePixmapCleanupHooks::executeImageHooks(cacheKey());
1165 if (d->ref.loadRelaxed() != 1)
1168 if (d && invalidateCache)
1175 dst->dpmx = src->dpmx;
1176 dst->dpmy = src->dpmy;
1177 dst->devicePixelRatio = src->devicePixelRatio;
1184 dst->text = src->text;
1185 dst->offset = src->offset;
1186 dst->colorSpace = src->colorSpace;
1191 dst->setDotsPerMeterX(src.dotsPerMeterX());
1192 dst->setDotsPerMeterY(src.dotsPerMeterY());
1193 dst->setDevicePixelRatio(src.devicePixelRatio());
1194 const auto textKeys = src.textKeys();
1195 for (
const auto &key: textKeys)
1196 dst->setText(key, src.text(key));
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236QImage Q_TRACE_INSTRUMENT(qtgui) QImage::copy(
const QRect& r)
const
1238 Q_TRACE_SCOPE(QImage_copy, r);
1243 QImage image(d->width, d->height, d->format);
1249 if (image.d->nbytes != d->nbytes) {
1250 qsizetype bpl = qMin(bytesPerLine(), image.bytesPerLine());
1251 for (
int i = 0; i < height(); i++)
1252 memcpy(image.scanLine(i), scanLine(i), bpl);
1254 memcpy(image.bits(), bits(), d->nbytes);
1255 image.d->colortable = d->colortable;
1256 image.d->has_alpha_clut = d->has_alpha_clut;
1257 copyMetadata(image.d, d);
1268 if (w <= 0 || h <= 0)
1271 QImage image(w, h, d->format);
1275 if (x < 0 || y < 0 || x + w > d->width || y + h > d->height) {
1288 image.d->colortable = d->colortable;
1290 int pixels_to_copy = qMax(w - dx, 0);
1293 else if (pixels_to_copy > d->width - x)
1294 pixels_to_copy = d->width - x;
1295 int lines_to_copy = qMax(h - dy, 0);
1298 else if (lines_to_copy > d->height - y)
1299 lines_to_copy = d->height - y;
1301 bool byteAligned =
true;
1302 if (d->format == Format_Mono || d->format == Format_MonoLSB)
1303 byteAligned = !(dx & 7) && !(x & 7) && !(pixels_to_copy & 7);
1306 const uchar *src = d->data + ((x * d->depth) >> 3) + y * d->bytes_per_line;
1307 uchar *dest = image.d->data + ((dx * d->depth) >> 3) + dy * image.d->bytes_per_line;
1308 const qsizetype bytes_to_copy = (qsizetype(pixels_to_copy) * d->depth) >> 3;
1309 for (
int i = 0; i < lines_to_copy; ++i) {
1310 memcpy(dest, src, bytes_to_copy);
1311 src += d->bytes_per_line;
1312 dest += image.d->bytes_per_line;
1314 }
else if (d->format == Format_Mono) {
1315 const uchar *src = d->data + y * d->bytes_per_line;
1316 uchar *dest = image.d->data + dy * image.d->bytes_per_line;
1317 for (
int i = 0; i < lines_to_copy; ++i) {
1318 for (
int j = 0; j < pixels_to_copy; ++j) {
1319 if (src[(x + j) >> 3] & (0x80 >> ((x + j) & 7)))
1320 dest[(dx + j) >> 3] |= (0x80 >> ((dx + j) & 7));
1322 dest[(dx + j) >> 3] &= ~(0x80 >> ((dx + j) & 7));
1324 src += d->bytes_per_line;
1325 dest += image.d->bytes_per_line;
1328 Q_ASSERT(d->format == Format_MonoLSB);
1329 const uchar *src = d->data + y * d->bytes_per_line;
1330 uchar *dest = image.d->data + dy * image.d->bytes_per_line;
1331 for (
int i = 0; i < lines_to_copy; ++i) {
1332 for (
int j = 0; j < pixels_to_copy; ++j) {
1333 if (src[(x + j) >> 3] & (0x1 << ((x + j) & 7)))
1334 dest[(dx + j) >> 3] |= (0x1 << ((dx + j) & 7));
1336 dest[(dx + j) >> 3] &= ~(0x1 << ((dx + j) & 7));
1338 src += d->bytes_per_line;
1339 dest += image.d->bytes_per_line;
1343 copyMetadata(image.d, d);
1344 image.d->has_alpha_clut = d->has_alpha_clut;
1351
1352
1353
1354
1355
1356bool QImage::isNull()
const
1362
1363
1364
1365
1366
1367
1368int QImage::width()
const
1370 return d ? d->width : 0;
1374
1375
1376
1377
1378
1379
1380int QImage::height()
const
1382 return d ? d->height : 0;
1386
1387
1388
1389
1390
1391
1392QSize QImage::size()
const
1394 return d ? QSize(d->width, d->height) : QSize(0, 0);
1398
1399
1400
1401
1402
1403
1404
1405QRect QImage::rect()
const
1407 return d ? QRect(0, 0, d->width, d->height) : QRect();
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422int QImage::depth()
const
1424 return d ? d->depth : 0;
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438int QImage::colorCount()
const
1440 return d ? d->colortable.size() : 0;
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454void QImage::setColorTable(
const QList<QRgb> &colors)
1458 detachMetadata(
true);
1464 d->colortable = colors;
1465 d->has_alpha_clut =
false;
1466 for (
int i = 0; i < d->colortable.size(); ++i) {
1467 if (qAlpha(d->colortable.at(i)) != 255) {
1468 d->has_alpha_clut =
true;
1475
1476
1477
1478
1479
1480QList<QRgb> QImage::colorTable()
const
1482 return d ? d->colortable : QList<QRgb>();
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496qreal QImage::devicePixelRatio()
const
1500 return d->devicePixelRatio;
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524void QImage::setDevicePixelRatio(qreal scaleFactor)
1529 if (scaleFactor == d->devicePixelRatio)
1534 d->devicePixelRatio = scaleFactor;
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547QSizeF QImage::deviceIndependentSize()
const
1550 return QSizeF(0, 0);
1551 return QSizeF(d->width, d->height) / d->devicePixelRatio;
1556
1557
1558
1559
1560
1561
1562qsizetype QImage::sizeInBytes()
const
1564 return d ? d->nbytes : 0;
1568
1569
1570
1571
1572
1573
1574qsizetype QImage::bytesPerLine()
const
1576 return d ? d->bytes_per_line : 0;
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591QRgb QImage::color(
int i)
const
1593 Q_ASSERT(i < colorCount());
1594 return d ? d->colortable.at(i) : QRgb(uint(-1));
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609void QImage::setColor(
int i, QRgb c)
1613 if (i < 0 || d->depth > 8 || i >= 1<<d->depth) {
1614 qWarning(
"QImage::setColor: Index out of bound %d", i);
1617 detachMetadata(
true);
1623 if (i >= d->colortable.size())
1625 d->colortable[i] = c;
1626 d->has_alpha_clut |= (qAlpha(c) != 255);
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651uchar *QImage::scanLine(
int i)
1662 return d->data + i * d->bytes_per_line;
1666
1667
1668const uchar *QImage::scanLine(
int i)
const
1673 Q_ASSERT(i >= 0 && i < height());
1674 return d->data + i * d->bytes_per_line;
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692const uchar *QImage::constScanLine(
int i)
const
1697 Q_ASSERT(i >= 0 && i < height());
1698 return d->data + i * d->bytes_per_line;
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712uchar *QImage::bits()
1726
1727
1728
1729
1730
1731
1732const uchar *QImage::bits()
const
1734 return d ? d->data :
nullptr;
1739
1740
1741
1742
1743
1744
1745
1746
1747const uchar *QImage::constBits()
const
1749 return d ? d->data :
nullptr;
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1772void QImage::fill(uint pixel)
1783 if (d->depth == 1 || d->depth == 8) {
1785 if (d->depth == 1) {
1794 qt_rectfill<quint8>(d->data, pixel, 0, 0,
1795 w, d->height, d->bytes_per_line);
1797 }
else if (d->depth == 16) {
1798 if (d->format == Format_RGB444)
1800 qt_rectfill<quint16>(
reinterpret_cast<quint16*>(d->data), pixel,
1801 0, 0, d->width, d->height, d->bytes_per_line);
1803 }
else if (d->depth == 24) {
1804 if (d->format == Format_RGB666)
1806 qt_rectfill<quint24>(
reinterpret_cast<quint24*>(d->data), pixel,
1807 0, 0, d->width, d->height, d->bytes_per_line);
1809 }
else if (d->format >= QImage::Format_RGBX64 && d->format <= QImage::Format_RGBA64_Premultiplied) {
1810 qt_rectfill<quint64>(
reinterpret_cast<quint64*>(d->data), QRgba64::fromArgb32(pixel),
1811 0, 0, d->width, d->height, d->bytes_per_line);
1813 }
else if (d->format >= QImage::Format_RGBX16FPx4 && d->format <= QImage::Format_RGBA16FPx4_Premultiplied) {
1815 QRgbaFloat16 cf = QRgbaFloat16::fromArgb32(pixel);
1816 ::memcpy(&cu, &cf,
sizeof(quint64));
1817 qt_rectfill<quint64>(
reinterpret_cast<quint64*>(d->data), cu,
1818 0, 0, d->width, d->height, d->bytes_per_line);
1820 }
else if (d->format >= QImage::Format_RGBX32FPx4 && d->format <= QImage::Format_RGBA32FPx4_Premultiplied) {
1821 QRgbaFloat32 cf = QRgbaFloat32::fromArgb32(pixel);
1822 uchar *data = d->data;
1823 for (
int y = 0; y < d->height; ++y) {
1824 QRgbaFloat32 *line =
reinterpret_cast<QRgbaFloat32 *>(data);
1825 for (
int x = 0; x < d->width; ++x)
1827 data += d->bytes_per_line;
1831 Q_ASSERT(d->depth == 32);
1833 if (d->format == Format_RGB32)
1834 pixel |= 0xff000000;
1835 if (d->format == Format_RGBX8888)
1836#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
1837 pixel |= 0xff000000;
1839 pixel |= 0x000000ff;
1841 if (d->format == Format_BGR30 || d->format == Format_RGB30)
1842 pixel |= 0xc0000000;
1844 qt_rectfill<uint>(
reinterpret_cast<uint*>(d->data), pixel,
1845 0, 0, d->width, d->height, d->bytes_per_line);
1850
1851
1852
1853
1854
1855
1857void QImage::fill(Qt::GlobalColor color)
1859 fill(QColor(color));
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1879void QImage::fill(
const QColor &color)
1889 QRgba64 opaque = color.rgba64();
1890 opaque.setAlpha(65535);
1891 switch (d->format) {
1892 case QImage::Format_RGB32:
1893 case QImage::Format_ARGB32:
1896 case QImage::Format_ARGB32_Premultiplied:
1897 fill(qPremultiply(color.rgba()));
1899 case QImage::Format_RGBX8888:
1900 fill(ARGB2RGBA(color.rgba() | 0xff000000));
1902 case QImage::Format_RGBA8888:
1903 fill(ARGB2RGBA(color.rgba()));
1905 case QImage::Format_RGBA8888_Premultiplied:
1906 fill(ARGB2RGBA(qPremultiply(color.rgba())));
1908 case QImage::Format_BGR30:
1909 fill(qConvertRgb64ToRgb30<PixelOrderBGR>(opaque));
1911 case QImage::Format_RGB30:
1912 fill(qConvertRgb64ToRgb30<PixelOrderRGB>(opaque));
1914 case QImage::Format_RGB16:
1915 fill((uint) qConvertRgb32To16(color.rgba()));
1917 case QImage::Format_Indexed8: {
1919 for (
int i=0; i<d->colortable.size(); ++i) {
1920 if (color.rgba() == d->colortable.at(i)) {
1928 case QImage::Format_Mono:
1929 case QImage::Format_MonoLSB:
1930 if (color == Qt::color1)
1935 case QImage::Format_RGBX64:
1936 qt_rectfill<quint64>(
reinterpret_cast<quint64*>(d->data), opaque,
1937 0, 0, d->width, d->height, d->bytes_per_line);
1939 case QImage::Format_RGBA64:
1940 qt_rectfill<quint64>(
reinterpret_cast<quint64*>(d->data), color.rgba64(),
1941 0, 0, d->width, d->height, d->bytes_per_line);
1943 case QImage::Format_RGBA64_Premultiplied:
1944 qt_rectfill<quint64>(
reinterpret_cast<quint64 *>(d->data), color.rgba64().premultiplied(),
1945 0, 0, d->width, d->height, d->bytes_per_line);
1947 case QImage::Format_RGBX16FPx4:
1948 case QImage::Format_RGBA16FPx4:
1949 case QImage::Format_RGBA16FPx4_Premultiplied:
1950 case QImage::Format_RGBX32FPx4:
1951 case QImage::Format_RGBA32FPx4:
1952 case QImage::Format_RGBA32FPx4_Premultiplied:{
1954 color.getRgbF(&r, &g, &b, &a);
1955 if (!hasAlphaChannel())
1957 if (depth() == 64) {
1958 QRgbaFloat16 c16{qfloat16(r), qfloat16(g), qfloat16(b), qfloat16(a)};
1959 if (d->format == Format_RGBA16FPx4_Premultiplied)
1960 c16 = c16.premultiplied();
1961 qt_rectfill<QRgbaFloat16>(
reinterpret_cast<QRgbaFloat16 *>(d->data), c16,
1962 0, 0, d->width, d->height, d->bytes_per_line);
1964 QRgbaFloat32 c32{r, g, b, a};
1965 if (d->format == Format_RGBA32FPx4_Premultiplied)
1966 c32 = c32.premultiplied();
1967 qt_rectfill<QRgbaFloat32>(
reinterpret_cast<QRgbaFloat32 *>(d->data), c32,
1968 0, 0, d->width, d->height, d->bytes_per_line);
1974 p.setCompositionMode(QPainter::CompositionMode_Source);
1975 p.fillRect(rect(), color);
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2001void QImage::invertPixels(InvertMode mode)
2012 QImage::Format originalFormat = d->format;
2014 if (hasAlphaChannel() && qPixelLayouts[d->format].premultiplied) {
2015 if (d->format == QImage::Format_RGBA16FPx4_Premultiplied) {
2016 if (!d->convertInPlace(QImage::Format_RGBA16FPx4, { }))
2017 *
this = convertToFormat(QImage::Format_RGBA16FPx4);
2018 }
else if (d->format == QImage::Format_RGBA32FPx4_Premultiplied) {
2019 if (!d->convertInPlace(QImage::Format_RGBA32FPx4, { }))
2020 *
this = convertToFormat(QImage::Format_RGBA32FPx4);
2021 }
else if (depth() > 32) {
2022 if (!d->convertInPlace(QImage::Format_RGBA64, { }))
2023 *
this = convertToFormat(QImage::Format_RGBA64);
2025 if (!d->convertInPlace(QImage::Format_ARGB32, { }))
2026 *
this = convertToFormat(QImage::Format_ARGB32);
2032 qsizetype bpl = (qsizetype(d->width) * d->depth + 7) / 8;
2033 int pad = d->bytes_per_line - bpl;
2034 uchar *sl = d->data;
2035 for (
int y=0; y<d->height; ++y) {
2036 for (qsizetype x=0; x<bpl; ++x)
2040 }
else if (format() >= QImage::Format_RGBX16FPx4 && format() <= QImage::Format_RGBA16FPx4_Premultiplied) {
2041 qfloat16 *p =
reinterpret_cast<qfloat16 *>(d->data);
2042 qfloat16 *end =
reinterpret_cast<qfloat16 *>(d->data + d->nbytes);
2044 p[0] = qfloat16(1) - p[0];
2045 p[1] = qfloat16(1) - p[1];
2046 p[2] = qfloat16(1) - p[2];
2047 if (mode == InvertRgba)
2048 p[3] = qfloat16(1) - p[3];
2051 }
else if (format() >= QImage::Format_RGBX32FPx4 && format() <= QImage::Format_RGBA32FPx4_Premultiplied) {
2052 uchar *data = d->data;
2053 for (
int y = 0; y < d->height; ++y) {
2054 float *p =
reinterpret_cast<
float *>(data);
2055 for (
int x = 0; x < d->width; ++x) {
2059 if (mode == InvertRgba)
2063 data += d->bytes_per_line;
2065 }
else if (depth() == 64) {
2066 quint16 *p = (quint16*)d->data;
2067 quint16 *end = (quint16*)(d->data + d->nbytes);
2068 quint16 xorbits = 0xffff;
2073 if (mode == InvertRgba)
2079 quint32 *p = (quint32*)d->data;
2080 quint32 *end = (quint32*)(d->data + d->nbytes);
2081 quint32 xorbits = 0xffffffff;
2082 switch (d->format) {
2083 case QImage::Format_RGBA8888:
2084 if (mode == InvertRgba)
2087 case QImage::Format_RGBX8888:
2088#if Q_BYTE_ORDER == Q_BIG_ENDIAN
2089 xorbits = 0xffffff00;
2092 xorbits = 0x00ffffff;
2095 case QImage::Format_ARGB32:
2096 if (mode == InvertRgba)
2099 case QImage::Format_RGB32:
2100 xorbits = 0x00ffffff;
2102 case QImage::Format_BGR30:
2103 case QImage::Format_RGB30:
2104 xorbits = 0x3fffffff;
2115 if (originalFormat != d->format) {
2116 if (!d->convertInPlace(originalFormat, { }))
2117 *
this = convertToFormat(originalFormat);
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2146void QImage::setColorCount(
int colorCount)
2149 qWarning(
"QImage::setColorCount: null image");
2153 detachMetadata(
true);
2159 if (colorCount == d->colortable.size())
2161 if (colorCount <= 0) {
2162 d->colortable.clear();
2165 int nc = d->colortable.size();
2166 d->colortable.resize(colorCount);
2167 for (
int i = nc; i < colorCount; ++i)
2168 d->colortable[i] = 0;
2172
2173
2174
2175
2176QImage::Format QImage::format()
const
2180 Q_ASSERT(d->format < NImageFormats);
2181 Q_ASSERT(d->format > Format_Invalid);
2183 return d ? d->format : Format_Invalid;
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2212
2213
2214QImage QImage::convertToFormat_helper(Format format, Qt::ImageConversionFlags flags)
const
2216 if (!d || d->format == format)
2219 if (d->format == Format_Invalid || format <= Format_Invalid || format >= NImageFormats)
2222 const QPixelLayout *destLayout = &qPixelLayouts[format];
2223 Image_Converter converter = qimage_converter_map[d->format][format];
2224 if (!converter && format > QImage::Format_Indexed8 && d->format > QImage::Format_Indexed8) {
2225 if (qt_highColorPrecision(d->format, !destLayout->hasAlphaChannel)
2226 && qt_highColorPrecision(format, !hasAlphaChannel())) {
2227#if QT_CONFIG(raster_fp)
2228 if (qt_fpColorPrecision(d->format) && qt_fpColorPrecision(format))
2229 converter = convert_generic_over_rgba32f;
2232 converter = convert_generic_over_rgb64;
2234 converter = convert_generic;
2237 QImage image(d->width, d->height, format);
2241 copyMetadata(image.d, d);
2243 converter(image.d, d, flags);
2248 Q_ASSERT(format != QImage::Format_ARGB32 && format != QImage::Format_RGB32);
2249 Q_ASSERT(d->format != QImage::Format_ARGB32 && d->format != QImage::Format_RGB32);
2251 if (!hasAlphaChannel())
2252 return convertToFormat(Format_RGB32, flags).convertToFormat(format, flags);
2254 return convertToFormat(Format_ARGB32, flags).convertToFormat(format, flags);
2258
2259
2260bool QImage::convertToFormat_inplace(Format format, Qt::ImageConversionFlags flags)
2262 return d && d->convertInPlace(format, flags);
2265static inline int pixel_distance(QRgb p1, QRgb p2) {
2267 int g1 = qGreen(p1);
2269 int a1 = qAlpha(p1);
2272 int g2 = qGreen(p2);
2274 int a2 = qAlpha(p2);
2276 return abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2) + abs(a1 - a2);
2279static inline int closestMatch(QRgb pixel,
const QList<QRgb> &clut) {
2281 int current_distance = INT_MAX;
2282 for (
int i=0; i<clut.size(); ++i) {
2283 int dist = pixel_distance(pixel, clut.at(i));
2284 if (dist < current_distance) {
2285 current_distance = dist;
2292static QImage convertWithPalette(
const QImage &src, QImage::Format format,
2293 const QList<QRgb> &clut) {
2294 QImage dest(src.size(), format);
2295 dest.setColorTable(clut);
2297 copyMetadata(QImageData::get(dest), QImageData::get(src));
2299 int h = src.height();
2300 int w = src.width();
2302 QHash<QRgb,
int> cache;
2304 if (format == QImage::Format_Indexed8) {
2305 for (
int y=0; y<h; ++y) {
2306 const QRgb *src_pixels = (
const QRgb *) src.scanLine(y);
2307 uchar *dest_pixels = (uchar *) dest.scanLine(y);
2308 for (
int x=0; x<w; ++x) {
2309 int src_pixel = src_pixels[x];
2310 int value = cache.value(src_pixel, -1);
2312 value = closestMatch(src_pixel, clut);
2313 cache.insert(src_pixel, value);
2315 dest_pixels[x] = (uchar) value;
2319 QList<QRgb> table = clut;
2321 for (
int y=0; y<h; ++y) {
2322 const QRgb *src_pixels = (
const QRgb *) src.scanLine(y);
2323 for (
int x=0; x<w; ++x) {
2324 int src_pixel = src_pixels[x];
2325 int value = cache.value(src_pixel, -1);
2327 value = closestMatch(src_pixel, table);
2328 cache.insert(src_pixel, value);
2330 dest.setPixel(x, y, value);
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348QImage QImage::convertToFormat(Format format,
const QList<QRgb> &colorTable, Qt::ImageConversionFlags flags)
const
2350 if (!d || d->format == format)
2353 if (format <= QImage::Format_Invalid || format >= QImage::NImageFormats)
2355 if (format <= QImage::Format_Indexed8)
2356 return convertWithPalette(convertToFormat(QImage::Format_ARGB32, flags), format, colorTable);
2358 return convertToFormat(format, flags);
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2384bool QImage::reinterpretAsFormat(Format format)
2386 if (format <= Format_Invalid || format >= NImageFormats)
2390 if (d->format == format)
2392 if (qt_depthForFormat(format) != qt_depthForFormat(d->format))
2394 if (!isDetached()) {
2395 QImageData *oldD = d;
2410
2411
2412
2413
2414
2415
2416
2417
2418
2420void QImage::convertTo(Format format, Qt::ImageConversionFlags flags)
2422 if (!d || format <= QImage::Format_Invalid || format >= QImage::NImageFormats)
2425 if (d->format == format)
2429 if (convertToFormat_inplace(format, flags))
2432 *
this = convertToFormat_helper(format, flags);
2436
2437
2438
2439
2440
2441
2442
2445
2446
2447
2448
2449
2450bool QImage::valid(
int x,
int y)
const
2453 && x >= 0 && x < d->width
2454 && y >= 0 && y < d->height;
2458
2459
2460
2461
2462
2463
2464
2465
2466
2469
2470
2471
2472
2473int QImage::pixelIndex(
int x,
int y)
const
2475 if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) {
2476 qWarning(
"QImage::pixelIndex: coordinate (%d,%d) out of range", x, y);
2479 const uchar * s = scanLine(y);
2482 return (*(s + (x >> 3)) >> (7- (x & 7))) & 1;
2483 case Format_MonoLSB:
2484 return (*(s + (x >> 3)) >> (x & 7)) & 1;
2485 case Format_Indexed8:
2488 qWarning(
"QImage::pixelIndex: Not applicable for %d-bpp images (no palette)", d->depth);
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2510
2511
2512
2513
2514QRgb QImage::pixel(
int x,
int y)
const
2516 if (!d || x < 0 || x >= d->width || y < 0 || y >= d->height) {
2517 qWarning(
"QImage::pixel: coordinate (%d,%d) out of range", x, y);
2521 const uchar *s = d->data + y * d->bytes_per_line;
2524 switch (d->format) {
2526 index = (*(s + (x >> 3)) >> (~x & 7)) & 1;
2528 case Format_MonoLSB:
2529 index = (*(s + (x >> 3)) >> (x & 7)) & 1;
2531 case Format_Indexed8:
2538 if (index >= d->colortable.size()) {
2539 qWarning(
"QImage::pixel: color table index %d out of range.", index);
2542 return d->colortable.at(index);
2544 std::optional<QRgb> out;
2545 switch (d->format) {
2548 case Format_ARGB32_Premultiplied:
2549 out =
reinterpret_cast<
const QRgb *>(s)[x];
2551 case Format_RGBX8888:
2552 case Format_RGBA8888:
2553 case Format_RGBA8888_Premultiplied:
2554 out = RGBA2ARGB(
reinterpret_cast<
const quint32 *>(s)[x]);
2557 case Format_A2BGR30_Premultiplied:
2558 out = qConvertA2rgb30ToArgb32<PixelOrderBGR>(
reinterpret_cast<
const quint32 *>(s)[x]);
2561 case Format_A2RGB30_Premultiplied:
2562 out = qConvertA2rgb30ToArgb32<PixelOrderRGB>(
reinterpret_cast<
const quint32 *>(s)[x]);
2565 return qConvertRgb16To32(
reinterpret_cast<
const quint16 *>(s)[x]);
2568 case Format_RGBA64_Premultiplied:
2569 out =
reinterpret_cast<
const QRgba64 *>(s)[x].toArgb32();
2571 case Format_RGBX16FPx4:
2572 case Format_RGBA16FPx4:
2573 case Format_RGBA16FPx4_Premultiplied:
2574 out =
reinterpret_cast<
const QRgbaFloat16 *>(s)[x].toArgb32();
2576 case Format_RGBX32FPx4:
2577 case Format_RGBA32FPx4:
2578 case Format_RGBA32FPx4_Premultiplied:
2579 out =
reinterpret_cast<
const QRgbaFloat32 *>(s)[x].toArgb32();
2583 const QPixelLayout *layout = &qPixelLayouts[d->format];
2586 if (!layout->hasAlphaChannel)
2591 return *layout->fetchToARGB32PM(&result, s, x, 1,
nullptr,
nullptr);
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2616
2617
2618
2619
2620void QImage::setPixel(
int x,
int y, uint index_or_rgb)
2622 if (!d || x < 0 || x >= width() || y < 0 || y >= height()) {
2623 qWarning(
"QImage::setPixel: coordinate (%d,%d) out of range", x, y);
2627 uchar * s = scanLine(y);
2630 case Format_MonoLSB:
2631 if (index_or_rgb > 1) {
2632 qWarning(
"QImage::setPixel: Index %d out of range", index_or_rgb);
2633 }
else if (format() == Format_MonoLSB) {
2634 if (index_or_rgb==0)
2635 *(s + (x >> 3)) &= ~(1 << (x & 7));
2637 *(s + (x >> 3)) |= (1 << (x & 7));
2639 if (index_or_rgb==0)
2640 *(s + (x >> 3)) &= ~(1 << (7-(x & 7)));
2642 *(s + (x >> 3)) |= (1 << (7-(x & 7)));
2645 case Format_Indexed8:
2646 if (index_or_rgb >= (uint)d->colortable.size()) {
2647 qWarning(
"QImage::setPixel: Index %d out of range", index_or_rgb);
2650 s[x] = index_or_rgb;
2655 ((uint *)s)[x] = 0xff000000 | index_or_rgb;
2658 case Format_ARGB32_Premultiplied:
2659 ((uint *)s)[x] = index_or_rgb;
2662 ((quint16 *)s)[x] = qConvertRgb32To16(index_or_rgb);
2664 case Format_RGBX8888:
2665 ((uint *)s)[x] = ARGB2RGBA(0xff000000 | index_or_rgb);
2667 case Format_RGBA8888:
2668 case Format_RGBA8888_Premultiplied:
2669 ((uint *)s)[x] = ARGB2RGBA(index_or_rgb);
2672 ((uint *)s)[x] = qConvertRgb32ToRgb30<PixelOrderBGR>(index_or_rgb);
2674 case Format_A2BGR30_Premultiplied:
2675 ((uint *)s)[x] = qConvertArgb32ToA2rgb30<PixelOrderBGR>(index_or_rgb);
2678 ((uint *)s)[x] = qConvertRgb32ToRgb30<PixelOrderRGB>(index_or_rgb);
2680 case Format_A2RGB30_Premultiplied:
2681 ((uint *)s)[x] = qConvertArgb32ToA2rgb30<PixelOrderRGB>(index_or_rgb);
2684 ((QRgba64 *)s)[x] = QRgba64::fromArgb32(index_or_rgb | 0xff000000);
2687 case Format_RGBA64_Premultiplied:
2688 ((QRgba64 *)s)[x] = QRgba64::fromArgb32(index_or_rgb);
2690 case Format_RGBX16FPx4:
2691 ((QRgbaFloat16 *)s)[x] = QRgbaFloat16::fromArgb32(index_or_rgb | 0xff000000);
2693 case Format_RGBA16FPx4:
2694 case Format_RGBA16FPx4_Premultiplied:
2695 ((QRgbaFloat16 *)s)[x] = QRgbaFloat16::fromArgb32(index_or_rgb);
2697 case Format_RGBX32FPx4:
2698 ((QRgbaFloat32 *)s)[x] = QRgbaFloat32::fromArgb32(index_or_rgb | 0xff000000);
2700 case Format_RGBA32FPx4:
2701 case Format_RGBA32FPx4_Premultiplied:
2702 ((QRgbaFloat32 *)s)[x] = QRgbaFloat32::fromArgb32(index_or_rgb);
2704 case Format_Invalid:
2712 const QPixelLayout *layout = &qPixelLayouts[d->format];
2713 if (!hasAlphaChannel())
2714 layout->storeFromRGB32(s, &index_or_rgb, x, 1,
nullptr,
nullptr);
2716 layout->storeFromARGB32PM(s, &index_or_rgb, x, 1,
nullptr,
nullptr);
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2736
2737
2738
2739
2740
2741QColor QImage::pixelColor(
int x,
int y)
const
2743 if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) {
2744 qWarning(
"QImage::pixelColor: coordinate (%d,%d) out of range", x, y);
2749 const uchar * s = constScanLine(y);
2750 switch (d->format) {
2752 case Format_A2BGR30_Premultiplied:
2753 c = qConvertA2rgb30ToRgb64<PixelOrderBGR>(
reinterpret_cast<
const quint32 *>(s)[x]);
2756 case Format_A2RGB30_Premultiplied:
2757 c = qConvertA2rgb30ToRgb64<PixelOrderRGB>(
reinterpret_cast<
const quint32 *>(s)[x]);
2761 case Format_RGBA64_Premultiplied:
2762 c =
reinterpret_cast<
const QRgba64 *>(s)[x];
2764 case Format_Grayscale16: {
2765 quint16 v =
reinterpret_cast<
const quint16 *>(s)[x];
2766 return QColor(qRgba64(v, v, v, 0xffff));
2768 case Format_RGBX16FPx4:
2769 case Format_RGBA16FPx4:
2770 case Format_RGBA16FPx4_Premultiplied: {
2771 QRgbaFloat16 p =
reinterpret_cast<
const QRgbaFloat16 *>(s)[x];
2772 if (d->format == Format_RGBA16FPx4_Premultiplied)
2773 p = p.unpremultiplied();
2774 else if (d->format == Format_RGBX16FPx4)
2777 color.setRgbF(p.red(), p.green(), p.blue(), p.alpha());
2780 case Format_RGBX32FPx4:
2781 case Format_RGBA32FPx4:
2782 case Format_RGBA32FPx4_Premultiplied: {
2783 QRgbaFloat32 p =
reinterpret_cast<
const QRgbaFloat32 *>(s)[x];
2784 if (d->format == Format_RGBA32FPx4_Premultiplied)
2785 p = p.unpremultiplied();
2786 else if (d->format == Format_RGBX32FPx4)
2789 color.setRgbF(p.red(), p.green(), p.blue(), p.alpha());
2793 c = QRgba64::fromArgb32(pixel(x, y));
2797 switch (d->format) {
2807 if (hasAlphaChannel() && qPixelLayouts[d->format].premultiplied)
2808 c = c.unpremultiplied();
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2829
2830
2831
2832
2833
2834void QImage::setPixelColor(
int x,
int y,
const QColor &color)
2836 if (!d || x < 0 || x >= width() || y < 0 || y >= height()) {
2837 qWarning(
"QImage::setPixelColor: coordinate (%d,%d) out of range", x, y);
2841 if (!color.isValid()) {
2842 qWarning(
"QImage::setPixelColor: color is invalid");
2847 QRgba64 c = color.rgba64();
2848 if (!hasAlphaChannel())
2850 else if (qPixelLayouts[d->format].premultiplied)
2851 c = c.premultiplied();
2853 uchar * s = scanLine(y);
2854 switch (d->format) {
2856 case Format_MonoLSB:
2857 case Format_Indexed8:
2858 qWarning(
"QImage::setPixelColor: called on monochrome or indexed format");
2861 ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderBGR>(c) | 0xc0000000;
2863 case Format_A2BGR30_Premultiplied:
2864 ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderBGR>(c);
2867 ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderRGB>(c) | 0xc0000000;
2869 case Format_A2RGB30_Premultiplied:
2870 ((uint *)s)[x] = qConvertRgb64ToRgb30<PixelOrderRGB>(c);
2874 case Format_RGBA64_Premultiplied:
2875 ((QRgba64 *)s)[x] = c;
2877 case Format_RGBX16FPx4:
2878 case Format_RGBA16FPx4:
2879 case Format_RGBA16FPx4_Premultiplied: {
2881 color.getRgbF(&r, &g, &b, &a);
2882 if (d->format == Format_RGBX16FPx4)
2884 QRgbaFloat16 c16f{qfloat16(r), qfloat16(g), qfloat16(b), qfloat16(a)};
2885 if (d->format == Format_RGBA16FPx4_Premultiplied)
2886 c16f = c16f.premultiplied();
2887 ((QRgbaFloat16 *)s)[x] = c16f;
2890 case Format_RGBX32FPx4:
2891 case Format_RGBA32FPx4:
2892 case Format_RGBA32FPx4_Premultiplied: {
2894 color.getRgbF(&r, &g, &b, &a);
2895 if (d->format == Format_RGBX32FPx4)
2897 QRgbaFloat32 c32f{r, g, b, a};
2898 if (d->format == Format_RGBA32FPx4_Premultiplied)
2899 c32f = c32f.premultiplied();
2900 ((QRgbaFloat32 *)s)[x] = c32f;
2904 setPixel(x, y, c.toArgb32());
2910
2911
2912
2913
2914
2915
2916
2917
2918bool QImage::allGray()
const
2923 switch (d->format) {
2925 case Format_MonoLSB:
2926 case Format_Indexed8:
2927 for (
int i = 0; i < d->colortable.size(); ++i) {
2928 if (!qIsGray(d->colortable.at(i)))
2934 case Format_Grayscale8:
2935 case Format_Grayscale16:
2939 case Format_ARGB32_Premultiplied:
2940#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
2941 case Format_RGBX8888:
2942 case Format_RGBA8888:
2943 case Format_RGBA8888_Premultiplied:
2945 for (
int j = 0; j < d->height; ++j) {
2946 const QRgb *b = (
const QRgb *)constScanLine(j);
2947 for (
int i = 0; i < d->width; ++i) {
2954 for (
int j = 0; j < d->height; ++j) {
2955 const quint16 *b = (
const quint16 *)constScanLine(j);
2956 for (
int i = 0; i < d->width; ++i) {
2957 if (!qIsGray(qConvertRgb16To32(b[i])))
2966 Q_DECL_UNINITIALIZED uint buffer[BufferSize];
2967 const QPixelLayout *layout = &qPixelLayouts[d->format];
2968 const auto fetch = layout->fetchToARGB32PM;
2969 for (
int j = 0; j < d->height; ++j) {
2970 const uchar *b = constScanLine(j);
2972 while (x < d->width) {
2973 int l = qMin(d->width - x, BufferSize);
2974 const uint *ptr = fetch(buffer, b, x, l,
nullptr,
nullptr);
2975 for (
int i = 0; i < l; ++i) {
2976 if (!qIsGray(ptr[i]))
2986
2987
2988
2989
2990
2991
2992
2993
2994bool QImage::isGrayscale()
const
2999 if (d->format == QImage::Format_Alpha8)
3002 if (d->format == QImage::Format_Grayscale8 || d->format == QImage::Format_Grayscale16)
3011 Q_ASSERT(d->format == QImage::Format_Indexed8);
3012 for (
int i = 0; i < colorCount(); i++)
3013 if (d->colortable.at(i) != qRgb(i,i,i))
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064QImage Q_TRACE_INSTRUMENT(qtgui) QImage::scaled(
const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode)
const
3067 qWarning(
"QImage::scaled: Image is a null image");
3073 QSize newSize = size();
3074 if (aspectMode == Qt::KeepAspectRatioByExpanding) {
3075 const QSizeF fs = QSizeF(newSize).scaled(s, aspectMode);
3076 if (std::max(fs.width(), fs.height()) > INT_MAX)
3079 newSize.scale(s, aspectMode);
3080 newSize.rwidth() = qMax(newSize.width(), 1);
3081 newSize.rheight() = qMax(newSize.height(), 1);
3082 if (newSize == size())
3085 Q_TRACE_SCOPE(QImage_scaled, s, aspectMode, mode);
3087 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(), (qreal)newSize.height() / height());
3088 QImage img = transformed(wm, mode);
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109QImage Q_TRACE_INSTRUMENT(qtgui) QImage::scaledToWidth(
int w, Qt::TransformationMode mode)
const
3112 qWarning(
"QImage::scaleWidth: Image is a null image");
3118 Q_TRACE_SCOPE(QImage_scaledToWidth, w, mode);
3120 qreal factor = (qreal) w / width();
3121 QTransform wm = QTransform::fromScale(factor, factor);
3122 return transformed(wm, mode);
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142QImage Q_TRACE_INSTRUMENT(qtgui) QImage::scaledToHeight(
int h, Qt::TransformationMode mode)
const
3145 qWarning(
"QImage::scaleHeight: Image is a null image");
3151 Q_TRACE_SCOPE(QImage_scaledToHeight, h, mode);
3153 qreal factor = (qreal) h / height();
3154 QTransform wm = QTransform::fromScale(factor, factor);
3155 return transformed(wm, mode);
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175QImage Q_TRACE_INSTRUMENT(qtgui) QImage::createAlphaMask(Qt::ImageConversionFlags flags)
const
3177 if (!d || d->format == QImage::Format_RGB32)
3180 if (d->depth == 1) {
3183 return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags);
3186 if (d->format != Format_Indexed8 && d->format != Format_ARGB32
3187 && d->format != Format_ARGB32_Premultiplied) {
3189 return convertToFormat(Format_ARGB32_Premultiplied, flags).createAlphaMask(flags);
3192 QImage mask(d->width, d->height, Format_MonoLSB);
3193 if (!mask.isNull()) {
3194 dither_to_Mono(mask.d, d, flags,
true);
3195 copyPhysicalMetadata(mask.d, d);
3200#ifndef QT_NO_IMAGE_HEURISTIC_MASK
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3225QImage QImage::createHeuristicMask(
bool clipTight)
const
3230 if (d->depth != 32) {
3231 QImage img32 = convertToFormat(Format_RGB32);
3232 return img32.createHeuristicMask(clipTight);
3235#define PIX(x,y) (*((const QRgb*)scanLine(y)+x) & 0x00ffffff
)
3239 QImage m(w, h, Format_MonoLSB);
3242 m.setColor(0, QColor(Qt::color0).rgba());
3243 m.setColor(1, QColor(Qt::color1).rgba());
3246 QRgb background =
PIX(0,0);
3247 if (background !=
PIX(w-1,0) &&
3248 background !=
PIX(0,h-1) &&
3249 background !=
PIX(w-1,h-1)) {
3250 background =
PIX(w-1,0);
3251 if (background !=
PIX(w-1,h-1) &&
3252 background !=
PIX(0,h-1) &&
3253 PIX(0,h-1) ==
PIX(w-1,h-1)) {
3254 background =
PIX(w-1,h-1);
3260 uchar *ypp, *ypc, *ypn;
3263 ypn = m.scanLine(0);
3265 for (y = 0; y < h; y++) {
3268 ypn = (y == h-1) ?
nullptr : m.scanLine(y+1);
3269 const QRgb *p = (
const QRgb *)scanLine(y);
3270 for (x = 0; x < w; x++) {
3273 if ((x == 0 || y == 0 || x == w-1 || y == h-1 ||
3274 !(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) ||
3275 !(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) ||
3276 !(*(ypp + (x >> 3)) & (1 << (x & 7))) ||
3277 !(*(ypn + (x >> 3)) & (1 << (x & 7)))) &&
3278 ( (*(ypc + (x >> 3)) & (1 << (x & 7)))) &&
3279 ((*p & 0x00ffffff) == background)) {
3281 *(ypc + (x >> 3)) &= ~(1 << (x & 7));
3289 ypn = m.scanLine(0);
3291 for (y = 0; y < h; y++) {
3294 ypn = (y == h-1) ?
nullptr : m.scanLine(y+1);
3295 const QRgb *p = (
const QRgb *)scanLine(y);
3296 for (x = 0; x < w; x++) {
3297 if ((*p & 0x00ffffff) != background) {
3299 *(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7));
3301 *(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7));
3303 *(ypp + (x >> 3)) |= (1 << (x & 7));
3305 *(ypn + (x >> 3)) |= (1 << (x & 7));
3314 copyPhysicalMetadata(m.d, d);
3320
3321
3322
3323
3324
3325
3326
3327
3329QImage QImage::createMaskFromColor(QRgb color, Qt::MaskMode mode)
const
3333 QImage maskImage(size(), QImage::Format_MonoLSB);
3336 uchar *s = maskImage.bits();
3340 if (depth() == 32) {
3341 for (
int h = 0; h < d->height; h++) {
3342 const uint *sl = (
const uint *) scanLine(h);
3343 for (
int w = 0; w < d->width; w++) {
3345 *(s + (w >> 3)) |= (1 << (w & 7));
3347 s += maskImage.bytesPerLine();
3350 for (
int h = 0; h < d->height; h++) {
3351 for (
int w = 0; w < d->width; w++) {
3352 if ((uint) pixel(w, h) == color)
3353 *(s + (w >> 3)) |= (1 << (w & 7));
3355 s += maskImage.bytesPerLine();
3358 if (mode == Qt::MaskOutColor)
3359 maskImage.invertPixels();
3361 copyPhysicalMetadata(maskImage.d, d);
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3380
3381
3382
3383
3384
3385
3386
3387
3388
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3404
3405
3406
3407
3408
3409
3410
3411
3413template<
class T>
inline void do_mirror_data(QImageData *dst, QImageData *src,
3414 int dstX0,
int dstY0,
3415 int dstXIncr,
int dstYIncr,
3421 const int srcXEnd = (dstX0 && !dstY0) ? w / 2 : w;
3422 const int srcYEnd = dstY0 ? h / 2 : h;
3423 for (
int srcY = 0, dstY = dstY0; srcY < srcYEnd; ++srcY, dstY += dstYIncr) {
3424 T *srcPtr = (T *) (src->data + srcY * src->bytes_per_line);
3425 T *dstPtr = (T *) (dst->data + dstY * dst->bytes_per_line);
3426 for (
int srcX = 0, dstX = dstX0; srcX < srcXEnd; ++srcX, dstX += dstXIncr)
3427 std::swap(srcPtr[srcX], dstPtr[dstX]);
3430 if (dstX0 && dstY0 && (h & 1)) {
3432 int srcXEnd2 = w / 2;
3433 T *srcPtr = (T *) (src->data + srcY * src->bytes_per_line);
3434 for (
int srcX = 0, dstX = dstX0; srcX < srcXEnd2; ++srcX, dstX += dstXIncr)
3435 std::swap(srcPtr[srcX], srcPtr[dstX]);
3438 for (
int srcY = 0, dstY = dstY0; srcY < h; ++srcY, dstY += dstYIncr) {
3439 T *srcPtr = (T *) (src->data + srcY * src->bytes_per_line);
3440 T *dstPtr = (T *) (dst->data + dstY * dst->bytes_per_line);
3441 for (
int srcX = 0, dstX = dstX0; srcX < w; ++srcX, dstX += dstXIncr)
3442 dstPtr[dstX] = srcPtr[srcX];
3447inline void do_flip(QImageData *dst, QImageData *src,
int w,
int h,
int depth)
3449 const int data_bytes_per_line = w * (depth / 8);
3451 uint *srcPtr =
reinterpret_cast<uint *>(src->data);
3452 uint *dstPtr =
reinterpret_cast<uint *>(dst->data + (h - 1) * dst->bytes_per_line);
3454 const int uint_per_line = (data_bytes_per_line + 3) >> 2;
3455 for (
int y = 0; y < h; ++y) {
3457 for (
int x = 0; x < uint_per_line; x++) {
3458 const uint d = dstPtr[x];
3459 const uint s = srcPtr[x];
3463 srcPtr += src->bytes_per_line >> 2;
3464 dstPtr -= dst->bytes_per_line >> 2;
3468 const uchar *srcPtr = src->data;
3469 uchar *dstPtr = dst->data + (h - 1) * dst->bytes_per_line;
3470 for (
int y = 0; y < h; ++y) {
3471 memcpy(dstPtr, srcPtr, data_bytes_per_line);
3472 srcPtr += src->bytes_per_line;
3473 dstPtr -= dst->bytes_per_line;
3478inline void do_mirror(QImageData *dst, QImageData *src,
bool horizontal,
bool vertical)
3480 Q_ASSERT(src->width == dst->width && src->height == dst->height && src->depth == dst->depth);
3482 int h = src->height;
3483 int depth = src->depth;
3485 if (src->depth == 1) {
3490 if (vertical && !horizontal) {
3492 do_flip(dst, src, w, h, depth);
3496 int dstX0 = 0, dstXIncr = 1;
3497 int dstY0 = 0, dstYIncr = 1;
3511 do_mirror_data<QRgbaFloat32>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h);
3514 do_mirror_data<quint64>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h);
3517 do_mirror_data<quint32>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h);
3520 do_mirror_data<quint24>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h);
3523 do_mirror_data<quint16>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h);
3526 do_mirror_data<quint8>(dst, src, dstX0, dstY0, dstXIncr, dstYIncr, w, h);
3535 if (horizontal && dst->depth == 1) {
3536 Q_ASSERT(dst->format == QImage::Format_Mono || dst->format == QImage::Format_MonoLSB);
3537 const int shift = 8 - (dst->width % 8);
3538 const uchar *bitflip = qt_get_bitflip_array();
3539 for (
int y = 0; y < h; ++y) {
3540 uchar *begin = dst->data + y * dst->bytes_per_line;
3541 uchar *end = begin + dst->bytes_per_line;
3542 for (uchar *p = begin; p < end; ++p) {
3546 if (shift != 8 && p != begin) {
3547 if (dst->format == QImage::Format_Mono) {
3548 for (
int i = 0; i < shift; ++i) {
3550 p[-1] |= (*p & (128 >> i)) >> (7 - i);
3553 for (
int i = 0; i < shift; ++i) {
3555 p[-1] |= (*p & (1 << i)) << (7 - i);
3561 if (dst->format == QImage::Format_Mono)
3571
3572
3573QImage QImage::mirrored_helper(
bool horizontal,
bool vertical)
const
3578 if ((d->width <= 1 && d->height <= 1) || (!horizontal && !vertical))
3582 QImage result(d->width, d->height, d->format);
3589 result.d->colortable = d->colortable;
3590 result.d->has_alpha_clut = d->has_alpha_clut;
3591 copyMetadata(result.d, d);
3593 do_mirror(result.d, d, horizontal, vertical);
3599
3600
3601void QImage::mirrored_inplace(
bool horizontal,
bool vertical)
3603 if (!d || (d->width <= 1 && d->height <= 1) || (!horizontal && !vertical))
3612 do_mirror(d, d, horizontal, vertical);
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3629
3630
3631
3632
3633
3634
3635
3636
3638static inline void rgbSwapped_generic(
int width,
int height,
const QImage *src, QImage *dst,
const QPixelLayout* layout)
3640 const RbSwapFunc func = layout->rbSwap;
3642 qWarning(
"Trying to rb-swap an image format where it doesn't make sense");
3648 for (
int i = 0; i < height; ++i) {
3649 uchar *q = dst->scanLine(i);
3650 const uchar *p = src->constScanLine(i);
3656
3657
3658QImage Q_TRACE_INSTRUMENT(qtgui) QImage::rgbSwapped_helper()
const
3663 Q_TRACE_SCOPE(QImage_rgbSwapped_helper);
3667 switch (d->format) {
3668 case Format_Invalid:
3673 case Format_Grayscale8:
3674 case Format_Grayscale16:
3677 case Format_MonoLSB:
3678 case Format_Indexed8:
3680 for (
int i = 0; i < res.d->colortable.size(); i++) {
3681 QRgb c = res.d->colortable.at(i);
3682 res.d->colortable[i] = QRgb(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00));
3685 case Format_RGBX8888:
3686 case Format_RGBA8888:
3687 case Format_RGBA8888_Premultiplied:
3688#if Q_BYTE_ORDER == Q_BIG_ENDIAN
3689 res = QImage(d->width, d->height, d->format);
3691 for (
int i = 0; i < d->height; i++) {
3692 uint *q = (uint*)res.scanLine(i);
3693 const uint *p = (
const uint*)constScanLine(i);
3694 const uint *end = p + d->width;
3697 *q = ((c << 16) & 0xff000000) | ((c >> 16) & 0xff00) | (c & 0x00ff00ff);
3709 case Format_ARGB32_Premultiplied:
3710 res = QImage(d->width, d->height, d->format);
3712 for (
int i = 0; i < d->height; i++) {
3713 uint *q = (uint*)res.scanLine(i);
3714 const uint *p = (
const uint*)constScanLine(i);
3715 const uint *end = p + d->width;
3718 *q = ((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00);
3725 res = QImage(d->width, d->height, d->format);
3727 for (
int i = 0; i < d->height; i++) {
3728 ushort *q = (ushort*)res.scanLine(i);
3729 const ushort *p = (
const ushort*)constScanLine(i);
3730 const ushort *end = p + d->width;
3733 *q = ((c << 11) & 0xf800) | ((c >> 11) & 0x1f) | (c & 0x07e0);
3740 res = QImage(d->width, d->height, d->format);
3742 rgbSwapped_generic(d->width, d->height,
this, &res, &qPixelLayouts[d->format]);
3745 copyMetadata(res.d, d);
3750
3751
3752void QImage::rgbSwapped_inplace()
3763 switch (d->format) {
3764 case Format_Invalid:
3769 case Format_Grayscale8:
3770 case Format_Grayscale16:
3773 case Format_MonoLSB:
3774 case Format_Indexed8:
3775 for (
int i = 0; i < d->colortable.size(); i++) {
3776 QRgb c = d->colortable.at(i);
3777 d->colortable[i] = QRgb(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00));
3780 case Format_RGBX8888:
3781 case Format_RGBA8888:
3782 case Format_RGBA8888_Premultiplied:
3783#if Q_BYTE_ORDER == Q_BIG_ENDIAN
3784 for (
int i = 0; i < d->height; i++) {
3785 uint *p = (uint*)scanLine(i);
3786 uint *end = p + d->width;
3789 *p = ((c << 16) & 0xff000000) | ((c >> 16) & 0xff00) | (c & 0x00ff00ff);
3800 case Format_ARGB32_Premultiplied:
3801 for (
int i = 0; i < d->height; i++) {
3802 uint *p = (uint*)scanLine(i);
3803 uint *end = p + d->width;
3806 *p = ((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00);
3812 for (
int i = 0; i < d->height; i++) {
3813 ushort *p = (ushort*)scanLine(i);
3814 ushort *end = p + d->width;
3817 *p = ((c << 11) & 0xf800) | ((c >> 11) & 0x1f) | (c & 0x07e0);
3823 case Format_A2BGR30_Premultiplied:
3825 case Format_A2RGB30_Premultiplied:
3826 for (
int i = 0; i < d->height; i++) {
3827 uint *p = (uint*)scanLine(i);
3828 uint *end = p + d->width;
3830 *p = qRgbSwapRgb30(*p);
3836 rgbSwapped_generic(d->width, d->height,
this,
this, &qPixelLayouts[d->format]);
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3861bool QImage::load(
const QString &fileName,
const char* format)
3863 *
this = QImageReader(fileName, format).read();
3868
3869
3870
3871
3872
3874bool QImage::load(QIODevice* device,
const char* format)
3876 *
this = QImageReader(device, format).read();
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3895bool QImage::loadFromData(QByteArrayView data,
const char *format)
3897 *
this = fromData(data, format);
3902
3903
3904
3905
3906
3907
3909bool QImage::loadFromData(
const uchar *buf,
int len,
const char *format)
3911 return loadFromData(QByteArrayView(buf, len), format);
3915
3916
3917
3918
3919
3920
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3937QImage QImage::fromData(QByteArrayView data,
const char *format)
3939 QByteArray a = QByteArray::fromRawData(data.constData(), data.size());
3942 b.open(QIODevice::ReadOnly);
3943 return QImageReader(&b, format).read();
3947
3948
3949
3950
3951
3952
3954QImage QImage::fromData(
const uchar *data,
int size,
const char *format)
3956 return fromData(QByteArrayView(data, size), format);
3960
3961
3962
3963
3964
3965
3966
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984bool QImage::save(
const QString &fileName,
const char *format,
int quality)
const
3988 QImageWriter writer(fileName, format);
3989 return d->doImageIO(
this, &writer, quality);
3993
3994
3995
3996
3997
3998
3999
4000
4001
4003bool QImage::save(QIODevice* device,
const char* format,
int quality)
const
4007 QImageWriter writer(device, format);
4008 return d->doImageIO(
this, &writer, quality);
4012
4014bool QImageData::doImageIO(
const QImage *image, QImageWriter *writer,
int quality)
const
4016 if (quality > 100 || quality < -1)
4017 qWarning(
"QImage::save: Quality out of range [-1, 100]");
4019 writer->setQuality(qMin(quality,100));
4020 const bool result = writer->write(*image);
4023 qWarning(
"QImage::save: failed to write image - %s", qPrintable(writer->errorString()));
4028bool QImageData::isWithinIOAllocationLimit(
int width,
int height, QImage::Format format,
4029 const char *context)
4031 if (
const int mbLimit = QImageReader::allocationLimit()) {
4032 qsizetype depth = qMax(qt_depthForFormat(format), 32);
4033 QImageData::ImageSizeParameters szp =
4034 QImageData::calculateImageParameters(width, height, depth);
4037 const qsizetype mb = szp.totalSize >> 20;
4038 if (mb > mbLimit || (mb == mbLimit && szp.totalSize % (1 << 20))) {
4040 qCWarning(lcImageIo,
"%s: Rejecting image as it exceeds the current "
4041 "allocation limit of %i megabytes", context, mbLimit);
4050
4051
4052#if !defined(QT_NO_DATASTREAM)
4054
4055
4056
4057
4058
4059
4060
4061
4062
4064QDataStream &operator<<(QDataStream &s,
const QImage &image)
4066 if (s.version() >= 5) {
4067 if (image.isNull()) {
4075 QImageWriter writer(s.device(), s.version() == 1 ?
"bmp" :
"png");
4076 writer.write(image);
4081
4082
4083
4084
4085
4086
4087
4088
4090QDataStream &operator>>(QDataStream &s, QImage &image)
4092 if (s.version() >= 5) {
4100 image = QImageReader(s.device(), s.version() == 1 ?
"bmp" :
"png").read();
4101 if (image.isNull() && s.version() >= 5)
4102 s.setStatus(QDataStream::ReadPastEnd);
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4122bool QImage::operator==(
const QImage & i)
const
4131 if (i.d->height != d->height || i.d->width != d->width || i.d->format != d->format || i.d->colorSpace != d->colorSpace)
4134 if (d->format != Format_RGB32) {
4135 if (d->format >= Format_ARGB32) {
4136 const int n = d->width * d->depth / 8;
4137 if (n == d->bytes_per_line && n == i.d->bytes_per_line) {
4138 if (memcmp(bits(), i.bits(), d->nbytes))
4141 for (
int y = 0; y < d->height; ++y) {
4142 if (memcmp(scanLine(y), i.scanLine(y), n))
4147 const int w = width();
4148 const int h = height();
4149 const QList<QRgb> &colortable = d->colortable;
4150 const QList<QRgb> &icolortable = i.d->colortable;
4151 for (
int y=0; y<h; ++y) {
4152 for (
int x=0; x<w; ++x) {
4153 if (colortable[pixelIndex(x, y)] != icolortable[i.pixelIndex(x, y)])
4160 for(
int l = 0; l < d->height; l++) {
4162 const uint *p1 =
reinterpret_cast<
const uint*>(scanLine(l));
4163 const uint *p2 =
reinterpret_cast<
const uint*>(i.scanLine(l));
4165 if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff))
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4187bool QImage::operator!=(
const QImage & i)
const
4189 return !(*
this == i);
4196
4197
4198
4199
4200
4201
4202
4203int QImage::dotsPerMeterX()
const
4205 return d ? qRound(d->dpmx) : 0;
4209
4210
4211
4212
4213
4214
4215
4216int QImage::dotsPerMeterY()
const
4218 return d ? qRound(d->dpmy) : 0;
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233void QImage::setDotsPerMeterX(
int x)
4235 if (!d || !x || d->dpmx == x)
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255void QImage::setDotsPerMeterY(
int y)
4257 if (!d || !y || d->dpmy == y)
4266
4267
4268
4269
4270
4271
4272
4273QPoint QImage::offset()
const
4275 return d ? d->offset : QPoint();
4280
4281
4282
4283
4284
4285
4286
4287void QImage::setOffset(
const QPoint& p)
4289 if (!d || d->offset == p)
4298
4299
4300
4301
4302
4303
4304
4305QStringList QImage::textKeys()
const
4307 return d ? QStringList(d->text.keys()) : QStringList();
4311
4312
4313
4314
4315
4316
4317QString QImage::text(
const QString &key)
const
4323 return d->text.value(key);
4326 for (
auto it = d->text.begin(), end = d->text.end(); it != end; ++it)
4327 tmp += it.key() +
": "_L1 + it.value().simplified() +
"\n\n"_L1;
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357void QImage::setText(
const QString &key,
const QString &value)
4364 d->text.insert(key, value);
4368
4369
4370
4371
4372QPaintEngine *QImage::paintEngine()
const
4377 if (!d->paintEngine) {
4378 QPaintDevice *paintDevice =
const_cast<QImage *>(
this);
4379 QPlatformIntegration *platformIntegration = QGuiApplicationPrivate::platformIntegration();
4380 if (platformIntegration)
4381 d->paintEngine = platformIntegration->createImagePaintEngine(paintDevice);
4382 if (!d->paintEngine)
4383 d->paintEngine =
new QRasterPaintEngine(paintDevice);
4386 return d->paintEngine;
4391
4392
4393
4394
4395int QImage::metric(PaintDeviceMetric metric)
const
4408 return qRound(d->width * 1000 / d->dpmx);
4411 return qRound(d->height * 1000 / d->dpmy);
4414 return d->colortable.size();
4420 return qRound(d->dpmx * 0.0254);
4424 return qRound(d->dpmy * 0.0254);
4427 case PdmPhysicalDpiX:
4428 return qRound(d->dpmx * 0.0254);
4431 case PdmPhysicalDpiY:
4432 return qRound(d->dpmy * 0.0254);
4435 case PdmDevicePixelRatio:
4436 return d->devicePixelRatio;
4439 case PdmDevicePixelRatioScaled:
4440 return d->devicePixelRatio * QPaintDevice::devicePixelRatioFScale();
4443 case PdmDevicePixelRatioF_EncodedA:
4445 case PdmDevicePixelRatioF_EncodedB:
4446 return QPaintDevice::encodeMetricF(metric, d->devicePixelRatio);
4450 qWarning(
"QImage::metric(): Unhandled metric type %d", metric);
4459
4460
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4482#define IWX_MSB(b) if (trigx < maxws && trigy < maxhs) {
4483 if (*(sptr+sbpl*(trigy>>12
)+(trigx>>15
)) &
4484 (1
<< (7
-((trigx>>12
)&7
))))
4491#define IWX_LSB(b) if (trigx < maxws && trigy < maxhs) {
4492 if (*(sptr+sbpl*(trigy>>12
)+(trigx>>15
)) &
4493 (1
<< ((trigx>>12
)&7
)))
4500#define IWX_PIX(b) if (trigx < maxws && trigy < maxhs) {
4501 if ((*(sptr+sbpl*(trigy>>12
)+(trigx>>15
)) &
4502 (1
<< (7
-((trigx>>12
)&7
)))) == 0
)
4509bool qt_xForm_helper(
const QTransform &trueMat,
int xoffset,
int type,
int depth,
4510 uchar *dptr, qsizetype dbpl,
int p_inc,
int dHeight,
4511 const uchar *sptr, qsizetype sbpl,
int sWidth,
int sHeight)
4513 int m11 =
int(trueMat.m11()*4096.0);
4514 int m12 =
int(trueMat.m12()*4096.0);
4515 int m21 =
int(trueMat.m21()*4096.0);
4516 int m22 =
int(trueMat.m22()*4096.0);
4517 int dx = qRound(trueMat.dx()*4096.0);
4518 int dy = qRound(trueMat.dy()*4096.0);
4520 int m21ydx = dx + (xoffset<<16) + (m11 + m21) / 2;
4521 int m22ydy = dy + (m12 + m22) / 2;
4524 uint maxws = sWidth<<12;
4525 uint maxhs = sHeight<<12;
4527 for (
int y=0; y<dHeight; y++) {
4530 uchar *maxp = dptr + dbpl;
4534 while (dptr < maxp) {
4535 if (trigx < maxws && trigy < maxhs)
4536 *dptr = *(sptr+sbpl*(trigy>>12)+(trigx>>12));
4544 while (dptr < maxp) {
4545 if (trigx < maxws && trigy < maxhs)
4546 *((ushort*)dptr) = *((
const ushort *)(sptr+sbpl*(trigy>>12) +
4556 while (dptr < maxp) {
4557 if (trigx < maxws && trigy < maxhs) {
4558 const uchar *p2 = sptr+sbpl*(trigy>>12) + ((trigx>>12)*3);
4570 while (dptr < maxp) {
4571 if (trigx < maxws && trigy < maxhs)
4572 *((uint*)dptr) = *((
const uint *)(sptr+sbpl*(trigy>>12) +
4587 while (dptr < maxp) {
4600 while (dptr < maxp) {
4625
4626
4627
4628
4629
4630
4631qint64 QImage::cacheKey()
const
4636 return (((qint64) d->ser_no) << 32) | ((qint64) d->detach_no);
4640
4641
4642
4643
4644
4645
4647bool QImage::isDetached()
const
4649 return d && d->ref.loadRelaxed() == 1;
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4672void QImage::setAlphaChannel(
const QImage &alphaChannel)
4674 if (!d || alphaChannel.isNull())
4677 if (d->paintEngine && d->paintEngine->isActive()) {
4678 qWarning(
"QImage::setAlphaChannel: "
4679 "Unable to set alpha channel while image is being painted on");
4683 const Format alphaFormat = qt_alphaVersionForPainting(d->format);
4684 if (d->format == alphaFormat)
4687 convertTo(alphaFormat);
4693 if (alphaChannel.format() == QImage::Format_Alpha8 || (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()))
4694 sourceImage = alphaChannel;
4696 sourceImage = alphaChannel.convertToFormat(QImage::Format_Grayscale8);
4697 if (!sourceImage.reinterpretAsFormat(QImage::Format_Alpha8))
4700 QPainter painter(
this);
4701 if (sourceImage.size() != size())
4702 painter.setRenderHint(QPainter::SmoothPixmapTransform);
4703 painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
4704 painter.drawImage(rect(), sourceImage);
4708
4709
4710
4711
4712
4713bool QImage::hasAlphaChannel()
const
4717 const QPixelFormat format = pixelFormat();
4718 if (format.alphaUsage() == QPixelFormat::UsesAlpha)
4720 if (format.colorModel() == QPixelFormat::Indexed)
4721 return d->has_alpha_clut;
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735int QImage::bitPlaneCount()
const
4740 switch (d->format) {
4741 case QImage::Format_Invalid:
4743 case QImage::Format_BGR30:
4744 case QImage::Format_RGB30:
4747 case QImage::Format_RGB32:
4748 case QImage::Format_RGBX8888:
4751 case QImage::Format_RGB666:
4754 case QImage::Format_RGB555:
4757 case QImage::Format_ARGB8555_Premultiplied:
4760 case QImage::Format_RGB444:
4763 case QImage::Format_RGBX64:
4764 case QImage::Format_RGBX16FPx4:
4767 case QImage::Format_RGBX32FPx4:
4771 bpc = qt_depthForFormat(d->format);
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788QImage QImage::smoothScaled(
int w,
int h)
const
4791 switch (src.format()) {
4792 case QImage::Format_RGB32:
4793 case QImage::Format_ARGB32_Premultiplied:
4794#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
4795 case QImage::Format_RGBX8888:
4797 case QImage::Format_RGBA8888_Premultiplied:
4798#if QT_CONFIG(raster_64bit)
4799 case QImage::Format_RGBX64:
4800 case QImage::Format_RGBA64_Premultiplied:
4802 case QImage::Format_RGBA64:
4803 case QImage::Format_Grayscale16:
4804 src.convertTo(QImage::Format_RGBA64_Premultiplied);
4807#if QT_CONFIG(raster_fp)
4808 case QImage::Format_RGBX32FPx4:
4809 case QImage::Format_RGBA32FPx4_Premultiplied:
4811 case QImage::Format_RGBX16FPx4:
4812 src.convertTo(QImage::Format_RGBX32FPx4);
4814 case QImage::Format_RGBA16FPx4:
4815 case QImage::Format_RGBA16FPx4_Premultiplied:
4816 case QImage::Format_RGBA32FPx4:
4817 src.convertTo(QImage::Format_RGBA32FPx4_Premultiplied);
4820 case QImage::Format_CMYK8888:
4823 if (src.hasAlphaChannel())
4824 src.convertTo(QImage::Format_ARGB32_Premultiplied);
4826 src.convertTo(QImage::Format_RGB32);
4828 src = qSmoothScaleImage(src, w, h);
4830 copyMetadata(src.d, d);
4834static QImage rotated90(
const QImage &image)
4836 QImage out(image.height(), image.width(), image.format());
4839 copyMetadata(QImageData::get(out), QImageData::get(image));
4840 if (image.colorCount() > 0)
4841 out.setColorTable(image.colorTable());
4842 int w = image.width();
4843 int h = image.height();
4844 const MemRotateFunc memrotate = qMemRotateFunctions[qPixelLayouts[image.format()].bpp][2];
4846 memrotate(image.constBits(), w, h, image.bytesPerLine(), out.bits(), out.bytesPerLine());
4848 for (
int y=0; y<h; ++y) {
4849 if (image.colorCount())
4850 for (
int x=0; x<w; ++x)
4851 out.setPixel(h-y-1, x, image.pixelIndex(x, y));
4853 for (
int x=0; x<w; ++x)
4854 out.setPixel(h-y-1, x, image.pixel(x, y));
4860static QImage rotated180(
const QImage &image)
4862 const MemRotateFunc memrotate = qMemRotateFunctions[qPixelLayouts[image.format()].bpp][1];
4864 return image.flipped(Qt::Horizontal | Qt::Vertical);
4866 QImage out(image.width(), image.height(), image.format());
4869 copyMetadata(QImageData::get(out), QImageData::get(image));
4870 if (image.colorCount() > 0)
4871 out.setColorTable(image.colorTable());
4872 int w = image.width();
4873 int h = image.height();
4874 memrotate(image.constBits(), w, h, image.bytesPerLine(), out.bits(), out.bytesPerLine());
4878static QImage rotated270(
const QImage &image)
4880 QImage out(image.height(), image.width(), image.format());
4883 copyMetadata(QImageData::get(out), QImageData::get(image));
4884 if (image.colorCount() > 0)
4885 out.setColorTable(image.colorTable());
4886 int w = image.width();
4887 int h = image.height();
4888 const MemRotateFunc memrotate = qMemRotateFunctions[qPixelLayouts[image.format()].bpp][0];
4890 memrotate(image.constBits(), w, h, image.bytesPerLine(), out.bits(), out.bytesPerLine());
4892 for (
int y=0; y<h; ++y) {
4893 if (image.colorCount())
4894 for (
int x=0; x<w; ++x)
4895 out.setPixel(y, w-x-1, image.pixelIndex(x, y));
4897 for (
int x=0; x<w; ++x)
4898 out.setPixel(y, w-x-1, image.pixel(x, y));
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4928QImage Q_TRACE_INSTRUMENT(qtgui) QImage::transformed(
const QTransform &matrix, Qt::TransformationMode mode )
const
4933 Q_TRACE_PARAM_REPLACE(
const QTransform &,
double[9]);
4934 Q_TRACE_SCOPE(QImage_transformed, QList<
double>({matrix.m11(), matrix.m12(), matrix.m13(),
4935 matrix.m21(), matrix.m22(), matrix.m23(),
4936 matrix.m31(), matrix.m32(), matrix.m33()}).data(), mode);
4939 const int ws = width();
4940 const int hs = height();
4947 QTransform mat = trueMatrix(matrix, ws, hs);
4948 bool complex_xform =
false;
4949 bool scale_xform =
false;
4950 bool nonpaintable_scale_xform =
false;
4951 if (mat.type() <= QTransform::TxScale) {
4952 if (mat.type() == QTransform::TxNone)
4954 else if (mat.m11() == -1. && mat.m22() == -1.)
4955 return rotated180(*
this);
4957 hdf = qAbs(mat.m22()) * hs;
4958 wdf = qAbs(mat.m11()) * ws;
4961 if (mat.type() <= QTransform::TxRotate && mat.m11() == 0 && mat.m22() == 0) {
4962 if (mat.m12() == 1. && mat.m21() == -1.)
4963 return rotated90(*
this);
4964 else if (mat.m12() == -1. && mat.m21() == 1.)
4965 return rotated270(*
this);
4968 QPolygonF a(QRectF(0, 0, ws, hs));
4970 const QRectF r = a.boundingRect();
4971 wdf = std::ceil(r.right()) - std::floor(r.left());
4972 hdf = std::ceil(r.bottom()) - std::floor(r.top());
4973 complex_xform =
true;
4976 if (!(wdf < INT_MAX && hdf < INT_MAX))
4978 const int wd = qRound(wdf);
4979 const int hd = qRound(hdf);
4984 if (qint64(hd) * 2 < hs || qint64(wd) * 2 < ws)
4985 nonpaintable_scale_xform =
true;
4987 if (format() == QImage::Format_CMYK8888)
4988 nonpaintable_scale_xform =
true;
4991 if (wd == 0 || hd == 0)
4994 if (scale_xform && mode == Qt::SmoothTransformation) {
4996 case QImage::Format_RGB32:
4997 case QImage::Format_ARGB32_Premultiplied:
4998#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
4999 case QImage::Format_RGBX8888:
5001 case QImage::Format_RGBA8888_Premultiplied:
5002#if QT_CONFIG(raster_64bit)
5003 case QImage::Format_RGBX64:
5004 case QImage::Format_RGBA64_Premultiplied:
5006 case QImage::Format_CMYK8888:
5008 if (mat.m11() > 0.0F && mat.m22() > 0.0F)
5009 return smoothScaled(wd, hd);
5015 if (nonpaintable_scale_xform
5016#if QT_CONFIG(qtgui_threadpool)
5017 || qint64(ws) * hs >= (1 << 20)
5021 if (mat.m11() < 0.0F && mat.m22() < 0.0F) {
5022 scaledImage = smoothScaled(wd, hd).flipped(Qt::Horizontal | Qt::Vertical);
5023 }
else if (mat.m11() < 0.0F) {
5024 scaledImage = smoothScaled(wd, hd).flipped(Qt::Horizontal);
5025 }
else if (mat.m22() < 0.0F) {
5026 scaledImage = smoothScaled(wd, hd).flipped(Qt::Vertical);
5028 scaledImage = smoothScaled(wd, hd);
5032 case QImage::Format_Mono:
5033 case QImage::Format_MonoLSB:
5034 case QImage::Format_Indexed8:
5037 return scaledImage.convertToFormat(format());
5044 qsizetype sbpl = bytesPerLine();
5045 const uchar *sptr = bits();
5047 QImage::Format target_format = d->format;
5049 if (complex_xform || mode == Qt::SmoothTransformation) {
5050 if (d->format < QImage::Format_RGB32 || (!hasAlphaChannel() && complex_xform)) {
5051 target_format = qt_alphaVersion(d->format);
5055 QImage dImage(wd, hd, target_format);
5058 if (target_format == QImage::Format_MonoLSB
5059 || target_format == QImage::Format_Mono
5060 || target_format == QImage::Format_Indexed8) {
5061 dImage.d->colortable = d->colortable;
5062 dImage.d->has_alpha_clut = d->has_alpha_clut | complex_xform;
5066 if (target_format == QImage::Format_Indexed8) {
5067 if (dImage.d->colortable.size() < 256) {
5069 dImage.d->colortable.append(0x0);
5070 memset(dImage.bits(), dImage.d->colortable.size() - 1, dImage.d->nbytes);
5072 memset(dImage.bits(), 0, dImage.d->nbytes);
5075 memset(dImage.bits(), 0x00, dImage.d->nbytes);
5077 if (target_format >= QImage::Format_RGB32 && target_format != QImage::Format_CMYK8888) {
5079 QImage sImage = (devicePixelRatio() != 1) ? QImage(constBits(), width(), height(), format()) : *
this;
5081 && (d->format == QImage::Format_MonoLSB
5082 || d->format == QImage::Format_Mono
5083 || d->format == QImage::Format_Indexed8)) {
5084 sImage.d->colortable = d->colortable;
5085 sImage.d->has_alpha_clut = d->has_alpha_clut;
5088 Q_ASSERT(sImage.devicePixelRatio() == 1);
5089 Q_ASSERT(sImage.devicePixelRatio() == dImage.devicePixelRatio());
5091 QPainter p(&dImage);
5092 if (mode == Qt::SmoothTransformation) {
5093 p.setRenderHint(QPainter::Antialiasing);
5094 p.setRenderHint(QPainter::SmoothPixmapTransform);
5096 p.setTransform(mat);
5097 p.drawImage(QPoint(0, 0), sImage);
5100 mat = mat.inverted(&invertible);
5106 qsizetype dbpl = dImage.bytesPerLine();
5107 qt_xForm_helper(mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, ws, hs);
5109 copyMetadata(dImage.d, d);
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5135QTransform QImage::trueMatrix(
const QTransform &matrix,
int w,
int h)
5137 const QRectF mapped = matrix.mapRect(QRectF(0, 0, w, h));
5139 return matrix * QTransform().translate(-std::floor(mapped.left()), -std::floor(mapped.top()));
5143
5144
5145
5146
5147
5148
5149void QImage::setColorSpace(
const QColorSpace &colorSpace)
5153 if (d->colorSpace == colorSpace)
5155 if (colorSpace.isValid() && !qt_compatibleColorModelSource(pixelFormat().colorModel(), colorSpace.colorModel()))
5158 detachMetadata(
false);
5160 d->colorSpace = colorSpace;
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175void QImage::convertToColorSpace(
const QColorSpace &colorSpace)
5177 if (!d || !d->colorSpace.isValid())
5179 if (!colorSpace.isValidTarget()) {
5180 qWarning() <<
"QImage::convertToColorSpace: Output colorspace is not valid";
5183 if (d->colorSpace == colorSpace)
5185 if (!qt_compatibleColorModelTarget(pixelFormat().colorModel(),
5186 colorSpace.colorModel(), colorSpace.transformModel())) {
5187 *
this = convertedToColorSpace(colorSpace);
5190 applyColorTransform(d->colorSpace.transformationToColorSpace(colorSpace));
5191 if (d->ref.loadRelaxed() != 1)
5192 detachMetadata(
false);
5193 d->colorSpace = colorSpace;
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209void QImage::convertToColorSpace(
const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags)
5211 if (!d || !d->colorSpace.isValid())
5213 if (!colorSpace.isValidTarget()) {
5214 qWarning() <<
"QImage::convertToColorSpace: Output colorspace is not valid";
5217 if (!qt_compatibleColorModelTarget(toPixelFormat(format).colorModel(),
5218 colorSpace.colorModel(), colorSpace.transformModel())) {
5219 qWarning() <<
"QImage::convertToColorSpace: Color space is not compatible with format";
5223 if (d->colorSpace == colorSpace)
5224 return convertTo(format, flags);
5225 applyColorTransform(d->colorSpace.transformationToColorSpace(colorSpace), format, flags);
5226 d->colorSpace = colorSpace;
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243QImage QImage::convertedToColorSpace(
const QColorSpace &colorSpace)
const
5245 if (!d || !d->colorSpace.isValid())
5247 if (!colorSpace.isValidTarget()) {
5248 qWarning() <<
"QImage::convertedToColorSpace: Output colorspace is not valid";
5251 if (d->colorSpace == colorSpace)
5253 QImage image = colorTransformed(d->colorSpace.transformationToColorSpace(colorSpace));
5254 image.setColorSpace(colorSpace);
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272QImage QImage::convertedToColorSpace(
const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags)
const &
5274 if (!d || !d->colorSpace.isValid())
5276 if (!colorSpace.isValidTarget()) {
5277 qWarning() <<
"QImage::convertedToColorSpace: Output colorspace is not valid";
5280 if (!qt_compatibleColorModelTarget(toPixelFormat(format).colorModel(),
5281 colorSpace.colorModel(), colorSpace.transformModel())) {
5282 qWarning() <<
"QImage::convertedToColorSpace: Color space is not compatible with format";
5285 if (d->colorSpace == colorSpace)
5286 return convertedTo(format, flags);
5287 QImage image = colorTransformed(d->colorSpace.transformationToColorSpace(colorSpace), format, flags);
5288 image.setColorSpace(colorSpace);
5292QImage QImage::convertedToColorSpace(
const QColorSpace &colorSpace, QImage::Format format, Qt::ImageConversionFlags flags) &&
5294 if (!d || !d->colorSpace.isValid())
5296 if (!colorSpace.isValidTarget()) {
5297 qWarning() <<
"QImage::convertedToColorSpace: Output colorspace is not valid";
5300 if (!qt_compatibleColorModelTarget(toPixelFormat(format).colorModel(),
5301 colorSpace.colorModel(), colorSpace.transformModel())) {
5302 qWarning() <<
"QImage::convertedToColorSpace: Color space is not compatible with format";
5305 if (d->colorSpace == colorSpace)
5306 return convertedTo(format, flags);
5307 applyColorTransform(d->colorSpace.transformationToColorSpace(colorSpace), format, flags);
5308 return std::move(*
this);
5312
5313
5314
5315
5316QColorSpace QImage::colorSpace()
const
5319 return QColorSpace();
5320 return d->colorSpace;
5324
5325
5326
5327
5328void QImage::applyColorTransform(
const QColorTransform &transform)
5330 if (transform.isIdentity())
5333 if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), QColorTransformPrivate::get(transform)->colorSpaceIn->colorModel) ||
5334 !qt_compatibleColorModelTarget(pixelFormat().colorModel(), QColorTransformPrivate::get(transform)->colorSpaceOut->colorModel,
5335 QColorTransformPrivate::get(transform)->colorSpaceOut->transformModel)) {
5336 qWarning() <<
"QImage::applyColorTransform can not apply format switching transform without switching format";
5343 if (pixelFormat().colorModel() == QPixelFormat::Indexed) {
5344 for (
int i = 0; i < d->colortable.size(); ++i)
5345 d->colortable[i] = transform.map(d->colortable[i]);
5348 QImage::Format oldFormat = format();
5349 if (qt_fpColorPrecision(oldFormat)) {
5350 if (oldFormat != QImage::Format_RGBX32FPx4 && oldFormat != QImage::Format_RGBA32FPx4
5351 && oldFormat != QImage::Format_RGBA32FPx4_Premultiplied)
5352 convertTo(QImage::Format_RGBA32FPx4);
5353 }
else if (depth() > 32) {
5354 if (oldFormat != QImage::Format_RGBX64 && oldFormat != QImage::Format_RGBA64
5355 && oldFormat != QImage::Format_RGBA64_Premultiplied)
5356 convertTo(QImage::Format_RGBA64);
5357 }
else if (oldFormat != QImage::Format_ARGB32 && oldFormat != QImage::Format_RGB32
5358 && oldFormat != QImage::Format_ARGB32_Premultiplied && oldFormat != QImage::Format_CMYK8888
5359 && oldFormat != QImage::Format_Grayscale8 && oldFormat != QImage::Format_Grayscale16) {
5360 if (hasAlphaChannel())
5361 convertTo(QImage::Format_ARGB32);
5363 convertTo(QImage::Format_RGB32);
5366 QColorTransformPrivate::TransformFlags flags = QColorTransformPrivate::Unpremultiplied;
5368 case Format_ARGB32_Premultiplied:
5369 case Format_RGBA64_Premultiplied:
5370 case Format_RGBA32FPx4_Premultiplied:
5371 flags = QColorTransformPrivate::Premultiplied;
5373 case Format_Grayscale8:
5374 case Format_Grayscale16:
5376 case Format_CMYK8888:
5378 case Format_RGBX32FPx4:
5379 flags = QColorTransformPrivate::InputOpaque;
5383 case Format_RGBA32FPx4:
5389 std::function<
void(
int,
int)> transformSegment;
5391 if (format() == Format_Grayscale8) {
5392 transformSegment = [&](
int yStart,
int yEnd) {
5393 for (
int y = yStart; y < yEnd; ++y) {
5394 uint8_t *scanline =
reinterpret_cast<uint8_t *>(d->data + y * d->bytes_per_line);
5395 QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
5398 }
else if (format() == Format_Grayscale16) {
5399 transformSegment = [&](
int yStart,
int yEnd) {
5400 for (
int y = yStart; y < yEnd; ++y) {
5401 uint16_t *scanline =
reinterpret_cast<uint16_t *>(d->data + y * d->bytes_per_line);
5402 QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
5405 }
else if (qt_fpColorPrecision(format())) {
5406 transformSegment = [&](
int yStart,
int yEnd) {
5407 for (
int y = yStart; y < yEnd; ++y) {
5408 QRgbaFloat32 *scanline =
reinterpret_cast<QRgbaFloat32 *>(d->data + y * d->bytes_per_line);
5409 QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
5412 }
else if (depth() > 32) {
5413 transformSegment = [&](
int yStart,
int yEnd) {
5414 for (
int y = yStart; y < yEnd; ++y) {
5415 QRgba64 *scanline =
reinterpret_cast<QRgba64 *>(d->data + y * d->bytes_per_line);
5416 QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
5419 }
else if (oldFormat == QImage::Format_CMYK8888) {
5420 transformSegment = [&](
int yStart,
int yEnd) {
5421 for (
int y = yStart; y < yEnd; ++y) {
5422 QCmyk32 *scanline =
reinterpret_cast<QCmyk32 *>(d->data + y * d->bytes_per_line);
5423 QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
5427 transformSegment = [&](
int yStart,
int yEnd) {
5428 for (
int y = yStart; y < yEnd; ++y) {
5429 QRgb *scanline =
reinterpret_cast<QRgb *>(d->data + y * d->bytes_per_line);
5430 QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
5435#if QT_CONFIG(qtgui_threadpool)
5436 int segments = (qsizetype(width()) * height()) >> 16;
5437 segments = std::min(segments, height());
5438 QThreadPool *threadPool = QGuiApplicationPrivate::qtGuiThreadPool();
5439 if (segments > 1 && threadPool && !threadPool->contains(QThread::currentThread())) {
5440 QLatch latch(segments);
5442 for (
int i = 0; i < segments; ++i) {
5443 int yn = (height() - y) / (segments - i);
5444 threadPool->start([&, y, yn]() {
5445 transformSegment(y, y + yn);
5453 transformSegment(0, height());
5455 if (oldFormat != format())
5456 *
this = std::move(*
this).convertToFormat(oldFormat);
5460
5461
5462
5463
5464
5465
5466
5467void QImage::applyColorTransform(
const QColorTransform &transform, QImage::Format toFormat, Qt::ImageConversionFlags flags)
5471 if (transform.isIdentity())
5472 return convertTo(toFormat, flags);
5474 *
this = colorTransformed(transform, toFormat, flags);
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489QImage QImage::colorTransformed(
const QColorTransform &transform)
const &
5493 if (transform.isIdentity())
5496 const QColorSpacePrivate *inColorSpace = QColorTransformPrivate::get(transform)->colorSpaceIn.constData();
5497 const QColorSpacePrivate *outColorSpace = QColorTransformPrivate::get(transform)->colorSpaceOut.constData();
5498 if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), inColorSpace->colorModel)) {
5499 qWarning() <<
"QImage::colorTransformed: Invalid input color space for transform";
5502 if (!qt_compatibleColorModelTarget(pixelFormat().colorModel(), outColorSpace->colorModel, outColorSpace->transformModel)) {
5504 switch (outColorSpace->colorModel) {
5505 case QColorSpace::ColorModel::Rgb:
5506 return colorTransformed(transform, qt_highColorPrecision(format(),
true) ? QImage::Format_RGBX64 : QImage::Format_RGB32);
5507 case QColorSpace::ColorModel::Gray:
5508 return colorTransformed(transform, qt_highColorPrecision(format(),
true) ? QImage::Format_Grayscale16 : QImage::Format_Grayscale8);
5509 case QColorSpace::ColorModel::Cmyk:
5510 return colorTransformed(transform, QImage::Format_CMYK8888);
5511 case QColorSpace::ColorModel::Undefined:
5517 QImage image = copy();
5518 image.applyColorTransform(transform);
5522static bool isRgb32Data(QImage::Format f)
5525 case QImage::Format_RGB32:
5526 case QImage::Format_ARGB32:
5527 case QImage::Format_ARGB32_Premultiplied:
5535static bool isRgb64Data(QImage::Format f)
5538 case QImage::Format_RGBX64:
5539 case QImage::Format_RGBA64:
5540 case QImage::Format_RGBA64_Premultiplied:
5548static bool isRgb32fpx4Data(QImage::Format f)
5551 case QImage::Format_RGBX32FPx4:
5552 case QImage::Format_RGBA32FPx4:
5553 case QImage::Format_RGBA32FPx4_Premultiplied:
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574QImage QImage::colorTransformed(
const QColorTransform &transform, QImage::Format toFormat, Qt::ImageConversionFlags flags)
const &
5578 if (toFormat == QImage::Format_Invalid)
5579 toFormat = format();
5580 if (transform.isIdentity())
5581 return convertedTo(toFormat, flags);
5583 const QColorSpacePrivate *inColorSpace = QColorTransformPrivate::get(transform)->colorSpaceIn.constData();
5584 const QColorSpacePrivate *outColorSpace = QColorTransformPrivate::get(transform)->colorSpaceOut.constData();
5585 if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), inColorSpace->colorModel)) {
5586 qWarning() <<
"QImage::colorTransformed: Invalid input color space for transform";
5589 if (!qt_compatibleColorModelTarget(toPixelFormat(toFormat).colorModel(), outColorSpace->colorModel, outColorSpace->transformModel)) {
5590 qWarning() <<
"QImage::colorTransformed: Invalid output color space for transform";
5594 QImage fromImage = *
this;
5596 QImage::Format tmpFormat = toFormat;
5598 case QImage::Format_RGB32:
5599 case QImage::Format_ARGB32:
5600 case QImage::Format_ARGB32_Premultiplied:
5601 case QImage::Format_RGBX32FPx4:
5602 case QImage::Format_RGBA32FPx4:
5603 case QImage::Format_RGBA32FPx4_Premultiplied:
5604 case QImage::Format_RGBX64:
5605 case QImage::Format_RGBA64:
5606 case QImage::Format_RGBA64_Premultiplied:
5607 case QImage::Format_Grayscale8:
5608 case QImage::Format_Grayscale16:
5609 case QImage::Format_CMYK8888:
5612 case QImage::Format_RGB16:
5613 case QImage::Format_RGB444:
5614 case QImage::Format_RGB555:
5615 case QImage::Format_RGB666:
5616 case QImage::Format_RGB888:
5617 case QImage::Format_BGR888:
5618 case QImage::Format_RGBX8888:
5619 tmpFormat = QImage::Format_RGB32;
5621 case QImage::Format_Mono:
5622 case QImage::Format_MonoLSB:
5623 case QImage::Format_Indexed8:
5624 case QImage::Format_ARGB8565_Premultiplied:
5625 case QImage::Format_ARGB6666_Premultiplied:
5626 case QImage::Format_ARGB8555_Premultiplied:
5627 case QImage::Format_ARGB4444_Premultiplied:
5628 case QImage::Format_RGBA8888:
5629 case QImage::Format_RGBA8888_Premultiplied:
5630 tmpFormat = QImage::Format_ARGB32;
5632 case QImage::Format_BGR30:
5633 case QImage::Format_RGB30:
5634 tmpFormat = QImage::Format_RGBX64;
5636 case QImage::Format_A2BGR30_Premultiplied:
5637 case QImage::Format_A2RGB30_Premultiplied:
5638 tmpFormat = QImage::Format_RGBA64;
5640 case QImage::Format_RGBX16FPx4:
5641 case QImage::Format_RGBA16FPx4:
5642 case QImage::Format_RGBA16FPx4_Premultiplied:
5643 tmpFormat = QImage::Format_RGBA32FPx4;
5645 case QImage::Format_Alpha8:
5646 return convertedTo(QImage::Format_Alpha8);
5647 case QImage::Format_Invalid:
5648 case QImage::NImageFormats:
5652 QColorSpace::ColorModel inColorData = qt_csColorData(pixelFormat().colorModel());
5653 QColorSpace::ColorModel outColorData = qt_csColorData(toPixelFormat(toFormat).colorModel());
5655 if (inColorData != outColorData) {
5656 if (fromImage.format() == QImage::Format_Grayscale8 && outColorData == QColorSpace::ColorModel::Rgb)
5657 tmpFormat = QImage::Format_RGB32;
5658 else if (tmpFormat == QImage::Format_Grayscale8 && qt_highColorPrecision(fromImage.format()))
5659 tmpFormat = QImage::Format_Grayscale16;
5660 else if (fromImage.format() == QImage::Format_Grayscale16 && outColorData == QColorSpace::ColorModel::Rgb)
5661 tmpFormat = QImage::Format_RGBX64;
5663 if (tmpFormat == QImage::Format_Grayscale8 && fromImage.format() == QImage::Format_Grayscale16)
5664 tmpFormat = QImage::Format_Grayscale16;
5665 else if (qt_fpColorPrecision(fromImage.format()) && !qt_fpColorPrecision(tmpFormat))
5666 tmpFormat = QImage::Format_RGBA32FPx4;
5667 else if (isRgb32Data(tmpFormat) && qt_highColorPrecision(fromImage.format(),
true))
5668 tmpFormat = QImage::Format_RGBA64;
5671 QImage toImage(size(), tmpFormat);
5672 copyMetadata(&toImage, *
this);
5674 std::function<
void(
int,
int)> transformSegment;
5675 QColorTransformPrivate::TransformFlags transFlags = QColorTransformPrivate::Unpremultiplied;
5677 if (inColorData != outColorData) {
5679 if (inColorData == QColorSpace::ColorModel::Gray && outColorData == QColorSpace::ColorModel::Rgb) {
5681 if (format() == QImage::Format_Grayscale8) {
5682 transformSegment = [&](
int yStart,
int yEnd) {
5683 for (
int y = yStart; y < yEnd; ++y) {
5684 const quint8 *in_scanline =
reinterpret_cast<
const quint8 *>(d->data + y * d->bytes_per_line);
5685 QRgb *out_scanline =
reinterpret_cast<QRgb *>(toImage.d->data + y * toImage.bytesPerLine());
5686 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5690 transformSegment = [&](
int yStart,
int yEnd) {
5691 for (
int y = yStart; y < yEnd; ++y) {
5692 const quint16 *in_scanline =
reinterpret_cast<
const quint16 *>(d->data + y * d->bytes_per_line);
5693 QRgba64 *out_scanline =
reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine());
5694 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5698 }
else if (inColorData == QColorSpace::ColorModel::Gray && outColorData == QColorSpace::ColorModel::Cmyk) {
5700 if (format() == QImage::Format_Grayscale8) {
5701 transformSegment = [&](
int yStart,
int yEnd) {
5702 for (
int y = yStart; y < yEnd; ++y) {
5703 const quint8 *in_scanline =
reinterpret_cast<
const quint8 *>(d->data + y * d->bytes_per_line);
5704 QCmyk32 *out_scanline =
reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine());
5705 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5709 transformSegment = [&](
int yStart,
int yEnd) {
5710 for (
int y = yStart; y < yEnd; ++y) {
5711 const quint16 *in_scanline =
reinterpret_cast<
const quint16 *>(d->data + y * d->bytes_per_line);
5712 QCmyk32 *out_scanline =
reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine());
5713 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5717 }
else if (inColorData == QColorSpace::ColorModel::Rgb && outColorData == QColorSpace::ColorModel::Gray) {
5719 if (tmpFormat == QImage::Format_Grayscale8) {
5720 fromImage.convertTo(QImage::Format_RGB32);
5721 transformSegment = [&](
int yStart,
int yEnd) {
5722 for (
int y = yStart; y < yEnd; ++y) {
5723 const QRgb *in_scanline =
reinterpret_cast<
const QRgb *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5724 quint8 *out_scanline =
reinterpret_cast<quint8 *>(toImage.d->data + y * toImage.bytesPerLine());
5725 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5729 fromImage.convertTo(QImage::Format_RGBX64);
5730 transformSegment = [&](
int yStart,
int yEnd) {
5731 for (
int y = yStart; y < yEnd; ++y) {
5732 const QRgba64 *in_scanline =
reinterpret_cast<
const QRgba64 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5733 quint16 *out_scanline =
reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine());
5734 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5738 }
else if (inColorData == QColorSpace::ColorModel::Cmyk && outColorData == QColorSpace::ColorModel::Gray) {
5740 if (tmpFormat == QImage::Format_Grayscale8) {
5741 transformSegment = [&](
int yStart,
int yEnd) {
5742 for (
int y = yStart; y < yEnd; ++y) {
5743 const QCmyk32 *in_scanline =
reinterpret_cast<
const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5744 quint8 *out_scanline =
reinterpret_cast<quint8 *>(toImage.d->data + y * toImage.bytesPerLine());
5745 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5749 transformSegment = [&](
int yStart,
int yEnd) {
5750 for (
int y = yStart; y < yEnd; ++y) {
5751 const QCmyk32 *in_scanline =
reinterpret_cast<
const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5752 quint16 *out_scanline =
reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine());
5753 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5757 }
else if (inColorData == QColorSpace::ColorModel::Cmyk && outColorData == QColorSpace::ColorModel::Rgb) {
5759 if (isRgb32Data(tmpFormat) ) {
5760 transformSegment = [&](
int yStart,
int yEnd) {
5761 for (
int y = yStart; y < yEnd; ++y) {
5762 const QCmyk32 *in_scanline =
reinterpret_cast<
const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5763 QRgb *out_scanline =
reinterpret_cast<QRgb *>(toImage.d->data + y * toImage.bytesPerLine());
5764 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5767 }
else if (isRgb64Data(tmpFormat)) {
5768 transformSegment = [&](
int yStart,
int yEnd) {
5769 for (
int y = yStart; y < yEnd; ++y) {
5770 const QCmyk32 *in_scanline =
reinterpret_cast<
const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5771 QRgba64 *out_scanline =
reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine());
5772 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5776 Q_ASSERT(isRgb32fpx4Data(tmpFormat));
5777 transformSegment = [&](
int yStart,
int yEnd) {
5778 for (
int y = yStart; y < yEnd; ++y) {
5779 const QCmyk32 *in_scanline =
reinterpret_cast<
const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5780 QRgbaFloat32 *out_scanline =
reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine());
5781 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), QColorTransformPrivate::InputOpaque);
5785 }
else if (inColorData == QColorSpace::ColorModel::Rgb && outColorData == QColorSpace::ColorModel::Cmyk) {
5787 if (!fromImage.hasAlphaChannel())
5788 transFlags = QColorTransformPrivate::InputOpaque;
5789 else if (qPixelLayouts[fromImage.format()].premultiplied)
5790 transFlags = QColorTransformPrivate::Premultiplied;
5791 if (isRgb32Data(fromImage.format()) ) {
5792 transformSegment = [&](
int yStart,
int yEnd) {
5793 for (
int y = yStart; y < yEnd; ++y) {
5794 const QRgb *in_scanline =
reinterpret_cast<
const QRgb *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5795 QCmyk32 *out_scanline =
reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine());
5796 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5799 }
else if (isRgb64Data(fromImage.format())) {
5800 transformSegment = [&](
int yStart,
int yEnd) {
5801 for (
int y = yStart; y < yEnd; ++y) {
5802 const QRgba64 *in_scanline =
reinterpret_cast<
const QRgba64 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5803 QCmyk32 *out_scanline =
reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine());
5804 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5808 Q_ASSERT(isRgb32fpx4Data(fromImage.format()));
5809 transformSegment = [&](
int yStart,
int yEnd) {
5810 for (
int y = yStart; y < yEnd; ++y) {
5811 const QRgbaFloat32 *in_scanline =
reinterpret_cast<
const QRgbaFloat32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5812 QCmyk32 *out_scanline =
reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine());
5813 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5822 if (pixelFormat().colorModel() == QPixelFormat::Indexed) {
5823 for (
int i = 0; i < d->colortable.size(); ++i)
5824 fromImage.d->colortable[i] = transform.map(d->colortable[i]);
5825 return fromImage.convertedTo(toFormat, flags);
5828 QImage::Format oldFormat = format();
5829 if (qt_fpColorPrecision(oldFormat)) {
5830 if (oldFormat != QImage::Format_RGBX32FPx4 && oldFormat != QImage::Format_RGBA32FPx4
5831 && oldFormat != QImage::Format_RGBA32FPx4_Premultiplied)
5832 fromImage.convertTo(QImage::Format_RGBA32FPx4);
5833 }
else if (qt_highColorPrecision(oldFormat,
true)) {
5834 if (oldFormat != QImage::Format_RGBX64 && oldFormat != QImage::Format_RGBA64
5835 && oldFormat != QImage::Format_RGBA64_Premultiplied && oldFormat != QImage::Format_Grayscale16)
5836 fromImage.convertTo(QImage::Format_RGBA64);
5837 }
else if (oldFormat != QImage::Format_ARGB32 && oldFormat != QImage::Format_RGB32
5838 && oldFormat != QImage::Format_ARGB32_Premultiplied && oldFormat != QImage::Format_CMYK8888
5839 && oldFormat != QImage::Format_Grayscale8 && oldFormat != QImage::Format_Grayscale16) {
5840 if (hasAlphaChannel())
5841 fromImage.convertTo(QImage::Format_ARGB32);
5843 fromImage.convertTo(QImage::Format_RGB32);
5846 if (!fromImage.hasAlphaChannel())
5847 transFlags = QColorTransformPrivate::InputOpaque;
5848 else if (qPixelLayouts[fromImage.format()].premultiplied)
5849 transFlags = QColorTransformPrivate::Premultiplied;
5851 if (fromImage.format() == Format_Grayscale8) {
5852 transformSegment = [&](
int yStart,
int yEnd) {
5853 for (
int y = yStart; y < yEnd; ++y) {
5854 const quint8 *in_scanline =
reinterpret_cast<
const quint8 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5855 if (tmpFormat == Format_Grayscale8) {
5856 quint8 *out_scanline =
reinterpret_cast<quint8 *>(toImage.d->data + y * toImage.bytesPerLine());
5857 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5859 Q_ASSERT(tmpFormat == Format_Grayscale16);
5860 quint16 *out_scanline =
reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine());
5861 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5865 }
else if (fromImage.format() == Format_Grayscale16) {
5866 transformSegment = [&](
int yStart,
int yEnd) {
5867 for (
int y = yStart; y < yEnd; ++y) {
5868 const quint16 *in_scanline =
reinterpret_cast<
const quint16 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5869 quint16 *out_scanline =
reinterpret_cast<quint16 *>(toImage.d->data + y * toImage.bytesPerLine());
5870 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5873 }
else if (fromImage.format() == Format_CMYK8888) {
5874 Q_ASSERT(tmpFormat == Format_CMYK8888);
5875 transformSegment = [&](
int yStart,
int yEnd) {
5876 for (
int y = yStart; y < yEnd; ++y) {
5877 const QCmyk32 *in_scanline =
reinterpret_cast<
const QCmyk32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5878 QCmyk32 *out_scanline =
reinterpret_cast<QCmyk32 *>(toImage.d->data + y * toImage.bytesPerLine());
5879 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5882 }
else if (isRgb32fpx4Data(fromImage.format())) {
5883 Q_ASSERT(isRgb32fpx4Data(tmpFormat));
5884 transformSegment = [&](
int yStart,
int yEnd) {
5885 for (
int y = yStart; y < yEnd; ++y) {
5886 const QRgbaFloat32 *in_scanline =
reinterpret_cast<
const QRgbaFloat32 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5887 QRgbaFloat32 *out_scanline =
reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine());
5888 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5891 }
else if (isRgb64Data(fromImage.format())) {
5892 transformSegment = [&](
int yStart,
int yEnd) {
5893 for (
int y = yStart; y < yEnd; ++y) {
5894 const QRgba64 *in_scanline =
reinterpret_cast<
const QRgba64 *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5895 if (isRgb32fpx4Data(tmpFormat)) {
5896 QRgbaFloat32 *out_scanline =
reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine());
5897 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5899 Q_ASSERT(isRgb64Data(tmpFormat));
5900 QRgba64 *out_scanline =
reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine());
5901 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5906 transformSegment = [&](
int yStart,
int yEnd) {
5907 for (
int y = yStart; y < yEnd; ++y) {
5908 const QRgb *in_scanline =
reinterpret_cast<
const QRgb *>(fromImage.constBits() + y * fromImage.bytesPerLine());
5909 if (isRgb32fpx4Data(tmpFormat)) {
5910 QRgbaFloat32 *out_scanline =
reinterpret_cast<QRgbaFloat32 *>(toImage.d->data + y * toImage.bytesPerLine());
5911 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5912 }
else if (isRgb64Data(tmpFormat)) {
5913 QRgba64 *out_scanline =
reinterpret_cast<QRgba64 *>(toImage.d->data + y * toImage.bytesPerLine());
5914 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5916 Q_ASSERT(isRgb32Data(tmpFormat));
5917 QRgb *out_scanline =
reinterpret_cast<QRgb *>(toImage.d->data + y * toImage.bytesPerLine());
5918 QColorTransformPrivate::get(transform)->apply(out_scanline, in_scanline, width(), transFlags);
5925#if QT_CONFIG(qtgui_threadpool)
5926 int segments = (qsizetype(width()) * height()) >> 16;
5927 segments = std::min(segments, height());
5928 QThreadPool *threadPool = QGuiApplicationPrivate::qtGuiThreadPool();
5929 if (segments > 1 && threadPool && !threadPool->contains(QThread::currentThread())) {
5930 QLatch latch(segments);
5932 for (
int i = 0; i < segments; ++i) {
5933 int yn = (height() - y) / (segments - i);
5934 threadPool->start([&, y, yn]() {
5935 transformSegment(y, y + yn);
5943 transformSegment(0, height());
5945 if (tmpFormat != toFormat)
5946 toImage.convertTo(toFormat);
5952
5953
5954
5955
5956
5957
5958
5959QImage QImage::colorTransformed(
const QColorTransform &transform) &&
5964 const QColorSpacePrivate *inColorSpace = QColorTransformPrivate::get(transform)->colorSpaceIn.constData();
5965 const QColorSpacePrivate *outColorSpace = QColorTransformPrivate::get(transform)->colorSpaceOut.constData();
5966 if (!qt_compatibleColorModelSource(pixelFormat().colorModel(), inColorSpace->colorModel)) {
5967 qWarning() <<
"QImage::colorTransformed: Invalid input color space for transform";
5970 if (!qt_compatibleColorModelTarget(pixelFormat().colorModel(), outColorSpace->colorModel, outColorSpace->transformModel)) {
5972 switch (outColorSpace->colorModel) {
5973 case QColorSpace::ColorModel::Rgb:
5974 return colorTransformed(transform, qt_highColorPrecision(format(),
true) ? QImage::Format_RGBX64 : QImage::Format_RGB32);
5975 case QColorSpace::ColorModel::Gray:
5976 return colorTransformed(transform, qt_highColorPrecision(format(),
true) ? QImage::Format_Grayscale16 : QImage::Format_Grayscale8);
5977 case QColorSpace::ColorModel::Cmyk:
5978 return colorTransformed(transform, QImage::Format_CMYK8888);
5979 case QColorSpace::ColorModel::Undefined:
5985 applyColorTransform(transform);
5986 return std::move(*
this);
5990
5991
5992
5993
5994
5995
5996
5997QImage QImage::colorTransformed(
const QColorTransform &transform, QImage::Format format, Qt::ImageConversionFlags flags) &&
6000 return colorTransformed(transform, format, flags);
6003bool QImageData::convertInPlace(QImage::Format newFormat, Qt::ImageConversionFlags flags)
6005 if (format == newFormat)
6009 if (ref.loadRelaxed() > 1 || !own_data)
6012 InPlace_Image_Converter converter = qimage_inplace_converter_map[format][newFormat];
6014 return converter(
this, flags);
6015 if (format > QImage::Format_Indexed8 && newFormat > QImage::Format_Indexed8 && !qimage_converter_map[format][newFormat]) {
6018 if (qt_highColorPrecision(newFormat, !qPixelLayouts[newFormat].hasAlphaChannel)
6019 && qt_highColorPrecision(format, !qPixelLayouts[format].hasAlphaChannel)) {
6020#if QT_CONFIG(raster_fp)
6021 if (qt_fpColorPrecision(format) && qt_fpColorPrecision(newFormat))
6022 return convert_generic_inplace_over_rgba32f(
this, newFormat, flags);
6024 return convert_generic_inplace_over_rgb64(
this, newFormat, flags);
6026 return convert_generic_inplace(
this, newFormat, flags);
6032
6033
6034
6037
6038
6039
6041#ifndef QT_NO_DEBUG_STREAM
6042QDebug operator<<(QDebug dbg,
const QImage &i)
6044 QDebugStateSaver saver(dbg);
6051 dbg << i.size() <<
",format=" << i.format() <<
",depth=" << i.depth();
6053 dbg <<
",colorCount=" << i.colorCount();
6054 const int bytesPerLine = i.bytesPerLine();
6055 dbg <<
",devicePixelRatio=" << i.devicePixelRatio()
6056 <<
",bytesPerLine=" << bytesPerLine <<
",sizeInBytes=" << i.sizeInBytes();
6057 if (dbg.verbosity() > 2 && i.height() > 0) {
6058 const int outputLength = qMin(bytesPerLine, 24);
6060 << QByteArray(
reinterpret_cast<
const char *>(i.scanLine(0)), outputLength).toHex()
6069static constexpr QPixelFormat pixelformats[] = {
6073 QPixelFormat(QPixelFormat::Indexed,
6080 QPixelFormat::IgnoresAlpha,
6081 QPixelFormat::AtBeginning,
6082 QPixelFormat::NotPremultiplied,
6083 QPixelFormat::UnsignedByte,
6084 QPixelFormat::BigEndian),
6086 QPixelFormat(QPixelFormat::Indexed,
6093 QPixelFormat::IgnoresAlpha,
6094 QPixelFormat::AtBeginning,
6095 QPixelFormat::NotPremultiplied,
6096 QPixelFormat::UnsignedByte,
6097 QPixelFormat::BigEndian),
6099 QPixelFormat(QPixelFormat::Indexed,
6106 QPixelFormat::IgnoresAlpha,
6107 QPixelFormat::AtBeginning,
6108 QPixelFormat::NotPremultiplied,
6109 QPixelFormat::UnsignedByte,
6110 QPixelFormat::BigEndian),
6112 QPixelFormat(QPixelFormat::RGB,
6119 QPixelFormat::IgnoresAlpha,
6120 QPixelFormat::AtBeginning,
6121 QPixelFormat::NotPremultiplied,
6122 QPixelFormat::UnsignedInteger,
6123 QPixelFormat::CurrentSystemEndian),
6125 QPixelFormat(QPixelFormat::RGB,
6132 QPixelFormat::UsesAlpha,
6133 QPixelFormat::AtBeginning,
6134 QPixelFormat::NotPremultiplied,
6135 QPixelFormat::UnsignedInteger,
6136 QPixelFormat::CurrentSystemEndian),
6138 QPixelFormat(QPixelFormat::RGB,
6145 QPixelFormat::UsesAlpha,
6146 QPixelFormat::AtBeginning,
6147 QPixelFormat::Premultiplied,
6148 QPixelFormat::UnsignedInteger,
6149 QPixelFormat::CurrentSystemEndian),
6151 QPixelFormat(QPixelFormat::RGB,
6158 QPixelFormat::IgnoresAlpha,
6159 QPixelFormat::AtBeginning,
6160 QPixelFormat::NotPremultiplied,
6161 QPixelFormat::UnsignedShort,
6162 QPixelFormat::CurrentSystemEndian),
6164 QPixelFormat(QPixelFormat::RGB,
6171 QPixelFormat::UsesAlpha,
6172 QPixelFormat::AtBeginning,
6173 QPixelFormat::Premultiplied,
6174 QPixelFormat::UnsignedInteger,
6175 QPixelFormat::CurrentSystemEndian),
6177 QPixelFormat(QPixelFormat::RGB,
6184 QPixelFormat::IgnoresAlpha,
6185 QPixelFormat::AtBeginning,
6186 QPixelFormat::NotPremultiplied,
6187 QPixelFormat::UnsignedInteger,
6188 QPixelFormat::CurrentSystemEndian),
6190 QPixelFormat(QPixelFormat::RGB,
6197 QPixelFormat::UsesAlpha,
6198 QPixelFormat::AtEnd,
6199 QPixelFormat::Premultiplied,
6200 QPixelFormat::UnsignedInteger,
6201 QPixelFormat::CurrentSystemEndian),
6203 QPixelFormat(QPixelFormat::RGB,
6210 QPixelFormat::IgnoresAlpha,
6211 QPixelFormat::AtBeginning,
6212 QPixelFormat::NotPremultiplied,
6213 QPixelFormat::UnsignedShort,
6214 QPixelFormat::CurrentSystemEndian),
6216 QPixelFormat(QPixelFormat::RGB,
6223 QPixelFormat::UsesAlpha,
6224 QPixelFormat::AtBeginning,
6225 QPixelFormat::Premultiplied,
6226 QPixelFormat::UnsignedInteger,
6227 QPixelFormat::CurrentSystemEndian),
6229 QPixelFormat(QPixelFormat::RGB,
6236 QPixelFormat::IgnoresAlpha,
6237 QPixelFormat::AtBeginning,
6238 QPixelFormat::NotPremultiplied,
6239 QPixelFormat::UnsignedByte,
6240 QPixelFormat::BigEndian),
6242 QPixelFormat(QPixelFormat::RGB,
6249 QPixelFormat::IgnoresAlpha,
6250 QPixelFormat::AtBeginning,
6251 QPixelFormat::NotPremultiplied,
6252 QPixelFormat::UnsignedShort,
6253 QPixelFormat::CurrentSystemEndian),
6255 QPixelFormat(QPixelFormat::RGB,
6262 QPixelFormat::UsesAlpha,
6263 QPixelFormat::AtBeginning,
6264 QPixelFormat::Premultiplied,
6265 QPixelFormat::UnsignedShort,
6266 QPixelFormat::CurrentSystemEndian),
6268 QPixelFormat(QPixelFormat::RGB,
6275 QPixelFormat::IgnoresAlpha,
6276 QPixelFormat::AtEnd,
6277 QPixelFormat::NotPremultiplied,
6278 QPixelFormat::UnsignedByte,
6279 QPixelFormat::BigEndian),
6281 QPixelFormat(QPixelFormat::RGB,
6288 QPixelFormat::UsesAlpha,
6289 QPixelFormat::AtEnd,
6290 QPixelFormat::NotPremultiplied,
6291 QPixelFormat::UnsignedByte,
6292 QPixelFormat::BigEndian),
6294 QPixelFormat(QPixelFormat::RGB,
6301 QPixelFormat::UsesAlpha,
6302 QPixelFormat::AtEnd,
6303 QPixelFormat::Premultiplied,
6304 QPixelFormat::UnsignedByte,
6305 QPixelFormat::BigEndian),
6307 QPixelFormat(QPixelFormat::BGR,
6314 QPixelFormat::IgnoresAlpha,
6315 QPixelFormat::AtBeginning,
6316 QPixelFormat::NotPremultiplied,
6317 QPixelFormat::UnsignedInteger,
6318 QPixelFormat::CurrentSystemEndian),
6320 QPixelFormat(QPixelFormat::BGR,
6327 QPixelFormat::UsesAlpha,
6328 QPixelFormat::AtBeginning,
6329 QPixelFormat::Premultiplied,
6330 QPixelFormat::UnsignedInteger,
6331 QPixelFormat::CurrentSystemEndian),
6333 QPixelFormat(QPixelFormat::RGB,
6340 QPixelFormat::IgnoresAlpha,
6341 QPixelFormat::AtBeginning,
6342 QPixelFormat::NotPremultiplied,
6343 QPixelFormat::UnsignedInteger,
6344 QPixelFormat::CurrentSystemEndian),
6346 QPixelFormat(QPixelFormat::RGB,
6353 QPixelFormat::UsesAlpha,
6354 QPixelFormat::AtBeginning,
6355 QPixelFormat::Premultiplied,
6356 QPixelFormat::UnsignedInteger,
6357 QPixelFormat::CurrentSystemEndian),
6359 QPixelFormat(QPixelFormat::Alpha,
6366 QPixelFormat::UsesAlpha,
6367 QPixelFormat::AtBeginning,
6368 QPixelFormat::Premultiplied,
6369 QPixelFormat::UnsignedByte,
6370 QPixelFormat::BigEndian),
6372 QPixelFormat(QPixelFormat::Grayscale,
6379 QPixelFormat::IgnoresAlpha,
6380 QPixelFormat::AtBeginning,
6381 QPixelFormat::NotPremultiplied,
6382 QPixelFormat::UnsignedByte,
6383 QPixelFormat::BigEndian),
6385 QPixelFormat(QPixelFormat::RGB,
6392 QPixelFormat::IgnoresAlpha,
6393 QPixelFormat::AtEnd,
6394 QPixelFormat::NotPremultiplied,
6395 QPixelFormat::UnsignedShort,
6396 QPixelFormat::CurrentSystemEndian),
6398 QPixelFormat(QPixelFormat::RGB,
6405 QPixelFormat::UsesAlpha,
6406 QPixelFormat::AtEnd,
6407 QPixelFormat::NotPremultiplied,
6408 QPixelFormat::UnsignedShort,
6409 QPixelFormat::CurrentSystemEndian),
6411 QPixelFormat(QPixelFormat::RGB,
6418 QPixelFormat::UsesAlpha,
6419 QPixelFormat::AtEnd,
6420 QPixelFormat::Premultiplied,
6421 QPixelFormat::UnsignedShort,
6422 QPixelFormat::CurrentSystemEndian),
6424 QPixelFormat(QPixelFormat::Grayscale,
6431 QPixelFormat::IgnoresAlpha,
6432 QPixelFormat::AtBeginning,
6433 QPixelFormat::NotPremultiplied,
6434 QPixelFormat::UnsignedShort,
6435 QPixelFormat::CurrentSystemEndian),
6437 QPixelFormat(QPixelFormat::BGR,
6444 QPixelFormat::IgnoresAlpha,
6445 QPixelFormat::AtBeginning,
6446 QPixelFormat::NotPremultiplied,
6447 QPixelFormat::UnsignedByte,
6448 QPixelFormat::BigEndian),
6450 QPixelFormat(QPixelFormat::RGB,
6457 QPixelFormat::IgnoresAlpha,
6458 QPixelFormat::AtEnd,
6459 QPixelFormat::NotPremultiplied,
6460 QPixelFormat::FloatingPoint,
6461 QPixelFormat::CurrentSystemEndian),
6463 QPixelFormat(QPixelFormat::RGB,
6470 QPixelFormat::UsesAlpha,
6471 QPixelFormat::AtEnd,
6472 QPixelFormat::NotPremultiplied,
6473 QPixelFormat::FloatingPoint,
6474 QPixelFormat::CurrentSystemEndian),
6476 QPixelFormat(QPixelFormat::RGB,
6483 QPixelFormat::UsesAlpha,
6484 QPixelFormat::AtEnd,
6485 QPixelFormat::Premultiplied,
6486 QPixelFormat::FloatingPoint,
6487 QPixelFormat::CurrentSystemEndian),
6489 QPixelFormat(QPixelFormat::RGB,
6496 QPixelFormat::IgnoresAlpha,
6497 QPixelFormat::AtEnd,
6498 QPixelFormat::NotPremultiplied,
6499 QPixelFormat::FloatingPoint,
6500 QPixelFormat::CurrentSystemEndian),
6502 QPixelFormat(QPixelFormat::RGB,
6509 QPixelFormat::UsesAlpha,
6510 QPixelFormat::AtEnd,
6511 QPixelFormat::NotPremultiplied,
6512 QPixelFormat::FloatingPoint,
6513 QPixelFormat::CurrentSystemEndian),
6515 QPixelFormat(QPixelFormat::RGB,
6522 QPixelFormat::UsesAlpha,
6523 QPixelFormat::AtEnd,
6524 QPixelFormat::Premultiplied,
6525 QPixelFormat::FloatingPoint,
6526 QPixelFormat::CurrentSystemEndian),
6528 QPixelFormat(QPixelFormat::CMYK,
6535 QPixelFormat::IgnoresAlpha,
6536 QPixelFormat::AtBeginning,
6537 QPixelFormat::NotPremultiplied,
6538 QPixelFormat::UnsignedInteger,
6539 QPixelFormat::CurrentSystemEndian),
6541static_assert(
sizeof(pixelformats) /
sizeof(*pixelformats) == QImage::NImageFormats);
6544
6545
6546QPixelFormat QImage::pixelFormat()
const noexcept
6548 return toPixelFormat(format());
6552
6553
6554QPixelFormat QImage::toPixelFormat(QImage::Format format)
noexcept
6556 Q_ASSERT(
static_cast<
int>(format) < NImageFormats &&
static_cast<
int>(format) >= 0);
6557 return pixelformats[format];
6561
6562
6563QImage::Format QImage::toImageFormat(QPixelFormat format)
noexcept
6565 for (
int i = 0; i < NImageFormats; i++) {
6566 if (format == pixelformats[i])
6569 return Format_Invalid;
6574 Qt::Orientations orients = {};
6575 if (orient.testFlag(QImageIOHandler::TransformationMirror))
6576 orients |= Qt::Horizontal;
6577 if (orient.testFlag(QImageIOHandler::TransformationFlip))
6578 orients |= Qt::Vertical;
6584 if (orient == QImageIOHandler::TransformationNone)
6586 if (orient == QImageIOHandler::TransformationRotate270) {
6587 src = rotated270(src);
6589 src.flip(toOrientations(orient));
6590 if (orient & QImageIOHandler::TransformationRotate90)
6591 src = rotated90(src);
6597 QMap<QString, QString> text = qt_getImageTextFromDescription(description);
6598 const auto textKeys = image.textKeys();
6599 for (
const QString &key : textKeys) {
6600 if (!key.isEmpty() && !text.contains(key))
6601 text.insert(key, image.text(key));
6608 QMap<QString, QString> text;
6609 for (
const auto &pair : QStringView{description}.tokenize(u"\n\n")) {
6610 int index = pair.indexOf(u':');
6611 if (index >= 0 && pair.indexOf(u' ') < index) {
6612 if (!pair.trimmed().isEmpty())
6613 text.insert(
"Description"_L1, pair.toString().simplified());
6615 const auto key = pair.left(index);
6616 if (!key.trimmed().isEmpty())
6617 text.insert(key.toString(), pair.mid(index + 2).toString().simplified());
6625#include "moc_qimage.cpp"
Combined button and popup list for selecting options.
Q_TRACE_METADATA(qtcore, "ENUM { AUTO, RANGE User ... MaxUser } QEvent::Type;")
static QImage rotated90(const QImage &src)
static void copyMetadata(QImage *dst, const QImage &src)
#define QT_XFORM_TYPE_LSBFIRST
static void copyMetadata(QImageData *dst, const QImageData *src)
#define QT_XFORM_TYPE_MSBFIRST
static int next_qimage_serial_number()
#define QIMAGE_SANITYCHECK_MEMORY(image)
static void copyPhysicalMetadata(QImageData *dst, const QImageData *src)
static QImage rotated270(const QImage &src)
QMap< QString, QString > qt_getImageText(const QImage &image, const QString &description)
Q_GUI_EXPORT void qt_imageTransform(QImage &src, QImageIOHandler::Transformations orient)
static QImage rotated180(const QImage &src)
static Qt::Orientations toOrientations(QImageIOHandler::Transformations orient)
QMap< QString, QString > qt_getImageTextFromDescription(const QString &description)