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