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