Coverage Report

Created: 2026-11-10 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/image/qxpmhandler.cpp
Line
Count
Source
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:critical reason:data-parser
4
5
#include "private/qxpmhandler_p.h"
6
7
#ifndef QT_NO_IMAGEFORMAT_XPM
8
9
#include <qbytearraymatcher.h>
10
#include <qdebug.h>
11
#include <qimage.h>
12
#include <qimagereader.h>
13
#include <qloggingcategory.h>
14
#include <qmap.h>
15
#include <qtextstream.h>
16
#include <qvariant.h>
17
18
#include <private/qcolor_p.h>
19
#include <private/qduplicatetracker_p.h> // for easier std::pmr detection
20
#include <private/qtools_p.h>
21
#include <private/qimage_p.h>
22
23
#include <algorithm>
24
#include <array>
25
26
QT_BEGIN_NAMESPACE
27
28
using namespace QtMiscUtils;
29
30
static quint64 xpmHash(const QString &str)
31
0
{
32
0
    unsigned int hashValue = 0;
33
0
    for (int i = 0; i < str.size(); ++i) {
34
0
        hashValue <<= 8;
35
0
        hashValue += (unsigned int)str.at(i).unicode();
36
0
    }
37
0
    return hashValue;
38
0
}
39
static quint64 xpmHash(char *str)
40
0
{
41
0
    unsigned int hashValue = 0;
42
0
    while (*str != '\0') {
43
0
        hashValue <<= 8;
44
0
        hashValue += (unsigned int)*str;
45
0
        ++str;
46
0
    }
47
0
    return hashValue;
48
0
}
49
50
static constexpr struct XPMRGBData {
51
    QRgb  value;
52
    const char name[21];
53
} xpmRgbTbl[] = {
54
  { qRgb(240,248,255),  "aliceblue" },
55
  { qRgb(250,235,215),  "antiquewhite" },
56
  { qRgb(255,239,219),  "antiquewhite1" },
57
  { qRgb(238,223,204),  "antiquewhite2" },
58
  { qRgb(205,192,176),  "antiquewhite3" },
59
  { qRgb(139,131,120),  "antiquewhite4" },
60
  { qRgb(127,255,212),  "aquamarine" },
61
  { qRgb(127,255,212),  "aquamarine1" },
62
  { qRgb(118,238,198),  "aquamarine2" },
63
  { qRgb(102,205,170),  "aquamarine3" },
64
  { qRgb( 69,139,116),  "aquamarine4" },
65
  { qRgb(240,255,255),  "azure" },
66
  { qRgb(240,255,255),  "azure1" },
67
  { qRgb(224,238,238),  "azure2" },
68
  { qRgb(193,205,205),  "azure3" },
69
  { qRgb(131,139,139),  "azure4" },
70
  { qRgb(245,245,220),  "beige" },
71
  { qRgb(255,228,196),  "bisque" },
72
  { qRgb(255,228,196),  "bisque1" },
73
  { qRgb(238,213,183),  "bisque2" },
74
  { qRgb(205,183,158),  "bisque3" },
75
  { qRgb(139,125,107),  "bisque4" },
76
  { qRgb(  0,  0,  0),  "black" },
77
  { qRgb(255,235,205),  "blanchedalmond" },
78
  { qRgb(  0,  0,255),  "blue" },
79
  { qRgb(  0,  0,255),  "blue1" },
80
  { qRgb(  0,  0,238),  "blue2" },
81
  { qRgb(  0,  0,205),  "blue3" },
82
  { qRgb(  0,  0,139),  "blue4" },
83
  { qRgb(138, 43,226),  "blueviolet" },
84
  { qRgb(165, 42, 42),  "brown" },
85
  { qRgb(255, 64, 64),  "brown1" },
86
  { qRgb(238, 59, 59),  "brown2" },
87
  { qRgb(205, 51, 51),  "brown3" },
88
  { qRgb(139, 35, 35),  "brown4" },
89
  { qRgb(222,184,135),  "burlywood" },
90
  { qRgb(255,211,155),  "burlywood1" },
91
  { qRgb(238,197,145),  "burlywood2" },
92
  { qRgb(205,170,125),  "burlywood3" },
93
  { qRgb(139,115, 85),  "burlywood4" },
94
  { qRgb( 95,158,160),  "cadetblue" },
95
  { qRgb(152,245,255),  "cadetblue1" },
96
  { qRgb(142,229,238),  "cadetblue2" },
97
  { qRgb(122,197,205),  "cadetblue3" },
98
  { qRgb( 83,134,139),  "cadetblue4" },
99
  { qRgb(127,255,  0),  "chartreuse" },
100
  { qRgb(127,255,  0),  "chartreuse1" },
101
  { qRgb(118,238,  0),  "chartreuse2" },
102
  { qRgb(102,205,  0),  "chartreuse3" },
103
  { qRgb( 69,139,  0),  "chartreuse4" },
104
  { qRgb(210,105, 30),  "chocolate" },
105
  { qRgb(255,127, 36),  "chocolate1" },
106
  { qRgb(238,118, 33),  "chocolate2" },
107
  { qRgb(205,102, 29),  "chocolate3" },
108
  { qRgb(139, 69, 19),  "chocolate4" },
109
  { qRgb(255,127, 80),  "coral" },
110
  { qRgb(255,114, 86),  "coral1" },
111
  { qRgb(238,106, 80),  "coral2" },
112
  { qRgb(205, 91, 69),  "coral3" },
113
  { qRgb(139, 62, 47),  "coral4" },
114
  { qRgb(100,149,237),  "cornflowerblue" },
115
  { qRgb(255,248,220),  "cornsilk" },
116
  { qRgb(255,248,220),  "cornsilk1" },
117
  { qRgb(238,232,205),  "cornsilk2" },
118
  { qRgb(205,200,177),  "cornsilk3" },
119
  { qRgb(139,136,120),  "cornsilk4" },
120
  { qRgb(  0,255,255),  "cyan" },
121
  { qRgb(  0,255,255),  "cyan1" },
122
  { qRgb(  0,238,238),  "cyan2" },
123
  { qRgb(  0,205,205),  "cyan3" },
124
  { qRgb(  0,139,139),  "cyan4" },
125
  { qRgb(  0,  0,139),  "darkblue" },
126
  { qRgb(  0,139,139),  "darkcyan" },
127
  { qRgb(184,134, 11),  "darkgoldenrod" },
128
  { qRgb(255,185, 15),  "darkgoldenrod1" },
129
  { qRgb(238,173, 14),  "darkgoldenrod2" },
130
  { qRgb(205,149, 12),  "darkgoldenrod3" },
131
  { qRgb(139,101,  8),  "darkgoldenrod4" },
132
  { qRgb(169,169,169),  "darkgray" },
133
  { qRgb(  0,100,  0),  "darkgreen" },
134
  { qRgb(169,169,169),  "darkgrey" },
135
  { qRgb(189,183,107),  "darkkhaki" },
136
  { qRgb(139,  0,139),  "darkmagenta" },
137
  { qRgb( 85,107, 47),  "darkolivegreen" },
138
  { qRgb(202,255,112),  "darkolivegreen1" },
139
  { qRgb(188,238,104),  "darkolivegreen2" },
140
  { qRgb(162,205, 90),  "darkolivegreen3" },
141
  { qRgb(110,139, 61),  "darkolivegreen4" },
142
  { qRgb(255,140,  0),  "darkorange" },
143
  { qRgb(255,127,  0),  "darkorange1" },
144
  { qRgb(238,118,  0),  "darkorange2" },
145
  { qRgb(205,102,  0),  "darkorange3" },
146
  { qRgb(139, 69,  0),  "darkorange4" },
147
  { qRgb(153, 50,204),  "darkorchid" },
148
  { qRgb(191, 62,255),  "darkorchid1" },
149
  { qRgb(178, 58,238),  "darkorchid2" },
150
  { qRgb(154, 50,205),  "darkorchid3" },
151
  { qRgb(104, 34,139),  "darkorchid4" },
152
  { qRgb(139,  0,  0),  "darkred" },
153
  { qRgb(233,150,122),  "darksalmon" },
154
  { qRgb(143,188,143),  "darkseagreen" },
155
  { qRgb(193,255,193),  "darkseagreen1" },
156
  { qRgb(180,238,180),  "darkseagreen2" },
157
  { qRgb(155,205,155),  "darkseagreen3" },
158
  { qRgb(105,139,105),  "darkseagreen4" },
159
  { qRgb( 72, 61,139),  "darkslateblue" },
160
  { qRgb( 47, 79, 79),  "darkslategray" },
161
  { qRgb(151,255,255),  "darkslategray1" },
162
  { qRgb(141,238,238),  "darkslategray2" },
163
  { qRgb(121,205,205),  "darkslategray3" },
164
  { qRgb( 82,139,139),  "darkslategray4" },
165
  { qRgb( 47, 79, 79),  "darkslategrey" },
166
  { qRgb(  0,206,209),  "darkturquoise" },
167
  { qRgb(148,  0,211),  "darkviolet" },
168
  { qRgb(255, 20,147),  "deeppink" },
169
  { qRgb(255, 20,147),  "deeppink1" },
170
  { qRgb(238, 18,137),  "deeppink2" },
171
  { qRgb(205, 16,118),  "deeppink3" },
172
  { qRgb(139, 10, 80),  "deeppink4" },
173
  { qRgb(  0,191,255),  "deepskyblue" },
174
  { qRgb(  0,191,255),  "deepskyblue1" },
175
  { qRgb(  0,178,238),  "deepskyblue2" },
176
  { qRgb(  0,154,205),  "deepskyblue3" },
177
  { qRgb(  0,104,139),  "deepskyblue4" },
178
  { qRgb(105,105,105),  "dimgray" },
179
  { qRgb(105,105,105),  "dimgrey" },
180
  { qRgb( 30,144,255),  "dodgerblue" },
181
  { qRgb( 30,144,255),  "dodgerblue1" },
182
  { qRgb( 28,134,238),  "dodgerblue2" },
183
  { qRgb( 24,116,205),  "dodgerblue3" },
184
  { qRgb( 16, 78,139),  "dodgerblue4" },
185
  { qRgb(178, 34, 34),  "firebrick" },
186
  { qRgb(255, 48, 48),  "firebrick1" },
187
  { qRgb(238, 44, 44),  "firebrick2" },
188
  { qRgb(205, 38, 38),  "firebrick3" },
189
  { qRgb(139, 26, 26),  "firebrick4" },
190
  { qRgb(255,250,240),  "floralwhite" },
191
  { qRgb( 34,139, 34),  "forestgreen" },
192
  { qRgb(220,220,220),  "gainsboro" },
193
  { qRgb(248,248,255),  "ghostwhite" },
194
  { qRgb(255,215,  0),  "gold" },
195
  { qRgb(255,215,  0),  "gold1" },
196
  { qRgb(238,201,  0),  "gold2" },
197
  { qRgb(205,173,  0),  "gold3" },
198
  { qRgb(139,117,  0),  "gold4" },
199
  { qRgb(218,165, 32),  "goldenrod" },
200
  { qRgb(255,193, 37),  "goldenrod1" },
201
  { qRgb(238,180, 34),  "goldenrod2" },
202
  { qRgb(205,155, 29),  "goldenrod3" },
203
  { qRgb(139,105, 20),  "goldenrod4" },
204
  { qRgb(190,190,190),  "gray" },
205
  { qRgb(  0,  0,  0),  "gray0" },
206
  { qRgb(  3,  3,  3),  "gray1" },
207
  { qRgb( 26, 26, 26),  "gray10" },
208
  { qRgb(255,255,255),  "gray100" },
209
  { qRgb( 28, 28, 28),  "gray11" },
210
  { qRgb( 31, 31, 31),  "gray12" },
211
  { qRgb( 33, 33, 33),  "gray13" },
212
  { qRgb( 36, 36, 36),  "gray14" },
213
  { qRgb( 38, 38, 38),  "gray15" },
214
  { qRgb( 41, 41, 41),  "gray16" },
215
  { qRgb( 43, 43, 43),  "gray17" },
216
  { qRgb( 46, 46, 46),  "gray18" },
217
  { qRgb( 48, 48, 48),  "gray19" },
218
  { qRgb(  5,  5,  5),  "gray2" },
219
  { qRgb( 51, 51, 51),  "gray20" },
220
  { qRgb( 54, 54, 54),  "gray21" },
221
  { qRgb( 56, 56, 56),  "gray22" },
222
  { qRgb( 59, 59, 59),  "gray23" },
223
  { qRgb( 61, 61, 61),  "gray24" },
224
  { qRgb( 64, 64, 64),  "gray25" },
225
  { qRgb( 66, 66, 66),  "gray26" },
226
  { qRgb( 69, 69, 69),  "gray27" },
227
  { qRgb( 71, 71, 71),  "gray28" },
228
  { qRgb( 74, 74, 74),  "gray29" },
229
  { qRgb(  8,  8,  8),  "gray3" },
230
  { qRgb( 77, 77, 77),  "gray30" },
231
  { qRgb( 79, 79, 79),  "gray31" },
232
  { qRgb( 82, 82, 82),  "gray32" },
233
  { qRgb( 84, 84, 84),  "gray33" },
234
  { qRgb( 87, 87, 87),  "gray34" },
235
  { qRgb( 89, 89, 89),  "gray35" },
236
  { qRgb( 92, 92, 92),  "gray36" },
237
  { qRgb( 94, 94, 94),  "gray37" },
238
  { qRgb( 97, 97, 97),  "gray38" },
239
  { qRgb( 99, 99, 99),  "gray39" },
240
  { qRgb( 10, 10, 10),  "gray4" },
241
  { qRgb(102,102,102),  "gray40" },
242
  { qRgb(105,105,105),  "gray41" },
243
  { qRgb(107,107,107),  "gray42" },
244
  { qRgb(110,110,110),  "gray43" },
245
  { qRgb(112,112,112),  "gray44" },
246
  { qRgb(115,115,115),  "gray45" },
247
  { qRgb(117,117,117),  "gray46" },
248
  { qRgb(120,120,120),  "gray47" },
249
  { qRgb(122,122,122),  "gray48" },
250
  { qRgb(125,125,125),  "gray49" },
251
  { qRgb( 13, 13, 13),  "gray5" },
252
  { qRgb(127,127,127),  "gray50" },
253
  { qRgb(130,130,130),  "gray51" },
254
  { qRgb(133,133,133),  "gray52" },
255
  { qRgb(135,135,135),  "gray53" },
256
  { qRgb(138,138,138),  "gray54" },
257
  { qRgb(140,140,140),  "gray55" },
258
  { qRgb(143,143,143),  "gray56" },
259
  { qRgb(145,145,145),  "gray57" },
260
  { qRgb(148,148,148),  "gray58" },
261
  { qRgb(150,150,150),  "gray59" },
262
  { qRgb( 15, 15, 15),  "gray6" },
263
  { qRgb(153,153,153),  "gray60" },
264
  { qRgb(156,156,156),  "gray61" },
265
  { qRgb(158,158,158),  "gray62" },
266
  { qRgb(161,161,161),  "gray63" },
267
  { qRgb(163,163,163),  "gray64" },
268
  { qRgb(166,166,166),  "gray65" },
269
  { qRgb(168,168,168),  "gray66" },
270
  { qRgb(171,171,171),  "gray67" },
271
  { qRgb(173,173,173),  "gray68" },
272
  { qRgb(176,176,176),  "gray69" },
273
  { qRgb( 18, 18, 18),  "gray7" },
274
  { qRgb(179,179,179),  "gray70" },
275
  { qRgb(181,181,181),  "gray71" },
276
  { qRgb(184,184,184),  "gray72" },
277
  { qRgb(186,186,186),  "gray73" },
278
  { qRgb(189,189,189),  "gray74" },
279
  { qRgb(191,191,191),  "gray75" },
280
  { qRgb(194,194,194),  "gray76" },
281
  { qRgb(196,196,196),  "gray77" },
282
  { qRgb(199,199,199),  "gray78" },
283
  { qRgb(201,201,201),  "gray79" },
284
  { qRgb( 20, 20, 20),  "gray8" },
285
  { qRgb(204,204,204),  "gray80" },
286
  { qRgb(207,207,207),  "gray81" },
287
  { qRgb(209,209,209),  "gray82" },
288
  { qRgb(212,212,212),  "gray83" },
289
  { qRgb(214,214,214),  "gray84" },
290
  { qRgb(217,217,217),  "gray85" },
291
  { qRgb(219,219,219),  "gray86" },
292
  { qRgb(222,222,222),  "gray87" },
293
  { qRgb(224,224,224),  "gray88" },
294
  { qRgb(227,227,227),  "gray89" },
295
  { qRgb( 23, 23, 23),  "gray9" },
296
  { qRgb(229,229,229),  "gray90" },
297
  { qRgb(232,232,232),  "gray91" },
298
  { qRgb(235,235,235),  "gray92" },
299
  { qRgb(237,237,237),  "gray93" },
300
  { qRgb(240,240,240),  "gray94" },
301
  { qRgb(242,242,242),  "gray95" },
302
  { qRgb(245,245,245),  "gray96" },
303
  { qRgb(247,247,247),  "gray97" },
304
  { qRgb(250,250,250),  "gray98" },
305
  { qRgb(252,252,252),  "gray99" },
306
  { qRgb(  0,255,  0),  "green" },
307
  { qRgb(  0,255,  0),  "green1" },
308
  { qRgb(  0,238,  0),  "green2" },
309
  { qRgb(  0,205,  0),  "green3" },
310
  { qRgb(  0,139,  0),  "green4" },
311
  { qRgb(173,255, 47),  "greenyellow" },
312
  { qRgb(190,190,190),  "grey" },
313
  { qRgb(  0,  0,  0),  "grey0" },
314
  { qRgb(  3,  3,  3),  "grey1" },
315
  { qRgb( 26, 26, 26),  "grey10" },
316
  { qRgb(255,255,255),  "grey100" },
317
  { qRgb( 28, 28, 28),  "grey11" },
318
  { qRgb( 31, 31, 31),  "grey12" },
319
  { qRgb( 33, 33, 33),  "grey13" },
320
  { qRgb( 36, 36, 36),  "grey14" },
321
  { qRgb( 38, 38, 38),  "grey15" },
322
  { qRgb( 41, 41, 41),  "grey16" },
323
  { qRgb( 43, 43, 43),  "grey17" },
324
  { qRgb( 46, 46, 46),  "grey18" },
325
  { qRgb( 48, 48, 48),  "grey19" },
326
  { qRgb(  5,  5,  5),  "grey2" },
327
  { qRgb( 51, 51, 51),  "grey20" },
328
  { qRgb( 54, 54, 54),  "grey21" },
329
  { qRgb( 56, 56, 56),  "grey22" },
330
  { qRgb( 59, 59, 59),  "grey23" },
331
  { qRgb( 61, 61, 61),  "grey24" },
332
  { qRgb( 64, 64, 64),  "grey25" },
333
  { qRgb( 66, 66, 66),  "grey26" },
334
  { qRgb( 69, 69, 69),  "grey27" },
335
  { qRgb( 71, 71, 71),  "grey28" },
336
  { qRgb( 74, 74, 74),  "grey29" },
337
  { qRgb(  8,  8,  8),  "grey3" },
338
  { qRgb( 77, 77, 77),  "grey30" },
339
  { qRgb( 79, 79, 79),  "grey31" },
340
  { qRgb( 82, 82, 82),  "grey32" },
341
  { qRgb( 84, 84, 84),  "grey33" },
342
  { qRgb( 87, 87, 87),  "grey34" },
343
  { qRgb( 89, 89, 89),  "grey35" },
344
  { qRgb( 92, 92, 92),  "grey36" },
345
  { qRgb( 94, 94, 94),  "grey37" },
346
  { qRgb( 97, 97, 97),  "grey38" },
347
  { qRgb( 99, 99, 99),  "grey39" },
348
  { qRgb( 10, 10, 10),  "grey4" },
349
  { qRgb(102,102,102),  "grey40" },
350
  { qRgb(105,105,105),  "grey41" },
351
  { qRgb(107,107,107),  "grey42" },
352
  { qRgb(110,110,110),  "grey43" },
353
  { qRgb(112,112,112),  "grey44" },
354
  { qRgb(115,115,115),  "grey45" },
355
  { qRgb(117,117,117),  "grey46" },
356
  { qRgb(120,120,120),  "grey47" },
357
  { qRgb(122,122,122),  "grey48" },
358
  { qRgb(125,125,125),  "grey49" },
359
  { qRgb( 13, 13, 13),  "grey5" },
360
  { qRgb(127,127,127),  "grey50" },
361
  { qRgb(130,130,130),  "grey51" },
362
  { qRgb(133,133,133),  "grey52" },
363
  { qRgb(135,135,135),  "grey53" },
364
  { qRgb(138,138,138),  "grey54" },
365
  { qRgb(140,140,140),  "grey55" },
366
  { qRgb(143,143,143),  "grey56" },
367
  { qRgb(145,145,145),  "grey57" },
368
  { qRgb(148,148,148),  "grey58" },
369
  { qRgb(150,150,150),  "grey59" },
370
  { qRgb( 15, 15, 15),  "grey6" },
371
  { qRgb(153,153,153),  "grey60" },
372
  { qRgb(156,156,156),  "grey61" },
373
  { qRgb(158,158,158),  "grey62" },
374
  { qRgb(161,161,161),  "grey63" },
375
  { qRgb(163,163,163),  "grey64" },
376
  { qRgb(166,166,166),  "grey65" },
377
  { qRgb(168,168,168),  "grey66" },
378
  { qRgb(171,171,171),  "grey67" },
379
  { qRgb(173,173,173),  "grey68" },
380
  { qRgb(176,176,176),  "grey69" },
381
  { qRgb( 18, 18, 18),  "grey7" },
382
  { qRgb(179,179,179),  "grey70" },
383
  { qRgb(181,181,181),  "grey71" },
384
  { qRgb(184,184,184),  "grey72" },
385
  { qRgb(186,186,186),  "grey73" },
386
  { qRgb(189,189,189),  "grey74" },
387
  { qRgb(191,191,191),  "grey75" },
388
  { qRgb(194,194,194),  "grey76" },
389
  { qRgb(196,196,196),  "grey77" },
390
  { qRgb(199,199,199),  "grey78" },
391
  { qRgb(201,201,201),  "grey79" },
392
  { qRgb( 20, 20, 20),  "grey8" },
393
  { qRgb(204,204,204),  "grey80" },
394
  { qRgb(207,207,207),  "grey81" },
395
  { qRgb(209,209,209),  "grey82" },
396
  { qRgb(212,212,212),  "grey83" },
397
  { qRgb(214,214,214),  "grey84" },
398
  { qRgb(217,217,217),  "grey85" },
399
  { qRgb(219,219,219),  "grey86" },
400
  { qRgb(222,222,222),  "grey87" },
401
  { qRgb(224,224,224),  "grey88" },
402
  { qRgb(227,227,227),  "grey89" },
403
  { qRgb( 23, 23, 23),  "grey9" },
404
  { qRgb(229,229,229),  "grey90" },
405
  { qRgb(232,232,232),  "grey91" },
406
  { qRgb(235,235,235),  "grey92" },
407
  { qRgb(237,237,237),  "grey93" },
408
  { qRgb(240,240,240),  "grey94" },
409
  { qRgb(242,242,242),  "grey95" },
410
  { qRgb(245,245,245),  "grey96" },
411
  { qRgb(247,247,247),  "grey97" },
412
  { qRgb(250,250,250),  "grey98" },
413
  { qRgb(252,252,252),  "grey99" },
414
  { qRgb(240,255,240),  "honeydew" },
415
  { qRgb(240,255,240),  "honeydew1" },
416
  { qRgb(224,238,224),  "honeydew2" },
417
  { qRgb(193,205,193),  "honeydew3" },
418
  { qRgb(131,139,131),  "honeydew4" },
419
  { qRgb(255,105,180),  "hotpink" },
420
  { qRgb(255,110,180),  "hotpink1" },
421
  { qRgb(238,106,167),  "hotpink2" },
422
  { qRgb(205, 96,144),  "hotpink3" },
423
  { qRgb(139, 58, 98),  "hotpink4" },
424
  { qRgb(205, 92, 92),  "indianred" },
425
  { qRgb(255,106,106),  "indianred1" },
426
  { qRgb(238, 99, 99),  "indianred2" },
427
  { qRgb(205, 85, 85),  "indianred3" },
428
  { qRgb(139, 58, 58),  "indianred4" },
429
  { qRgb(255,255,240),  "ivory" },
430
  { qRgb(255,255,240),  "ivory1" },
431
  { qRgb(238,238,224),  "ivory2" },
432
  { qRgb(205,205,193),  "ivory3" },
433
  { qRgb(139,139,131),  "ivory4" },
434
  { qRgb(240,230,140),  "khaki" },
435
  { qRgb(255,246,143),  "khaki1" },
436
  { qRgb(238,230,133),  "khaki2" },
437
  { qRgb(205,198,115),  "khaki3" },
438
  { qRgb(139,134, 78),  "khaki4" },
439
  { qRgb(230,230,250),  "lavender" },
440
  { qRgb(255,240,245),  "lavenderblush" },
441
  { qRgb(255,240,245),  "lavenderblush1" },
442
  { qRgb(238,224,229),  "lavenderblush2" },
443
  { qRgb(205,193,197),  "lavenderblush3" },
444
  { qRgb(139,131,134),  "lavenderblush4" },
445
  { qRgb(124,252,  0),  "lawngreen" },
446
  { qRgb(255,250,205),  "lemonchiffon" },
447
  { qRgb(255,250,205),  "lemonchiffon1" },
448
  { qRgb(238,233,191),  "lemonchiffon2" },
449
  { qRgb(205,201,165),  "lemonchiffon3" },
450
  { qRgb(139,137,112),  "lemonchiffon4" },
451
  { qRgb(173,216,230),  "lightblue" },
452
  { qRgb(191,239,255),  "lightblue1" },
453
  { qRgb(178,223,238),  "lightblue2" },
454
  { qRgb(154,192,205),  "lightblue3" },
455
  { qRgb(104,131,139),  "lightblue4" },
456
  { qRgb(240,128,128),  "lightcoral" },
457
  { qRgb(224,255,255),  "lightcyan" },
458
  { qRgb(224,255,255),  "lightcyan1" },
459
  { qRgb(209,238,238),  "lightcyan2" },
460
  { qRgb(180,205,205),  "lightcyan3" },
461
  { qRgb(122,139,139),  "lightcyan4" },
462
  { qRgb(238,221,130),  "lightgoldenrod" },
463
  { qRgb(255,236,139),  "lightgoldenrod1" },
464
  { qRgb(238,220,130),  "lightgoldenrod2" },
465
  { qRgb(205,190,112),  "lightgoldenrod3" },
466
  { qRgb(139,129, 76),  "lightgoldenrod4" },
467
  { qRgb(250,250,210),  "lightgoldenrodyellow" },
468
  { qRgb(211,211,211),  "lightgray" },
469
  { qRgb(144,238,144),  "lightgreen" },
470
  { qRgb(211,211,211),  "lightgrey" },
471
  { qRgb(255,182,193),  "lightpink" },
472
  { qRgb(255,174,185),  "lightpink1" },
473
  { qRgb(238,162,173),  "lightpink2" },
474
  { qRgb(205,140,149),  "lightpink3" },
475
  { qRgb(139, 95,101),  "lightpink4" },
476
  { qRgb(255,160,122),  "lightsalmon" },
477
  { qRgb(255,160,122),  "lightsalmon1" },
478
  { qRgb(238,149,114),  "lightsalmon2" },
479
  { qRgb(205,129, 98),  "lightsalmon3" },
480
  { qRgb(139, 87, 66),  "lightsalmon4" },
481
  { qRgb( 32,178,170),  "lightseagreen" },
482
  { qRgb(135,206,250),  "lightskyblue" },
483
  { qRgb(176,226,255),  "lightskyblue1" },
484
  { qRgb(164,211,238),  "lightskyblue2" },
485
  { qRgb(141,182,205),  "lightskyblue3" },
486
  { qRgb( 96,123,139),  "lightskyblue4" },
487
  { qRgb(132,112,255),  "lightslateblue" },
488
  { qRgb(119,136,153),  "lightslategray" },
489
  { qRgb(119,136,153),  "lightslategrey" },
490
  { qRgb(176,196,222),  "lightsteelblue" },
491
  { qRgb(202,225,255),  "lightsteelblue1" },
492
  { qRgb(188,210,238),  "lightsteelblue2" },
493
  { qRgb(162,181,205),  "lightsteelblue3" },
494
  { qRgb(110,123,139),  "lightsteelblue4" },
495
  { qRgb(255,255,224),  "lightyellow" },
496
  { qRgb(255,255,224),  "lightyellow1" },
497
  { qRgb(238,238,209),  "lightyellow2" },
498
  { qRgb(205,205,180),  "lightyellow3" },
499
  { qRgb(139,139,122),  "lightyellow4" },
500
  { qRgb( 50,205, 50),  "limegreen" },
501
  { qRgb(250,240,230),  "linen" },
502
  { qRgb(255,  0,255),  "magenta" },
503
  { qRgb(255,  0,255),  "magenta1" },
504
  { qRgb(238,  0,238),  "magenta2" },
505
  { qRgb(205,  0,205),  "magenta3" },
506
  { qRgb(139,  0,139),  "magenta4" },
507
  { qRgb(176, 48, 96),  "maroon" },
508
  { qRgb(255, 52,179),  "maroon1" },
509
  { qRgb(238, 48,167),  "maroon2" },
510
  { qRgb(205, 41,144),  "maroon3" },
511
  { qRgb(139, 28, 98),  "maroon4" },
512
  { qRgb(102,205,170),  "mediumaquamarine" },
513
  { qRgb(  0,  0,205),  "mediumblue" },
514
  { qRgb(186, 85,211),  "mediumorchid" },
515
  { qRgb(224,102,255),  "mediumorchid1" },
516
  { qRgb(209, 95,238),  "mediumorchid2" },
517
  { qRgb(180, 82,205),  "mediumorchid3" },
518
  { qRgb(122, 55,139),  "mediumorchid4" },
519
  { qRgb(147,112,219),  "mediumpurple" },
520
  { qRgb(171,130,255),  "mediumpurple1" },
521
  { qRgb(159,121,238),  "mediumpurple2" },
522
  { qRgb(137,104,205),  "mediumpurple3" },
523
  { qRgb( 93, 71,139),  "mediumpurple4" },
524
  { qRgb( 60,179,113),  "mediumseagreen" },
525
  { qRgb(123,104,238),  "mediumslateblue" },
526
  { qRgb(  0,250,154),  "mediumspringgreen" },
527
  { qRgb( 72,209,204),  "mediumturquoise" },
528
  { qRgb(199, 21,133),  "mediumvioletred" },
529
  { qRgb( 25, 25,112),  "midnightblue" },
530
  { qRgb(245,255,250),  "mintcream" },
531
  { qRgb(255,228,225),  "mistyrose" },
532
  { qRgb(255,228,225),  "mistyrose1" },
533
  { qRgb(238,213,210),  "mistyrose2" },
534
  { qRgb(205,183,181),  "mistyrose3" },
535
  { qRgb(139,125,123),  "mistyrose4" },
536
  { qRgb(255,228,181),  "moccasin" },
537
  { qRgb(255,222,173),  "navajowhite" },
538
  { qRgb(255,222,173),  "navajowhite1" },
539
  { qRgb(238,207,161),  "navajowhite2" },
540
  { qRgb(205,179,139),  "navajowhite3" },
541
  { qRgb(139,121, 94),  "navajowhite4" },
542
  { qRgb(  0,  0,128),  "navy" },
543
  { qRgb(  0,  0,128),  "navyblue" },
544
  { qRgb(253,245,230),  "oldlace" },
545
  { qRgb(107,142, 35),  "olivedrab" },
546
  { qRgb(192,255, 62),  "olivedrab1" },
547
  { qRgb(179,238, 58),  "olivedrab2" },
548
  { qRgb(154,205, 50),  "olivedrab3" },
549
  { qRgb(105,139, 34),  "olivedrab4" },
550
  { qRgb(255,165,  0),  "orange" },
551
  { qRgb(255,165,  0),  "orange1" },
552
  { qRgb(238,154,  0),  "orange2" },
553
  { qRgb(205,133,  0),  "orange3" },
554
  { qRgb(139, 90,  0),  "orange4" },
555
  { qRgb(255, 69,  0),  "orangered" },
556
  { qRgb(255, 69,  0),  "orangered1" },
557
  { qRgb(238, 64,  0),  "orangered2" },
558
  { qRgb(205, 55,  0),  "orangered3" },
559
  { qRgb(139, 37,  0),  "orangered4" },
560
  { qRgb(218,112,214),  "orchid" },
561
  { qRgb(255,131,250),  "orchid1" },
562
  { qRgb(238,122,233),  "orchid2" },
563
  { qRgb(205,105,201),  "orchid3" },
564
  { qRgb(139, 71,137),  "orchid4" },
565
  { qRgb(238,232,170),  "palegoldenrod" },
566
  { qRgb(152,251,152),  "palegreen" },
567
  { qRgb(154,255,154),  "palegreen1" },
568
  { qRgb(144,238,144),  "palegreen2" },
569
  { qRgb(124,205,124),  "palegreen3" },
570
  { qRgb( 84,139, 84),  "palegreen4" },
571
  { qRgb(175,238,238),  "paleturquoise" },
572
  { qRgb(187,255,255),  "paleturquoise1" },
573
  { qRgb(174,238,238),  "paleturquoise2" },
574
  { qRgb(150,205,205),  "paleturquoise3" },
575
  { qRgb(102,139,139),  "paleturquoise4" },
576
  { qRgb(219,112,147),  "palevioletred" },
577
  { qRgb(255,130,171),  "palevioletred1" },
578
  { qRgb(238,121,159),  "palevioletred2" },
579
  { qRgb(205,104,137),  "palevioletred3" },
580
  { qRgb(139, 71, 93),  "palevioletred4" },
581
  { qRgb(255,239,213),  "papayawhip" },
582
  { qRgb(255,218,185),  "peachpuff" },
583
  { qRgb(255,218,185),  "peachpuff1" },
584
  { qRgb(238,203,173),  "peachpuff2" },
585
  { qRgb(205,175,149),  "peachpuff3" },
586
  { qRgb(139,119,101),  "peachpuff4" },
587
  { qRgb(205,133, 63),  "peru" },
588
  { qRgb(255,192,203),  "pink" },
589
  { qRgb(255,181,197),  "pink1" },
590
  { qRgb(238,169,184),  "pink2" },
591
  { qRgb(205,145,158),  "pink3" },
592
  { qRgb(139, 99,108),  "pink4" },
593
  { qRgb(221,160,221),  "plum" },
594
  { qRgb(255,187,255),  "plum1" },
595
  { qRgb(238,174,238),  "plum2" },
596
  { qRgb(205,150,205),  "plum3" },
597
  { qRgb(139,102,139),  "plum4" },
598
  { qRgb(176,224,230),  "powderblue" },
599
  { qRgb(160, 32,240),  "purple" },
600
  { qRgb(155, 48,255),  "purple1" },
601
  { qRgb(145, 44,238),  "purple2" },
602
  { qRgb(125, 38,205),  "purple3" },
603
  { qRgb( 85, 26,139),  "purple4" },
604
  { qRgb(255,  0,  0),  "red" },
605
  { qRgb(255,  0,  0),  "red1" },
606
  { qRgb(238,  0,  0),  "red2" },
607
  { qRgb(205,  0,  0),  "red3" },
608
  { qRgb(139,  0,  0),  "red4" },
609
  { qRgb(188,143,143),  "rosybrown" },
610
  { qRgb(255,193,193),  "rosybrown1" },
611
  { qRgb(238,180,180),  "rosybrown2" },
612
  { qRgb(205,155,155),  "rosybrown3" },
613
  { qRgb(139,105,105),  "rosybrown4" },
614
  { qRgb( 65,105,225),  "royalblue" },
615
  { qRgb( 72,118,255),  "royalblue1" },
616
  { qRgb( 67,110,238),  "royalblue2" },
617
  { qRgb( 58, 95,205),  "royalblue3" },
618
  { qRgb( 39, 64,139),  "royalblue4" },
619
  { qRgb(139, 69, 19),  "saddlebrown" },
620
  { qRgb(250,128,114),  "salmon" },
621
  { qRgb(255,140,105),  "salmon1" },
622
  { qRgb(238,130, 98),  "salmon2" },
623
  { qRgb(205,112, 84),  "salmon3" },
624
  { qRgb(139, 76, 57),  "salmon4" },
625
  { qRgb(244,164, 96),  "sandybrown" },
626
  { qRgb( 46,139, 87),  "seagreen" },
627
  { qRgb( 84,255,159),  "seagreen1" },
628
  { qRgb( 78,238,148),  "seagreen2" },
629
  { qRgb( 67,205,128),  "seagreen3" },
630
  { qRgb( 46,139, 87),  "seagreen4" },
631
  { qRgb(255,245,238),  "seashell" },
632
  { qRgb(255,245,238),  "seashell1" },
633
  { qRgb(238,229,222),  "seashell2" },
634
  { qRgb(205,197,191),  "seashell3" },
635
  { qRgb(139,134,130),  "seashell4" },
636
  { qRgb(160, 82, 45),  "sienna" },
637
  { qRgb(255,130, 71),  "sienna1" },
638
  { qRgb(238,121, 66),  "sienna2" },
639
  { qRgb(205,104, 57),  "sienna3" },
640
  { qRgb(139, 71, 38),  "sienna4" },
641
  { qRgb(135,206,235),  "skyblue" },
642
  { qRgb(135,206,255),  "skyblue1" },
643
  { qRgb(126,192,238),  "skyblue2" },
644
  { qRgb(108,166,205),  "skyblue3" },
645
  { qRgb( 74,112,139),  "skyblue4" },
646
  { qRgb(106, 90,205),  "slateblue" },
647
  { qRgb(131,111,255),  "slateblue1" },
648
  { qRgb(122,103,238),  "slateblue2" },
649
  { qRgb(105, 89,205),  "slateblue3" },
650
  { qRgb( 71, 60,139),  "slateblue4" },
651
  { qRgb(112,128,144),  "slategray" },
652
  { qRgb(198,226,255),  "slategray1" },
653
  { qRgb(185,211,238),  "slategray2" },
654
  { qRgb(159,182,205),  "slategray3" },
655
  { qRgb(108,123,139),  "slategray4" },
656
  { qRgb(112,128,144),  "slategrey" },
657
  { qRgb(255,250,250),  "snow" },
658
  { qRgb(255,250,250),  "snow1" },
659
  { qRgb(238,233,233),  "snow2" },
660
  { qRgb(205,201,201),  "snow3" },
661
  { qRgb(139,137,137),  "snow4" },
662
  { qRgb(  0,255,127),  "springgreen" },
663
  { qRgb(  0,255,127),  "springgreen1" },
664
  { qRgb(  0,238,118),  "springgreen2" },
665
  { qRgb(  0,205,102),  "springgreen3" },
666
  { qRgb(  0,139, 69),  "springgreen4" },
667
  { qRgb( 70,130,180),  "steelblue" },
668
  { qRgb( 99,184,255),  "steelblue1" },
669
  { qRgb( 92,172,238),  "steelblue2" },
670
  { qRgb( 79,148,205),  "steelblue3" },
671
  { qRgb( 54,100,139),  "steelblue4" },
672
  { qRgb(210,180,140),  "tan" },
673
  { qRgb(255,165, 79),  "tan1" },
674
  { qRgb(238,154, 73),  "tan2" },
675
  { qRgb(205,133, 63),  "tan3" },
676
  { qRgb(139, 90, 43),  "tan4" },
677
  { qRgb(216,191,216),  "thistle" },
678
  { qRgb(255,225,255),  "thistle1" },
679
  { qRgb(238,210,238),  "thistle2" },
680
  { qRgb(205,181,205),  "thistle3" },
681
  { qRgb(139,123,139),  "thistle4" },
682
  { qRgb(255, 99, 71),  "tomato" },
683
  { qRgb(255, 99, 71),  "tomato1" },
684
  { qRgb(238, 92, 66),  "tomato2" },
685
  { qRgb(205, 79, 57),  "tomato3" },
686
  { qRgb(139, 54, 38),  "tomato4" },
687
  { qRgb( 64,224,208),  "turquoise" },
688
  { qRgb(  0,245,255),  "turquoise1" },
689
  { qRgb(  0,229,238),  "turquoise2" },
690
  { qRgb(  0,197,205),  "turquoise3" },
691
  { qRgb(  0,134,139),  "turquoise4" },
692
  { qRgb(238,130,238),  "violet" },
693
  { qRgb(208, 32,144),  "violetred" },
694
  { qRgb(255, 62,150),  "violetred1" },
695
  { qRgb(238, 58,140),  "violetred2" },
696
  { qRgb(205, 50,120),  "violetred3" },
697
  { qRgb(139, 34, 82),  "violetred4" },
698
  { qRgb(245,222,179),  "wheat" },
699
  { qRgb(255,231,186),  "wheat1" },
700
  { qRgb(238,216,174),  "wheat2" },
701
  { qRgb(205,186,150),  "wheat3" },
702
  { qRgb(139,126,102),  "wheat4" },
703
  { qRgb(255,255,255),  "white" },
704
  { qRgb(245,245,245),  "whitesmoke" },
705
  { qRgb(255,255,  0),  "yellow" },
706
  { qRgb(255,255,  0),  "yellow1" },
707
  { qRgb(238,238,  0),  "yellow2" },
708
  { qRgb(205,205,  0),  "yellow3" },
709
  { qRgb(139,139,  0),  "yellow4" },
710
  { qRgb(154,205, 50),  "yellowgreen" } };
