]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-xpm.c
Fix a few memory leaks in the, most of them in error cases but one or two
[~andy/gtk] / gdk-pixbuf / io-xpm.c
1 /*
2  * io-xpm.c: GdkPixBuf I/O for XPM files.
3  * Copyright (C) 1999 Mark Crichton
4  * Author: Mark Crichton <crichton@gimp.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <glib.h>
26
27 /* We need gdk.h since we might need to parse X color names */
28 #include <gdk/gdk.h>
29
30 #include "gdk-pixbuf.h"
31 /*#include "gdk-pixbuf-io.h" */
32
33 /* I have must have done something to deserve this.
34  * XPM is such a crappy format to handle.
35  * This code is an ugly hybred from gdkpixmap.c
36  * modified to respect transparent colors.
37  * It's still a mess, though.
38  */
39
40 enum buf_op {
41     op_header,
42     op_cmap,
43     op_body
44 };
45
46 typedef struct {
47     gchar *color_string;
48     GdkColor color;
49     gint transparent;
50 } _XPMColor;
51
52 struct file_handle {
53     FILE *infile;
54     gchar *buffer;
55     guint buffer_size;
56 } file_handle;
57
58 struct mem_handle {
59     gchar **data;
60     int offset;
61 } mem_handle;
62
63 static gint
64  xpm_seek_string(FILE * infile,
65                  const gchar * str,
66                  gint skip_comments)
67 {
68     char instr[1024];
69
70     while (!feof(infile)) {
71         fscanf(infile, "%1023s", instr);
72         if (skip_comments == TRUE && strcmp(instr, "/*") == 0) {
73             fscanf(infile, "%1023s", instr);
74             while (!feof(infile) && strcmp(instr, "*/") != 0)
75                 fscanf(infile, "%1023s", instr);
76             fscanf(infile, "%1023s", instr);
77         }
78         if (strcmp(instr, str) == 0)
79             return TRUE;
80     }
81
82     return FALSE;
83 }
84
85 static gint
86  xpm_seek_char(FILE * infile,
87                gchar c)
88 {
89     gint b, oldb;
90
91     while ((b = getc(infile)) != EOF) {
92         if (c != b && b == '/') {
93             b = getc(infile);
94             if (b == EOF)
95                 return FALSE;
96             else if (b == '*') {        /* we have a comment */
97                 b = -1;
98                 do {
99                     oldb = b;
100                     b = getc(infile);
101                     if (b == EOF)
102                         return FALSE;
103                 }
104                 while (!(oldb == '*' && b == '/'));
105             }
106         } else if (c == b)
107             return TRUE;
108     }
109     return FALSE;
110 }
111
112 static gint
113  xpm_read_string(FILE * infile,
114                  gchar ** buffer,
115                  guint * buffer_size)
116 {
117     gint c;
118     guint cnt = 0, bufsiz, ret = FALSE;
119     gchar *buf;
120
121     buf = *buffer;
122     bufsiz = *buffer_size;
123     if (buf == NULL) {
124         bufsiz = 10 * sizeof(gchar);
125         buf = g_new(gchar, bufsiz);
126     }
127     do
128         c = getc(infile);
129     while (c != EOF && c != '"');
130
131     if (c != '"')
132         goto out;
133
134     while ((c = getc(infile)) != EOF) {
135         if (cnt == bufsiz) {
136             guint new_size = bufsiz * 2;
137             if (new_size > bufsiz)
138                 bufsiz = new_size;
139             else
140                 goto out;
141
142             buf = (gchar *) g_realloc(buf, bufsiz);
143             buf[bufsiz - 1] = '\0';
144         }
145         if (c != '"')
146             buf[cnt++] = c;
147         else {
148             buf[cnt] = 0;
149             ret = TRUE;
150             break;
151         }
152     }
153
154   out:
155     buf[bufsiz - 1] = '\0';     /* ensure null termination for errors */
156     *buffer = buf;
157     *buffer_size = bufsiz;
158     return ret;
159 }
160
161 static gchar *
162  xpm_skip_whitespaces(gchar * buffer)
163 {
164     gint32 index = 0;
165
166     while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
167         index++;
168
169     return &buffer[index];
170 }
171
172 static gchar *
173  xpm_skip_string(gchar * buffer)
174 {
175     gint32 index = 0;
176
177     while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
178         index++;
179
180     return &buffer[index];
181 }
182
183 /* Xlib crashed once at a color name lengths around 125 */
184 #define MAX_COLOR_LEN 120
185
186 static gchar *
187  xpm_extract_color(gchar * buffer)
188 {
189     gint counter, numnames;
190     gchar *ptr = NULL, ch, temp[128];
191     gchar color[MAX_COLOR_LEN], *retcol;
192     gint space;
193     counter = 0;
194     while (ptr == NULL) {
195         if (buffer[counter] == 'c') {
196             ch = buffer[counter + 1];
197             if (ch == 0x20 || ch == 0x09)
198                 ptr = &buffer[counter + 1];
199         } else if (buffer[counter] == 0)
200             return NULL;
201
202         counter++;
203     }
204     ptr = xpm_skip_whitespaces(ptr);
205
206     if (ptr[0] == 0)
207         return NULL;
208     else if (ptr[0] == '#') {
209         counter = 1;
210         while (ptr[counter] != 0 &&
211                ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
212                 (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
213                 (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
214             counter++;
215         retcol = g_new(gchar, counter + 1);
216         strncpy(retcol, ptr, counter);
217
218         retcol[counter] = 0;
219
220         return retcol;
221     }
222     color[0] = 0;
223     numnames = 0;
224
225     space = MAX_COLOR_LEN - 1;
226     while (space > 0) {
227         sscanf(ptr, "%127s", temp);
228
229         if (((gint) ptr[0] == 0) ||
230             (strcmp("s", temp) == 0) || (strcmp("m", temp) == 0) ||
231             (strcmp("g", temp) == 0) || (strcmp("g4", temp) == 0)) {
232             break;
233         } else {
234             if (numnames > 0) {
235                 space -= 1;
236                 strcat(color, " ");
237             }
238             strncat(color, temp, space);
239             space -= MIN(space, strlen(temp));
240             ptr = xpm_skip_string(ptr);
241             ptr = xpm_skip_whitespaces(ptr);
242             numnames++;
243         }
244     }
245
246     retcol = g_strdup(color);
247     return retcol;
248 }
249
250
251 /* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
252
253 static gchar *
254  file_buffer(enum buf_op op, gpointer handle)
255 {
256     struct file_handle *h = handle;
257
258     switch (op) {
259     case op_header:
260         if (xpm_seek_string(h->infile, "XPM", FALSE) != TRUE)
261             break;
262
263         if (xpm_seek_char(h->infile, '{') != TRUE)
264             break;
265         /* Fall through to the next xpm_seek_char. */
266
267     case op_cmap:
268         xpm_seek_char(h->infile, '"');
269         fseek(h->infile, -1, SEEK_CUR);
270         /* Fall through to the xpm_read_string. */
271
272     case op_body:
273         xpm_read_string(h->infile, &h->buffer, &h->buffer_size);
274         return h->buffer;
275     }
276     return 0;
277 }
278
279 /* This reads from memory */
280 static gchar *
281  mem_buffer(enum buf_op op, gpointer handle)
282 {
283     struct mem_handle *h = handle;
284     switch (op) {
285     case op_header:
286     case op_cmap:
287     case op_body:
288         if (h->data[h->offset])
289             return h->data[h->offset++];
290     }
291     return NULL;
292 }
293
294 /* This function does all the work. */
295
296 static GdkPixBuf *
297  _pixbuf_create_from_xpm(gchar * (*get_buf) (enum buf_op op, gpointer handle),
298                          gpointer handle)
299 {
300     gint w, h, n_col, cpp;
301     gint cnt, xcnt, ycnt, wbytes, n, ns;
302     gint is_trans = FALSE;
303     gchar *buffer, *name_buf;
304     gchar pixel_str[32];
305     GHashTable *color_hash;
306     _XPMColor *colors, *color, *fallbackcolor;
307     art_u8 *pixels, *pixtmp;
308     GdkPixBuf *pixbuf;
309
310     buffer = (*get_buf) (op_header, handle);
311     if (!buffer) {
312         g_warning("No XPM header found");
313         return NULL;
314     }
315     sscanf(buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);
316     if (cpp >= 32) {
317         g_warning("XPM has more than 31 chars per pixel.");
318         return NULL;
319     }
320     /* The hash is used for fast lookups of color from chars */
321     color_hash = g_hash_table_new(g_str_hash, g_str_equal);
322
323     name_buf = g_new(gchar, n_col * (cpp + 1));
324     colors = g_new(_XPMColor, n_col);
325
326     for (cnt = 0; cnt < n_col; cnt++) {
327         gchar *color_name;
328
329         buffer = (*get_buf) (op_cmap, handle);
330         if (!buffer) {
331             g_warning("Can't load XPM colormap");
332             g_hash_table_destroy(color_hash);
333             g_free(name_buf);
334             g_free(colors);
335             return NULL;
336         }
337         color = &colors[cnt];
338         color->color_string = &name_buf[cnt * (cpp + 1)];
339         strncpy(color->color_string, buffer, cpp);
340         color->color_string[cpp] = 0;
341         buffer += strlen(color->color_string);
342         color->transparent = FALSE;
343
344         color_name = xpm_extract_color(buffer);
345        
346         if ((color_name == NULL) || (g_strcasecmp(color_name, "None") == 0)
347             || (gdk_color_parse(color_name, &color->color) == FALSE)) {
348             color->transparent = TRUE;
349             is_trans = TRUE;
350         }
351
352         g_free(color_name);
353         g_hash_table_insert(color_hash, color->color_string, color);
354
355         if (cnt == 0)
356             fallbackcolor = color;
357     }
358
359     if (is_trans)
360         pixels = art_alloc(w * h * 4);
361     else
362         pixels = art_alloc(w * h * 3);
363
364     if (!pixels) {
365         g_warning("XPM: Cannot alloc ArtBuf");
366         g_hash_table_destroy(color_hash);
367         g_free(colors);
368         g_free(name_buf);
369         return NULL;
370     }
371     wbytes = w * cpp;
372     pixtmp = pixels;
373
374     for (ycnt = 0; ycnt < h; ycnt++) {
375         buffer = (*get_buf) (op_body, handle);
376         if ((!buffer) || (strlen(buffer) < wbytes))
377             continue;
378         for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
379             strncpy(pixel_str, &buffer[n], cpp);
380             pixel_str[cpp] = 0;
381             ns = 0;
382
383             color = g_hash_table_lookup(color_hash, pixel_str);
384
385             /* Bad XPM...punt */
386             if (!color)
387                 color = fallbackcolor;
388
389             *pixtmp++ = (color->color.red)>>8;
390             *pixtmp++ = (color->color.green)>>8;
391             *pixtmp++ = (color->color.blue)>>8;
392
393             if ((is_trans) && (color->transparent)) {
394                 *pixtmp++ = 0;
395             } else if (is_trans) {
396                 *pixtmp++ = 0xFF;
397             }
398         }
399     }
400     g_hash_table_destroy(color_hash);
401     g_free(colors);
402     g_free(name_buf);
403
404     /* Ok, now stuff the GdkPixBuf with goodies */
405
406     pixbuf = g_new(GdkPixBuf, 1);
407
408     if (is_trans)
409         pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4));
410     else
411         pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3));
412
413     /* Ok, I'm anal...shoot me */
414     if (!(pixbuf->art_pixbuf)) {
415         art_free(pixels);
416         g_free(pixbuf);
417         return NULL;
418     }
419
420     pixbuf->ref_count = 0;
421     pixbuf->unref_func = NULL;
422
423     return pixbuf;
424 }
425
426 /* Shared library entry point for file loading */
427 GdkPixBuf *image_load(FILE * f)
428 {
429     GdkPixBuf *pixbuf;
430     struct file_handle h;
431
432     g_return_val_if_fail(f != NULL, NULL);
433
434     memset(&h, 0, sizeof(h));
435     h.infile = f;
436     pixbuf = _pixbuf_create_from_xpm(file_buffer, &h);
437     g_free(h.buffer);
438
439     return pixbuf;
440 }
441
442 image_save()
443 {
444 }