fixed xpm colour parsing bug due to case sensitive colour name comparison
[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
109 #include "wx/stream.h"
110 #include "wx/image.h"
111 #include "wx/utils.h"
112 #include "wx/log.h"
113 #include "wx/intl.h"
114 #include <string.h>
115
116 #include "wx/xpmdecod.h"
117
118 #if wxUSE_STREAMS
119 bool wxXPMDecoder::CanRead(wxInputStream& stream)
120 {
121 unsigned char buf[9];
122
123 stream.Read(buf, 9);
124 stream.SeekI(-9, wxFromCurrent);
125
126 return (memcmp(buf, "/* XPM */", 9) == 0);
127 }
128
129 wxImage wxXPMDecoder::ReadFile(wxInputStream& stream)
130 {
131 size_t length = stream.GetSize();
132 wxCHECK_MSG(length != 0, wxNullImage, wxT("Cannot read XPM from stream of unknown size"));
133
134 char *xpm_buffer = new char[length];
135 char *p, *q;
136 size_t i;
137
138 if ( stream.Read(xpm_buffer, length).LastError() == wxSTREAM_READ_ERROR )
139 return FALSE;
140
141 /*
142 * Remove comments from the file:
143 */
144 for (p = xpm_buffer; *p != '\0'; p++)
145 {
146 if ( (*p == '"') || (*p == '\'') )
147 {
148 if (*p == '"')
149 {
150 for (p++; *p != '\0'; p++)
151 if ( (*p == '"') && (*(p - 1) != '\\') )
152 break;
153 }
154 else // *p == '\''
155 {
156 for (p++; *p != '\0'; p++)
157 if ( (*p == '\'') && (*(p - 1) != '\\') )
158 break;
159 }
160 if (*p == '\0')
161 break;
162 continue;
163 }
164 if ( (*p != '/') || (*(p + 1) != '*') )
165 continue;
166 for (q = p + 2; *q != '\0'; q++)
167 {
168 if ( (*q == '*') && (*(q + 1) == '/') )
169 break;
170 }
171 strcpy(p, q + 2);
172 }
173
174 /*
175 * Remove unquoted characters:
176 */
177 i = 0;
178 for (p = xpm_buffer; *p != '\0'; p++)
179 {
180 if ( *p != '"' )
181 continue;
182 for (q = p + 1; *q != '\0'; q++)
183 if (*q == '"')
184 break;
185 strncpy(xpm_buffer + i, p + 1, q - p - 1);
186 i += q - p - 1;
187 xpm_buffer[i++] = '\n';
188 p = q + 1;
189 }
190 xpm_buffer[i] = '\0';
191
192 /*
193 * Create array of lines and convert \n's to \0's:
194 */
195 const char **xpm_lines;
196 size_t lines_cnt = 0;
197 size_t line;
198
199 for (p = xpm_buffer; *p != '\0'; p++)
200 {
201 if ( *p == '\n' )
202 lines_cnt++;
203 }
204
205 xpm_lines = new const char*[lines_cnt];
206 xpm_lines[0] = xpm_buffer;
207 line = 1;
208 for (p = xpm_buffer; (*p != '\0') && (line < lines_cnt); p++)
209 {
210 if ( *p == '\n' )
211 {
212 xpm_lines[line] = p + 1;
213 *p = '\0';
214 line++;
215 }
216 }
217
218 /*
219 * Read the image:
220 */
221 wxImage img = ReadData(xpm_lines);
222
223 delete[] xpm_buffer;
224 #ifdef __WIN16__
225 delete[] (char**) xpm_lines;
226 #else
227 delete[] xpm_lines;
228 #endif
229 return img;
230 }
231 #endif // wxUSE_STREAMS
232
233
234 /*****************************************************************************\
235 * rgbtab.h *
236 * *
237 * A hard coded rgb.txt. To keep it short I removed all colornames with *
238 * trailing numbers, Blue3 etc, except the GrayXX. Sorry Grey-lovers I prefer *
239 * Gray ;-). But Grey is recognized on lookups, only on save Gray will be *
240 * used, maybe you want to do some substitue there too. *
241 * *
242 * To save memory the RGBs are coded in one long value, as done by the RGB *
243 * macro. *
244 * *
245 * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) *
246 \*****************************************************************************/
247
248
249 typedef struct
250 {
251 char *name;
252 wxUint32 rgb;
253 } rgbRecord;
254
255 #define myRGB(r,g,b) ((wxUint32)r<<16|(wxUint32)g<<8|(wxUint32)b)
256
257 static rgbRecord theRGBRecords[] =
258 {
259 {"aliceblue", myRGB(240, 248, 255)},
260 {"antiquewhite", myRGB(250, 235, 215)},
261 {"aquamarine", myRGB(50, 191, 193)},
262 {"azure", myRGB(240, 255, 255)},
263 {"beige", myRGB(245, 245, 220)},
264 {"bisque", myRGB(255, 228, 196)},
265 {"black", myRGB(0, 0, 0)},
266 {"blanchedalmond", myRGB(255, 235, 205)},
267 {"blue", myRGB(0, 0, 255)},
268 {"blueviolet", myRGB(138, 43, 226)},
269 {"brown", myRGB(165, 42, 42)},
270 {"burlywood", myRGB(222, 184, 135)},
271 {"cadetblue", myRGB(95, 146, 158)},
272 {"chartreuse", myRGB(127, 255, 0)},
273 {"chocolate", myRGB(210, 105, 30)},
274 {"coral", myRGB(255, 114, 86)},
275 {"cornflowerblue", myRGB(34, 34, 152)},
276 {"cornsilk", myRGB(255, 248, 220)},
277 {"cyan", myRGB(0, 255, 255)},
278 {"darkgoldenrod", myRGB(184, 134, 11)},
279 {"darkgreen", myRGB(0, 86, 45)},
280 {"darkkhaki", myRGB(189, 183, 107)},
281 {"darkolivegreen", myRGB(85, 86, 47)},
282 {"darkorange", myRGB(255, 140, 0)},
283 {"darkorchid", myRGB(139, 32, 139)},
284 {"darksalmon", myRGB(233, 150, 122)},
285 {"darkseagreen", myRGB(143, 188, 143)},
286 {"darkslateblue", myRGB(56, 75, 102)},
287 {"darkslategray", myRGB(47, 79, 79)},
288 {"darkturquoise", myRGB(0, 166, 166)},
289 {"darkviolet", myRGB(148, 0, 211)},
290 {"deeppink", myRGB(255, 20, 147)},
291 {"deepskyblue", myRGB(0, 191, 255)},
292 {"dimgray", myRGB(84, 84, 84)},
293 {"dodgerblue", myRGB(30, 144, 255)},
294 {"firebrick", myRGB(142, 35, 35)},
295 {"floralwhite", myRGB(255, 250, 240)},
296 {"forestgreen", myRGB(80, 159, 105)},
297 {"gainsboro", myRGB(220, 220, 220)},
298 {"ghostwhite", myRGB(248, 248, 255)},
299 {"gold", myRGB(218, 170, 0)},
300 {"goldenrod", myRGB(239, 223, 132)},
301 {"gray", myRGB(126, 126, 126)},
302 {"gray0", myRGB(0, 0, 0)},
303 {"gray1", myRGB(3, 3, 3)},
304 {"gray10", myRGB(26, 26, 26)},
305 {"gray100", myRGB(255, 255, 255)},
306 {"gray11", myRGB(28, 28, 28)},
307 {"gray12", myRGB(31, 31, 31)},
308 {"gray13", myRGB(33, 33, 33)},
309 {"gray14", myRGB(36, 36, 36)},
310 {"gray15", myRGB(38, 38, 38)},
311 {"gray16", myRGB(41, 41, 41)},
312 {"gray17", myRGB(43, 43, 43)},
313 {"gray18", myRGB(46, 46, 46)},
314 {"gray19", myRGB(48, 48, 48)},
315 {"gray2", myRGB(5, 5, 5)},
316 {"gray20", myRGB(51, 51, 51)},
317 {"gray21", myRGB(54, 54, 54)},
318 {"gray22", myRGB(56, 56, 56)},
319 {"gray23", myRGB(59, 59, 59)},
320 {"gray24", myRGB(61, 61, 61)},
321 {"gray25", myRGB(64, 64, 64)},
322 {"gray26", myRGB(66, 66, 66)},
323 {"gray27", myRGB(69, 69, 69)},
324 {"gray28", myRGB(71, 71, 71)},
325 {"gray29", myRGB(74, 74, 74)},
326 {"gray3", myRGB(8, 8, 8)},
327 {"gray30", myRGB(77, 77, 77)},
328 {"gray31", myRGB(79, 79, 79)},
329 {"gray32", myRGB(82, 82, 82)},
330 {"gray33", myRGB(84, 84, 84)},
331 {"gray34", myRGB(87, 87, 87)},
332 {"gray35", myRGB(89, 89, 89)},
333 {"gray36", myRGB(92, 92, 92)},
334 {"gray37", myRGB(94, 94, 94)},
335 {"gray38", myRGB(97, 97, 97)},
336 {"gray39", myRGB(99, 99, 99)},
337 {"gray4", myRGB(10, 10, 10)},
338 {"gray40", myRGB(102, 102, 102)},
339 {"gray41", myRGB(105, 105, 105)},
340 {"gray42", myRGB(107, 107, 107)},
341 {"gray43", myRGB(110, 110, 110)},
342 {"gray44", myRGB(112, 112, 112)},
343 {"gray45", myRGB(115, 115, 115)},
344 {"gray46", myRGB(117, 117, 117)},
345 {"gray47", myRGB(120, 120, 120)},
346 {"gray48", myRGB(122, 122, 122)},
347 {"gray49", myRGB(125, 125, 125)},
348 {"gray5", myRGB(13, 13, 13)},
349 {"gray50", myRGB(127, 127, 127)},
350 {"gray51", myRGB(130, 130, 130)},
351 {"gray52", myRGB(133, 133, 133)},
352 {"gray53", myRGB(135, 135, 135)},
353 {"gray54", myRGB(138, 138, 138)},
354 {"gray55", myRGB(140, 140, 140)},
355 {"gray56", myRGB(143, 143, 143)},
356 {"gray57", myRGB(145, 145, 145)},
357 {"gray58", myRGB(148, 148, 148)},
358 {"gray59", myRGB(150, 150, 150)},
359 {"gray6", myRGB(15, 15, 15)},
360 {"gray60", myRGB(153, 153, 153)},
361 {"gray61", myRGB(156, 156, 156)},
362 {"gray62", myRGB(158, 158, 158)},
363 {"gray63", myRGB(161, 161, 161)},
364 {"gray64", myRGB(163, 163, 163)},
365 {"gray65", myRGB(166, 166, 166)},
366 {"gray66", myRGB(168, 168, 168)},
367 {"gray67", myRGB(171, 171, 171)},
368 {"gray68", myRGB(173, 173, 173)},
369 {"gray69", myRGB(176, 176, 176)},
370 {"gray7", myRGB(18, 18, 18)},
371 {"gray70", myRGB(179, 179, 179)},
372 {"gray71", myRGB(181, 181, 181)},
373 {"gray72", myRGB(184, 184, 184)},
374 {"gray73", myRGB(186, 186, 186)},
375 {"gray74", myRGB(189, 189, 189)},
376 {"gray75", myRGB(191, 191, 191)},
377 {"gray76", myRGB(194, 194, 194)},
378 {"gray77", myRGB(196, 196, 196)},
379 {"gray78", myRGB(199, 199, 199)},
380 {"gray79", myRGB(201, 201, 201)},
381 {"gray8", myRGB(20, 20, 20)},
382 {"gray80", myRGB(204, 204, 204)},
383 {"gray81", myRGB(207, 207, 207)},
384 {"gray82", myRGB(209, 209, 209)},
385 {"gray83", myRGB(212, 212, 212)},
386 {"gray84", myRGB(214, 214, 214)},
387 {"gray85", myRGB(217, 217, 217)},
388 {"gray86", myRGB(219, 219, 219)},
389 {"gray87", myRGB(222, 222, 222)},
390 {"gray88", myRGB(224, 224, 224)},
391 {"gray89", myRGB(227, 227, 227)},
392 {"gray9", myRGB(23, 23, 23)},
393 {"gray90", myRGB(229, 229, 229)},
394 {"gray91", myRGB(232, 232, 232)},
395 {"gray92", myRGB(235, 235, 235)},
396 {"gray93", myRGB(237, 237, 237)},
397 {"gray94", myRGB(240, 240, 240)},
398 {"gray95", myRGB(242, 242, 242)},
399 {"gray96", myRGB(245, 245, 245)},
400 {"gray97", myRGB(247, 247, 247)},
401 {"gray98", myRGB(250, 250, 250)},
402 {"gray99", myRGB(252, 252, 252)},
403 {"green", myRGB(0, 255, 0)},
404 {"greenyellow", myRGB(173, 255, 47)},
405 {"honeydew", myRGB(240, 255, 240)},
406 {"hotpink", myRGB(255, 105, 180)},
407 {"indianred", myRGB(107, 57, 57)},
408 {"ivory", myRGB(255, 255, 240)},
409 {"khaki", myRGB(179, 179, 126)},
410 {"lavender", myRGB(230, 230, 250)},
411 {"lavenderblush", myRGB(255, 240, 245)},
412 {"lawngreen", myRGB(124, 252, 0)},
413 {"lemonchiffon", myRGB(255, 250, 205)},
414 {"lightblue", myRGB(176, 226, 255)},
415 {"lightcoral", myRGB(240, 128, 128)},
416 {"lightcyan", myRGB(224, 255, 255)},
417 {"lightgoldenrod", myRGB(238, 221, 130)},
418 {"lightgoldenrodyellow", myRGB(250, 250, 210)},
419 {"lightgray", myRGB(168, 168, 168)},
420 {"lightpink", myRGB(255, 182, 193)},
421 {"lightsalmon", myRGB(255, 160, 122)},
422 {"lightseagreen", myRGB(32, 178, 170)},
423 {"lightskyblue", myRGB(135, 206, 250)},
424 {"lightslateblue", myRGB(132, 112, 255)},
425 {"lightslategray", myRGB(119, 136, 153)},
426 {"lightsteelblue", myRGB(124, 152, 211)},
427 {"lightyellow", myRGB(255, 255, 224)},
428 {"limegreen", myRGB(0, 175, 20)},
429 {"linen", myRGB(250, 240, 230)},
430 {"magenta", myRGB(255, 0, 255)},
431 {"maroon", myRGB(143, 0, 82)},
432 {"mediumaquamarine", myRGB(0, 147, 143)},
433 {"mediumblue", myRGB(50, 50, 204)},
434 {"mediumforestgreen", myRGB(50, 129, 75)},
435 {"mediumgoldenrod", myRGB(209, 193, 102)},
436 {"mediumorchid", myRGB(189, 82, 189)},
437 {"mediumpurple", myRGB(147, 112, 219)},
438 {"mediumseagreen", myRGB(52, 119, 102)},
439 {"mediumslateblue", myRGB(106, 106, 141)},
440 {"mediumspringgreen", myRGB(35, 142, 35)},
441 {"mediumturquoise", myRGB(0, 210, 210)},
442 {"mediumvioletred", myRGB(213, 32, 121)},
443 {"midnightblue", myRGB(47, 47, 100)},
444 {"mintcream", myRGB(245, 255, 250)},
445 {"mistyrose", myRGB(255, 228, 225)},
446 {"moccasin", myRGB(255, 228, 181)},
447 {"navajowhite", myRGB(255, 222, 173)},
448 {"navy", myRGB(35, 35, 117)},
449 {"navyblue", myRGB(35, 35, 117)},
450 {"oldlace", myRGB(253, 245, 230)},
451 {"olivedrab", myRGB(107, 142, 35)},
452 {"orange", myRGB(255, 135, 0)},
453 {"orangered", myRGB(255, 69, 0)},
454 {"orchid", myRGB(239, 132, 239)},
455 {"palegoldenrod", myRGB(238, 232, 170)},
456 {"palegreen", myRGB(115, 222, 120)},
457 {"paleturquoise", myRGB(175, 238, 238)},
458 {"palevioletred", myRGB(219, 112, 147)},
459 {"papayawhip", myRGB(255, 239, 213)},
460 {"peachpuff", myRGB(255, 218, 185)},
461 {"peru", myRGB(205, 133, 63)},
462 {"pink", myRGB(255, 181, 197)},
463 {"plum", myRGB(197, 72, 155)},
464 {"powderblue", myRGB(176, 224, 230)},
465 {"purple", myRGB(160, 32, 240)},
466 {"red", myRGB(255, 0, 0)},
467 {"rosybrown", myRGB(188, 143, 143)},
468 {"royalblue", myRGB(65, 105, 225)},
469 {"saddlebrown", myRGB(139, 69, 19)},
470 {"salmon", myRGB(233, 150, 122)},
471 {"sandybrown", myRGB(244, 164, 96)},
472 {"seagreen", myRGB(82, 149, 132)},
473 {"seashell", myRGB(255, 245, 238)},
474 {"sienna", myRGB(150, 82, 45)},
475 {"skyblue", myRGB(114, 159, 255)},
476 {"slateblue", myRGB(126, 136, 171)},
477 {"slategray", myRGB(112, 128, 144)},
478 {"snow", myRGB(255, 250, 250)},
479 {"springgreen", myRGB(65, 172, 65)},
480 {"steelblue", myRGB(84, 112, 170)},
481 {"tan", myRGB(222, 184, 135)},
482 {"thistle", myRGB(216, 191, 216)},
483 {"tomato", myRGB(255, 99, 71)},
484 {"transparent", myRGB(0, 0, 1)},
485 {"turquoise", myRGB(25, 204, 223)},
486 {"violet", myRGB(156, 62, 206)},
487 {"violetred", myRGB(243, 62, 150)},
488 {"wheat", myRGB(245, 222, 179)},
489 {"white", myRGB(255, 255, 255)},
490 {"whitesmoke", myRGB(245, 245, 245)},
491 {"yellow", myRGB(255, 255, 0)},
492 {"yellowgreen", myRGB(50, 216, 56)},
493 {NULL, myRGB(0, 0, 0)}
494 };
495 static int numTheRGBRecords = 234;
496
497 static unsigned char ParseHexadecimal(char digit1, char digit2)
498 {
499 unsigned char i1, i2;
500
501 if (digit1 >= 'a')
502 i1 = digit1 - 'a' + 0x0A;
503 else if (digit1 >= 'A')
504 i1 = digit1 - 'A' + 0x0A;
505 else
506 i1 = digit1 - '0';
507 if (digit2 >= 'a')
508 i2 = digit2 - 'a' + 0x0A;
509 else if (digit2 >= 'A')
510 i2 = digit2 - 'A' + 0x0A;
511 else
512 i2 = digit2 - '0';
513 return (0x10 * i1 + i2);
514 }
515
516 static bool GetRGBFromName(const char *inname, bool *isNone,
517 unsigned char *r, unsigned char*g, unsigned char *b)
518 {
519 int left, right, middle;
520 int cmp;
521 wxUint32 rgbVal;
522 char *name;
523 char *grey, *p;
524
525 // #rrggbb are not in database, we parse them directly
526 if ( *inname == '#' && strlen(inname) == 7 )
527 {
528 *r = ParseHexadecimal(inname[1], inname[2]);
529 *g = ParseHexadecimal(inname[3], inname[4]);
530 *b = ParseHexadecimal(inname[5], inname[6]);
531 *isNone = FALSE;
532 return TRUE;
533 }
534
535 name = strdup(inname);
536
537 // theRGBRecords[] has no names with spaces, and no grey, but a
538 // lot of gray...
539
540 // so first extract ' '
541 while ((p = strchr(name, ' ')) != NULL)
542 {
543 while (*(p)) // till eof of string
544 {
545 *p = *(p + 1); // copy to the left
546 p++;
547 }
548 }
549 // fold to lower case
550 p = name;
551 while (*p)
552 {
553 *p = tolower(*p);
554 p++;
555 }
556
557 // substitute Grey with Gray, else rgbtab.h would have more than 100
558 // 'duplicate' entries
559 if ( (grey = strstr(name, "grey")) != NULL )
560 grey[2] = 'a';
561
562 // check for special 'none' colour:
563 bool found;
564 if ( strcmp(name, "none") == 0 )
565 {
566 *isNone = TRUE;
567 found = TRUE;
568 }
569 else // not "None"
570 {
571 found = FALSE;
572
573 // binary search:
574 left = 0;
575 right = numTheRGBRecords - 1;
576 do
577 {
578 middle = (left + right) / 2;
579 cmp = strcmp(name, theRGBRecords[middle].name);
580 if ( cmp == 0 )
581 {
582 rgbVal = theRGBRecords[middle].rgb;
583 *r = (unsigned char)((rgbVal >> 16) & 0xFF);
584 *g = (unsigned char)((rgbVal >> 8) & 0xFF);
585 *b = (unsigned char)((rgbVal) & 0xFF);
586 *isNone = FALSE;
587 found = TRUE;
588 break;
589 }
590 else if ( cmp < 0 )
591 {
592 right = middle - 1;
593 }
594 else // cmp > 0
595 {
596 left = middle + 1;
597 }
598 } while (left <= right);
599 }
600
601 free(name);
602
603 return found;
604 }
605
606 static const char *ParseColor(const char *data)
607 {
608 static const char *targets[] =
609 {"c ", "g ", "g4 ", "m ", "b ", "s ", NULL};
610
611 const char *p, *r;
612 const char *q;
613 int i;
614
615 for (i = 0; targets[i] != NULL; i++)
616 {
617 r = data;
618 for (q = targets[i]; *r != '\0'; r++)
619 {
620 if ( *r != *q )
621 continue;
622 if ( !isspace((int) (*(r - 1))) )
623 continue;
624 p = r;
625 for (;;)
626 {
627 if ( *q == '\0' )
628 return p;
629 if ( *p++ != *q++ )
630 break;
631 }
632 q = targets[i];
633 }
634 }
635 return NULL;
636 }
637
638 class wxXPMColourMapData : public wxObject
639 {
640 public:
641 unsigned char R,G,B;
642 };
643
644 wxImage wxXPMDecoder::ReadData(const char **xpm_data)
645 {
646 wxImage img;
647 int count;
648 unsigned width, height, colors_cnt, chars_per_pixel;
649 size_t i, j, i_key;
650 wxChar key[64];
651 const char *clr_def;
652 bool hasMask;
653 wxXPMColourMapData *clr_data;
654 wxHashTable clr_tbl(wxKEY_STRING);
655
656 /*
657 * Read hints and initialize structures:
658 */
659
660 count = sscanf(xpm_data[0], "%u %u %u %u",
661 &width, &height, &colors_cnt, &chars_per_pixel);
662 if ( count != 4 || width * height * colors_cnt == 0 )
663 {
664 wxLogError(_T("XPM: Not XPM data!"));
665 return wxNullImage;
666 }
667
668 // VS: XPM color map this large would be insane, since XPMs are encoded with
669 // 92 possible values on each position, 92^64 is *way* larger space than
670 // 8bit RGB...
671 wxCHECK_MSG(chars_per_pixel < 64, wxNullImage, wxT("XPM colormaps this large not supported."));
672
673 img.Create(width, height);
674 if ( !img.Ok() ) return img;
675
676 img.SetMask(FALSE);
677 key[chars_per_pixel] = wxT('\0');
678 hasMask = FALSE;
679 clr_tbl.DeleteContents(TRUE);
680
681 /*
682 * Create colour map:
683 */
684 for (i = 0; i < colors_cnt; i++)
685 {
686 for (i_key = 0; i_key < chars_per_pixel; i_key++)
687 key[i_key] = (wxChar)xpm_data[1 + i][i_key];
688 clr_def = ParseColor(xpm_data[1 + i]);
689 clr_data = new wxXPMColourMapData;
690
691 if ( clr_def == NULL )
692 {
693 wxLogError(_("XPM: malformed colour definition '%s'!"), xpm_data[1+i]);
694 clr_data->R = 255, clr_data->G = 0, clr_data->B = 255;
695 }
696 else
697 {
698 bool isNone;
699 if ( !GetRGBFromName(clr_def, &isNone,
700 &clr_data->R, &clr_data->G, &clr_data->B) )
701 {
702 wxLogError(_("XPM: malformed colour definition '%s'!"), xpm_data[1+i]);
703 clr_data->R = 255, clr_data->G = 0, clr_data->B = 255;
704 }
705 else
706 {
707 if ( isNone )
708 {
709 img.SetMask(TRUE);
710 img.SetMaskColour(255, 0, 255);
711 hasMask = TRUE;
712 clr_data->R = 255, clr_data->G = 0, clr_data->B = 255;
713 }
714 else
715 {
716 if ( hasMask && clr_data->R == 255 &&
717 clr_data->G == 0 && clr_data->B == 255 )
718 clr_data->B = 254;
719 }
720 }
721 }
722 clr_tbl.Put(key, clr_data);
723 }
724
725 /*
726 * Parse image data:
727 */
728
729 unsigned char *img_data = img.GetData();
730 for (j = 0; j < height; j++)
731 {
732 for (i = 0; i < width; i++, img_data += 3)
733 {
734 for (i_key = 0; i_key < chars_per_pixel; i_key++)
735 key[i_key] = (wxChar)xpm_data[1 + colors_cnt + j]
736 [chars_per_pixel * i + i_key];
737 clr_data = (wxXPMColourMapData*) clr_tbl.Get(key);
738 if ( clr_data == NULL )
739 {
740 wxLogError(_("XPM: Malformed pixel data!"));
741 }
742 else
743 {
744 img_data[0] = clr_data->R;
745 img_data[1] = clr_data->G;
746 img_data[2] = clr_data->B;
747 }
748 }
749 }
750
751 return img;
752 }
753
754