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