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