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