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