]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-xpm.c
Reindented to use the GNOME Indentation.
[~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, const gchar *str, gint skip_comments)
65 {
66         char instr[1024];
67
68         while (!feof(infile)) {
69                 fscanf(infile, "%1023s", instr);
70                 if (skip_comments == TRUE && strcmp(instr, "/*") == 0) {
71                         fscanf(infile, "%1023s", instr);
72                         while (!feof(infile) && strcmp(instr, "*/") != 0)
73                                 fscanf(infile, "%1023s", instr);
74                         fscanf(infile, "%1023s", instr);
75                 }
76                 if (strcmp(instr, str) == 0)
77                         return TRUE;
78         }
79
80         return FALSE;
81 }
82
83 static gint
84 xpm_seek_char(FILE *infile, gchar c)
85 {
86         gint b, oldb;
87
88         while ((b = getc(infile)) != EOF) {
89                 if (c != b && b == '/') {
90                         b = getc(infile);
91                         if (b == EOF)
92                                 return FALSE;
93                         else if (b == '*') {    /* we have a comment */
94                                 b = -1;
95                                 do {
96                                         oldb = b;
97                                         b = getc(infile);
98                                         if (b == EOF)
99                                                 return FALSE;
100                                 }
101                                 while (!(oldb == '*' && b == '/'));
102                         }
103                 } else if (c == b)
104                         return TRUE;
105         }
106         return FALSE;
107 }
108
109 static gint
110 xpm_read_string(FILE *infile, gchar **buffer, guint *buffer_size)
111 {
112         gint c;
113         guint cnt = 0, bufsiz, ret = FALSE;
114         gchar *buf;
115
116         buf = *buffer;
117         bufsiz = *buffer_size;
118         if (buf == NULL) {
119                 bufsiz = 10 * sizeof(gchar);
120                 buf = g_new(gchar, bufsiz);
121         }
122
123         do {
124                 c = getc(infile);
125         } while (c != EOF && c != '"');
126
127         if (c != '"')
128                 goto out;
129
130         while ((c = getc(infile)) != EOF) {
131                 if (cnt == bufsiz) {
132                         guint new_size = bufsiz * 2;
133                         if (new_size > bufsiz)
134                                 bufsiz = new_size;
135                         else
136                                 goto out;
137
138                         buf = (gchar *) g_realloc(buf, bufsiz);
139                         buf[bufsiz - 1] = '\0';
140                 }
141                 if (c != '"')
142                         buf[cnt++] = c;
143                 else {
144                         buf[cnt] = 0;
145                         ret = TRUE;
146                         break;
147                 }
148         }
149
150  out:
151         buf[bufsiz - 1] = '\0'; /* ensure null termination for errors */
152         *buffer = buf;
153         *buffer_size = bufsiz;
154         return ret;
155 }
156
157 static gchar *
158 xpm_skip_whitespaces(gchar *buffer)
159 {
160         gint32 index = 0;
161
162         while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
163                 index++;
164
165         return &buffer[index];
166 }
167
168 static gchar *
169 xpm_skip_string(gchar *buffer)
170 {
171         gint32 index = 0;
172
173         while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
174                 index++;
175
176         return &buffer[index];
177 }
178
179 /* Xlib crashed once at a color name lengths around 125 */
180 #define MAX_COLOR_LEN 120
181
182 static gchar *
183 xpm_extract_color(gchar *buffer)
184 {
185         gint counter, numnames;
186         gchar *ptr = NULL, ch, temp[128];
187         gchar color[MAX_COLOR_LEN], *retcol;
188         gint space;
189         counter = 0;
190         while (ptr == NULL) {
191                 if (buffer[counter] == 'c') {
192                         ch = buffer[counter + 1];
193                         if (ch == 0x20 || ch == 0x09)
194                                 ptr = &buffer[counter + 1];
195                 } else if (buffer[counter] == 0)
196                         return NULL;
197
198                 counter++;
199         }
200         ptr = xpm_skip_whitespaces(ptr);
201
202         if (ptr[0] == 0)
203                 return NULL;
204         else if (ptr[0] == '#') {
205                 counter = 1;
206                 while (ptr[counter] != 0 &&
207                        ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
208                         (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
209                         (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
210                         counter++;
211                 retcol = g_new(gchar, counter + 1);
212                 strncpy(retcol, ptr, counter);
213
214                 retcol[counter] = 0;
215
216                 return retcol;
217         }
218         color[0] = 0;
219         numnames = 0;
220
221         space = MAX_COLOR_LEN - 1;
222         while (space > 0) {
223                 sscanf(ptr, "%127s", temp);
224
225                 if (((gint) ptr[0] == 0) ||
226                     (strcmp("s", temp) == 0) || (strcmp("m", temp) == 0) ||
227                     (strcmp("g", temp) == 0) || (strcmp("g4", temp) == 0)) {
228                         break;
229                 } else {
230                         if (numnames > 0) {
231                                 space -= 1;
232                                 strcat(color, " ");
233                         }
234                         strncat(color, temp, space);
235                         space -= MIN(space, strlen(temp));
236                         ptr = xpm_skip_string(ptr);
237                         ptr = xpm_skip_whitespaces(ptr);
238                         numnames++;
239                 }
240         }
241
242         retcol = g_strdup(color);
243         return retcol;
244 }
245
246 /* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
247
248 static gchar *
249 file_buffer(enum buf_op op, gpointer handle)
250 {
251         struct file_handle *h = handle;
252
253         switch (op) {
254         case op_header:
255                 if (xpm_seek_string(h->infile, "XPM", FALSE) != TRUE)
256                         break;
257
258                 if (xpm_seek_char(h->infile, '{') != TRUE)
259                         break;
260                 /* Fall through to the next xpm_seek_char. */
261
262         case op_cmap:
263                 xpm_seek_char(h->infile, '"');
264                 fseek(h->infile, -1, SEEK_CUR);
265                 /* Fall through to the xpm_read_string. */
266
267         case op_body:
268                 xpm_read_string(h->infile, &h->buffer, &h->buffer_size);
269                 return h->buffer;
270         }
271         return 0;
272 }
273
274 /* This reads from memory */
275 static gchar *
276 mem_buffer(enum buf_op op, gpointer handle)
277 {
278         struct mem_handle *h = handle;
279         switch (op) {
280         case op_header:
281         case op_cmap:
282         case op_body:
283                 if (h->data[h->offset])
284                         return h->data[h->offset++];
285         }
286         return NULL;
287 }
288
289 /* This function does all the work. */
290
291 static GdkPixbuf *
292 _pixbuf_create_from_xpm(gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle)
293 {
294         gint w, h, n_col, cpp;
295         gint cnt, xcnt, ycnt, wbytes, n, ns;
296         gint is_trans = FALSE;
297         gchar *buffer, *name_buf;
298         gchar pixel_str[32];
299         GHashTable *color_hash;
300         _XPMColor *colors, *color, *fallbackcolor;
301         art_u8 *pixels, *pixtmp;
302         GdkPixbuf *pixbuf;
303         ArtPixBuf *art_pixbuf;
304
305         buffer = (*get_buf) (op_header, handle);
306         if (!buffer) {
307                 g_warning("No XPM header found");
308                 return NULL;
309         }
310         sscanf(buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);
311         if (cpp >= 32) {
312                 g_warning("XPM has more than 31 chars per pixel.");
313                 return NULL;
314         }
315         /* The hash is used for fast lookups of color from chars */
316         color_hash = g_hash_table_new(g_str_hash, g_str_equal);
317
318         name_buf = g_new(gchar, n_col * (cpp + 1));
319         colors = g_new(_XPMColor, n_col);
320
321         for (cnt = 0; cnt < n_col; cnt++) {
322                 gchar *color_name;
323
324                 buffer = (*get_buf) (op_cmap, handle);
325                 if (!buffer) {
326                         g_warning("Can't load XPM colormap");
327                         g_hash_table_destroy(color_hash);
328                         g_free(name_buf);
329                         g_free(colors);
330                         return NULL;
331                 }
332                 color = &colors[cnt];
333                 color->color_string = &name_buf[cnt * (cpp + 1)];
334                 strncpy(color->color_string, buffer, cpp);
335                 color->color_string[cpp] = 0;
336                 buffer += strlen(color->color_string);
337                 color->transparent = FALSE;
338
339                 color_name = xpm_extract_color(buffer);
340
341                 if ((color_name == NULL) || (g_strcasecmp(color_name, "None") == 0)
342                     || (gdk_color_parse(color_name, &color->color) == FALSE)) {
343                         color->transparent = TRUE;
344                         is_trans = TRUE;
345                 }
346
347                 g_free(color_name);
348                 g_hash_table_insert(color_hash, color->color_string, color);
349
350                 if (cnt == 0)
351                         fallbackcolor = color;
352         }
353
354         if (is_trans)
355                 pixels = art_alloc(w * h * 4);
356         else
357                 pixels = art_alloc(w * h * 3);
358
359         if (!pixels) {
360                 g_warning("XPM: Cannot alloc ArtBuf");
361                 g_hash_table_destroy(color_hash);
362                 g_free(colors);
363                 g_free(name_buf);
364                 return NULL;
365         }
366         wbytes = w * cpp;
367         pixtmp = pixels;
368
369         for (ycnt = 0; ycnt < h; ycnt++) {
370                 buffer = (*get_buf) (op_body, handle);
371                 if ((!buffer) || (strlen(buffer) < wbytes))
372                         continue;
373                 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
374                         strncpy(pixel_str, &buffer[n], cpp);
375                         pixel_str[cpp] = 0;
376                         ns = 0;
377
378                         color = g_hash_table_lookup(color_hash, pixel_str);
379
380                         /* Bad XPM...punt */
381                         if (!color)
382                                 color = fallbackcolor;
383
384                         *pixtmp++ = (color->color.red)>>8;
385                         *pixtmp++ = (color->color.green)>>8;
386                         *pixtmp++ = (color->color.blue)>>8;
387
388                         if ((is_trans) && (color->transparent)) {
389                                 *pixtmp++ = 0;
390                         } else if (is_trans) {
391                                 *pixtmp++ = 0xFF;
392                         }
393                 }
394         }
395         g_hash_table_destroy(color_hash);
396         g_free(colors);
397         g_free(name_buf);
398
399         /* Ok, now stuff the GdkPixbuf with goodies */
400
401         if (is_trans)
402                 art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4));
403         else
404                 art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3));
405
406         pixbuf = gdk_pixbuf_new (art_pixbuf, NULL);
407
408         if (!pixbuf)
409                 art_free(pixels);
410
411         return pixbuf;
412 }
413
414 /* Shared library entry point for file loading */
415 GdkPixbuf *
416 image_load (FILE *f)
417 {
418         GdkPixbuf *pixbuf;
419         struct file_handle h;
420
421         g_return_val_if_fail(f != NULL, NULL);
422
423         memset(&h, 0, sizeof(h));
424         h.infile = f;
425         pixbuf = _pixbuf_create_from_xpm(file_buffer, &h);
426         g_free(h.buffer);
427
428         return pixbuf;
429 }
430
431 image_save()
432 {
433 }