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 /////////////////////////////////////////////////////////////////////////////
11 This file is partially based on source code of ImageMagick by John Cristy. Its
12 license is as follows:
14 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25 % Read/Write ImageMagick Image Format. %
33 % Copyright (C) 2001 ImageMagick Studio, a non-profit organization dedicated %
34 % to making software imaging solutions freely available. %
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: %
43 % The above copyright notice and this permission notice shall be included in %
44 % all copies or substantial portions of ImageMagick. %
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 %
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. %
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 * Also contains some pieces from libxpm and its modification for win32 by
66 * HeDu <hedu@cul-ipn.uni-kiel.de>:
68 * Copyright (C) 1989-95 GROUPE BULL
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:
77 * The above copyright notice and this permission notice shall be included in
78 * all copies or substantial portions of the Software.
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.
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.
92 // For compilers that support precompilation, includes "wx.h".
93 #include "wx/wxprec.h"
99 #if wxUSE_IMAGE && wxUSE_XPM
101 #include "wx/xpmdecod.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"
117 bool wxXPMDecoder::CanRead(wxInputStream
& stream
)
119 unsigned char buf
[9];
121 if ( !stream
.Read(buf
, WXSIZEOF(buf
)) )
124 return memcmp(buf
, "/* XPM */", WXSIZEOF(buf
)) == 0;
127 wxImage
wxXPMDecoder::ReadFile(wxInputStream
& stream
)
129 size_t length
= stream
.GetSize();
130 wxCHECK_MSG( length
!= 0, wxNullImage
,
131 wxT("Cannot read XPM from stream of unknown size") );
133 // use a smart buffer to be sure to free memory even when we return on
135 wxCharBuffer
buffer(length
);
137 char *xpm_buffer
= (char *)buffer
.data();
138 if ( stream
.Read(xpm_buffer
, length
).GetLastError() == wxSTREAM_READ_ERROR
)
140 xpm_buffer
[length
] = '\0';
143 * Remove comments from the file:
146 for (p
= xpm_buffer
; *p
!= '\0'; p
++)
148 if ( (*p
== '"') || (*p
== '\'') )
152 for (p
++; *p
!= '\0'; p
++)
153 if ( (*p
== '"') && (*(p
- 1) != '\\') )
158 for (p
++; *p
!= '\0'; p
++)
159 if ( (*p
== '\'') && (*(p
- 1) != '\\') )
166 if ( (*p
!= '/') || (*(p
+ 1) != '*') )
168 for (q
= p
+ 2; *q
!= '\0'; q
++)
170 if ( (*q
== '*') && (*(q
+ 1) == '/') )
174 // memmove allows overlaps (unlike strcpy):
175 size_t cpylen
= strlen(q
+ 2) + 1;
176 memmove(p
, q
+ 2, cpylen
);
180 * Remove unquoted characters:
183 for (p
= xpm_buffer
; *p
!= '\0'; p
++)
187 for (q
= p
+ 1; *q
!= '\0'; q
++)
190 strncpy(xpm_buffer
+ i
, p
+ 1, q
- p
- 1);
192 xpm_buffer
[i
++] = '\n';
195 xpm_buffer
[i
] = '\0';
198 * Create array of lines and convert \n's to \0's:
200 const char **xpm_lines
;
201 size_t lines_cnt
= 0;
204 for (p
= xpm_buffer
; *p
!= '\0'; p
++)
212 // this doesn't really look an XPM image
216 xpm_lines
= new const char*[lines_cnt
+ 1];
217 xpm_lines
[0] = xpm_buffer
;
219 for (p
= xpm_buffer
; (*p
!= '\0') && (line
< lines_cnt
); p
++)
223 xpm_lines
[line
] = p
+ 1;
229 xpm_lines
[lines_cnt
] = NULL
;
234 wxImage img
= ReadData(xpm_lines
);
240 #endif // wxUSE_STREAMS
243 /*****************************************************************************\
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. *
251 * To save memory the RGBs are coded in one long value, as done by the RGB *
254 * Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) *
255 \*****************************************************************************/
264 #define myRGB(r,g,b) ((wxUint32)r<<16|(wxUint32)g<<8|(wxUint32)b)
266 static const rgbRecord theRGBRecords
[] =
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)}
505 static const int numTheRGBRecords
= 235;
507 static unsigned char ParseHexadecimal(char digit1
, char digit2
)
509 unsigned char i1
, i2
;
512 i1
= (unsigned char)(digit1
- 'a' + 0x0A);
513 else if (digit1
>= 'A')
514 i1
= (unsigned char)(digit1
- 'A' + 0x0A);
516 i1
= (unsigned char)(digit1
- '0');
518 i2
= (unsigned char)(digit2
- 'a' + 0x0A);
519 else if (digit2
>= 'A')
520 i2
= (unsigned char)(digit2
- 'A' + 0x0A);
522 i2
= (unsigned char)(digit2
- '0');
523 return (unsigned char)(0x10 * i1
+ i2
);
526 static bool GetRGBFromName(const char *inname
, bool *isNone
,
527 unsigned char *r
, unsigned char*g
, unsigned char *b
)
529 int left
, right
, middle
;
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))
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]);
547 name
= wxStrdupA(inname
);
549 // theRGBRecords[] has no names with spaces, and no grey, but a
552 // so first extract ' '
553 while ((p
= strchr(name
, ' ')) != NULL
)
555 while (*(p
)) // till eof of string
557 *p
= *(p
+ 1); // copy to the left
561 // fold to lower case
565 *p
= (char)tolower(*p
);
569 // substitute Grey with Gray, else rgbtab.h would have more than 100
570 // 'duplicate' entries
571 if ( (grey
= strstr(name
, "grey")) != NULL
)
574 // check for special 'none' colour:
576 if ( strcmp(name
, "none") == 0 )
587 right
= numTheRGBRecords
- 1;
590 middle
= (left
+ right
) / 2;
591 cmp
= strcmp(name
, theRGBRecords
[middle
].name
);
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);
610 } while (left
<= right
);
618 static const char *ParseColor(const char *data
)
620 static const char *const targets
[] =
621 {"c ", "g ", "g4 ", "m ", "b ", "s ", NULL
};
627 for (i
= 0; targets
[i
] != NULL
; i
++)
630 for (q
= targets
[i
]; *r
!= '\0'; r
++)
634 if ( !isspace((int) (*(r
- 1))) )
650 struct wxXPMColourMapData
652 wxXPMColourMapData() { R
= G
= B
= 0; }
655 WX_DECLARE_STRING_HASH_MAP(wxXPMColourMapData
, wxXPMColourMap
);
657 wxImage
wxXPMDecoder::ReadData(const char* const* xpm_data
)
659 wxCHECK_MSG(xpm_data
, wxNullImage
, wxT("NULL XPM data") );
663 unsigned width
, height
, colors_cnt
, chars_per_pixel
;
667 wxXPMColourMap clr_tbl
;
668 wxXPMColourMap::iterator it
;
673 * Read hints and initialize structures:
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 )
680 wxLogError(_("XPM: incorrect header format!"));
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
687 wxCHECK_MSG(chars_per_pixel
< 64, wxNullImage
, wxT("XPM colormaps this large not supported."));
689 if (!img
.Create(width
, height
, false))
692 key
[chars_per_pixel
] = '\0';
697 wxXPMColourMapData clr_data
;
698 for (i
= 0; i
< colors_cnt
; i
++)
700 const char *xmpColLine
= xpm_data
[1 + i
];
702 // we must have at least " x y" after the colour index, hence +5
703 if ( !xmpColLine
|| strlen(xmpColLine
) < chars_per_pixel
+ 5 )
705 wxLogError(_("XPM: incorrect colour description in line %d"),
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
);
714 if ( clr_def
== NULL
)
716 wxLogError(_("XPM: malformed colour definition '%s' at line %d!"),
717 xmpColLine
, (int)(1 + i
));
722 if ( !GetRGBFromName(clr_def
, &isNone
,
723 &clr_data
.R
, &clr_data
.G
, &clr_data
.B
) )
725 wxLogError(_("XPM: malformed colour definition '%s' at line %d!"),
726 xmpColLine
, (int)(1 + i
));
734 clr_tbl
[keyString
] = clr_data
;
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())
741 wxLongToLongHashMap rgb_table
;
743 const size_t n
= clr_tbl
.size();
744 wxXPMColourMap::const_iterator iter
= clr_tbl
.begin();
745 for (i
= 0; i
< n
; ++i
, ++iter
)
747 const wxXPMColourMapData
& data
= iter
->second
;
748 rgb
= (data
.R
<< 16) + (data
.G
<< 8) + data
.B
;
751 for (rgb
= 0; rgb
<= 0xffffff && rgb_table
.count(rgb
); ++rgb
)
755 wxLogError(_("XPM: no colors left to use for mask!"));
759 wxXPMColourMapData
& maskData
= clr_tbl
[maskKey
];
760 maskData
.R
= wxByte(rgb
>> 16);
761 maskData
.G
= wxByte(rgb
>> 8);
762 maskData
.B
= wxByte(rgb
);
764 img
.SetMaskColour(maskData
.R
, maskData
.G
, maskData
.B
);
771 unsigned char *img_data
= img
.GetData();
772 wxXPMColourMap::iterator entry
;
773 wxXPMColourMap::iterator end
= clr_tbl
.end();
775 for (j
= 0; j
< height
; j
++)
777 for (i
= 0; i
< width
; i
++, img_data
+= 3)
779 const char *xpmImgLine
= xpm_data
[1 + colors_cnt
+ j
];
780 if ( !xpmImgLine
|| strlen(xpmImgLine
) < width
*chars_per_pixel
)
782 wxLogError(_("XPM: truncated image data at line %d!"),
783 (int)(1 + colors_cnt
+ j
));
787 for (i_key
= 0; i_key
< chars_per_pixel
; i_key
++)
789 key
[i_key
] = xpmImgLine
[chars_per_pixel
* i
+ i_key
];
793 entry
= clr_tbl
.find(keyString
);
796 wxLogError(_("XPM: Malformed pixel data!"));
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
805 img_data
[0] = entry
->second
.R
;
806 img_data
[1] = entry
->second
.G
;
807 img_data
[2] = entry
->second
.B
;
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
];
815 for (it
= clr_tbl
.begin(), i
= 0; it
!= clr_tbl
.end(); it
++, i
++)
821 wxASSERT(i
== colors_cnt
);
822 img
.SetPalette(wxPalette(colors_cnt
, r
, g
, b
));
826 #endif // wxUSE_PALETTE
830 #endif // wxUSE_IMAGE && wxUSE_XPM