]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-xpm.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~andy/gtk] / gdk-pixbuf / io-xpm.c
1 /* GdkPixbuf library - JPEG image loader
2  *
3  * Copyright (C) 1999 Mark Crichton
4  * Copyright (C) 1999 The Free Software Foundation
5  *
6  * Authors: Mark Crichton <crichton@gimp.org>
7  *          Federico Mena-Quintero <federico@gimp.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <config.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <glib.h>
30 #include "gdk-pixbuf-private.h"
31
32 \f
33
34 /* I have must have done something to deserve this.
35  * XPM is such a crappy format to handle.
36  * This code is an ugly hybred from gdkpixmap.c
37  * modified to respect transparent colors.
38  * It's still a mess, though.
39  */
40
41 enum buf_op {
42         op_header,
43         op_cmap,
44         op_body
45 };
46
47 typedef struct {
48         gchar *color_string;
49         guint16 red;
50         guint16 green;
51         guint16 blue;
52         gint transparent;
53 } XPMColor;
54
55 struct file_handle {
56         FILE *infile;
57         gchar *buffer;
58         guint buffer_size;
59 };
60
61 struct mem_handle {
62         const gchar **data;
63         int offset;
64 };
65
66 /* The following 2 routines (parse_color, find_color) come from Tk, via the Win32
67  * port of GDK. The licensing terms on these (longer than the functions) is:
68  *
69  * This software is copyrighted by the Regents of the University of
70  * California, Sun Microsystems, Inc., and other parties.  The following
71  * terms apply to all files associated with the software unless explicitly
72  * disclaimed in individual files.
73  * 
74  * The authors hereby grant permission to use, copy, modify, distribute,
75  * and license this software and its documentation for any purpose, provided
76  * that existing copyright notices are retained in all copies and that this
77  * notice is included verbatim in any distributions. No written agreement,
78  * license, or royalty fee is required for any of the authorized uses.
79  * Modifications to this software may be copyrighted by their authors
80  * and need not follow the licensing terms described here, provided that
81  * the new terms are clearly indicated on the first page of each file where
82  * they apply.
83  * 
84  * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
85  * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
86  * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
87  * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
88  * POSSIBILITY OF SUCH DAMAGE.
89  * 
90  * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
91  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
92  * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
93  * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
94  * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
95  * MODIFICATIONS.
96  * 
97  * GOVERNMENT USE: If you are acquiring this software on behalf of the
98  * U.S. government, the Government shall have only "Restricted Rights"
99  * in the software and related documentation as defined in the Federal 
100  * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2).  If you
101  * are acquiring the software on behalf of the Department of Defense, the
102  * software shall be classified as "Commercial Computer Software" and the
103  * Government shall have only "Restricted Rights" as defined in Clause
104  * 252.227-7013 (c) (1) of DFARs.  Notwithstanding the foregoing, the
105  * authors grant the U.S. Government and others acting in its behalf
106  * permission to use and distribute the software in accordance with the
107  * terms specified in this license.
108  */
109
110 typedef struct {
111     const char *name;
112     unsigned char red;
113     unsigned char green;
114     unsigned char blue;
115 } XPMColorEntry;
116
117 static XPMColorEntry xColors[] = {
118     { "alice blue", 240, 248, 255 },
119     { "AliceBlue", 240, 248, 255 },
120     { "antique white", 250, 235, 215 },
121     { "AntiqueWhite", 250, 235, 215 },
122     { "AntiqueWhite1", 255, 239, 219 },
123     { "AntiqueWhite2", 238, 223, 204 },
124     { "AntiqueWhite3", 205, 192, 176 },
125     { "AntiqueWhite4", 139, 131, 120 },
126     { "aquamarine", 127, 255, 212 },
127     { "aquamarine1", 127, 255, 212 },
128     { "aquamarine2", 118, 238, 198 },
129     { "aquamarine3", 102, 205, 170 },
130     { "aquamarine4", 69, 139, 116 },
131     { "azure", 240, 255, 255 },
132     { "azure1", 240, 255, 255 },
133     { "azure2", 224, 238, 238 },
134     { "azure3", 193, 205, 205 },
135     { "azure4", 131, 139, 139 },
136     { "beige", 245, 245, 220 },
137     { "bisque", 255, 228, 196 },
138     { "bisque1", 255, 228, 196 },
139     { "bisque2", 238, 213, 183 },
140     { "bisque3", 205, 183, 158 },
141     { "bisque4", 139, 125, 107 },
142     { "black", 0, 0, 0 },
143     { "blanched almond", 255, 235, 205 },
144     { "BlanchedAlmond", 255, 235, 205 },
145     { "blue", 0, 0, 255 },
146     { "blue violet", 138, 43, 226 },
147     { "blue1", 0, 0, 255 },
148     { "blue2", 0, 0, 238 },
149     { "blue3", 0, 0, 205 },
150     { "blue4", 0, 0, 139 },
151     { "BlueViolet", 138, 43, 226 },
152     { "brown", 165, 42, 42 },
153     { "brown1", 255, 64, 64 },
154     { "brown2", 238, 59, 59 },
155     { "brown3", 205, 51, 51 },
156     { "brown4", 139, 35, 35 },
157     { "burlywood", 222, 184, 135 },
158     { "burlywood1", 255, 211, 155 },
159     { "burlywood2", 238, 197, 145 },
160     { "burlywood3", 205, 170, 125 },
161     { "burlywood4", 139, 115, 85 },
162     { "cadet blue", 95, 158, 160 },
163     { "CadetBlue", 95, 158, 160 },
164     { "CadetBlue1", 152, 245, 255 },
165     { "CadetBlue2", 142, 229, 238 },
166     { "CadetBlue3", 122, 197, 205 },
167     { "CadetBlue4", 83, 134, 139 },
168     { "chartreuse", 127, 255, 0 },
169     { "chartreuse1", 127, 255, 0 },
170     { "chartreuse2", 118, 238, 0 },
171     { "chartreuse3", 102, 205, 0 },
172     { "chartreuse4", 69, 139, 0 },
173     { "chocolate", 210, 105, 30 },
174     { "chocolate1", 255, 127, 36 },
175     { "chocolate2", 238, 118, 33 },
176     { "chocolate3", 205, 102, 29 },
177     { "chocolate4", 139, 69, 19 },
178     { "coral", 255, 127, 80 },
179     { "coral1", 255, 114, 86 },
180     { "coral2", 238, 106, 80 },
181     { "coral3", 205, 91, 69 },
182     { "coral4", 139, 62, 47 },
183     { "cornflower blue", 100, 149, 237 },
184     { "CornflowerBlue", 100, 149, 237 },
185     { "cornsilk", 255, 248, 220 },
186     { "cornsilk1", 255, 248, 220 },
187     { "cornsilk2", 238, 232, 205 },
188     { "cornsilk3", 205, 200, 177 },
189     { "cornsilk4", 139, 136, 120 },
190     { "cyan", 0, 255, 255 },
191     { "cyan1", 0, 255, 255 },
192     { "cyan2", 0, 238, 238 },
193     { "cyan3", 0, 205, 205 },
194     { "cyan4", 0, 139, 139 },
195     { "dark blue", 0, 0, 139 },
196     { "dark cyan", 0, 139, 139 },
197     { "dark goldenrod", 184, 134, 11 },
198     { "dark gray", 169, 169, 169 },
199     { "dark green", 0, 100, 0 },
200     { "dark grey", 169, 169, 169 },
201     { "dark khaki", 189, 183, 107 },
202     { "dark magenta", 139, 0, 139 },
203     { "dark olive green", 85, 107, 47 },
204     { "dark orange", 255, 140, 0 },
205     { "dark orchid", 153, 50, 204 },
206     { "dark red", 139, 0, 0 },
207     { "dark salmon", 233, 150, 122 },
208     { "dark sea green", 143, 188, 143 },
209     { "dark slate blue", 72, 61, 139 },
210     { "dark slate gray", 47, 79, 79 },
211     { "dark slate grey", 47, 79, 79 },
212     { "dark turquoise", 0, 206, 209 },
213     { "dark violet", 148, 0, 211 },
214     { "DarkBlue", 0, 0, 139 },
215     { "DarkCyan", 0, 139, 139 },
216     { "DarkGoldenrod", 184, 134, 11 },
217     { "DarkGoldenrod1", 255, 185, 15 },
218     { "DarkGoldenrod2", 238, 173, 14 },
219     { "DarkGoldenrod3", 205, 149, 12 },
220     { "DarkGoldenrod4", 139, 101, 8 },
221     { "DarkGray", 169, 169, 169 },
222     { "DarkGreen", 0, 100, 0 },
223     { "DarkGrey", 169, 169, 169 },
224     { "DarkKhaki", 189, 183, 107 },
225     { "DarkMagenta", 139, 0, 139 },
226     { "DarkOliveGreen", 85, 107, 47 },
227     { "DarkOliveGreen1", 202, 255, 112 },
228     { "DarkOliveGreen2", 188, 238, 104 },
229     { "DarkOliveGreen3", 162, 205, 90 },
230     { "DarkOliveGreen4", 110, 139, 61 },
231     { "DarkOrange", 255, 140, 0 },
232     { "DarkOrange1", 255, 127, 0 },
233     { "DarkOrange2", 238, 118, 0 },
234     { "DarkOrange3", 205, 102, 0 },
235     { "DarkOrange4", 139, 69, 0 },
236     { "DarkOrchid", 153, 50, 204 },
237     { "DarkOrchid1", 191, 62, 255 },
238     { "DarkOrchid2", 178, 58, 238 },
239     { "DarkOrchid3", 154, 50, 205 },
240     { "DarkOrchid4", 104, 34, 139 },
241     { "DarkRed", 139, 0, 0 },
242     { "DarkSalmon", 233, 150, 122 },
243     { "DarkSeaGreen", 143, 188, 143 },
244     { "DarkSeaGreen1", 193, 255, 193 },
245     { "DarkSeaGreen2", 180, 238, 180 },
246     { "DarkSeaGreen3", 155, 205, 155 },
247     { "DarkSeaGreen4", 105, 139, 105 },
248     { "DarkSlateBlue", 72, 61, 139 },
249     { "DarkSlateGray", 47, 79, 79 },
250     { "DarkSlateGray1", 151, 255, 255 },
251     { "DarkSlateGray2", 141, 238, 238 },
252     { "DarkSlateGray3", 121, 205, 205 },
253     { "DarkSlateGray4", 82, 139, 139 },
254     { "DarkSlateGrey", 47, 79, 79 },
255     { "DarkTurquoise", 0, 206, 209 },
256     { "DarkViolet", 148, 0, 211 },
257     { "deep pink", 255, 20, 147 },
258     { "deep sky blue", 0, 191, 255 },
259     { "DeepPink", 255, 20, 147 },
260     { "DeepPink1", 255, 20, 147 },
261     { "DeepPink2", 238, 18, 137 },
262     { "DeepPink3", 205, 16, 118 },
263     { "DeepPink4", 139, 10, 80 },
264     { "DeepSkyBlue", 0, 191, 255 },
265     { "DeepSkyBlue1", 0, 191, 255 },
266     { "DeepSkyBlue2", 0, 178, 238 },
267     { "DeepSkyBlue3", 0, 154, 205 },
268     { "DeepSkyBlue4", 0, 104, 139 },
269     { "dim gray", 105, 105, 105 },
270     { "dim grey", 105, 105, 105 },
271     { "DimGray", 105, 105, 105 },
272     { "DimGrey", 105, 105, 105 },
273     { "dodger blue", 30, 144, 255 },
274     { "DodgerBlue", 30, 144, 255 },
275     { "DodgerBlue1", 30, 144, 255 },
276     { "DodgerBlue2", 28, 134, 238 },
277     { "DodgerBlue3", 24, 116, 205 },
278     { "DodgerBlue4", 16, 78, 139 },
279     { "firebrick", 178, 34, 34 },
280     { "firebrick1", 255, 48, 48 },
281     { "firebrick2", 238, 44, 44 },
282     { "firebrick3", 205, 38, 38 },
283     { "firebrick4", 139, 26, 26 },
284     { "floral white", 255, 250, 240 },
285     { "FloralWhite", 255, 250, 240 },
286     { "forest green", 34, 139, 34 },
287     { "ForestGreen", 34, 139, 34 },
288     { "gainsboro", 220, 220, 220 },
289     { "ghost white", 248, 248, 255 },
290     { "GhostWhite", 248, 248, 255 },
291     { "gold", 255, 215, 0 },
292     { "gold1", 255, 215, 0 },
293     { "gold2", 238, 201, 0 },
294     { "gold3", 205, 173, 0 },
295     { "gold4", 139, 117, 0 },
296     { "goldenrod", 218, 165, 32 },
297     { "goldenrod1", 255, 193, 37 },
298     { "goldenrod2", 238, 180, 34 },
299     { "goldenrod3", 205, 155, 29 },
300     { "goldenrod4", 139, 105, 20 },
301     { "gray", 190, 190, 190 },
302     { "gray0", 0, 0, 0 },
303     { "gray1", 3, 3, 3 },
304     { "gray10", 26, 26, 26 },
305     { "gray100", 255, 255, 255 },
306     { "gray11", 28, 28, 28 },
307     { "gray12", 31, 31, 31 },
308     { "gray13", 33, 33, 33 },
309     { "gray14", 36, 36, 36 },
310     { "gray15", 38, 38, 38 },
311     { "gray16", 41, 41, 41 },
312     { "gray17", 43, 43, 43 },
313     { "gray18", 46, 46, 46 },
314     { "gray19", 48, 48, 48 },
315     { "gray2", 5, 5, 5 },
316     { "gray20", 51, 51, 51 },
317     { "gray21", 54, 54, 54 },
318     { "gray22", 56, 56, 56 },
319     { "gray23", 59, 59, 59 },
320     { "gray24", 61, 61, 61 },
321     { "gray25", 64, 64, 64 },
322     { "gray26", 66, 66, 66 },
323     { "gray27", 69, 69, 69 },
324     { "gray28", 71, 71, 71 },
325     { "gray29", 74, 74, 74 },
326     { "gray3", 8, 8, 8 },
327     { "gray30", 77, 77, 77 },
328     { "gray31", 79, 79, 79 },
329     { "gray32", 82, 82, 82 },
330     { "gray33", 84, 84, 84 },
331     { "gray34", 87, 87, 87 },
332     { "gray35", 89, 89, 89 },
333     { "gray36", 92, 92, 92 },
334     { "gray37", 94, 94, 94 },
335     { "gray38", 97, 97, 97 },
336     { "gray39", 99, 99, 99 },
337     { "gray4", 10, 10, 10 },
338     { "gray40", 102, 102, 102 },
339     { "gray41", 105, 105, 105 },
340     { "gray42", 107, 107, 107 },
341     { "gray43", 110, 110, 110 },
342     { "gray44", 112, 112, 112 },
343     { "gray45", 115, 115, 115 },
344     { "gray46", 117, 117, 117 },
345     { "gray47", 120, 120, 120 },
346     { "gray48", 122, 122, 122 },
347     { "gray49", 125, 125, 125 },
348     { "gray5", 13, 13, 13 },
349     { "gray50", 127, 127, 127 },
350     { "gray51", 130, 130, 130 },
351     { "gray52", 133, 133, 133 },
352     { "gray53", 135, 135, 135 },
353     { "gray54", 138, 138, 138 },
354     { "gray55", 140, 140, 140 },
355     { "gray56", 143, 143, 143 },
356     { "gray57", 145, 145, 145 },
357     { "gray58", 148, 148, 148 },
358     { "gray59", 150, 150, 150 },
359     { "gray6", 15, 15, 15 },
360     { "gray60", 153, 153, 153 },
361     { "gray61", 156, 156, 156 },
362     { "gray62", 158, 158, 158 },
363     { "gray63", 161, 161, 161 },
364     { "gray64", 163, 163, 163 },
365     { "gray65", 166, 166, 166 },
366     { "gray66", 168, 168, 168 },
367     { "gray67", 171, 171, 171 },
368     { "gray68", 173, 173, 173 },
369     { "gray69", 176, 176, 176 },
370     { "gray7", 18, 18, 18 },
371     { "gray70", 179, 179, 179 },
372     { "gray71", 181, 181, 181 },
373     { "gray72", 184, 184, 184 },
374     { "gray73", 186, 186, 186 },
375     { "gray74", 189, 189, 189 },
376     { "gray75", 191, 191, 191 },
377     { "gray76", 194, 194, 194 },
378     { "gray77", 196, 196, 196 },
379     { "gray78", 199, 199, 199 },
380     { "gray79", 201, 201, 201 },
381     { "gray8", 20, 20, 20 },
382     { "gray80", 204, 204, 204 },
383     { "gray81", 207, 207, 207 },
384     { "gray82", 209, 209, 209 },
385     { "gray83", 212, 212, 212 },
386     { "gray84", 214, 214, 214 },
387     { "gray85", 217, 217, 217 },
388     { "gray86", 219, 219, 219 },
389     { "gray87", 222, 222, 222 },
390     { "gray88", 224, 224, 224 },
391     { "gray89", 227, 227, 227 },
392     { "gray9", 23, 23, 23 },
393     { "gray90", 229, 229, 229 },
394     { "gray91", 232, 232, 232 },
395     { "gray92", 235, 235, 235 },
396     { "gray93", 237, 237, 237 },
397     { "gray94", 240, 240, 240 },
398     { "gray95", 242, 242, 242 },
399     { "gray96", 245, 245, 245 },
400     { "gray97", 247, 247, 247 },
401     { "gray98", 250, 250, 250 },
402     { "gray99", 252, 252, 252 },
403     { "green", 0, 255, 0 },
404     { "green yellow", 173, 255, 47 },
405     { "green1", 0, 255, 0 },
406     { "green2", 0, 238, 0 },
407     { "green3", 0, 205, 0 },
408     { "green4", 0, 139, 0 },
409     { "GreenYellow", 173, 255, 47 },
410     { "grey", 190, 190, 190 },
411     { "grey0", 0, 0, 0 },
412     { "grey1", 3, 3, 3 },
413     { "grey10", 26, 26, 26 },
414     { "grey100", 255, 255, 255 },
415     { "grey11", 28, 28, 28 },
416     { "grey12", 31, 31, 31 },
417     { "grey13", 33, 33, 33 },
418     { "grey14", 36, 36, 36 },
419     { "grey15", 38, 38, 38 },
420     { "grey16", 41, 41, 41 },
421     { "grey17", 43, 43, 43 },
422     { "grey18", 46, 46, 46 },
423     { "grey19", 48, 48, 48 },
424     { "grey2", 5, 5, 5 },
425     { "grey20", 51, 51, 51 },
426     { "grey21", 54, 54, 54 },
427     { "grey22", 56, 56, 56 },
428     { "grey23", 59, 59, 59 },
429     { "grey24", 61, 61, 61 },
430     { "grey25", 64, 64, 64 },
431     { "grey26", 66, 66, 66 },
432     { "grey27", 69, 69, 69 },
433     { "grey28", 71, 71, 71 },
434     { "grey29", 74, 74, 74 },
435     { "grey3", 8, 8, 8 },
436     { "grey30", 77, 77, 77 },
437     { "grey31", 79, 79, 79 },
438     { "grey32", 82, 82, 82 },
439     { "grey33", 84, 84, 84 },
440     { "grey34", 87, 87, 87 },
441     { "grey35", 89, 89, 89 },
442     { "grey36", 92, 92, 92 },
443     { "grey37", 94, 94, 94 },
444     { "grey38", 97, 97, 97 },
445     { "grey39", 99, 99, 99 },
446     { "grey4", 10, 10, 10 },
447     { "grey40", 102, 102, 102 },
448     { "grey41", 105, 105, 105 },
449     { "grey42", 107, 107, 107 },
450     { "grey43", 110, 110, 110 },
451     { "grey44", 112, 112, 112 },
452     { "grey45", 115, 115, 115 },
453     { "grey46", 117, 117, 117 },
454     { "grey47", 120, 120, 120 },
455     { "grey48", 122, 122, 122 },
456     { "grey49", 125, 125, 125 },
457     { "grey5", 13, 13, 13 },
458     { "grey50", 127, 127, 127 },
459     { "grey51", 130, 130, 130 },
460     { "grey52", 133, 133, 133 },
461     { "grey53", 135, 135, 135 },
462     { "grey54", 138, 138, 138 },
463     { "grey55", 140, 140, 140 },
464     { "grey56", 143, 143, 143 },
465     { "grey57", 145, 145, 145 },
466     { "grey58", 148, 148, 148 },
467     { "grey59", 150, 150, 150 },
468     { "grey6", 15, 15, 15 },
469     { "grey60", 153, 153, 153 },
470     { "grey61", 156, 156, 156 },
471     { "grey62", 158, 158, 158 },
472     { "grey63", 161, 161, 161 },
473     { "grey64", 163, 163, 163 },
474     { "grey65", 166, 166, 166 },
475     { "grey66", 168, 168, 168 },
476     { "grey67", 171, 171, 171 },
477     { "grey68", 173, 173, 173 },
478     { "grey69", 176, 176, 176 },
479     { "grey7", 18, 18, 18 },
480     { "grey70", 179, 179, 179 },
481     { "grey71", 181, 181, 181 },
482     { "grey72", 184, 184, 184 },
483     { "grey73", 186, 186, 186 },
484     { "grey74", 189, 189, 189 },
485     { "grey75", 191, 191, 191 },
486     { "grey76", 194, 194, 194 },
487     { "grey77", 196, 196, 196 },
488     { "grey78", 199, 199, 199 },
489     { "grey79", 201, 201, 201 },
490     { "grey8", 20, 20, 20 },
491     { "grey80", 204, 204, 204 },
492     { "grey81", 207, 207, 207 },
493     { "grey82", 209, 209, 209 },
494     { "grey83", 212, 212, 212 },
495     { "grey84", 214, 214, 214 },
496     { "grey85", 217, 217, 217 },
497     { "grey86", 219, 219, 219 },
498     { "grey87", 222, 222, 222 },
499     { "grey88", 224, 224, 224 },
500     { "grey89", 227, 227, 227 },
501     { "grey9", 23, 23, 23 },
502     { "grey90", 229, 229, 229 },
503     { "grey91", 232, 232, 232 },
504     { "grey92", 235, 235, 235 },
505     { "grey93", 237, 237, 237 },
506     { "grey94", 240, 240, 240 },
507     { "grey95", 242, 242, 242 },
508     { "grey96", 245, 245, 245 },
509     { "grey97", 247, 247, 247 },
510     { "grey98", 250, 250, 250 },
511     { "grey99", 252, 252, 252 },
512     { "honeydew", 240, 255, 240 },
513     { "honeydew1", 240, 255, 240 },
514     { "honeydew2", 224, 238, 224 },
515     { "honeydew3", 193, 205, 193 },
516     { "honeydew4", 131, 139, 131 },
517     { "hot pink", 255, 105, 180 },
518     { "HotPink", 255, 105, 180 },
519     { "HotPink1", 255, 110, 180 },
520     { "HotPink2", 238, 106, 167 },
521     { "HotPink3", 205, 96, 144 },
522     { "HotPink4", 139, 58, 98 },
523     { "indian red", 205, 92, 92 },
524     { "IndianRed", 205, 92, 92 },
525     { "IndianRed1", 255, 106, 106 },
526     { "IndianRed2", 238, 99, 99 },
527     { "IndianRed3", 205, 85, 85 },
528     { "IndianRed4", 139, 58, 58 },
529     { "ivory", 255, 255, 240 },
530     { "ivory1", 255, 255, 240 },
531     { "ivory2", 238, 238, 224 },
532     { "ivory3", 205, 205, 193 },
533     { "ivory4", 139, 139, 131 },
534     { "khaki", 240, 230, 140 },
535     { "khaki1", 255, 246, 143 },
536     { "khaki2", 238, 230, 133 },
537     { "khaki3", 205, 198, 115 },
538     { "khaki4", 139, 134, 78 },
539     { "lavender", 230, 230, 250 },
540     { "lavender blush", 255, 240, 245 },
541     { "LavenderBlush", 255, 240, 245 },
542     { "LavenderBlush1", 255, 240, 245 },
543     { "LavenderBlush2", 238, 224, 229 },
544     { "LavenderBlush3", 205, 193, 197 },
545     { "LavenderBlush4", 139, 131, 134 },
546     { "lawn green", 124, 252, 0 },
547     { "LawnGreen", 124, 252, 0 },
548     { "lemon chiffon", 255, 250, 205 },
549     { "LemonChiffon", 255, 250, 205 },
550     { "LemonChiffon1", 255, 250, 205 },
551     { "LemonChiffon2", 238, 233, 191 },
552     { "LemonChiffon3", 205, 201, 165 },
553     { "LemonChiffon4", 139, 137, 112 },
554     { "light blue", 173, 216, 230 },
555     { "light coral", 240, 128, 128 },
556     { "light cyan", 224, 255, 255 },
557     { "light goldenrod", 238, 221, 130 },
558     { "light goldenrod yellow", 250, 250, 210 },
559     { "light gray", 211, 211, 211 },
560     { "light green", 144, 238, 144 },
561     { "light grey", 211, 211, 211 },
562     { "light pink", 255, 182, 193 },
563     { "light salmon", 255, 160, 122 },
564     { "light sea green", 32, 178, 170 },
565     { "light sky blue", 135, 206, 250 },
566     { "light slate blue", 132, 112, 255 },
567     { "light slate gray", 119, 136, 153 },
568     { "light slate grey", 119, 136, 153 },
569     { "light steel blue", 176, 196, 222 },
570     { "light yellow", 255, 255, 224 },
571     { "LightBlue", 173, 216, 230 },
572     { "LightBlue1", 191, 239, 255 },
573     { "LightBlue2", 178, 223, 238 },
574     { "LightBlue3", 154, 192, 205 },
575     { "LightBlue4", 104, 131, 139 },
576     { "LightCoral", 240, 128, 128 },
577     { "LightCyan", 224, 255, 255 },
578     { "LightCyan1", 224, 255, 255 },
579     { "LightCyan2", 209, 238, 238 },
580     { "LightCyan3", 180, 205, 205 },
581     { "LightCyan4", 122, 139, 139 },
582     { "LightGoldenrod", 238, 221, 130 },
583     { "LightGoldenrod1", 255, 236, 139 },
584     { "LightGoldenrod2", 238, 220, 130 },
585     { "LightGoldenrod3", 205, 190, 112 },
586     { "LightGoldenrod4", 139, 129, 76 },
587     { "LightGoldenrodYellow", 250, 250, 210 },
588     { "LightGray", 211, 211, 211 },
589     { "LightGreen", 144, 238, 144 },
590     { "LightGrey", 211, 211, 211 },
591     { "LightPink", 255, 182, 193 },
592     { "LightPink1", 255, 174, 185 },
593     { "LightPink2", 238, 162, 173 },
594     { "LightPink3", 205, 140, 149 },
595     { "LightPink4", 139, 95, 101 },
596     { "LightSalmon", 255, 160, 122 },
597     { "LightSalmon1", 255, 160, 122 },
598     { "LightSalmon2", 238, 149, 114 },
599     { "LightSalmon3", 205, 129, 98 },
600     { "LightSalmon4", 139, 87, 66 },
601     { "LightSeaGreen", 32, 178, 170 },
602     { "LightSkyBlue", 135, 206, 250 },
603     { "LightSkyBlue1", 176, 226, 255 },
604     { "LightSkyBlue2", 164, 211, 238 },
605     { "LightSkyBlue3", 141, 182, 205 },
606     { "LightSkyBlue4", 96, 123, 139 },
607     { "LightSlateBlue", 132, 112, 255 },
608     { "LightSlateGray", 119, 136, 153 },
609     { "LightSlateGrey", 119, 136, 153 },
610     { "LightSteelBlue", 176, 196, 222 },
611     { "LightSteelBlue1", 202, 225, 255 },
612     { "LightSteelBlue2", 188, 210, 238 },
613     { "LightSteelBlue3", 162, 181, 205 },
614     { "LightSteelBlue4", 110, 123, 139 },
615     { "LightYellow", 255, 255, 224 },
616     { "LightYellow1", 255, 255, 224 },
617     { "LightYellow2", 238, 238, 209 },
618     { "LightYellow3", 205, 205, 180 },
619     { "LightYellow4", 139, 139, 122 },
620     { "lime green", 50, 205, 50 },
621     { "LimeGreen", 50, 205, 50 },
622     { "linen", 250, 240, 230 },
623     { "magenta", 255, 0, 255 },
624     { "magenta1", 255, 0, 255 },
625     { "magenta2", 238, 0, 238 },
626     { "magenta3", 205, 0, 205 },
627     { "magenta4", 139, 0, 139 },
628     { "maroon", 176, 48, 96 },
629     { "maroon1", 255, 52, 179 },
630     { "maroon2", 238, 48, 167 },
631     { "maroon3", 205, 41, 144 },
632     { "maroon4", 139, 28, 98 },
633     { "medium aquamarine", 102, 205, 170 },
634     { "medium blue", 0, 0, 205 },
635     { "medium orchid", 186, 85, 211 },
636     { "medium purple", 147, 112, 219 },
637     { "medium sea green", 60, 179, 113 },
638     { "medium slate blue", 123, 104, 238 },
639     { "medium spring green", 0, 250, 154 },
640     { "medium turquoise", 72, 209, 204 },
641     { "medium violet red", 199, 21, 133 },
642     { "MediumAquamarine", 102, 205, 170 },
643     { "MediumBlue", 0, 0, 205 },
644     { "MediumOrchid", 186, 85, 211 },
645     { "MediumOrchid1", 224, 102, 255 },
646     { "MediumOrchid2", 209, 95, 238 },
647     { "MediumOrchid3", 180, 82, 205 },
648     { "MediumOrchid4", 122, 55, 139 },
649     { "MediumPurple", 147, 112, 219 },
650     { "MediumPurple1", 171, 130, 255 },
651     { "MediumPurple2", 159, 121, 238 },
652     { "MediumPurple3", 137, 104, 205 },
653     { "MediumPurple4", 93, 71, 139 },
654     { "MediumSeaGreen", 60, 179, 113 },
655     { "MediumSlateBlue", 123, 104, 238 },
656     { "MediumSpringGreen", 0, 250, 154 },
657     { "MediumTurquoise", 72, 209, 204 },
658     { "MediumVioletRed", 199, 21, 133 },
659     { "midnight blue", 25, 25, 112 },
660     { "MidnightBlue", 25, 25, 112 },
661     { "mint cream", 245, 255, 250 },
662     { "MintCream", 245, 255, 250 },
663     { "misty rose", 255, 228, 225 },
664     { "MistyRose", 255, 228, 225 },
665     { "MistyRose1", 255, 228, 225 },
666     { "MistyRose2", 238, 213, 210 },
667     { "MistyRose3", 205, 183, 181 },
668     { "MistyRose4", 139, 125, 123 },
669     { "moccasin", 255, 228, 181 },
670     { "navajo white", 255, 222, 173 },
671     { "NavajoWhite", 255, 222, 173 },
672     { "NavajoWhite1", 255, 222, 173 },
673     { "NavajoWhite2", 238, 207, 161 },
674     { "NavajoWhite3", 205, 179, 139 },
675     { "NavajoWhite4", 139, 121, 94 },
676     { "navy", 0, 0, 128 },
677     { "navy blue", 0, 0, 128 },
678     { "NavyBlue", 0, 0, 128 },
679     { "old lace", 253, 245, 230 },
680     { "OldLace", 253, 245, 230 },
681     { "olive drab", 107, 142, 35 },
682     { "OliveDrab", 107, 142, 35 },
683     { "OliveDrab1", 192, 255, 62 },
684     { "OliveDrab2", 179, 238, 58 },
685     { "OliveDrab3", 154, 205, 50 },
686     { "OliveDrab4", 105, 139, 34 },
687     { "orange", 255, 165, 0 },
688     { "orange red", 255, 69, 0 },
689     { "orange1", 255, 165, 0 },
690     { "orange2", 238, 154, 0 },
691     { "orange3", 205, 133, 0 },
692     { "orange4", 139, 90, 0 },
693     { "OrangeRed", 255, 69, 0 },
694     { "OrangeRed1", 255, 69, 0 },
695     { "OrangeRed2", 238, 64, 0 },
696     { "OrangeRed3", 205, 55, 0 },
697     { "OrangeRed4", 139, 37, 0 },
698     { "orchid", 218, 112, 214 },
699     { "orchid1", 255, 131, 250 },
700     { "orchid2", 238, 122, 233 },
701     { "orchid3", 205, 105, 201 },
702     { "orchid4", 139, 71, 137 },
703     { "pale goldenrod", 238, 232, 170 },
704     { "pale green", 152, 251, 152 },
705     { "pale turquoise", 175, 238, 238 },
706     { "pale violet red", 219, 112, 147 },
707     { "PaleGoldenrod", 238, 232, 170 },
708     { "PaleGreen", 152, 251, 152 },
709     { "PaleGreen1", 154, 255, 154 },
710     { "PaleGreen2", 144, 238, 144 },
711     { "PaleGreen3", 124, 205, 124 },
712     { "PaleGreen4", 84, 139, 84 },
713     { "PaleTurquoise", 175, 238, 238 },
714     { "PaleTurquoise1", 187, 255, 255 },
715     { "PaleTurquoise2", 174, 238, 238 },
716     { "PaleTurquoise3", 150, 205, 205 },
717     { "PaleTurquoise4", 102, 139, 139 },
718     { "PaleVioletRed", 219, 112, 147 },
719     { "PaleVioletRed1", 255, 130, 171 },
720     { "PaleVioletRed2", 238, 121, 159 },
721     { "PaleVioletRed3", 205, 104, 137 },
722     { "PaleVioletRed4", 139, 71, 93 },
723     { "papaya whip", 255, 239, 213 },
724     { "PapayaWhip", 255, 239, 213 },
725     { "peach puff", 255, 218, 185 },
726     { "PeachPuff", 255, 218, 185 },
727     { "PeachPuff1", 255, 218, 185 },
728     { "PeachPuff2", 238, 203, 173 },
729     { "PeachPuff3", 205, 175, 149 },
730     { "PeachPuff4", 139, 119, 101 },
731     { "peru", 205, 133, 63 },
732     { "pink", 255, 192, 203 },
733     { "pink1", 255, 181, 197 },
734     { "pink2", 238, 169, 184 },
735     { "pink3", 205, 145, 158 },
736     { "pink4", 139, 99, 108 },
737     { "plum", 221, 160, 221 },
738     { "plum1", 255, 187, 255 },
739     { "plum2", 238, 174, 238 },
740     { "plum3", 205, 150, 205 },
741     { "plum4", 139, 102, 139 },
742     { "powder blue", 176, 224, 230 },
743     { "PowderBlue", 176, 224, 230 },
744     { "purple", 160, 32, 240 },
745     { "purple1", 155, 48, 255 },
746     { "purple2", 145, 44, 238 },
747     { "purple3", 125, 38, 205 },
748     { "purple4", 85, 26, 139 },
749     { "red", 255, 0, 0 },
750     { "red1", 255, 0, 0 },
751     { "red2", 238, 0, 0 },
752     { "red3", 205, 0, 0 },
753     { "red4", 139, 0, 0 },
754     { "rosy brown", 188, 143, 143 },
755     { "RosyBrown", 188, 143, 143 },
756     { "RosyBrown1", 255, 193, 193 },
757     { "RosyBrown2", 238, 180, 180 },
758     { "RosyBrown3", 205, 155, 155 },
759     { "RosyBrown4", 139, 105, 105 },
760     { "royal blue", 65, 105, 225 },
761     { "RoyalBlue", 65, 105, 225 },
762     { "RoyalBlue1", 72, 118, 255 },
763     { "RoyalBlue2", 67, 110, 238 },
764     { "RoyalBlue3", 58, 95, 205 },
765     { "RoyalBlue4", 39, 64, 139 },
766     { "saddle brown", 139, 69, 19 },
767     { "SaddleBrown", 139, 69, 19 },
768     { "salmon", 250, 128, 114 },
769     { "salmon1", 255, 140, 105 },
770     { "salmon2", 238, 130, 98 },
771     { "salmon3", 205, 112, 84 },
772     { "salmon4", 139, 76, 57 },
773     { "sandy brown", 244, 164, 96 },
774     { "SandyBrown", 244, 164, 96 },
775     { "sea green", 46, 139, 87 },
776     { "SeaGreen", 46, 139, 87 },
777     { "SeaGreen1", 84, 255, 159 },
778     { "SeaGreen2", 78, 238, 148 },
779     { "SeaGreen3", 67, 205, 128 },
780     { "SeaGreen4", 46, 139, 87 },
781     { "seashell", 255, 245, 238 },
782     { "seashell1", 255, 245, 238 },
783     { "seashell2", 238, 229, 222 },
784     { "seashell3", 205, 197, 191 },
785     { "seashell4", 139, 134, 130 },
786     { "sienna", 160, 82, 45 },
787     { "sienna1", 255, 130, 71 },
788     { "sienna2", 238, 121, 66 },
789     { "sienna3", 205, 104, 57 },
790     { "sienna4", 139, 71, 38 },
791     { "sky blue", 135, 206, 235 },
792     { "SkyBlue", 135, 206, 235 },
793     { "SkyBlue1", 135, 206, 255 },
794     { "SkyBlue2", 126, 192, 238 },
795     { "SkyBlue3", 108, 166, 205 },
796     { "SkyBlue4", 74, 112, 139 },
797     { "slate blue", 106, 90, 205 },
798     { "slate gray", 112, 128, 144 },
799     { "slate grey", 112, 128, 144 },
800     { "SlateBlue", 106, 90, 205 },
801     { "SlateBlue1", 131, 111, 255 },
802     { "SlateBlue2", 122, 103, 238 },
803     { "SlateBlue3", 105, 89, 205 },
804     { "SlateBlue4", 71, 60, 139 },
805     { "SlateGray", 112, 128, 144 },
806     { "SlateGray1", 198, 226, 255 },
807     { "SlateGray2", 185, 211, 238 },
808     { "SlateGray3", 159, 182, 205 },
809     { "SlateGray4", 108, 123, 139 },
810     { "SlateGrey", 112, 128, 144 },
811     { "snow", 255, 250, 250 },
812     { "snow1", 255, 250, 250 },
813     { "snow2", 238, 233, 233 },
814     { "snow3", 205, 201, 201 },
815     { "snow4", 139, 137, 137 },
816     { "spring green", 0, 255, 127 },
817     { "SpringGreen", 0, 255, 127 },
818     { "SpringGreen1", 0, 255, 127 },
819     { "SpringGreen2", 0, 238, 118 },
820     { "SpringGreen3", 0, 205, 102 },
821     { "SpringGreen4", 0, 139, 69 },
822     { "steel blue", 70, 130, 180 },
823     { "SteelBlue", 70, 130, 180 },
824     { "SteelBlue1", 99, 184, 255 },
825     { "SteelBlue2", 92, 172, 238 },
826     { "SteelBlue3", 79, 148, 205 },
827     { "SteelBlue4", 54, 100, 139 },
828     { "tan", 210, 180, 140 },
829     { "tan1", 255, 165, 79 },
830     { "tan2", 238, 154, 73 },
831     { "tan3", 205, 133, 63 },
832     { "tan4", 139, 90, 43 },
833     { "thistle", 216, 191, 216 },
834     { "thistle1", 255, 225, 255 },
835     { "thistle2", 238, 210, 238 },
836     { "thistle3", 205, 181, 205 },
837     { "thistle4", 139, 123, 139 },
838     { "tomato", 255, 99, 71 },
839     { "tomato1", 255, 99, 71 },
840     { "tomato2", 238, 92, 66 },
841     { "tomato3", 205, 79, 57 },
842     { "tomato4", 139, 54, 38 },
843     { "turquoise", 64, 224, 208 },
844     { "turquoise1", 0, 245, 255 },
845     { "turquoise2", 0, 229, 238 },
846     { "turquoise3", 0, 197, 205 },
847     { "turquoise4", 0, 134, 139 },
848     { "violet", 238, 130, 238 },
849     { "violet red", 208, 32, 144 },
850     { "VioletRed", 208, 32, 144 },
851     { "VioletRed1", 255, 62, 150 },
852     { "VioletRed2", 238, 58, 140 },
853     { "VioletRed3", 205, 50, 120 },
854     { "VioletRed4", 139, 34, 82 },
855     { "wheat", 245, 222, 179 },
856     { "wheat1", 255, 231, 186 },
857     { "wheat2", 238, 216, 174 },
858     { "wheat3", 205, 186, 150 },
859     { "wheat4", 139, 126, 102 },
860     { "white", 255, 255, 255 },
861     { "white smoke", 245, 245, 245 },
862     { "WhiteSmoke", 245, 245, 245 },
863     { "yellow", 255, 255, 0 },
864     { "yellow green", 154, 205, 50 },
865     { "yellow1", 255, 255, 0 },
866     { "yellow2", 238, 238, 0 },
867     { "yellow3", 205, 205, 0 },
868     { "yellow4", 139, 139, 0 },
869     { "YellowGreen", 154, 205, 50 }
870 };
871  
872 #define numXColors (sizeof (xColors) / sizeof (*xColors))
873  
874 /*
875  *----------------------------------------------------------------------
876  *
877  * find_color --
878  *
879  *      This routine finds the color entry that corresponds to the
880  *      specified color.
881  *
882  * Results:
883  *      Returns non-zero on success.  The RGB values of the XColor
884  *      will be initialized to the proper values on success.
885  *
886  * Side effects:
887  *      None.
888  *
889  *----------------------------------------------------------------------
890  */
891
892 static int
893 compare_xcolor_entries (const void *a, const void *b)
894 {
895   return strcasecmp ((const char *) a, ((const XPMColorEntry *) b)->name);
896 }
897
898 static gboolean
899 find_color(const char *name,
900            XPMColor   *colorPtr)
901 {
902         XPMColorEntry *found;
903
904         found = bsearch (name, xColors, numXColors, sizeof (XPMColorEntry),
905                          compare_xcolor_entries);
906         if (found == NULL)
907           return 0;
908         
909         colorPtr->red = (found->red * 65535) / 255;
910         colorPtr->green = (found->green * 65535) / 255;
911         colorPtr->blue = (found->blue * 65535) / 255;
912         
913         return TRUE;
914 }
915
916 /*
917  *----------------------------------------------------------------------
918  *
919  * parse_color --
920  *
921  *      Partial implementation of X color name parsing interface.
922  *
923  * Results:
924  *      Returns non-zero on success.
925  *
926  * Side effects:
927  *      None.
928  *
929  *----------------------------------------------------------------------
930  */
931
932 static gboolean
933 parse_color (const char *spec,
934              XPMColor   *colorPtr)
935 {
936         if (spec[0] == '#') {
937                 char fmt[16];
938                 int i, red, green, blue;
939
940                 if ((i = strlen(spec+1))%3) {
941                         return FALSE;
942                 }
943                 i /= 3;
944
945                 sprintf(fmt, "%%%dx%%%dx%%%dx", i, i, i);
946                 if (sscanf(spec+1, fmt, &red, &green, &blue) != 3) {
947                         return FALSE;
948                 }
949                 if (i == 4) {
950                         colorPtr->red = red;
951                         colorPtr->green = green;
952                         colorPtr->blue = blue;
953                 } else if (i == 1) {
954                         colorPtr->red = (red * 65535) / 15;
955                         colorPtr->green = (green * 65535) / 15;
956                         colorPtr->blue = (blue * 65535) / 15;
957                 } else if (i == 2)
958                 {
959                         colorPtr->red = (red * 65535) / 255;
960                         colorPtr->green = (green * 65535) / 255;
961                         colorPtr->blue = (blue * 65535) / 255;
962                 } else /* if (i == 3) */ {
963                         colorPtr->red = (red * 65535) / 4095;
964                         colorPtr->green = (green * 65535) / 4095;
965                         colorPtr->blue = (blue * 65535) / 4095;
966                 }
967         } else {
968                 if (!find_color(spec, colorPtr))
969                         return FALSE;
970         }
971         return TRUE;
972 }
973
974 static gint
975 xpm_seek_string (FILE *infile, const gchar *str, gint skip_comments)
976 {
977         char instr[1024];
978
979         while (!feof (infile)) {
980                 fscanf (infile, "%1023s", instr);
981                 if (skip_comments == TRUE && strcmp (instr, "/*") == 0) {
982                         fscanf (infile, "%1023s", instr);
983                         while (!feof (infile) && strcmp (instr, "*/") != 0)
984                                 fscanf (infile, "%1023s", instr);
985                         fscanf (infile, "%1023s", instr);
986                 }
987
988                 if (strcmp (instr, str) == 0)
989                         return TRUE;
990         }
991
992         return FALSE;
993 }
994
995 static gint
996 xpm_seek_char (FILE *infile, gchar c)
997 {
998         gint b, oldb;
999
1000         while ((b = getc (infile)) != EOF) {
1001                 if (c != b && b == '/') {
1002                         b = getc (infile);
1003                         if (b == EOF)
1004                                 return FALSE;
1005
1006                         else if (b == '*') {    /* we have a comment */
1007                                 b = -1;
1008                                 do {
1009                                         oldb = b;
1010                                         b = getc (infile);
1011                                         if (b == EOF)
1012                                                 return FALSE;
1013                                 } while (!(oldb == '*' && b == '/'));
1014                         }
1015                 } else if (c == b)
1016                         return TRUE;
1017         }
1018
1019         return FALSE;
1020 }
1021
1022 static gint
1023 xpm_read_string (FILE *infile, gchar **buffer, guint *buffer_size)
1024 {
1025         gint c;
1026         guint cnt = 0, bufsiz, ret = FALSE;
1027         gchar *buf;
1028
1029         buf = *buffer;
1030         bufsiz = *buffer_size;
1031         if (buf == NULL) {
1032                 bufsiz = 10 * sizeof (gchar);
1033                 buf = g_new (gchar, bufsiz);
1034         }
1035
1036         do {
1037                 c = getc (infile);
1038         } while (c != EOF && c != '"');
1039
1040         if (c != '"')
1041                 goto out;
1042
1043         while ((c = getc (infile)) != EOF) {
1044                 if (cnt == bufsiz) {
1045                         guint new_size = bufsiz * 2;
1046
1047                         if (new_size > bufsiz)
1048                                 bufsiz = new_size;
1049                         else
1050                                 goto out;
1051
1052                         buf = g_realloc (buf, bufsiz);
1053                         buf[bufsiz - 1] = '\0';
1054                 }
1055
1056                 if (c != '"')
1057                         buf[cnt++] = c;
1058                 else {
1059                         buf[cnt] = 0;
1060                         ret = TRUE;
1061                         break;
1062                 }
1063         }
1064
1065  out:
1066         buf[bufsiz - 1] = '\0'; /* ensure null termination for errors */
1067         *buffer = buf;
1068         *buffer_size = bufsiz;
1069         return ret;
1070 }
1071
1072 static const gchar *
1073 xpm_skip_whitespaces (const gchar *buffer)
1074 {
1075         gint32 index = 0;
1076
1077         while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
1078                 index++;
1079
1080         return &buffer[index];
1081 }
1082
1083 static const gchar *
1084 xpm_skip_string (const gchar *buffer)
1085 {
1086         gint32 index = 0;
1087
1088         while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
1089                 index++;
1090
1091         return &buffer[index];
1092 }
1093
1094 /* Xlib crashed once at a color name lengths around 125 */
1095 #define MAX_COLOR_LEN 120
1096
1097 static gchar *
1098 xpm_extract_color (const gchar *buffer)
1099 {
1100         gint counter, numnames;
1101         const gchar *ptr = NULL;
1102         gchar ch, temp[128];
1103         gchar color[MAX_COLOR_LEN], *retcol;
1104         gint space;
1105
1106         counter = 0;
1107         while (ptr == NULL) {
1108                 if ((buffer[counter] == 'c') || (buffer[counter] == 'g')) {
1109                         ch = buffer[counter + 1];
1110                         if (ch == 0x20 || ch == 0x09)
1111                                 ptr = &buffer[counter + 1];
1112                 } else if (buffer[counter] == 0)
1113                         return NULL;
1114
1115                 counter++;
1116         }
1117         ptr = xpm_skip_whitespaces (ptr);
1118
1119         if (ptr[0] == 0)
1120                 return NULL;
1121         else if (ptr[0] == '#') {
1122                 counter = 1;
1123                 while (ptr[counter] != 0 &&
1124                        ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
1125                         (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
1126                         (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
1127                         counter++;
1128                 retcol = g_new (gchar, counter + 1);
1129                 strncpy (retcol, ptr, counter);
1130
1131                 retcol[counter] = 0;
1132
1133                 return retcol;
1134         }
1135         color[0] = 0;
1136         numnames = 0;
1137
1138         space = MAX_COLOR_LEN - 1;
1139         while (space > 0) {
1140                 sscanf (ptr, "%127s", temp);
1141
1142                 if (((gint) ptr[0] == 0) ||
1143                     (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
1144                     (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
1145                         break;
1146                 else {
1147                         if (numnames > 0) {
1148                                 space -= 1;
1149                                 strcat (color, " ");
1150                         }
1151
1152                         strncat (color, temp, space);
1153                         space -= MIN (space, strlen (temp));
1154                         ptr = xpm_skip_string (ptr);
1155                         ptr = xpm_skip_whitespaces (ptr);
1156                         numnames++;
1157                 }
1158         }
1159
1160         retcol = g_strdup (color);
1161         return retcol;
1162 }
1163
1164 /* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
1165
1166 static const gchar *
1167 file_buffer (enum buf_op op, gpointer handle)
1168 {
1169         struct file_handle *h = handle;
1170
1171         switch (op) {
1172         case op_header:
1173                 if (xpm_seek_string (h->infile, "XPM", FALSE) != TRUE)
1174                         break;
1175
1176                 if (xpm_seek_char (h->infile, '{') != TRUE)
1177                         break;
1178                 /* Fall through to the next xpm_seek_char. */
1179
1180         case op_cmap:
1181                 xpm_seek_char (h->infile, '"');
1182                 fseek (h->infile, -1, SEEK_CUR);
1183                 /* Fall through to the xpm_read_string. */
1184
1185         case op_body:
1186                 xpm_read_string (h->infile, &h->buffer, &h->buffer_size);
1187                 return h->buffer;
1188
1189         default:
1190                 g_assert_not_reached ();
1191         }
1192
1193         return NULL;
1194 }
1195
1196 /* This reads from memory */
1197 static const gchar *
1198 mem_buffer (enum buf_op op, gpointer handle)
1199 {
1200         struct mem_handle *h = handle;
1201         switch (op) {
1202         case op_header:
1203         case op_cmap:
1204         case op_body:
1205                 if (h->data[h->offset]) {
1206                         const gchar* retval;
1207
1208                         retval = h->data[h->offset];
1209                         h->offset += 1;
1210                         return retval;
1211                 }
1212                 break;
1213
1214         default:
1215                 g_assert_not_reached ();
1216                 break;
1217         }
1218
1219         return NULL;
1220 }
1221
1222 /* Destroy notification function for the pixbuf */
1223 static void
1224 free_buffer (guchar *pixels, gpointer data)
1225 {
1226         free (pixels);
1227 }
1228
1229 /* This function does all the work. */
1230 static GdkPixbuf *
1231 pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle)
1232 {
1233         gint w, h, n_col, cpp;
1234         gint cnt, xcnt, ycnt, wbytes, n, ns;
1235         gint is_trans = FALSE;
1236         const gchar *buffer;
1237         gchar *name_buf;
1238         gchar pixel_str[32];
1239         GHashTable *color_hash;
1240         XPMColor *colors, *color, *fallbackcolor;
1241         guchar *pixels, *pixtmp;
1242
1243         fallbackcolor = NULL;
1244
1245         buffer = (*get_buf) (op_header, handle);
1246         if (!buffer) {
1247                 g_warning ("No XPM header found");
1248                 return NULL;
1249         }
1250         sscanf (buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);
1251         if (cpp >= 32) {
1252                 g_warning ("XPM has more than 31 chars per pixel.");
1253                 return NULL;
1254         }
1255
1256         /* The hash is used for fast lookups of color from chars */
1257         color_hash = g_hash_table_new (g_str_hash, g_str_equal);
1258
1259         name_buf = g_new (gchar, n_col * (cpp + 1));
1260         colors = g_new (XPMColor, n_col);
1261
1262         for (cnt = 0; cnt < n_col; cnt++) {
1263                 gchar *color_name;
1264
1265                 buffer = (*get_buf) (op_cmap, handle);
1266                 if (!buffer) {
1267                         g_warning ("Can't load XPM colormap");
1268                         g_hash_table_destroy (color_hash);
1269                         g_free (name_buf);
1270                         g_free (colors);
1271                         return NULL;
1272                 }
1273
1274                 color = &colors[cnt];
1275                 color->color_string = &name_buf[cnt * (cpp + 1)];
1276                 strncpy (color->color_string, buffer, cpp);
1277                 color->color_string[cpp] = 0;
1278                 buffer += strlen (color->color_string);
1279                 color->transparent = FALSE;
1280
1281                 color_name = xpm_extract_color (buffer);
1282
1283                 if ((color_name == NULL) || (g_strcasecmp (color_name, "None") == 0)
1284                     || (parse_color (color_name, color) == FALSE)) {
1285                         color->transparent = TRUE;
1286                         is_trans = TRUE;
1287                 }
1288
1289                 g_free (color_name);
1290                 g_hash_table_insert (color_hash, color->color_string, color);
1291
1292                 if (cnt == 0)
1293                         fallbackcolor = color;
1294         }
1295
1296         if (is_trans)
1297                 pixels = malloc (w * h * 4);
1298         else
1299                 pixels = malloc (w * h * 3);
1300
1301         if (!pixels) {
1302                 g_hash_table_destroy (color_hash);
1303                 g_free (colors);
1304                 g_free (name_buf);
1305                 return NULL;
1306         }
1307
1308         wbytes = w * cpp;
1309         pixtmp = pixels;
1310
1311         for (ycnt = 0; ycnt < h; ycnt++) {
1312                 buffer = (*get_buf) (op_body, handle);
1313                 if ((!buffer) || (strlen (buffer) < wbytes))
1314                         continue;
1315
1316                 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
1317                         strncpy (pixel_str, &buffer[n], cpp);
1318                         pixel_str[cpp] = 0;
1319                         ns = 0;
1320
1321                         color = g_hash_table_lookup (color_hash, pixel_str);
1322
1323                         /* Bad XPM...punt */
1324                         if (!color)
1325                                 color = fallbackcolor;
1326
1327                         *pixtmp++ = color->red >> 8;
1328                         *pixtmp++ = color->green >> 8;
1329                         *pixtmp++ = color->blue >> 8;
1330
1331                         if (is_trans && color->transparent)
1332                                 *pixtmp++ = 0;
1333                         else if (is_trans)
1334                                 *pixtmp++ = 0xFF;
1335                 }
1336         }
1337
1338         g_hash_table_destroy (color_hash);
1339         g_free (colors);
1340         g_free (name_buf);
1341
1342         return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, is_trans, 8,
1343                                          w, h, is_trans ? (w * 4) : (w * 3),
1344                                          free_buffer, NULL);
1345 }
1346
1347 /* Shared library entry point for file loading */
1348 GdkPixbuf *
1349 gdk_pixbuf__xpm_image_load (FILE *f)
1350 {
1351         GdkPixbuf *pixbuf;
1352         struct file_handle h;
1353
1354         memset (&h, 0, sizeof (h));
1355         h.infile = f;
1356         pixbuf = pixbuf_create_from_xpm (file_buffer, &h);
1357         g_free (h.buffer);
1358
1359         return pixbuf;
1360 }
1361
1362 /* Shared library entry point for memory loading */
1363 GdkPixbuf *
1364 gdk_pixbuf__xpm_image_load_xpm_data (const gchar **data)
1365 {
1366         GdkPixbuf *pixbuf;
1367         struct mem_handle h;
1368
1369         h.data = data;
1370         h.offset = 0;
1371         
1372         pixbuf = pixbuf_create_from_xpm (mem_buffer, &h);
1373         
1374         return pixbuf;
1375 }