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,
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);
78 if (strcmp(instr, str) == 0)
86 xpm_seek_char(FILE * infile,
91 while ((b = getc(infile)) != EOF) {
92 if (c != b && b == '/') {
96 else if (b == '*') { /* we have a comment */
104 while (!(oldb == '*' && b == '/'));
113 xpm_read_string(FILE * infile,
118 guint cnt = 0, bufsiz, ret = FALSE;
122 bufsiz = *buffer_size;
124 bufsiz = 10 * sizeof(gchar);
125 buf = g_new(gchar, bufsiz);
129 while (c != EOF && c != '"');
134 while ((c = getc(infile)) != EOF) {
136 guint new_size = bufsiz * 2;
137 if (new_size > bufsiz)
142 buf = (gchar *) g_realloc(buf, bufsiz);
143 buf[bufsiz - 1] = '\0';
155 buf[bufsiz - 1] = '\0'; /* ensure null termination for errors */
157 *buffer_size = bufsiz;
162 xpm_skip_whitespaces(gchar * buffer)
166 while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
169 return &buffer[index];
173 xpm_skip_string(gchar * buffer)
177 while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
180 return &buffer[index];
183 /* Xlib crashed once at a color name lengths around 125 */
184 #define MAX_COLOR_LEN 120
187 xpm_extract_color(gchar * buffer)
189 gint counter, numnames;
190 gchar *ptr = NULL, ch, temp[128];
191 gchar color[MAX_COLOR_LEN], *retcol;
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)
204 ptr = xpm_skip_whitespaces(ptr);
208 else if (ptr[0] == '#') {
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')))
215 retcol = g_new(gchar, counter + 1);
216 strncpy(retcol, ptr, counter);
225 space = MAX_COLOR_LEN - 1;
227 sscanf(ptr, "%127s", temp);
229 if (((gint) ptr[0] == 0) ||
230 (strcmp("s", temp) == 0) || (strcmp("m", temp) == 0) ||
231 (strcmp("g", temp) == 0) || (strcmp("g4", temp) == 0)) {
238 strncat(color, temp, space);
239 space -= MIN(space, strlen(temp));
240 ptr = xpm_skip_string(ptr);
241 ptr = xpm_skip_whitespaces(ptr);
246 retcol = g_strdup(color);
251 /* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
254 file_buffer(enum buf_op op, gpointer handle)
256 struct file_handle *h = handle;
260 if (xpm_seek_string(h->infile, "XPM", FALSE) != TRUE)
263 if (xpm_seek_char(h->infile, '{') != TRUE)
265 /* Fall through to the next xpm_seek_char. */
268 xpm_seek_char(h->infile, '"');
269 fseek(h->infile, -1, SEEK_CUR);
270 /* Fall through to the xpm_read_string. */
273 xpm_read_string(h->infile, &h->buffer, &h->buffer_size);
279 /* This reads from memory */
281 mem_buffer(enum buf_op op, gpointer handle)
283 struct mem_handle *h = handle;
288 if (h->data[h->offset])
289 return h->data[h->offset++];
294 /* This function does all the work. */
297 _pixbuf_create_from_xpm(gchar * (*get_buf) (enum buf_op op, gpointer handle),
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;
305 GHashTable *color_hash;
306 _XPMColor *colors, *color, *fallbackcolor;
307 art_u8 *pixels, *pixtmp;
310 buffer = (*get_buf) (op_header, handle);
312 g_warning("No XPM header found");
315 sscanf(buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);
317 g_warning("XPM has more than 31 chars per pixel.");
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);
323 name_buf = g_new(gchar, n_col * (cpp + 1));
324 colors = g_new(_XPMColor, n_col);
326 for (cnt = 0; cnt < n_col; cnt++) {
329 buffer = (*get_buf) (op_cmap, handle);
331 g_warning("Can't load XPM colormap");
332 g_hash_table_destroy(color_hash);
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;
344 color_name = xpm_extract_color(buffer);
346 if ((color_name == NULL) || (g_strcasecmp(color_name, "None") == 0)
347 || (gdk_color_parse(color_name, &color->color) == FALSE)) {
348 color->transparent = TRUE;
353 g_hash_table_insert(color_hash, color->color_string, color);
356 fallbackcolor = color;
360 pixels = art_alloc(w * h * 4);
362 pixels = art_alloc(w * h * 3);
365 g_warning("XPM: Cannot alloc ArtBuf");
366 g_hash_table_destroy(color_hash);
374 for (ycnt = 0; ycnt < h; ycnt++) {
375 buffer = (*get_buf) (op_body, handle);
376 if ((!buffer) || (strlen(buffer) < wbytes))
378 for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
379 strncpy(pixel_str, &buffer[n], cpp);
383 color = g_hash_table_lookup(color_hash, pixel_str);
387 color = fallbackcolor;
389 *pixtmp++ = (color->color.red)>>8;
390 *pixtmp++ = (color->color.green)>>8;
391 *pixtmp++ = (color->color.blue)>>8;
393 if ((is_trans) && (color->transparent)) {
395 } else if (is_trans) {
400 g_hash_table_destroy(color_hash);
404 /* Ok, now stuff the GdkPixBuf with goodies */
406 pixbuf = g_new(GdkPixBuf, 1);
409 pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, w, h, (w * 4));
411 pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, w, h, (w * 3));
413 /* Ok, I'm anal...shoot me */
414 if (!(pixbuf->art_pixbuf)) {
420 pixbuf->ref_count = 0;
421 pixbuf->unref_func = NULL;
426 /* Shared library entry point for file loading */
427 GdkPixBuf *image_load(FILE * f)
430 struct file_handle h;
432 g_return_val_if_fail(f != NULL, NULL);
434 memset(&h, 0, sizeof(h));
436 pixbuf = _pixbuf_create_from_xpm(file_buffer, &h);