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