2 * io-xpm.c: GdkPixbuf I/O for XPM files.
3 * Copyright (C) 1999 Mark Crichton
4 * Author: Mark Crichton <crichton@gimp.org>
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.
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.
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.
27 /* We need gdk.h since we might need to parse X color names */
30 #include "gdk-pixbuf.h"
31 /*#include "gdk-pixbuf-io.h" */
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.
64 xpm_seek_string(FILE *infile, const gchar *str, gint skip_comments)
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);
76 if (strcmp(instr, str) == 0)
84 xpm_seek_char(FILE *infile, gchar c)
88 while ((b = getc(infile)) != EOF) {
89 if (c != b && b == '/') {
93 else if (b == '*') { /* we have a comment */
101 while (!(oldb == '*' && b == '/'));
110 xpm_read_string(FILE *infile, gchar **buffer, guint *buffer_size)
113 guint cnt = 0, bufsiz, ret = FALSE;
117 bufsiz = *buffer_size;
119 bufsiz = 10 * sizeof(gchar);
120 buf = g_new(gchar, bufsiz);
125 } while (c != EOF && c != '"');
130 while ((c = getc(infile)) != EOF) {
132 guint new_size = bufsiz * 2;
133 if (new_size > bufsiz)
138 buf = (gchar *) g_realloc(buf, bufsiz);
139 buf[bufsiz - 1] = '\0';
151 buf[bufsiz - 1] = '\0'; /* ensure null termination for errors */
153 *buffer_size = bufsiz;
158 xpm_skip_whitespaces(gchar *buffer)
162 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
165 return &buffer[index];
169 xpm_skip_string(gchar *buffer)
173 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
176 return &buffer[index];
179 /* Xlib crashed once at a color name lengths around 125 */
180 #define MAX_COLOR_LEN 120
183 xpm_extract_color(gchar *buffer)
185 gint counter, numnames;
186 gchar *ptr = NULL, ch, temp[128];
187 gchar color[MAX_COLOR_LEN], *retcol;
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)
200 ptr = xpm_skip_whitespaces(ptr);
204 else if (ptr[0] == '#') {
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')))
211 retcol = g_new(gchar, counter + 1);
212 strncpy(retcol, ptr, counter);
221 space = MAX_COLOR_LEN - 1;
223 sscanf(ptr, "%127s", temp);
225 if (((gint) ptr[0] == 0) ||
226 (strcmp("s", temp) == 0) || (strcmp("m", temp) == 0) ||
227 (strcmp("g", temp) == 0) || (strcmp("g4", temp) == 0)) {
234 strncat(color, temp, space);
235 space -= MIN(space, strlen(temp));
236 ptr = xpm_skip_string(ptr);
237 ptr = xpm_skip_whitespaces(ptr);
242 retcol = g_strdup(color);
246 /* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
249 file_buffer(enum buf_op op, gpointer handle)
251 struct file_handle *h = handle;
255 if (xpm_seek_string(h->infile, "XPM", FALSE) != TRUE)
258 if (xpm_seek_char(h->infile, '{') != TRUE)
260 /* Fall through to the next xpm_seek_char. */
263 xpm_seek_char(h->infile, '"');
264 fseek(h->infile, -1, SEEK_CUR);
265 /* Fall through to the xpm_read_string. */
268 xpm_read_string(h->infile, &h->buffer, &h->buffer_size);
274 /* This reads from memory */
276 mem_buffer(enum buf_op op, gpointer handle)
278 struct mem_handle *h = handle;
283 if (h->data[h->offset])
284 return h->data[h->offset++];
289 /* This function does all the work. */
292 _pixbuf_create_from_xpm(gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle)
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;
299 GHashTable *color_hash;
300 _XPMColor *colors, *color, *fallbackcolor;
301 art_u8 *pixels, *pixtmp;
303 ArtPixBuf *art_pixbuf;
305 buffer = (*get_buf) (op_header, handle);
307 g_warning("No XPM header found");
310 sscanf(buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);
312 g_warning("XPM has more than 31 chars per pixel.");
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);
318 name_buf = g_new(gchar, n_col * (cpp + 1));
319 colors = g_new(_XPMColor, n_col);
321 for (cnt = 0; cnt < n_col; cnt++) {
324 buffer = (*get_buf) (op_cmap, handle);
326 g_warning("Can't load XPM colormap");
327 g_hash_table_destroy(color_hash);
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;
339 color_name = xpm_extract_color(buffer);
341 if ((color_name == NULL) || (g_strcasecmp(color_name, "None") == 0)
342 || (gdk_color_parse(color_name, &color->color) == FALSE)) {
343 color->transparent = TRUE;
348 g_hash_table_insert(color_hash, color->color_string, color);
351 fallbackcolor = color;
355 pixels = art_alloc(w * h * 4);
357 pixels = art_alloc(w * h * 3);
360 g_warning("XPM: Cannot alloc ArtBuf");
361 g_hash_table_destroy(color_hash);
369 for (ycnt = 0; ycnt < h; ycnt++) {
370 buffer = (*get_buf) (op_body, handle);
371 if ((!buffer) || (strlen(buffer) < wbytes))
373 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
374 strncpy(pixel_str, &buffer[n], cpp);
378 color = g_hash_table_lookup(color_hash, pixel_str);
382 color = fallbackcolor;
384 *pixtmp++ = (color->color.red)>>8;
385 *pixtmp++ = (color->color.green)>>8;
386 *pixtmp++ = (color->color.blue)>>8;
388 if ((is_trans) && (color->transparent)) {
390 } else if (is_trans) {
395 g_hash_table_destroy(color_hash);
399 /* Ok, now stuff the GdkPixbuf with goodies */
402 art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4));
404 art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3));
406 pixbuf = gdk_pixbuf_new (art_pixbuf, NULL);
414 /* Shared library entry point for file loading */
419 struct file_handle h;
421 g_return_val_if_fail(f != NULL, NULL);
423 memset(&h, 0, sizeof(h));
425 pixbuf = _pixbuf_create_from_xpm(file_buffer, &h);