711
712
713
inline bool operator<(const char *name, const XPMRGBData &data)
714
0
{ return qstrcmp(name, data.name) < 0; }
715
inline bool operator<(const XPMRGBData &data, const char *name)
716
0
{ return qstrcmp(data.name, name) < 0; }
717
718
static inline std::optional<QRgb> qt_get_named_xpm_rgb(const char *name_no_space)
719
0
{
720
0
    const auto end = std::end(xpmRgbTbl);
721
0
    const auto it = std::lower_bound(std::begin(xpmRgbTbl), end, name_no_space);
722
0
    if (it != end && !(name_no_space < *it))
723
0
        return it->value;
724
0
    return {};
725
0
}
726
727
/*****************************************************************************
728
  Misc. utility functions
729
 *****************************************************************************/
730
static QString fbname(const QString &fileName) // get file basename (sort of)
731
0
{
732
0
    QString s = fileName;
733
0
    if (!s.isEmpty()) {
734
0
        int i = qMax(s.lastIndexOf(u'/'), s.lastIndexOf(u'\\'));
735
0
        if (i < 0)
736
0
            i = 0;
737
0
        auto checkChar = [](QChar ch) -> bool {
738
0
            uchar uc = ch.unicode();
739
0
            return isAsciiLetterOrNumber(uc) || uc == '_';
740
0
        };
741
0
        int start = -1;
742
0
        for (; i < s.size(); ++i) {
743
0
            if (checkChar(s.at(i))) {
744
0
                start = i;
745
0
            } else if (start > 0)
746
0
                break;
747
0
        }
748
0
        if (start < 0)
749
0
            s.clear();
750
0
        else
751
0
            s = s.mid(start, i - start);
752
0
    }
753
0
    if (s.isEmpty())
754
0
        s = QString::fromLatin1("dummy");
755
0
    return s;
756
0
}
757
758
// Skip until ", read until the next ", return the rest in *buf
759
// Returns false on error, true on success
760
761
static bool read_xpm_string(QByteArray &buf, QIODevice *d, const char * const *source, int &index,
762
                            QByteArray &state)
