]> git.saurik.com Git - wxWidgets.git/blame - src/common/xpmdecod.cpp
Further wxUniv fixes
[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
424f5e27
VS
108
109#include "wx/stream.h"
110#include "wx/image.h"
111#include "wx/utils.h"
112#include "wx/log.h"
113#include "wx/intl.h"
114#include <string.h>
115
116#include "wx/xpmdecod.h"
117
118#if wxUSE_STREAMS
119bool wxXPMDecoder::CanRead(wxInputStream& stream)
120{
121 unsigned char buf[9];
122
123 stream.Read(buf, 9);
124 stream.SeekI(-9, wxFromCurrent);
125
126 return (memcmp(buf, "/* XPM */", 9) == 0);
127}
128
129wxImage wxXPMDecoder::ReadFile(wxInputStream& stream)
130{
131 size_t length = stream.GetSize();
132 wxCHECK_MSG(length != 0, wxNullImage, wxT("Cannot read XPM from stream of unknown size"));
38755449 133
424f5e27
VS
134 char *xpm_buffer = new char[length];
135 char *p, *q;
136 size_t i;
137
78c1f5f1 138 if ( stream.Read(xpm_buffer, length).LastError() == wxSTREAM_READ_ERROR )
424f5e27 139 return FALSE;
38755449 140
424f5e27
VS
141 /*
142 * Remove comments from the file:
143 */
144 for (p = xpm_buffer; *p != '\0'; p++)
145 {
146 if ( (*p == '"') || (*p == '\'') )
147 {
148 if (*p == '"')
149 {
150 for (p++; *p != '\0'; p++)
151 if ( (*p == '"') && (*(p - 1) != '\\') )
152 break;
153 }
154 else // *p == '\''
155 {
156 for (p++; *p != '\0'; p++)
157 if ( (*p == '\'') && (*(p - 1) != '\\') )
158 break;
159 }
160 if (*p == '\0')
161 break;
162 continue;
163 }
164 if ( (*p != '/') || (*(p + 1) != '*') )
165 continue;
166 for (q = p + 2; *q != '\0'; q++)
167 {
168 if ( (*q == '*') && (*(q + 1) == '/') )
169 break;
170 }
171 strcpy(p, q + 2);
172 }
173
174 /*
175 * Remove unquoted characters:
176 */
177 i = 0;
178 for (p = xpm_buffer; *p != '\0'; p++)
179 {
38755449 180 if ( *p != '"' )
424f5e27
VS
181 continue;
182 for (q = p + 1; *q != '\0'; q++)
183 if (*q == '"')
184 break;
185 strncpy(xpm_buffer + i, p + 1, q - p - 1);
186 i += q - p - 1;
187 xpm_buffer[i++] = '\n';
188 p = q + 1;
189 }
190 xpm_buffer[i] = '\0';
38755449 191
424f5e27
VS
192 /*
193 * Create array of lines and convert \n's to \0's:
194 */
195 const char **xpm_lines;
196 size_t lines_cnt = 0;
197 size_t line;
38755449 198
424f5e27
VS
199 for (p = xpm_buffer; *p != '\0'; p++)
200 {
38755449 201 if ( *p == '\n' )
424f5e27
VS
202 lines_cnt++;
203 }
38755449 204
424f5e27
VS
205 xpm_lines = new const char*[lines_cnt];
206 xpm_lines[0] = xpm_buffer;
207 line = 1;
208 for (p = xpm_buffer; (*p != '\0') && (line < lines_cnt); p++)
209 {
38755449 210 if ( *p == '\n' )
424f5e27
VS
211 {
212 xpm_lines[line] = p + 1;
213 *p = '\0';
214 line++;
215 }
216 }
38755449 217
424f5e27
VS
218 /*
219 * Read the image:
220 */
221 wxImage img = ReadData(xpm_lines);
38755449 222
424f5e27 223 delete[] xpm_buffer;
788722ac
JS
224#ifdef __WIN16__
225 delete[] (char**) xpm_lines;
226#else
424f5e27 227 delete[] xpm_lines;
788722ac 228#endif
424f5e27
VS
229 return img;
230}
231#endif // wxUSE_STREAMS
232
233
234/*****************************************************************************\
235* rgbtab.h *
236* *
237* A hard coded rgb.txt. To keep it short I removed all colornames with *
238* trailing numbers, Blue3 etc, except the GrayXX. Sorry Grey-lovers I prefer *
239* Gray ;-). But Grey is recognized on lookups, only on save Gray will be *
240* used, maybe you want to do some substitue there too. *
241* *
242* To save memory the RGBs are coded in one long value, as done by the RGB *
243* macro. *
244* *
245* Developed by HeDu 3/94 (hedu@cul-ipn.uni-kiel.de) *
246\*****************************************************************************/
247
248
38755449 249typedef struct
424f5e27
VS
250{
251 char *name;
252 wxUint32 rgb;
253} rgbRecord;
254
255#define myRGB(r,g,b) ((wxUint32)r<<16|(wxUint32)g<<8|(wxUint32)b)
256
257static rgbRecord theRGBRecords[] =
258{
1e6feb95
VZ
259 {"AliceBlue", myRGB(240, 248, 255)},
260 {"AntiqueWhite", myRGB(250, 235, 215)},
261 {"Aquamarine", myRGB(50, 191, 193)},
262 {"Azure", myRGB(240, 255, 255)},
263 {"Beige", myRGB(245, 245, 220)},
264 {"Bisque", myRGB(255, 228, 196)},
265 {"Black", myRGB(0, 0, 0)},
266 {"BlanchedAlmond", myRGB(255, 235, 205)},
267 {"Blue", myRGB(0, 0, 255)},
268 {"BlueViolet", myRGB(138, 43, 226)},
269 {"Brown", myRGB(165, 42, 42)},
424f5e27 270 {"burlywood", myRGB(222, 184, 135)},
1e6feb95 271 {"CadetBlue", myRGB(95, 146, 158)},
424f5e27
VS
272 {"chartreuse", myRGB(127, 255, 0)},
273 {"chocolate", myRGB(210, 105, 30)},
1e6feb95
VZ
274 {"Coral", myRGB(255, 114, 86)},
275 {"CornflowerBlue", myRGB(34, 34, 152)},
424f5e27 276 {"cornsilk", myRGB(255, 248, 220)},
1e6feb95
VZ
277 {"Cyan", myRGB(0, 255, 255)},
278 {"DarkGoldenrod", myRGB(184, 134, 11)},
279 {"DarkGreen", myRGB(0, 86, 45)},
280 {"DarkKhaki", myRGB(189, 183, 107)},
281 {"DarkOliveGreen", myRGB(85, 86, 47)},
282 {"DarkOrange", myRGB(255, 140, 0)},
283 {"DarkOrchid", myRGB(139, 32, 139)},
284 {"DarkSalmon", myRGB(233, 150, 122)},
285 {"DarkSeaGreen", myRGB(143, 188, 143)},
286 {"DarkSlateBlue", myRGB(56, 75, 102)},
287 {"DarkSlateGray", myRGB(47, 79, 79)},
288 {"DarkTurquoise", myRGB(0, 166, 166)},
289 {"DarkViolet", myRGB(148, 0, 211)},
290 {"DeepPink", myRGB(255, 20, 147)},
291 {"DeepSkyBlue", myRGB(0, 191, 255)},
292 {"DimGray", myRGB(84, 84, 84)},
293 {"DodgerBlue", myRGB(30, 144, 255)},
294 {"Firebrick", myRGB(142, 35, 35)},
295 {"FloralWhite", myRGB(255, 250, 240)},
296 {"ForestGreen", myRGB(80, 159, 105)},
424f5e27 297 {"gainsboro", myRGB(220, 220, 220)},
1e6feb95
VZ
298 {"GhostWhite", myRGB(248, 248, 255)},
299 {"Gold", myRGB(218, 170, 0)},
300 {"Goldenrod", myRGB(239, 223, 132)},
301 {"Gray", myRGB(126, 126, 126)},
302 {"Gray0", myRGB(0, 0, 0)},
303 {"Gray1", myRGB(3, 3, 3)},
304 {"Gray10", myRGB(26, 26, 26)},
305 {"Gray100", myRGB(255, 255, 255)},
306 {"Gray11", myRGB(28, 28, 28)},
307 {"Gray12", myRGB(31, 31, 31)},
308 {"Gray13", myRGB(33, 33, 33)},
309 {"Gray14", myRGB(36, 36, 36)},
310 {"Gray15", myRGB(38, 38, 38)},
311 {"Gray16", myRGB(41, 41, 41)},
312 {"Gray17", myRGB(43, 43, 43)},
313 {"Gray18", myRGB(46, 46, 46)},
314 {"Gray19", myRGB(48, 48, 48)},
315 {"Gray2", myRGB(5, 5, 5)},
316 {"Gray20", myRGB(51, 51, 51)},
317 {"Gray21", myRGB(54, 54, 54)},
318 {"Gray22", myRGB(56, 56, 56)},
319 {"Gray23", myRGB(59, 59, 59)},
320 {"Gray24", myRGB(61, 61, 61)},
321 {"Gray25", myRGB(64, 64, 64)},
322 {"Gray26", myRGB(66, 66, 66)},
323 {"Gray27", myRGB(69, 69, 69)},
324 {"Gray28", myRGB(71, 71, 71)},
325 {"Gray29", myRGB(74, 74, 74)},
326 {"Gray3", myRGB(8, 8, 8)},
327 {"Gray30", myRGB(77, 77, 77)},
328 {"Gray31", myRGB(79, 79, 79)},
329 {"Gray32", myRGB(82, 82, 82)},
330 {"Gray33", myRGB(84, 84, 84)},
331 {"Gray34", myRGB(87, 87, 87)},
332 {"Gray35", myRGB(89, 89, 89)},
333 {"Gray36", myRGB(92, 92, 92)},
334 {"Gray37", myRGB(94, 94, 94)},
335 {"Gray38", myRGB(97, 97, 97)},
336 {"Gray39", myRGB(99, 99, 99)},
337 {"Gray4", myRGB(10, 10, 10)},
338 {"Gray40", myRGB(102, 102, 102)},
339 {"Gray41", myRGB(105, 105, 105)},
340 {"Gray42", myRGB(107, 107, 107)},
341 {"Gray43", myRGB(110, 110, 110)},
342 {"Gray44", myRGB(112, 112, 112)},
343 {"Gray45", myRGB(115, 115, 115)},
344 {"Gray46", myRGB(117, 117, 117)},
345 {"Gray47", myRGB(120, 120, 120)},
346 {"Gray48", myRGB(122, 122, 122)},
347 {"Gray49", myRGB(125, 125, 125)},
348 {"Gray5", myRGB(13, 13, 13)},
349 {"Gray50", myRGB(127, 127, 127)},
350 {"Gray51", myRGB(130, 130, 130)},
351 {"Gray52", myRGB(133, 133, 133)},
352 {"Gray53", myRGB(135, 135, 135)},
353 {"Gray54", myRGB(138, 138, 138)},
354 {"Gray55", myRGB(140, 140, 140)},
355 {"Gray56", myRGB(143, 143, 143)},
356 {"Gray57", myRGB(145, 145, 145)},
357 {"Gray58", myRGB(148, 148, 148)},
358 {"Gray59", myRGB(150, 150, 150)},
359 {"Gray6", myRGB(15, 15, 15)},
360 {"Gray60", myRGB(153, 153, 153)},
361 {"Gray61", myRGB(156, 156, 156)},
362 {"Gray62", myRGB(158, 158, 158)},
363 {"Gray63", myRGB(161, 161, 161)},
364 {"Gray64", myRGB(163, 163, 163)},
365 {"Gray65", myRGB(166, 166, 166)},
366 {"Gray66", myRGB(168, 168, 168)},
367 {"Gray67", myRGB(171, 171, 171)},
368 {"Gray68", myRGB(173, 173, 173)},
369 {"Gray69", myRGB(176, 176, 176)},
370 {"Gray7", myRGB(18, 18, 18)},
371 {"Gray70", myRGB(179, 179, 179)},
372 {"Gray71", myRGB(181, 181, 181)},
373 {"Gray72", myRGB(184, 184, 184)},
374 {"Gray73", myRGB(186, 186, 186)},
375 {"Gray74", myRGB(189, 189, 189)},
376 {"Gray75", myRGB(191, 191, 191)},
377 {"Gray76", myRGB(194, 194, 194)},
378 {"Gray77", myRGB(196, 196, 196)},
379 {"Gray78", myRGB(199, 199, 199)},
380 {"Gray79", myRGB(201, 201, 201)},
381 {"Gray8", myRGB(20, 20, 20)},
382 {"Gray80", myRGB(204, 204, 204)},
383 {"Gray81", myRGB(207, 207, 207)},
384 {"Gray82", myRGB(209, 209, 209)},
385 {"Gray83", myRGB(212, 212, 212)},
386 {"Gray84", myRGB(214, 214, 214)},
387 {"Gray85", myRGB(217, 217, 217)},
388 {"Gray86", myRGB(219, 219, 219)},
389 {"Gray87", myRGB(222, 222, 222)},
390 {"Gray88", myRGB(224, 224, 224)},
391 {"Gray89", myRGB(227, 227, 227)},
392 {"Gray9", myRGB(23, 23, 23)},
393 {"Gray90", myRGB(229, 229, 229)},
394 {"Gray91", myRGB(232, 232, 232)},
395 {"Gray92", myRGB(235, 235, 235)},
396 {"Gray93", myRGB(237, 237, 237)},
397 {"Gray94", myRGB(240, 240, 240)},
398 {"Gray95", myRGB(242, 242, 242)},
399 {"Gray96", myRGB(245, 245, 245)},
400 {"Gray97", myRGB(247, 247, 247)},
401 {"Gray98", myRGB(250, 250, 250)},
402 {"Gray99", myRGB(252, 252, 252)},
403 {"Green", myRGB(0, 255, 0)},
404 {"GreenYellow", myRGB(173, 255, 47)},
424f5e27 405 {"honeydew", myRGB(240, 255, 240)},
1e6feb95
VZ
406 {"HotPink", myRGB(255, 105, 180)},
407 {"IndianRed", myRGB(107, 57, 57)},
424f5e27 408 {"ivory", myRGB(255, 255, 240)},
1e6feb95 409 {"Khaki", myRGB(179, 179, 126)},
424f5e27 410 {"lavender", myRGB(230, 230, 250)},
1e6feb95
VZ
411 {"LavenderBlush", myRGB(255, 240, 245)},
412 {"LawnGreen", myRGB(124, 252, 0)},
413 {"LemonChiffon", myRGB(255, 250, 205)},
414 {"LightBlue", myRGB(176, 226, 255)},
415 {"LightCoral", myRGB(240, 128, 128)},
416 {"LightCyan", myRGB(224, 255, 255)},
417 {"LightGoldenrod", myRGB(238, 221, 130)},
418 {"LightGoldenrodYellow", myRGB(250, 250, 210)},
419 {"LightGray", myRGB(168, 168, 168)},
420 {"LightPink", myRGB(255, 182, 193)},
421 {"LightSalmon", myRGB(255, 160, 122)},
422 {"LightSeaGreen", myRGB(32, 178, 170)},
423 {"LightSkyBlue", myRGB(135, 206, 250)},
424 {"LightSlateBlue", myRGB(132, 112, 255)},
425 {"LightSlateGray", myRGB(119, 136, 153)},
426 {"LightSteelBlue", myRGB(124, 152, 211)},
427 {"LightYellow", myRGB(255, 255, 224)},
428 {"LimeGreen", myRGB(0, 175, 20)},
424f5e27 429 {"linen", myRGB(250, 240, 230)},
1e6feb95
VZ
430 {"Magenta", myRGB(255, 0, 255)},
431 {"Maroon", myRGB(143, 0, 82)},
432 {"MediumAquamarine", myRGB(0, 147, 143)},
433 {"MediumBlue", myRGB(50, 50, 204)},
434 {"MediumForestGreen", myRGB(50, 129, 75)},
435 {"MediumGoldenrod", myRGB(209, 193, 102)},
436 {"MediumOrchid", myRGB(189, 82, 189)},
437 {"MediumPurple", myRGB(147, 112, 219)},
438 {"MediumSeaGreen", myRGB(52, 119, 102)},
439 {"MediumSlateBlue", myRGB(106, 106, 141)},
440 {"MediumSpringGreen", myRGB(35, 142, 35)},
441 {"MediumTurquoise", myRGB(0, 210, 210)},
442 {"MediumVioletRed", myRGB(213, 32, 121)},
443 {"MidnightBlue", myRGB(47, 47, 100)},
444 {"MintCream", myRGB(245, 255, 250)},
445 {"MistyRose", myRGB(255, 228, 225)},
424f5e27 446 {"moccasin", myRGB(255, 228, 181)},
1e6feb95
VZ
447 {"NavajoWhite", myRGB(255, 222, 173)},
448 {"Navy", myRGB(35, 35, 117)},
449 {"NavyBlue", myRGB(35, 35, 117)},
450 {"OldLace", myRGB(253, 245, 230)},
451 {"OliveDrab", myRGB(107, 142, 35)},
452 {"Orange", myRGB(255, 135, 0)},
453 {"OrangeRed", myRGB(255, 69, 0)},
454 {"Orchid", myRGB(239, 132, 239)},
455 {"PaleGoldenrod", myRGB(238, 232, 170)},
456 {"PaleGreen", myRGB(115, 222, 120)},
457 {"PaleTurquoise", myRGB(175, 238, 238)},
458 {"PaleVioletRed", myRGB(219, 112, 147)},
459 {"PapayaWhip", myRGB(255, 239, 213)},
460 {"PeachPuff", myRGB(255, 218, 185)},
424f5e27 461 {"peru", myRGB(205, 133, 63)},
1e6feb95
VZ
462 {"Pink", myRGB(255, 181, 197)},
463 {"Plum", myRGB(197, 72, 155)},
464 {"PowderBlue", myRGB(176, 224, 230)},
424f5e27 465 {"purple", myRGB(160, 32, 240)},
1e6feb95
VZ
466 {"Red", myRGB(255, 0, 0)},
467 {"RosyBrown", myRGB(188, 143, 143)},
468 {"RoyalBlue", myRGB(65, 105, 225)},
469 {"SaddleBrown", myRGB(139, 69, 19)},
470 {"Salmon", myRGB(233, 150, 122)},
471 {"SandyBrown", myRGB(244, 164, 96)},
472 {"SeaGreen", myRGB(82, 149, 132)},
424f5e27 473 {"seashell", myRGB(255, 245, 238)},
1e6feb95
VZ
474 {"Sienna", myRGB(150, 82, 45)},
475 {"SkyBlue", myRGB(114, 159, 255)},
476 {"SlateBlue", myRGB(126, 136, 171)},
477 {"SlateGray", myRGB(112, 128, 144)},
424f5e27 478 {"snow", myRGB(255, 250, 250)},
1e6feb95
VZ
479 {"SpringGreen", myRGB(65, 172, 65)},
480 {"SteelBlue", myRGB(84, 112, 170)},
481 {"Tan", myRGB(222, 184, 135)},
482 {"Thistle", myRGB(216, 191, 216)},
424f5e27 483 {"tomato", myRGB(255, 99, 71)},
1e6feb95
VZ
484 {"Transparent", myRGB(0, 0, 1)},
485 {"Turquoise", myRGB(25, 204, 223)},
486 {"Violet", myRGB(156, 62, 206)},
487 {"VioletRed", myRGB(243, 62, 150)},
488 {"Wheat", myRGB(245, 222, 179)},
489 {"White", myRGB(255, 255, 255)},
490 {"WhiteSmoke", myRGB(245, 245, 245)},
491 {"Yellow", myRGB(255, 255, 0)},
492 {"YellowGreen", myRGB(50, 216, 56)},
424f5e27
VS
493 {NULL, myRGB(0, 0, 0)}
494};
495static int numTheRGBRecords = 234;
496
b326e313
VS
497static unsigned char ParseHexadecimal(char digit1, char digit2)
498{
499 unsigned char i1, i2;
500
501 if (digit1 >= 'a')
502 i1 = digit1 - 'a' + 0x0A;
503 else if (digit1 >= 'A')
504 i1 = digit1 - 'A' + 0x0A;
505 else
506 i1 = digit1 - '0';
507 if (digit2 >= 'a')
508 i2 = digit2 - 'a' + 0x0A;
509 else if (digit2 >= 'A')
510 i2 = digit2 - 'A' + 0x0A;
511 else
512 i2 = digit2 - '0';
513 return (0x10 * i1 + i2);
514}
515
424f5e27
VS
516static bool GetRGBFromName(const char *inname, bool *isNone,
517 unsigned char *r, unsigned char*g, unsigned char *b)
518{
519 int left, right, middle;
520 int cmp;
521 wxUint32 rgbVal;
522 char *name;
523 char *grey, *p;
524
525 // #rrggbb are not in database, we parse them directly
526 if ( *inname == '#' && strlen(inname) == 7 )
527 {
b326e313
VS
528 *r = ParseHexadecimal(inname[1], inname[2]);
529 *g = ParseHexadecimal(inname[3], inname[4]);
530 *b = ParseHexadecimal(inname[5], inname[6]);
424f5e27 531 *isNone = FALSE;
424f5e27
VS
532 return TRUE;
533 }
534
535 name = strdup(inname);
536
537 // theRGBRecords[] has no names with spaces, and no grey, but a
538 // lot of gray...
539
540 // so first extract ' '
38755449 541 while ((p = strchr(name, ' ')) != NULL)
424f5e27
VS
542 {
543 while (*(p)) // till eof of string
544 {
545 *p = *(p + 1); // copy to the left
546 p++;
547 }
548 }
549 // fold to lower case
550 p = name;
38755449 551 while (*p)
424f5e27
VS
552 {
553 *p = tolower(*p);
554 p++;
555 }
556
557 // substitute Grey with Gray, else rgbtab.h would have more than 100
558 // 'duplicate' entries
38755449 559 if ( (grey = strstr(name, "grey")) != NULL )
424f5e27
VS
560 grey[2] = 'a';
561
562 // check for special 'none' colour:
c48269b9 563 bool found;
424f5e27
VS
564 if ( strcmp(name, "none") == 0 )
565 {
566 *isNone = TRUE;
c48269b9 567 found = TRUE;
424f5e27 568 }
c48269b9 569 else // not "None"
424f5e27 570 {
c48269b9
VZ
571 found = FALSE;
572
573 // binary search:
574 left = 0;
575 right = numTheRGBRecords - 1;
576 do
424f5e27 577 {
c48269b9
VZ
578 middle = (left + right) / 2;
579 cmp = strcmp(name, theRGBRecords[middle].name);
580 if ( cmp == 0 )
581 {
582 rgbVal = theRGBRecords[middle].rgb;
583 *r = (unsigned char)((rgbVal >> 16) & 0xFF);
584 *g = (unsigned char)((rgbVal >> 8) & 0xFF);
585 *b = (unsigned char)((rgbVal) & 0xFF);
586 *isNone = FALSE;
587 found = TRUE;
588 break;
589 }
590 else if ( cmp < 0 )
591 {
592 right = middle - 1;
593 }
594 else // cmp > 0
595 {
596 left = middle + 1;
597 }
598 } while (left <= right);
599 }
424f5e27
VS
600
601 free(name);
c48269b9
VZ
602
603 return found;
424f5e27
VS
604}
605
606static const char *ParseColor(const char *data)
607{
38755449 608 static const char *targets[] =
424f5e27
VS
609 {"c ", "g ", "g4 ", "m ", "b ", "s ", NULL};
610
611 const char *p, *r;
612 const char *q;
613 int i;
614
615 for (i = 0; targets[i] != NULL; i++)
616 {
617 r = data;
618 for (q = targets[i]; *r != '\0'; r++)
619 {
620 if ( *r != *q )
621 continue;
622 if ( !isspace((int) (*(r - 1))) )
623 continue;
624 p = r;
625 for (;;)
626 {
627 if ( *q == '\0' )
628 return p;
629 if ( *p++ != *q++ )
630 break;
631 }
632 q = targets[i];
633 }
634 }
635 return NULL;
636}
637
638class wxXPMColourMapData : public wxObject
639{
640 public:
641 unsigned char R,G,B;
642};
643
644wxImage wxXPMDecoder::ReadData(const char **xpm_data)
645{
646 wxImage img;
647 int count;
648 unsigned width, height, colors_cnt, chars_per_pixel;
ffe107c8
VS
649 size_t i, j, i_key;
650 wxChar key[64];
424f5e27
VS
651 const char *clr_def;
652 bool hasMask;
653 wxXPMColourMapData *clr_data;
654 wxHashTable clr_tbl(wxKEY_STRING);
38755449 655
424f5e27
VS
656 /*
657 * Read hints and initialize structures:
658 */
b326e313 659
38755449 660 count = sscanf(xpm_data[0], "%u %u %u %u",
424f5e27
VS
661 &width, &height, &colors_cnt, &chars_per_pixel);
662 if ( count != 4 || width * height * colors_cnt == 0 )
663 {
664 wxLogError(_T("XPM: Not XPM data!"));
665 return wxNullImage;
666 }
667
668 // VS: XPM color map this large would be insane, since XPMs are encoded with
669 // 92 possible values on each position, 92^64 is *way* larger space than
670 // 8bit RGB...
671 wxCHECK_MSG(chars_per_pixel < 64, wxNullImage, wxT("XPM colormaps this large not supported."));
38755449 672
424f5e27
VS
673 img.Create(width, height);
674 if ( !img.Ok() ) return img;
675
676 img.SetMask(FALSE);
ffe107c8 677 key[chars_per_pixel] = wxT('\0');
424f5e27
VS
678 hasMask = FALSE;
679 clr_tbl.DeleteContents(TRUE);
38755449 680
424f5e27
VS
681 /*
682 * Create colour map:
683 */
684 for (i = 0; i < colors_cnt; i++)
685 {
ffe107c8
VS
686 for (i_key = 0; i_key < chars_per_pixel; i_key++)
687 key[i_key] = (wxChar)xpm_data[1 + i][i_key];
424f5e27
VS
688 clr_def = ParseColor(xpm_data[1 + i]);
689 clr_data = new wxXPMColourMapData;
690
691 if ( clr_def == NULL )
692 {
693 wxLogError(_("XPM: malformed colour definition '%s'!"), xpm_data[1+i]);
694 clr_data->R = 255, clr_data->G = 0, clr_data->B = 255;
695 }
696 else
697 {
698 bool isNone;
38755449 699 if ( !GetRGBFromName(clr_def, &isNone,
424f5e27
VS
700 &clr_data->R, &clr_data->G, &clr_data->B) )
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 if ( isNone )
708 {
709 img.SetMask(TRUE);
710 img.SetMaskColour(255, 0, 255);
711 hasMask = TRUE;
712 clr_data->R = 255, clr_data->G = 0, clr_data->B = 255;
713 }
714 else
715 {
38755449 716 if ( hasMask && clr_data->R == 255 &&
424f5e27
VS
717 clr_data->G == 0 && clr_data->B == 255 )
718 clr_data->B = 254;
719 }
720 }
721 }
722 clr_tbl.Put(key, clr_data);
723 }
724
725 /*
726 * Parse image data:
727 */
38755449 728
424f5e27
VS
729 unsigned char *img_data = img.GetData();
730 for (j = 0; j < height; j++)
731 {
732 for (i = 0; i < width; i++, img_data += 3)
733 {
ffe107c8
VS
734 for (i_key = 0; i_key < chars_per_pixel; i_key++)
735 key[i_key] = (wxChar)xpm_data[1 + colors_cnt + j]
736 [chars_per_pixel * i + i_key];
424f5e27
VS
737 clr_data = (wxXPMColourMapData*) clr_tbl.Get(key);
738 if ( clr_data == NULL )
739 {
740 wxLogError(_("XPM: Malformed pixel data!"));
741 }
742 else
743 {
744 img_data[0] = clr_data->R;
745 img_data[1] = clr_data->G;
746 img_data[2] = clr_data->B;
747 }
748 }
749 }
750
751 return img;
752}
753
754