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