]> Pileus Git - ~andy/gtk/blobdiff - gdk-pixbuf/io-xpm.c
Support for separately installed loaders. (#77486)
[~andy/gtk] / gdk-pixbuf / io-xpm.c
index b762c87054451435187398fa0b3aee8e9a5bea12..ca0b81ebccd384a4732449d43db1e5c31e9c598e 100644 (file)
@@ -1,3 +1,4 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /* GdkPixbuf library - XPM image loader
  *
  * Copyright (C) 1999 Mark Crichton
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h> /* for unlink */
+#endif
+#include <errno.h>
 #include "gdk-pixbuf-private.h"
 #include "gdk-pixbuf-io.h"
 
@@ -894,7 +898,8 @@ static XPMColorEntry xColors[] = {
 static int
 compare_xcolor_entries (const void *a, const void *b)
 {
-  return strcasecmp ((const char *) a, ((const XPMColorEntry *) b)->name);
+  return g_ascii_strcasecmp ((const char *) a, 
+                            ((const XPMColorEntry *) b)->name);
 }
 
 static gboolean
@@ -923,7 +928,7 @@ find_color(const char *name,
  *     Partial implementation of X color name parsing interface.
  *
  * Results:
- *     Returns non-zero on success.
+ *     Returns TRUE on success.
  *
  * Side effects:
  *     None.
@@ -939,13 +944,14 @@ parse_color (const char *spec,
                char fmt[16];
                int i, red, green, blue;
 
-               if ((i = strlen(spec+1))%3) {
+               if ((i = strlen (spec + 1)) % 3) {
                        return FALSE;
                }
                i /= 3;
 
-               sprintf(fmt, "%%%dx%%%dx%%%dx", i, i, i);
-               if (sscanf(spec+1, fmt, &red, &green, &blue) != 3) {
+               g_snprintf (fmt, 16, "%%%dx%%%dx%%%dx", i, i, i);
+
+               if (sscanf (spec + 1, fmt, &red, &green, &blue) != 3) {
                        return FALSE;
                }
                if (i == 4) {
@@ -974,20 +980,13 @@ parse_color (const char *spec,
 }
 
 static gint
-xpm_seek_string (FILE *infile, const gchar *str, gint skip_comments)
+xpm_seek_string (FILE *infile, const gchar *str)
 {
        char instr[1024];
 
        while (!feof (infile)) {
                if (fscanf (infile, "%1023s", instr) < 0)
                         return FALSE;
-               if (skip_comments == TRUE && strcmp (instr, "/*") == 0) {
-                       fscanf (infile, "%1023s", instr);
-                       while (!feof (infile) && strcmp (instr, "*/") != 0)
-                               fscanf (infile, "%1023s", instr);
-                       fscanf (infile, "%1023s", instr);
-               }
-
                if (strcmp (instr, str) == 0)
                        return TRUE;
        }
@@ -1072,96 +1071,78 @@ xpm_read_string (FILE *infile, gchar **buffer, guint *buffer_size)
        return ret;
 }
 
-static const gchar *
-xpm_skip_whitespaces (const gchar *buffer)
-{
-       gint32 index = 0;
-
-       while (buffer[index] != 0 && (buffer[index] == 0x20 || buffer[index] == 0x09))
-               index++;
-
-       return &buffer[index];
-}
-
-static const gchar *
-xpm_skip_string (const gchar *buffer)
-{
-       gint32 index = 0;
-
-       while (buffer[index] != 0 && buffer[index] != 0x20 && buffer[index] != 0x09)
-               index++;
-
-       return &buffer[index];
-}
-
-/* Xlib crashed once at a color name lengths around 125 */
-#define MAX_COLOR_LEN 120
-
 static gchar *
 xpm_extract_color (const gchar *buffer)
 {
-       gint counter, numnames;
-       const gchar *ptr = NULL;
-        gchar ch, temp[128];
-       gchar color[MAX_COLOR_LEN], *retcol;
-       gint space;
-
-       counter = 0;
-       while (ptr == NULL) {
-               if ((buffer[counter] == 'c') || (buffer[counter] == 'g')) {
-                       ch = buffer[counter + 1];
-                       if (ch == 0x20 || ch == 0x09)
-                               ptr = &buffer[counter + 1];
-               } else if (buffer[counter] == 0)
-                       return NULL;
-
-               counter++;
-       }
-       ptr = xpm_skip_whitespaces (ptr);
-
-       if (ptr[0] == 0)
-               return NULL;
-       else if (ptr[0] == '#') {
-               counter = 1;
-               while (ptr[counter] != 0 &&
-                      ((ptr[counter] >= '0' && ptr[counter] <= '9') ||
-                       (ptr[counter] >= 'a' && ptr[counter] <= 'f') ||
-                       (ptr[counter] >= 'A' && ptr[counter] <= 'F')))
-                       counter++;
-               retcol = g_new (gchar, counter + 1);
-               strncpy (retcol, ptr, counter);
-
-               retcol[counter] = 0;
-
-               return retcol;
-       }
-       color[0] = 0;
-       numnames = 0;
-
-       space = MAX_COLOR_LEN - 1;
-       while (space > 0) {
-               sscanf (ptr, "%127s", temp);
-
-               if (((gint) ptr[0] == 0) ||
-                   (strcmp ("s", temp) == 0) || (strcmp ("m", temp) == 0) ||
-                   (strcmp ("g", temp) == 0) || (strcmp ("g4", temp) == 0))
-                       break;
+       const gchar *p = &buffer[0];
+       gint new_key = 0;
+       gint key = 0;
+       gint current_key = 1;
+       gint space = 128;
+       gchar word[128], color[128], current_color[128];
+       gchar *r; 
+       
+       word[0] = '\0';
+       color[0] = '\0';
+       current_color[0] = '\0';
+        while (1) {
+               /* skip whitespace */
+               for (; *p != '\0' && g_ascii_isspace (*p); p++) {
+               } 
+               /* copy word */
+               for (r = word; *p != '\0' && !g_ascii_isspace (*p) && r - word < sizeof (word) - 1; p++, r++) {
+                       *r = *p;
+               }
+               *r = '\0';
+               if (*word == '\0') {
+                       if (color[0] == '\0')  /* incomplete colormap entry */
+                               return NULL;                            
+                       else  /* end of entry, still store the last color */
+                               new_key = 1;
+               } 
+               else if (key > 0 && color[0] == '\0')  /* next word must be a color name part */
+                       new_key = 0;
                else {
-                       if (numnames > 0) {
-                               space -= 1;
+                       if (strcmp (word, "c") == 0)
+                               new_key = 5;
+                       else if (strcmp (word, "g") == 0)
+                               new_key = 4;
+                       else if (strcmp (word, "g4") == 0)
+                               new_key = 3;
+                       else if (strcmp (word, "m") == 0)
+                               new_key = 2;
+                       else if (strcmp (word, "s") == 0)
+                               new_key = 1;
+                       else 
+                               new_key = 0;
+               }
+               if (new_key == 0) {  /* word is a color name part */
+                       if (key == 0)  /* key expected */
+                               return NULL;
+                       /* accumulate color name */
+                       if (color[0] != '\0') {
                                strcat (color, " ");
+                               space--;
                        }
-
-                       strncat (color, temp, space);
-                       space -= MIN (space, strlen (temp));
-                       ptr = xpm_skip_string (ptr);
-                       ptr = xpm_skip_whitespaces (ptr);
-                       numnames++;
+                       strncat (color, word, space);
+                       space -= MIN (space, strlen (word));
+               }
+               else {  /* word is a key */
+                       if (key > current_key) {
+                               current_key = key;
+                               strcpy (current_color, color);
+                       }
+                       space = 128;
+                       color[0] = '\0';
+                       key = new_key;
+                       if (*p == '\0') break;
                }
+               
        }
-
-       retcol = g_strdup (color);
-       return retcol;
+       if (current_key > 1)
+               return g_strdup (current_color);
+       else
+               return NULL; 
 }
 
 /* (almost) direct copy from gdkpixmap.c... loads an XPM from a file */
@@ -1173,7 +1154,7 @@ file_buffer (enum buf_op op, gpointer handle)
 
        switch (op) {
        case op_header:
-               if (xpm_seek_string (h->infile, "XPM", FALSE) != TRUE)
+               if (xpm_seek_string (h->infile, "XPM") != TRUE)
                        break;
 
                if (xpm_seek_char (h->infile, '{') != TRUE)
@@ -1222,37 +1203,62 @@ mem_buffer (enum buf_op op, gpointer handle)
        return NULL;
 }
 
-/* Destroy notification function for the pixbuf */
-static void
-free_buffer (guchar *pixels, gpointer data)
-{
-       free (pixels);
-}
-
 /* This function does all the work. */
 static GdkPixbuf *
-pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle)
+pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handle), gpointer handle,
+                        GError **error)
 {
-       gint w, h, n_col, cpp;
-       gint cnt, xcnt, ycnt, wbytes, n, ns;
+       gint w, h, n_col, cpp, x_hot, y_hot, items;
+       gint cnt, xcnt, ycnt, wbytes, n;
        gint is_trans = FALSE;
        const gchar *buffer;
         gchar *name_buf;
        gchar pixel_str[32];
        GHashTable *color_hash;
        XPMColor *colors, *color, *fallbackcolor;
-       guchar *pixels, *pixtmp;
+       guchar *pixtmp;
+       GdkPixbuf *pixbuf;
 
        fallbackcolor = NULL;
 
        buffer = (*get_buf) (op_header, handle);
        if (!buffer) {
-               g_warning ("No XPM header found");
+                g_set_error (error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("No XPM header found"));
                return NULL;
        }
-       sscanf (buffer, "%d %d %d %d", &w, &h, &n_col, &cpp);
-       if (cpp >= 32) {
-               g_warning ("XPM has more than 31 chars per pixel.");
+       items = sscanf (buffer, "%d %d %d %d %d %d", &w, &h, &n_col, &cpp, &x_hot, &y_hot);
+       if (w <= 0) {
+                g_set_error (error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("XPM file has image width <= 0"));
+               return NULL;
+
+       }
+       if (h <= 0) {
+                g_set_error (error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("XPM file has image height <= 0"));
+               return NULL;
+
+       }
+       if (n_col <= 0) {
+                g_set_error (error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("XPM file has invalid number of colors"));
+               return NULL;
+
+       }
+       if (cpp <= 0 || cpp >= 32) {
+                g_set_error (error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("XPM has invalid number of chars per pixel"));
                return NULL;
        }
 
@@ -1267,7 +1273,10 @@ pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handl
 
                buffer = (*get_buf) (op_cmap, handle);
                if (!buffer) {
-                       g_warning ("Can't load XPM colormap");
+                        g_set_error (error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                     _("Can't read XPM colormap"));
                        g_hash_table_destroy (color_hash);
                        g_free (name_buf);
                        g_free (colors);
@@ -1283,9 +1292,12 @@ pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handl
 
                color_name = xpm_extract_color (buffer);
 
-               if ((color_name == NULL) || (g_strcasecmp (color_name, "None") == 0)
+               if ((color_name == NULL) || (g_ascii_strcasecmp (color_name, "None") == 0)
                    || (parse_color (color_name, color) == FALSE)) {
                        color->transparent = TRUE;
+                       color->red = 0;
+                       color->green = 0;
+                       color->blue = 0;
                        is_trans = TRUE;
                }
 
@@ -1296,12 +1308,13 @@ pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handl
                        fallbackcolor = color;
        }
 
-       if (is_trans)
-               pixels = malloc (w * h * 4);
-       else
-               pixels = malloc (w * h * 3);
+       pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, is_trans, 8, w, h);
 
-       if (!pixels) {
+       if (!pixbuf) {
+                g_set_error (error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
+                             _("Can't allocate memory for loading XPM image"));
                g_hash_table_destroy (color_hash);
                g_free (colors);
                g_free (name_buf);
@@ -1309,9 +1322,10 @@ pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handl
        }
 
        wbytes = w * cpp;
-       pixtmp = pixels;
 
        for (ycnt = 0; ycnt < h; ycnt++) {
+               pixtmp = pixbuf->pixels + ycnt * pixbuf->rowstride;
+
                buffer = (*get_buf) (op_body, handle);
                if ((!buffer) || (strlen (buffer) < wbytes))
                        continue;
@@ -1319,7 +1333,6 @@ pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handl
                for (n = 0, cnt = 0, xcnt = 0; n < wbytes; n += cpp, xcnt++) {
                        strncpy (pixel_str, &buffer[n], cpp);
                        pixel_str[cpp] = 0;
-                       ns = 0;
 
                        color = g_hash_table_lookup (color_hash, pixel_str);
 
@@ -1342,37 +1355,52 @@ pixbuf_create_from_xpm (const gchar * (*get_buf) (enum buf_op op, gpointer handl
        g_free (colors);
        g_free (name_buf);
 
-       return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, is_trans, 8,
-                                        w, h, is_trans ? (w * 4) : (w * 3),
-                                        free_buffer, NULL);
+       if (items == 6) {
+               gchar hot[10];
+               g_snprintf (hot, 10, "%d", x_hot);
+               gdk_pixbuf_set_option (pixbuf, "x_hot", hot);
+               g_snprintf (hot, 10, "%d", y_hot);
+               gdk_pixbuf_set_option (pixbuf, "y_hot", hot);
+
+       }
+
+       return pixbuf;
 }
 
 /* Shared library entry point for file loading */
-GdkPixbuf *
-gdk_pixbuf__xpm_image_load (FILE *f)
+static GdkPixbuf *
+gdk_pixbuf__xpm_image_load (FILE *f,
+                            GError **error)
 {
        GdkPixbuf *pixbuf;
        struct file_handle h;
 
        memset (&h, 0, sizeof (h));
        h.infile = f;
-       pixbuf = pixbuf_create_from_xpm (file_buffer, &h);
+       pixbuf = pixbuf_create_from_xpm (file_buffer, &h, error);
        g_free (h.buffer);
 
        return pixbuf;
 }
 
 /* Shared library entry point for memory loading */
-GdkPixbuf *
+static GdkPixbuf *
 gdk_pixbuf__xpm_image_load_xpm_data (const gchar **data)
 {
         GdkPixbuf *pixbuf;
         struct mem_handle h;
-
+        GError *error = NULL;
+        
         h.data = data;
         h.offset = 0;
         
-       pixbuf = pixbuf_create_from_xpm (mem_buffer, &h);
+       pixbuf = pixbuf_create_from_xpm (mem_buffer, &h, &error);
+
+        if (error) {
+                g_warning ("Inline XPM data is broken: %s", error->message);
+                g_error_free (error);
+                error = NULL;
+        }
         
        return pixbuf;
 }
@@ -1381,8 +1409,8 @@ gdk_pixbuf__xpm_image_load_xpm_data (const gchar **data)
 typedef struct _XPMContext XPMContext;
 struct _XPMContext
 {
-       ModulePreparedNotifyFunc prepare_func;
-       ModuleUpdatedNotifyFunc update_func;
+       GdkPixbufModulePreparedFunc prepare_func;
+       GdkPixbufModuleUpdatedFunc update_func;
        gpointer user_data;
 
        gchar *tempname;
@@ -1396,27 +1424,24 @@ struct _XPMContext
  * This is very broken but it should be relayively simple to fix
  * in the future.
  */
-gpointer
-gdk_pixbuf__xpm_image_begin_load (ModulePreparedNotifyFunc prepare_func,
-                                  ModuleUpdatedNotifyFunc update_func,
-                                  ModuleFrameDoneNotifyFunc frame_done_func,
-                                  ModuleAnimationDoneNotifyFunc anim_done_func,
+static gpointer
+gdk_pixbuf__xpm_image_begin_load (GdkPixbufModuleSizeFunc size_func,
+                                  GdkPixbufModulePreparedFunc prepare_func,
+                                  GdkPixbufModuleUpdatedFunc update_func,
                                   gpointer user_data,
                                   GError **error)
 {
        XPMContext *context;
        gint fd;
 
-       g_warning ("load start");
        context = g_new (XPMContext, 1);
        context->prepare_func = prepare_func;
        context->update_func = update_func;
        context->user_data = user_data;
        context->all_okay = TRUE;
-       context->tempname = g_strdup ("/tmp/gdkpixbuf-xpm-tmp.XXXXXX");
-       fd = mkstemp (context->tempname);
+       fd = g_file_open_tmp ("gdkpixbuf-xpm-tmp.XXXXXX", &context->tempname,
+                            NULL);
        if (fd < 0) {
-               g_free (context->tempname);
                g_free (context);
                return NULL;
        }
@@ -1431,43 +1456,92 @@ gdk_pixbuf__xpm_image_begin_load (ModulePreparedNotifyFunc prepare_func,
        return context;
 }
 
-void
-gdk_pixbuf__xpm_image_stop_load (gpointer data)
+static gboolean
+gdk_pixbuf__xpm_image_stop_load (gpointer data,
+                                 GError **error)
 {
        XPMContext *context = (XPMContext*) data;
        GdkPixbuf *pixbuf;
-
-       g_return_if_fail (data != NULL);
-       g_warning ("stopped loading");
+       gboolean retval = FALSE;
+       
+       g_return_val_if_fail (data != NULL, FALSE);
 
        fflush (context->file);
        rewind (context->file);
        if (context->all_okay) {
-               pixbuf = gdk_pixbuf__xpm_image_load (context->file);
+               pixbuf = gdk_pixbuf__xpm_image_load (context->file, error);
 
-               (* context->prepare_func) (pixbuf, context->user_data);
-               (* context->update_func) (pixbuf, 0, 0, pixbuf->width, pixbuf->height, context->user_data);
-               gdk_pixbuf_unref (pixbuf);
+               if (pixbuf != NULL) {
+                       (* context->prepare_func) (pixbuf,
+                                                  NULL,
+                                                  context->user_data);
+                       (* context->update_func) (pixbuf, 0, 0, pixbuf->width, pixbuf->height, context->user_data);
+                       g_object_unref (pixbuf);
+
+                       retval = TRUE;
+               }
        }
 
        fclose (context->file);
        unlink (context->tempname);
        g_free (context->tempname);
        g_free ((XPMContext *) context);
+
+       return retval;
 }
 
-gboolean
-gdk_pixbuf__xpm_image_load_increment (gpointer data, guchar *buf, guint size)
+static gboolean
+gdk_pixbuf__xpm_image_load_increment (gpointer data,
+                                      const guchar *buf,
+                                      guint    size,
+                                      GError **error)
 {
        XPMContext *context = (XPMContext *) data;
 
        g_return_val_if_fail (data != NULL, FALSE);
-       g_warning ("load increment");
 
        if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
                context->all_okay = FALSE;
+               g_set_error (error,
+                            G_FILE_ERROR,
+                            g_file_error_from_errno (errno),
+                            _("Failed to write to temporary file when loading XPM image"));
                return FALSE;
        }
 
        return TRUE;
 }
+
+void
+MODULE_ENTRY (xpm, fill_vtable) (GdkPixbufModule *module)
+{
+       module->load = gdk_pixbuf__xpm_image_load;
+       module->load_xpm_data = gdk_pixbuf__xpm_image_load_xpm_data;
+       module->begin_load = gdk_pixbuf__xpm_image_begin_load;
+       module->stop_load = gdk_pixbuf__xpm_image_stop_load;
+       module->load_increment = gdk_pixbuf__xpm_image_load_increment;
+}
+
+void
+MODULE_ENTRY (xpm, fill_info) (GdkPixbufFormat *info)
+{
+       static GdkPixbufModulePattern signature[] = {
+               { "/* XPM */", NULL, 100 },
+               { NULL, NULL, 0 }
+       };
+       static gchar * mime_types[] = {
+               "image/x-xpixmap",
+               NULL
+       };
+       static gchar * extensions[] = {
+               "xpm",
+               NULL
+       };
+
+       info->name = "xpm";
+       info->signature = signature;
+       info->description = N_("The XPM image format");
+       info->mime_types = mime_types;
+       info->extensions = extensions;
+       info->flags = 0;
+}