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

source code of qtbase/src/gui/image/qxpmhandler.cpp