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