Added missing functions WinCE
[wxWidgets.git] / src / common / xpmdecod.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xpmdecod.cpp
3 // Purpose: wxXPMDecoder
4 // Author: John Cristy, Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) John Cristy, Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 /*
11
12 This file is partially based on source code of ImageMagick by John Cristy. Its
13 license is as follows:
14
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16 % %
17 % %
18 % %
19 % X X PPPP M M %
20 % X X P P MM MM %
21 % X PPPP M M M %
22 % X X P M M %
23 % X X P M M %
24 % %
25 % %
26 % Read/Write ImageMagick Image Format. %
27 % %
28 % %
29 % Software Design %
30 % John Cristy %
31 % July 1992 %
32 % %
33 % %
34 % Copyright (C) 2001 ImageMagick Studio, a non-profit organization dedicated %
35 % to making software imaging solutions freely available. %
36 % %
37 % Permission is hereby granted, free of charge, to any person obtaining a %
38 % copy of this software and associated documentation files ("ImageMagick"), %
39 % to deal in ImageMagick without restriction, including without limitation %
40 % the rights to use, copy, modify, merge, publish, distribute, sublicense, %
41 % and/or sell copies of ImageMagick, and to permit persons to whom the %
42 % ImageMagick is furnished to do so, subject to the following conditions: %
43 % %
44 % The above copyright notice and this permission notice shall be included in %
45 % all copies or substantial portions of ImageMagick. %
46 % %
47 % The software is provided "as is", without warranty of any kind, express or %
48 % implied, including but not limited to the warranties of merchantability, %
49 % fitness for a particular purpose and noninfringement. In no event shall %
50 % ImageMagick Studio be liable for any claim, damages or other liability, %
51 % whether in an action of contract, tort or otherwise, arising from, out of %
52 % or in connection with ImageMagick or the use or other dealings in %
53 % ImageMagick. %
54 % %
55 % Except as contained in this notice, the name of the ImageMagick Studio %
56 % shall not be used in advertising or otherwise to promote the sale, use or %
57 % other dealings in ImageMagick without prior written authorization from the %
58 % ImageMagick Studio. %
59 % %
60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
61 %
62 %
63 */
64
65 /*
66 * Also contains some pieces from libxpm and its modification for win32 by
67 * HeDu <hedu@cul-ipn.uni-kiel.de>:
68 *
69 * Copyright (C) 1989-95 GROUPE BULL
70 *
71 * Permission is hereby granted, free of charge, to any person obtaining a copy
72 * of this software and associated documentation files (the "Software"), to
73 * deal in the Software without restriction, including without limitation the
74 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
75 * sell copies of the Software, and to permit persons to whom the Software is
76 * furnished to do so, subject to the following conditions:
77 *
78 * The above copyright notice and this permission notice shall be included in
79 * all copies or substantial portions of the Software.
80 *
81 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
82 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
83 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
84 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
85 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
86 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
87 *
88 * Except as contained in this notice, the name of GROUPE BULL shall not be
89 * used in advertising or otherwise to promote the sale, use or other dealings
90 * in this Software without prior written authorization from GROUPE BULL.
91 */
92
93 #ifdef __GNUG__
94 #pragma implementation "xpmdecod.h"
95 #endif
96
97 // For compilers that support precompilation, includes "wx.h".
98 #include "wx/wxprec.h"
99
100 #ifdef __BORLANDC__
101 # pragma hdrstop
102 #endif
103
104 #ifndef WX_PRECOMP
105 # include "wx/defs.h"
106 #endif
107
108 #if wxUSE_IMAGE && wxUSE_XPM
109
110 #include "wx/stream.h"
111 #include "wx/image.h"
112 #include "wx/utils.h"
113 #include "wx/log.h"
114 #include "wx/hashmap.h"
115 #include "wx/intl.h"
116 #include <string.h>
117
118 #include <ctype.h>
119
120 #include "wx/xpmdecod.h"
121
122 #ifdef __WXWINCE__
123 char* strdup(const char* s)
124 {
125 char* s2 = new char[strlen(s)];
126 strcpy(s2, s);
127 return s2;
128 }
129
130 bool isspace(char c)
131 {
132 return (c == ' ');
133 }
134 #endif
135
136 #if wxUSE_STREAMS
137 bool wxXPMDecoder::CanRead(wxInputStream& stream)
138 {
139 unsigned char buf[9];
140
141 if ( !stream.Read(buf, WXSIZEOF(buf)) )
142 return FALSE;
143
144 stream.SeekI(-(off_t)WXSIZEOF(buf), wxFromCurrent);
145
146 return memcmp(buf, "/* XPM */", WXSIZEOF(buf)) == 0;
147 }
148
149 wxImage wxXPMDecoder::ReadFile(wxInputStream& stream)
150 {
151 size_t length = stream.GetSize();
152 wxCHECK_MSG( length != 0, wxNullImage,
153 wxT("Cannot read XPM from stream of unknown size") );
154
155 // use a smart buffer to be sure to free memory even when we return on
156 // error
157 wxCharBuffer buffer(length);
158
159 char *xpm_buffer = (char *)buffer.data();
160 if ( stream.Read(xpm_buffer, length).GetLastError() == wxSTREAM_READ_ERROR )
161 return wxNullImage;
162 xpm_buffer[length] = '\0';
163
164 /*
165 * Remove comments from the file:
166 */
167 char *p, *q;
168 for (p = xpm_buffer; *p != '\0'; p++)
169 {
170 if ( (*p == '"') || (*p == '\'') )
171 {
172 if (*p == '"')
173 {
174 for (p++; *p != '\0'; p++)
175 if ( (*p == '"') && (*(p - 1) != '\\') )
176 break;
177 }
178 else // *p == '\''
179 {
180 for (p++; *p != '\0'; p++)
181 if ( (*p == '\'') && (*(p - 1) != '\\') )
182 break;
183 }
184 if (*p == '\0')
185 break;
186 continue;
187 }
188 if ( (*p != '/') || (*(p + 1) != '*') )
189 continue;
190 for (q = p + 2; *q != '\0'; q++)
191 {
192 if ( (*q == '*') && (*(q + 1) == '/') )
193 break;
194 }
195 strcpy(p, q + 2);
196 }
197
198 /*
199 * Remove unquoted characters:
200 */
201 size_t i = 0;
202 for (p = xpm_buffer; *p != '\0'; p++)
203 {
204 if ( *p != '"' )
205 continue;
206 for (q = p + 1; *q != '\0'; q++)
207 if (*q == '"')
208 break;
209 strncpy(xpm_buffer + i, p + 1, q - p - 1);
210 i += q - p - 1;
211 xpm_buffer[i++] = '\n';
212 p = q + 1;
213 }
214 xpm_buffer[i] = '\0';
215
216 /*
217 * Create array of lines and convert \n's to \0's:
218 */
219 const char **xpm_lines;
220 size_t lines_cnt = 0;
221 size_t line;
222
223 for (p = xpm_buffer; *p != '\0'; p++)
224 {
225 if ( *p == '\n' )
226 lines_cnt++;
227 }
228
229 if ( !lines_cnt )
230 {
231 // this doesn't really look an XPM image
232 return wxNullImage;
233 }
234
235 xpm_lines = new const char*[lines_cnt];
236 xpm_lines[0] = xpm_buffer;
237 line = 1;
238 for (p = xpm_buffer; (*p != '\0') && (line < lines_cnt); p++)
239 {
240 if ( *p == '\n' )
241 {
242 xpm_lines[line] = p + 1;
243 *p = '\0';
244 line++;
245 }
246 }
247
248 /*
249 * Read the image:
250 */
251 wxImage img = ReadData(xpm_lines);
252
253 #ifdef __WIN16__
254 delete[] (char**) xpm_lines;
255 #else
256 delete[] xpm_lines;
257 #endif
258
259 return img;
260 }
261 #endif // wxUSE_STREAMS
262
263
264 /*****************************************************************************\
265 * rgbtab.h *
266 * *
267 * A hard coded rgb.txt. To keep it short I removed all colornames with *
268 * trailing numbers, Blue3 etc, except the GrayXX. Sorry Grey-lovers I prefer *
269 * Gray ;-). But Grey is recognized on lookups, only on save Gray will be *
270 * used, maybe you want to do some substitue there too. *
271 * *
272 * To save memory the RGBs are coded in one long value, as done by the RGB *
273 * macro. *
274 * *
275 * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) *
276 \*****************************************************************************/
277
278
279 typedef struct
280 {
281 const char *name;
282 wxUint32 rgb;
283 } rgbRecord;
284
285 #define myRGB(r,g,b) ((wxUint32)r<<16|(wxUint32)g<<8|(wxUint32)b)
286
287 static rgbRecord theRGBRecords[] =
288 {
289 {"aliceblue", myRGB(240, 248, 255)},
290 {"antiquewhite", myRGB(250, 235, 215)},
291 {"aquamarine", myRGB(50, 191, 193)},
292 {"azure", myRGB(240, 255, 255)},
293 {"beige", myRGB(245, 245, 220)},
294 {"bisque", myRGB(255, 228, 196)},
295 {"black", myRGB(0, 0, 0)},
296 {"blanchedalmond", myRGB(255, 235, 205)},
297 {"blue", myRGB(0, 0, 255)},
298 {"blueviolet", myRGB(138, 43, 226)},
299 {"brown", myRGB(165, 42, 42)},
300 {"burlywood", myRGB(222, 184, 135)},
301 {"cadetblue", myRGB(95, 146, 158)},
302 {"chartreuse", myRGB(127, 255, 0)},
303 {"chocolate", myRGB(210, 105, 30)},
304 {"coral", myRGB(255, 114, 86)},
305 {"cornflowerblue", myRGB(34, 34, 152)},
306 {"cornsilk", myRGB(255, 248, 220)},
307 {"cyan", myRGB(0, 255, 255)},
308 {"darkgoldenrod", myRGB(184, 134, 11)},
309 {"darkgreen", myRGB(0, 86, 45)},
310 {"darkkhaki", myRGB(189, 183, 107)},
311 {"darkolivegreen", myRGB(85, 86, 47)},
312 {"darkorange", myRGB(255, 140, 0)},
313 {"darkorchid", myRGB(139, 32, 139)},
314 {"darksalmon", myRGB(233, 150, 122)},
315 {"darkseagreen", myRGB(143, 188, 143)},
316 {"darkslateblue", myRGB(56, 75, 102)},
317 {"darkslategray", myRGB(47, 79, 79)},
318 {"darkturquoise", myRGB(0, 166, 166)},
319 {"darkviolet", myRGB(148, 0, 211)},
320 {"deeppink", myRGB(255, 20, 147)},
321 {"deepskyblue", myRGB(0, 191, 255)},
322 {"dimgray", myRGB(84, 84, 84)},
323 {"dodgerblue", myRGB(30, 144, 255)},
324 {"firebrick", myRGB(142, 35, 35)},
325 {"floralwhite", myRGB(255, 250, 240)},
326 {"forestgreen", myRGB(80, 159, 105)},
327 {"gainsboro", myRGB(220, 220, 220)},
328 {"ghostwhite", myRGB(248, 248, 255)},
329 {"gold", myRGB(218, 170, 0)},
330 {"goldenrod", myRGB(239, 223, 132)},
331 {"gray", myRGB(126, 126, 126)},
332 {"gray0", myRGB(0, 0, 0)},
333 {"gray1", myRGB(3, 3, 3)},
334 {"gray10", myRGB(26, 26, 26)},
335 {"gray100", myRGB(255, 255, 255)},
336 {"gray11", myRGB(28, 28, 28)},
337 {"gray12", myRGB(31, 31, 31)},
338 {"gray13", myRGB(33, 33, 33)},
339 {"gray14", myRGB(36, 36, 36)},
340 {"gray15", myRGB(38, 38, 38)},
341 {"gray16", myRGB(41, 41, 41)},
342 {"gray17", myRGB(43, 43, 43)},
343 {"gray18", myRGB(46, 46, 46)},
344 {"gray19", myRGB(48, 48, 48)},
345 {"gray2", myRGB(5, 5, 5)},
346 {"gray20", myRGB(51, 51, 51)},
347 {"gray21", myRGB(54, 54, 54)},
348 {"gray22", myRGB(56, 56, 56)},
349 {"gray23", myRGB(59, 59, 59)},
350 {"gray24", myRGB(61, 61, 61)},
351 {"gray25", myRGB(64, 64, 64)},
352 {"gray26", myRGB(66, 66, 66)},
353 {"gray27", myRGB(69, 69, 69)},
354 {"gray28", myRGB(71, 71, 71)},
355 {"gray29", myRGB(74, 74, 74)},
356 {"gray3", myRGB(8, 8, 8)},
357 {"gray30", myRGB(77, 77, 77)},
358 {"gray31", myRGB(79, 79, 79)},
359 {"gray32", myRGB(82, 82, 82)},
360 {"gray33", myRGB(84, 84, 84)},
361 {"gray34", myRGB(87, 87, 87)},
362 {"gray35", myRGB(89, 89, 89)},
363 {"gray36", myRGB(92, 92, 92)},
364 {"gray37", myRGB(94, 94, 94)},
365 {"gray38", myRGB(97, 97, 97)},
366 {"gray39", myRGB(99, 99, 99)},
367 {"gray4", myRGB(10, 10, 10)},
368 {"gray40", myRGB(102, 102, 102)},
369 {"gray41", myRGB(105, 105, 105)},
370 {"gray42", myRGB(107, 107, 107)},
371 {"gray43", myRGB(110, 110, 110)},
372 {"gray44", myRGB(112, 112, 112)},
373 {"gray45", myRGB(115, 115, 115)},
374 {"gray46", myRGB(117, 117, 117)},
375 {"gray47", myRGB(120, 120, 120)},
376 {"gray48", myRGB(122, 122, 122)},
377 {"gray49", myRGB(125, 125, 125)},
378 {"gray5", myRGB(13, 13, 13)},
379 {"gray50", myRGB(127, 127, 127)},
380 {"gray51", myRGB(130, 130, 130)},
381 {"gray52", myRGB(133, 133, 133)},
382 {"gray53", myRGB(135, 135, 135)},
383 {"gray54", myRGB(138, 138, 138)},
384 {"gray55", myRGB(140, 140, 140)},
385 {"gray56", myRGB(143, 143, 143)},
386 {"gray57", myRGB(145, 145, 145)},
387 {"gray58", myRGB(148, 148, 148)},
388 {"gray59", myRGB(150, 150, 150)},
389 {"gray6", myRGB(15, 15, 15)},
390 {"gray60", myRGB(153, 153, 153)},
391 {"gray61", myRGB(156, 156, 156)},
392 {"gray62", myRGB(158, 158, 158)},
393 {"gray63", myRGB(161, 161, 161)},
394 {"gray64", myRGB(163, 163, 163)},
395 {"gray65", myRGB(166, 166, 166)},
396 {"gray66", myRGB(168, 168, 168)},
397 {"gray67", myRGB(171, 171, 171)},
398 {"gray68", myRGB(173, 173, 173)},
399 {"gray69", myRGB(176, 176, 176)},
400 {"gray7", myRGB(18, 18, 18)},
401 {"gray70", myRGB(179, 179, 179)},
402 {"gray71", myRGB(181, 181, 181)},
403 {"gray72", myRGB(184, 184, 184)},
404 {"gray73", myRGB(186, 186, 186)},
405 {"gray74", myRGB(189, 189, 189)},
406 {"gray75", myRGB(191, 191, 191)},
407 {"gray76", myRGB(194, 194, 194)},
408 {"gray77", myRGB(196, 196, 196)},
409 {"gray78", myRGB(199, 199, 199)},
410 {"gray79", myRGB(201, 201, 201)},
411 {"gray8", myRGB(20, 20, 20)},
412 {"gray80", myRGB(204, 204, 204)},
413 {"gray81", myRGB(207, 207, 207)},
414 {"gray82", myRGB(209, 209, 209)},
415 {"gray83", myRGB(212, 212, 212)},
416 {"gray84", myRGB(214, 214, 214)},
417 {"gray85", myRGB(217, 217, 217)},
418 {"gray86", myRGB(219, 219, 219)},
419 {"gray87", myRGB(222, 222, 222)},
420 {"gray88", myRGB(224, 224, 224)},
421 {"gray89", myRGB(227, 227, 227)},
422 {"gray9", myRGB(23, 23, 23)},
423 {"gray90", myRGB(229, 229, 229)},
424 {"gray91", myRGB(232, 232, 232)},
425 {"gray92", myRGB(235, 235, 235)},
426 {"gray93", myRGB(237, 237, 237)},
427 {"gray94", myRGB(240, 240, 240)},
428 {"gray95", myRGB(242, 242, 242)},
429 {"gray96", myRGB(245, 245, 245)},
430 {"gray97", myRGB(247, 247, 247)},
431 {"gray98", myRGB(250, 250, 250)},
432 {"gray99", myRGB(252, 252, 252)},
433 {"green", myRGB(0, 255, 0)},
434 {"greenyellow", myRGB(173, 255, 47)},
435 {"honeydew", myRGB(240, 255, 240)},
436 {"hotpink", myRGB(255, 105, 180)},
437 {"indianred", myRGB(107, 57, 57)},
438 {"ivory", myRGB(255, 255, 240)},
439 {"khaki", myRGB(179, 179, 126)},
440 {"lavender", myRGB(230, 230, 250)},
441 {"lavenderblush", myRGB(255, 240, 245)},
442 {"lawngreen", myRGB(124, 252, 0)},
443 {"lemonchiffon", myRGB(255, 250, 205)},
444 {"lightblue", myRGB(176, 226, 255)},
445 {"lightcoral", myRGB(240, 128, 128)},
446 {"lightcyan", myRGB(224, 255, 255)},
447 {"lightgoldenrod", myRGB(238, 221, 130)},
448 {"lightgoldenrodyellow", myRGB(250, 250, 210)},
449 {"lightgray", myRGB(168, 168, 168)},
450 {"lightpink", myRGB(255, 182, 193)},
451 {"lightsalmon", myRGB(255, 160, 122)},
452 {"lightseagreen", myRGB(32, 178, 170)},
453 {"lightskyblue", myRGB(135, 206, 250)},
454 {"lightslateblue", myRGB(132, 112, 255)},
455 {"lightslategray", myRGB(119, 136, 153)},
456 {"lightsteelblue", myRGB(124, 152, 211)},
457 {"lightyellow", myRGB(255, 255, 224)},
458 {"limegreen", myRGB(0, 175, 20)},
459 {"linen", myRGB(250, 240, 230)},
460 {"magenta", myRGB(255, 0, 255)},
461 {"maroon", myRGB(143, 0, 82)},
462 {"mediumaquamarine", myRGB(0, 147, 143)},
463 {"mediumblue", myRGB(50, 50, 204)},
464 {"mediumforestgreen", myRGB(50, 129, 75)},
465 {"mediumgoldenrod", myRGB(209, 193, 102)},
466 {"mediumorchid", myRGB(189, 82, 189)},
467 {"mediumpurple", myRGB(147, 112, 219)},
468 {"mediumseagreen", myRGB(52, 119, 102)},
469 {"mediumslateblue", myRGB(106, 106, 141)},
470 {"mediumspringgreen", myRGB(35, 142, 35)},
471 {"mediumturquoise", myRGB(0, 210, 210)},
472 {"mediumvioletred", myRGB(213, 32, 121)},
473 {"midnightblue", myRGB(47, 47, 100)},
474 {"mintcream", myRGB(245, 255, 250)},
475 {"mistyrose", myRGB(255, 228, 225)},
476 {"moccasin", myRGB(255, 228, 181)},
477 {"navajowhite", myRGB(255, 222, 173)},
478 {"navy", myRGB(35, 35, 117)},
479 {"navyblue", myRGB(35, 35, 117)},
480 {"oldlace", myRGB(253, 245, 230)},
481 {"olivedrab", myRGB(107, 142, 35)},
482 {"orange", myRGB(255, 135, 0)},
483 {"orangered", myRGB(255, 69, 0)},
484 {"orchid", myRGB(239, 132, 239)},
485 {"palegoldenrod", myRGB(238, 232, 170)},
486 {"palegreen", myRGB(115, 222, 120)},
487 {"paleturquoise", myRGB(175, 238, 238)},
488 {"palevioletred", myRGB(219, 112, 147)},
489 {"papayawhip", myRGB(255, 239, 213)},
490 {"peachpuff", myRGB(255, 218, 185)},
491 {"peru", myRGB(205, 133, 63)},
492 {"pink", myRGB(255, 181, 197)},
493 {"plum", myRGB(197, 72, 155)},
494 {"powderblue", myRGB(176, 224, 230)},
495 {"purple", myRGB(160, 32, 240)},
496 {"red", myRGB(255, 0, 0)},
497 {"rosybrown", myRGB(188, 143, 143)},
498 {"royalblue", myRGB(65, 105, 225)},
499 {"saddlebrown", myRGB(139, 69, 19)},
500 {"salmon", myRGB(233, 150, 122)},
501 {"sandybrown", myRGB(244, 164, 96)},
502 {"seagreen", myRGB(82, 149, 132)},
503 {"seashell", myRGB(255, 245, 238)},
504 {"sienna", myRGB(150, 82, 45)},
505 {"silver", myRGB(192, 192, 192)},
506 {"skyblue", myRGB(114, 159, 255)},
507 {"slateblue", myRGB(126, 136, 171)},
508 {"slategray", myRGB(112, 128, 144)},
509 {"snow", myRGB(255, 250, 250)},
510 {"springgreen", myRGB(65, 172, 65)},
511 {"steelblue", myRGB(84, 112, 170)},
512 {"tan", myRGB(222, 184, 135)},
513 {"thistle", myRGB(216, 191, 216)},
514 {"tomato", myRGB(255, 99, 71)},
515 {"transparent", myRGB(0, 0, 1)},
516 {"turquoise", myRGB(25, 204, 223)},
517 {"violet", myRGB(156, 62, 206)},
518 {"violetred", myRGB(243, 62, 150)},
519 {"wheat", myRGB(245, 222, 179)},
520 {"white", myRGB(255, 255, 255)},
521 {"whitesmoke", myRGB(245, 245, 245)},
522 {"yellow", myRGB(255, 255, 0)},
523 {"yellowgreen", myRGB(50, 216, 56)},
524 {NULL, myRGB(0, 0, 0)}
525 };
526 static int numTheRGBRecords = 235;
527
528 static unsigned char ParseHexadecimal(char digit1, char digit2)
529 {
530 unsigned char i1, i2;
531
532 if (digit1 >= 'a')
533 i1 = digit1 - 'a' + 0x0A;
534 else if (digit1 >= 'A')
535 i1 = digit1 - 'A' + 0x0A;
536 else
537 i1 = digit1 - '0';
538 if (digit2 >= 'a')
539 i2 = digit2 - 'a' + 0x0A;
540 else if (digit2 >= 'A')
541 i2 = digit2 - 'A' + 0x0A;
542 else
543 i2 = digit2 - '0';
544 return (0x10 * i1 + i2);
545 }
546
547 static bool GetRGBFromName(const char *inname, bool *isNone,
548 unsigned char *r, unsigned char*g, unsigned char *b)
549 {
550 int left, right, middle;
551 int cmp;
552 wxUint32 rgbVal;
553 char *name;
554 char *grey, *p;
555
556 // Neither #rrggbb nor #rrrrggggbbbb are in database, we parse them directly
557 size_t inname_len = strlen(inname);
558 if ( *inname == '#' && (inname_len == 7 || inname_len == 13))
559 {
560 size_t ofs = (inname_len == 7) ? 2 : 4;
561 *r = ParseHexadecimal(inname[1], inname[2]);
562 *g = ParseHexadecimal(inname[1*ofs+1], inname[1*ofs+2]);
563 *b = ParseHexadecimal(inname[2*ofs+1], inname[2*ofs+2]);
564 *isNone = FALSE;
565 return TRUE;
566 }
567
568 name = strdup(inname);
569
570 // theRGBRecords[] has no names with spaces, and no grey, but a
571 // lot of gray...
572
573 // so first extract ' '
574 while ((p = strchr(name, ' ')) != NULL)
575 {
576 while (*(p)) // till eof of string
577 {
578 *p = *(p + 1); // copy to the left
579 p++;
580 }
581 }
582 // fold to lower case
583 p = name;
584 while (*p)
585 {
586 *p = tolower(*p);
587 p++;
588 }
589
590 // substitute Grey with Gray, else rgbtab.h would have more than 100
591 // 'duplicate' entries
592 if ( (grey = strstr(name, "grey")) != NULL )
593 grey[2] = 'a';
594
595 // check for special 'none' colour:
596 bool found;
597 if ( strcmp(name, "none") == 0 )
598 {
599 *isNone = TRUE;
600 found = TRUE;
601 }
602 else // not "None"
603 {
604 found = FALSE;
605
606 // binary search:
607 left = 0;
608 right = numTheRGBRecords - 1;
609 do
610 {
611 middle = (left + right) / 2;
612 cmp = strcmp(name, theRGBRecords[middle].name);
613 if ( cmp == 0 )
614 {
615 rgbVal = theRGBRecords[middle].rgb;
616 *r = (unsigned char)((rgbVal >> 16) & 0xFF);
617 *g = (unsigned char)((rgbVal >> 8) & 0xFF);
618 *b = (unsigned char)((rgbVal) & 0xFF);
619 *isNone = FALSE;
620 found = TRUE;
621 break;
622 }
623 else if ( cmp < 0 )
624 {
625 right = middle - 1;
626 }
627 else // cmp > 0
628 {
629 left = middle + 1;
630 }
631 } while (left <= right);
632 }
633
634 free(name);
635
636 return found;
637 }
638
639 static const char *ParseColor(const char *data)
640 {
641 static const char *targets[] =
642 {"c ", "g ", "g4 ", "m ", "b ", "s ", NULL};
643
644 const char *p, *r;
645 const char *q;
646 int i;
647
648 for (i = 0; targets[i] != NULL; i++)
649 {
650 r = data;
651 for (q = targets[i]; *r != '\0'; r++)
652 {
653 if ( *r != *q )
654 continue;
655 if ( !isspace((int) (*(r - 1))) )
656 continue;
657 p = r;
658 for (;;)
659 {
660 if ( *q == '\0' )
661 return p;
662 if ( *p++ != *q++ )
663 break;
664 }
665 q = targets[i];
666 }
667 }
668 return NULL;
669 }
670
671 struct wxXPMColourMapData
672 {
673 unsigned char R,G,B;
674 };
675 WX_DECLARE_STRING_HASH_MAP(wxXPMColourMapData, wxXPMColourMap);
676
677 wxImage wxXPMDecoder::ReadData(const char **xpm_data)
678 {
679 wxImage img;
680 int count;
681 unsigned width, height, colors_cnt, chars_per_pixel;
682 size_t i, j, i_key;
683 wxChar key[64];
684 const char *clr_def;
685 bool hasMask;
686 wxXPMColourMapData clr_data;
687 wxXPMColourMap clr_tbl;
688
689 /*
690 * Read hints and initialize structures:
691 */
692
693 count = sscanf(xpm_data[0], "%u %u %u %u",
694 &width, &height, &colors_cnt, &chars_per_pixel);
695 if ( count != 4 || width * height * colors_cnt == 0 )
696 {
697 wxLogError(_T("XPM: Not XPM data!"));
698 return wxNullImage;
699 }
700
701 // VS: XPM color map this large would be insane, since XPMs are encoded with
702 // 92 possible values on each position, 92^64 is *way* larger space than
703 // 8bit RGB...
704 wxCHECK_MSG(chars_per_pixel < 64, wxNullImage, wxT("XPM colormaps this large not supported."));
705
706 img.Create(width, height);
707 if ( !img.Ok() ) return img;
708
709 img.SetMask(FALSE);
710 key[chars_per_pixel] = wxT('\0');
711 hasMask = FALSE;
712
713 /*
714 * Create colour map:
715 */
716 for (i = 0; i < colors_cnt; i++)
717 {
718 for (i_key = 0; i_key < chars_per_pixel; i_key++)
719 key[i_key] = (wxChar)xpm_data[1 + i][i_key];
720 clr_def = ParseColor(xpm_data[1 + i] + chars_per_pixel);
721
722 if ( clr_def == NULL )
723 {
724 wxLogError(_("XPM: malformed colour definition '%s'!"), xpm_data[1+i]);
725 clr_data.R = 255, clr_data.G = 0, clr_data.B = 255;
726 }
727 else
728 {
729 bool isNone;
730 if ( !GetRGBFromName(clr_def, &isNone,
731 &clr_data.R, &clr_data.G, &clr_data.B) )
732 {
733 wxLogError(_("XPM: malformed colour definition '%s'!"), xpm_data[1+i]);
734 clr_data.R = 255, clr_data.G = 0, clr_data.B = 255;
735 }
736 else
737 {
738 if ( isNone )
739 {
740 img.SetMask(TRUE);
741 img.SetMaskColour(255, 0, 255);
742 hasMask = TRUE;
743 clr_data.R = 255, clr_data.G = 0, clr_data.B = 255;
744 }
745 else
746 {
747 if ( hasMask && clr_data.R == 255 &&
748 clr_data.G == 0 && clr_data.B == 255 )
749 clr_data.B = 254;
750 }
751 }
752 }
753 clr_tbl[key] = clr_data;
754 }
755
756 /*
757 * Parse image data:
758 */
759
760 unsigned char *img_data = img.GetData();
761 wxXPMColourMap::iterator entry;
762 wxXPMColourMap::iterator end = clr_tbl.end();
763
764 for (j = 0; j < height; j++)
765 {
766 for (i = 0; i < width; i++, img_data += 3)
767 {
768 for (i_key = 0; i_key < chars_per_pixel; i_key++)
769 key[i_key] = (wxChar)xpm_data[1 + colors_cnt + j]
770 [chars_per_pixel * i + i_key];
771 entry = clr_tbl.find(key);
772 if ( entry == end )
773 {
774 wxLogError(_("XPM: Malformed pixel data!"));
775 }
776 else
777 {
778 img_data[0] = entry->second.R;
779 img_data[1] = entry->second.G;
780 img_data[2] = entry->second.B;
781 }
782 }
783 }
784
785 return img;
786 }
787
788 #endif // wxUSE_IMAGE && wxUSE_XPM