Coverage Report

Created: 2025-07-23 08:13

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