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