763
0
{
764
0
    if (source) {
765
0
        buf = source[index++];
766
0
        return true;
767
0
    }
768
769
0
    buf = "";
770
0
    bool gotQuote = false;
771
0
    int offset = 0;
772
0
    forever {
773
0
        if (offset == state.size() || state.isEmpty()) {
774
0
            char buf[2048];
775
0
            qint64 bytesRead = d->read(buf, sizeof(buf));
776
0
            if (bytesRead <= 0)
777
0
                return false;
778
0
            state = QByteArray(buf, int(bytesRead));
779
0
            offset = 0;
780
0
        }
781
782
0
        if (!gotQuote) {
783
0
            if (state.at(offset++) == '"')
784
0
                gotQuote = true;
785
0
        } else {
786
0
            char c = state.at(offset++);
787
0
            if (c == '"')
788
0
                break;
789
0
            buf += c;
790
0
        }
791
0
    }
792
0
    state.remove(0, offset);
793
0
    return true;
794
0
}
795
796
// Tests if the given prefix can be the start of an XPM color specification
797
798
static bool is_xpm_color_spec_prefix(const QByteArray& prefix)
799
0
{
800
0
    return prefix == "c" ||
801
0
           prefix == "g" ||
802
0
           prefix == "g4" ||
803
0
           prefix == "m" ||
804
0
           prefix == "s";
805
0
}
806
807
// Reads XPM header.
808
809
static bool read_xpm_header(
810
    QIODevice *device, const char * const * source, int& index, QByteArray &state,
811
    int *cpp, int *ncols, int *w, int *h)
