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