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