812
0
{
813
0
    QByteArray buf(200, 0);
814
815
0
    if (!read_xpm_string(buf, device, source, index, state))
816
0
        return false;
817
818
#ifdef Q_CC_MSVC
819
        if (sscanf_s(buf, "%d %d %d %d", w, h, ncols, cpp) < 4)
820
#else
821
0
    if (sscanf(buf, "%d %d %d %d", w, h, ncols, cpp) < 4)
822
0
#endif
823
0
        return false;                                        // < 4 numbers parsed
824
825
0
    if (*w <= 0 || *w > 32767 || *h <= 0 || *h > 32767 || *ncols <= 0 || *ncols > (64 * 64 * 64 * 64) || *cpp <= 0 || *cpp > 15)
826
0
        return false;                                        // failed sanity check
827
828
0
    return true;
829
0
}
830
831
// Reads XPM body (color information & pixels).
832
833
static bool read_xpm_body(
834
    QIODevice *device, const char * const * source, int& index, QByteArray& state,
835
    int cpp, int ncols, int w, int h, QImage& image)
836
0
{
837
0
    QByteArray buf(200, 0);
838
0
    int i;
839
840
0
    if (cpp < 0 || cpp > 15)
841
0
        return false;
842
843
    // For > 256 colors, the image should be Format_ARGB32 if there is a "c none" color
844
    // specification, otherwise Format_RGB32. Allocate as the former, reinterpret later.
845
0
    const QImage::Format format = ncols <= 256 ? QImage::Format_Indexed8 : QImage::Format_ARGB32;
846
0
    if (!QImageIOHandler::allocateImage(QSize(w, h), format, &image))
847
0
        return false;
848
0
    if (ncols <= 256) {
849
0
        image.setColorCount(ncols);
850
0
    } else if (const int mbLimit = QImageReader::allocationLimit()) {
851
        // Bound also the temporary color map which uses ~64 bytes per color
852
0
        if (image.sizeInBytes() + qint64(ncols) * 64 > qint64(mbLimit) * 1024 * 1024) {
853
0
            qCWarning(lcImageIo, "XPM color table exceeds allocation limit");
854
0
            return false;
855
0
        }
856
0
    }
857
858
0
    QMap<quint64, int> colorMap;
859
0
    int currentColor;
860
0
    bool hasTransparency = false;
861
862
0
    for(currentColor=0; currentColor < ncols; ++currentColor) {
863
0
        if (!read_xpm_string(buf, device, source, index, state)) {
864
0
            qCWarning(lcImageIo, "XPM color specification missing");
865
0
            return false;
866
0
        }
867
0
        QByteArray index;
868
0
        index = buf.left(cpp);
869
0
        buf = buf.mid(cpp).simplified().trimmed().toLower();
870
0
        QList<QByteArray> tokens = buf.split(' ');
871
0
        i = tokens.indexOf('c');
872
0
        if (i < 0)
873
0
            i = tokens.indexOf('g');
874
0
        if (i < 0)
875
0
            i = tokens.indexOf("g4");
876
0
        if (i < 0)
877
0
            i = tokens.indexOf('m');
878
0
        if (i < 0) {
879
0
            qCWarning(lcImageIo, "XPM color specification is missing: %s", buf.constData());
880
0
            return false;        // no c/g/g4/m specification at all
881
0
        }
882
0
        QByteArray color;
883
0
        while ((++i < tokens.size()) && !is_xpm_color_spec_prefix(tokens.at(i))) {
884
0
            color.append(tokens.at(i));
885
0
        }
886
0
        if (color.isEmpty()) {
887
0
            qCWarning(lcImageIo, "XPM color value is missing from specification: %s", buf.constData());
888
0
            return false;        // no color value
889
0
        }
890
0
        buf = color;
891
0
        if (buf == "none") {
892
0
            hasTransparency = true;
893
0
            int transparentColor = currentColor;
894
0
            if (ncols <= 256) {
895
0
                image.setColor(transparentColor, 0);
896
0
                colorMap.insert(xpmHash(QLatin1StringView(index.constData())), transparentColor);
897
0
            } else {
898
0
                colorMap.insert(xpmHash(QLatin1StringView(index.constData())), 0);
899
0
            }
900
0
        } else {
901
0
            QRgb c_rgb = 0;
902
0
            if (((buf.size()-1) % 3) && (buf[0] == '#')) {
903
0
                buf.truncate(((buf.size()-1) / 4 * 3) + 1); // remove alpha channel left by imagemagick
904
0
            }
905
0
            if (buf[0] == '#') {
906
0
                c_rgb = qt_get_hex_rgb(buf).value_or(0);
907
0
            } else {
908
0
                c_rgb = qt_get_named_xpm_rgb(buf).value_or(0);
909
0
            }
910
0
            if (ncols <= 256) {
911
0
                image.setColor(currentColor, 0xff000000 | c_rgb);
912
0
                colorMap.insert(xpmHash(QLatin1StringView(index.constData())), currentColor);
913
0
            } else {
914
0
                colorMap.insert(xpmHash(QLatin1StringView(index.constData())), 0xff000000 | c_rgb);
915
0
            }
916
0
        }
917
0
    }
918
919
0
    if (ncols > 256 && !hasTransparency)
920
0
        image.reinterpretAsFormat(QImage::Format_RGB32); // free format change from ARGB32
921
922
    // Read pixels
923
0
    for(int y=0; y<h; y++) {
924
0
        if (!read_xpm_string(buf, device, source, index, state)) {
925
0
            qCWarning(lcImageIo, "XPM pixels missing on image line %d", y);
926
0
            return false;
927
0
        }
928
0
        if (image.depth() == 8) {
929
0
            uchar *p = image.scanLine(y);
930
0
            uchar *d = (uchar *)buf.data();
931
0
            uchar *end = d + buf.size();
932
0
            int x;
933
0
            if (cpp == 1) {
934
0
                char b[2];
935
0
                b[1] = '\0';
936
0
                for (x=0; x<w && d<end; x++) {
937
0
                    b[0] = *d++;
938
0
                    *p++ = (uchar)colorMap.value(xpmHash(b));
939
0
                }
940
0
            } else {
941
0
                char b[16];
942
0
                b[cpp] = '\0';
943
0
                for (x = 0; x < w && d + cpp <= end; x++) {
944
0
                    memcpy(b, (char *)d, cpp);
945
0
                    *p++ = (uchar)colorMap.value(xpmHash(b));
946
0
                    d += cpp;
947
0
                }
948
0
            }
949
            // avoid uninitialized memory for malformed xpms
950
0
            if (x < w) {
951
0
                qCWarning(lcImageIo, "XPM pixels missing on image line %d (possibly a C++ trigraph).", y);
952
0
                memset(p, 0, w - x);
953
0
            }
954
0
        } else {
955
0
            QRgb *p = (QRgb*)image.scanLine(y);
956
0
            uchar *d = (uchar *)buf.data();
957
0
            uchar *end = d + buf.size();
958
0
            int x;
959
0
            char b[16];
960
0
            b[cpp] = '\0';
961
0
            for (x = 0; x < w && d + cpp <= end; x++) {
962
0
                memcpy(b, (char *)d, cpp);
963
0
                *p++ = (QRgb)colorMap.value(xpmHash(b));
964
0
                d += cpp;
965
0
            }
966
            // avoid uninitialized memory for malformed xpms
967
0
            if (x < w) {
968
0
                qCWarning(lcImageIo, "XPM pixels missing on image line %d (possibly a C++ trigraph).", y);
969
0
                memset(p, 0, (w - x)*4);
970
0
            }
971
0
        }
972
0
    }
973
974
0
    if (device) {
975
        // Rewind unused characters, and skip to the end of the XPM struct.
976
0
        for (int i = state.size() - 1; i >= 0; --i)
977
0
            device->ungetChar(state[i]);
978
0
        char c;
979
0
        while (device->getChar(&c) && c != ';') {}
980
0
        while (device->getChar(&c) && c != '\n') {}
981
0
    }
982
0
    return true;
983
0
}
984
985
//
986
// INTERNAL
987
//
988
// Reads an .xpm from either the QImageIO or from the QString *.
989
// One of the two HAS to be 0, the other one is used.
990
//
991
992
bool qt_read_xpm_image_or_array(QIODevice *device, const char * const * source, QImage &image)
993
0
{
994
0
    if (!source)
995
0
        return true;
996
997
0
    QByteArray buf(200, 0);
998
0
    QByteArray state;
999
1000
0
    int cpp, ncols, w, h, index = 0;
1001
1002
0
    if (device) {
1003
        // "/* XPM */"
1004
0
        int readBytes;
1005
0
        if ((readBytes = device->readLine(buf.data(), buf.size())) < 0)
1006
0
            return false;
1007
1008
0
        static constexpr auto matcher = qMakeStaticByteArrayMatcher("/* XPM");
1009
1010
0
        if (matcher.indexIn(buf) != 0) {
1011
0
            while (readBytes > 0) {
1012
0
                device->ungetChar(buf.at(readBytes - 1));
1013
0
                --readBytes;
1014
0
            }
1015
0
            return false;
1016
0
        }// bad magic
1017
0
    }
1018
1019
0
    if (!read_xpm_header(device, source, index, state, &cpp, &ncols, &w, &h))
1020
0
        return false;
1021
1022
0
    return read_xpm_body(device, source, index, state, cpp, ncols, w, h, image);
1023
0
}
1024
1025
namespace {
1026
template <size_t N>
1027
struct CharBuffer : std::array<char, N>
1028
{
1029
0
    CharBuffer() {} // avoid value-initializing the whole array
1030
};
1031
}
1032
1033
static const char* xpm_color_name(int cpp, int index, CharBuffer<5> && returnable = {})
1034
0
{
1035
0
    static const char code[] = ".#abcdefghijklmnopqrstuvwxyzABCD"
1036
0
                               "EFGHIJKLMNOPQRSTUVWXYZ0123456789";
1037
    // cpp is limited to 4 and index is limited to 64^cpp
1038
0
    if (cpp > 1) {
1039
0
        if (cpp > 2) {
1040
0
            if (cpp > 3) {
1041
0
                returnable[4] = '\0';
1042
0
                returnable[3] = code[index % 64];
1043
0
                index /= 64;
1044
0
            } else
1045
0
                returnable[3] = '\0';
1046
0
            returnable[2] = code[index % 64];
1047
0
            index /= 64;
1048
0
        } else
1049
0
            returnable[2] = '\0';
1050
        // the following 4 lines are a joke!
1051
0
        if (index == 0)
1052
0
            index = 64*44+21;
1053
0
        else if (index == 64*44+21)
1054
0
            index = 0;
1055
0
        returnable[1] = code[index % 64];
1056
0
        index /= 64;
1057
0
    } else
1058
0
        returnable[1] = '\0';
1059
0
    returnable[0] = code[index];
1060
1061
0
    return returnable.data();
1062
0
}
1063
1064
1065
// write XPM image data
1066
static bool write_xpm_image(const QImage &sourceImage, QIODevice *device, const QString &fileName)
1067
0
{
1068
0
    if (!device->isWritable())
1069
0
        return false;
1070
1071
0
    QImage image;
1072
0
    if (sourceImage.format() != QImage::Format_RGB32 && sourceImage.format() != QImage::Format_ARGB32 && sourceImage.format() != QImage::Format_ARGB32_Premultiplied)
1073
0
        image = sourceImage.convertToFormat(QImage::Format_RGB32);
1074
0
    else
1075
0
        image = sourceImage;
1076
1077
0
#ifdef __cpp_lib_memory_resource
1078
0
    char buffer[1024];
1079
0
    std::pmr::monotonic_buffer_resource res{&buffer, sizeof buffer};
1080
0
    std::pmr::map<QRgb, int> colorMap(&res);
1081
#else
1082
    std::map<QRgb, int> colorMap;
1083
#endif
1084
1085
0
    const int w = image.width();
1086
0
    const int h = image.height();
1087
0
    int ncolors = 0;
1088
1089
    // build color table
1090
0
    for (int y = 0; y < h; ++y) {
1091
0
        const QRgb *yp = reinterpret_cast<const QRgb *>(image.constScanLine(y));
1092
0
        for (int x = 0; x < w; ++x) {
1093
0
            const auto [it, inserted] = colorMap.try_emplace(yp[x], ncolors);
1094
0
            if (inserted)
1095
0
                ++ncolors;
1096
0
        }
1097
0
    }
1098
1099
    // number of 64-bit characters per pixel needed to encode all colors
1100
0
    int cpp = 1;
1101
0
    for (int k = 64; ncolors > k; k *= 64) {
1102
0
        ++cpp;
1103
        // limit to 4 characters per pixel
1104
        // 64^4 colors is enough for a 4096x4096 image
1105
0
         if (cpp > 4) {
1106
0
             qCWarning(lcImageIo, "Qt does not support writing XPM images with more than "
1107
0
                       "64^4 colors (requested: %d colors).", ncolors);
1108
0
             return false;
1109
0
         }
1110
0
    }
1111
1112
    // write header
1113
0
    QTextStream s(device);
1114
0
    s << "/* XPM */" << Qt::endl
1115
0
      << "static char *" << fbname(fileName) << "[]={" << Qt::endl
1116
0
      << '\"' << w << ' ' << h << ' ' << ncolors << ' ' << cpp << '\"';
1117
1118
    // write palette
1119
0
    for (const auto &[color, index] : colorMap) {
1120
0
        const QString line = image.format() != QImage::Format_RGB32 && !qAlpha(color)
1121
0
            ? QString::asprintf("\"%s c None\"", xpm_color_name(cpp, index))
1122
0
            : QString::asprintf("\"%s c #%02x%02x%02x\"", xpm_color_name(cpp, index),
1123
0
                                qRed(color), qGreen(color), qBlue(color));
1124
0
        s << ',' << Qt::endl << line;
1125
0
    }
1126
1127
    // write pixels, limit to 4 characters per pixel
1128
0
    for (int y = 0; y < h; ++y) {
1129
0
        s << ',' << Qt::endl << '\"';
1130
0
        const QRgb *yp = reinterpret_cast<const QRgb *>(image.constScanLine(y));
1131
0
        for (int x = 0; x < w; ++x)
1132
0
            s << xpm_color_name(cpp, colorMap[yp[x]]);
1133
0
        s << '\"';
1134
0
    }
1135
0
    s << "};" << Qt::endl;
1136
0
    return static_cast<bool>(s);
1137
0
}
1138
1139
QXpmHandler::QXpmHandler()
1140
0
    : state(Ready), index(0)
