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