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

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