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