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