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