1141
0
{
1142
0
}
1143
1144
bool QXpmHandler::readHeader()
1145
0
{
1146
0
    state = Error;
1147
0
    if (!read_xpm_header(device(), nullptr, index, buffer, &cpp, &ncols, &width, &height))
1148
0
        return false;
1149
0
    state = ReadHeader;
1150
0
    return true;
1151
0
}
1152
1153
bool QXpmHandler::readImage(QImage *image)
1154
0
{
1155
0
    if (state == Error)
1156
0
        return false;
1157
1158
0
    if (state == Ready && !readHeader()) {
1159
0
        state = Error;
1160
0
        return false;
1161
0
    }
1162
1163
0
    if (!read_xpm_body(device(), nullptr, index, buffer, cpp, ncols, width, height, *image)) {
1164
0
        state = Error;
1165
0
        return false;
1166
0
    }
1167
1168
0
    state = Ready;
1169
0
    return true;
1170
0
}
1171
1172
bool QXpmHandler::canRead() const
1173
0
{
1174
0
    if (state == Ready && !canRead(device()))
1175
0
        return false;
1176
1177
0
    if (state != Error) {
1178
0
        setFormat("xpm");
1179
0
        return true;
1180
0
    }
1181
1182
0
    return false;
1183
0
}
1184
1185
bool QXpmHandler::canRead(QIODevice *device)
1186
0
{
1187
0
    if (!device) {
1188
0
        qCWarning(lcImageIo, "QXpmHandler::canRead() called with no device");
1189
0
        return false;
1190
0
    }
1191
1192
0
    char head[6];
1193
0
    if (device->peek(head, sizeof(head)) != sizeof(head))
1194
0
        return false;
1195
1196
0
    return qstrncmp(head, "/* XPM", 6) == 0;
1197
0
}
1198
1199
bool QXpmHandler::read(QImage *image)
1200
0
{
1201
0
    if (!canRead())
1202
0
        return false;
1203
0
    return readImage(image);
1204
0
}
1205
1206
bool QXpmHandler::write(const QImage &image)
1207
0
{
1208
0
    return write_xpm_image(image, device(), fileName);
1209
0
}
1210
1211
bool QXpmHandler::supportsOption(ImageOption option) const
1212
0
{
1213
0
    return option == Name
1214
0
        || option == Size
1215
0
        || option == ImageFormat;
1216
0
}
1217
1218
QVariant QXpmHandler::option(ImageOption option) const
1219
0
{
1220
0
    if (option == Name) {
1221
0
        return fileName;
1222
0
    } else if (option == Size) {
1223
0
        if (state == Error)
1224
0
            return QVariant();
1225
0
        if (state == Ready && !const_cast<QXpmHandler*>(this)->readHeader())
1226
0
            return QVariant();
1227
0
        return QSize(width, height);
1228
0
    } else if (option == ImageFormat) {
1229
0
        if (state == Error)
1230
0
            return QVariant();
1231
0
        if (state == Ready && !const_cast<QXpmHandler*>(this)->readHeader())
1232
0
            return QVariant();
1233
        // If we have more than 256 colors in the table, we need to
1234
        // figure out, if it contains transparency. That means reading
1235
        // the whole color table, which is too much work work pre-checking
1236
        // the image format
1237
0
        if (ncols <= 256)
1238
0
            return QImage::Format_Indexed8;
1239
0
        else
1240
0
            return QImage::Format_Invalid;
1241
0
    }
1242
1243
0
    return QVariant();
1244
0
}
1245
1246
void QXpmHandler::setOption(ImageOption option, const QVariant &value)
1247
0
{
1248
0
    if (option == Name)
1249
0
        fileName = value.toString();
1250
0
}
1251
1252
QT_END_NAMESPACE
1253
1254
#endif // QT_NO_IMAGEFORMAT_XPM