]> Pileus Git - ~andy/gtk/blobdiff - gdk-pixbuf/io-gif.c
Adopt to new prototypes for unicode,locale,filename conversion functions.
[~andy/gtk] / gdk-pixbuf / io-gif.c
index c36809ba0ad6978cd2633487c05b08b5710b6807..e61f41fb879577a23a140fbff9d78618cd70e294 100644 (file)
@@ -9,16 +9,16 @@
  *          Permission to relicense under the LGPL obtained.
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
+ * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
@@ -42,6 +42,7 @@
  * -2 -> failure; abort the load
  * -3 -> control needs to be passed back to the main loop
  *        \_ (most of the time returning 0 will get this, but not always)
+ *
  * >1 -> for functions that get a guchar, the char will be returned.
  *
  * -jrb (11/03/1999)
@@ -58,7 +59,7 @@
 #include <config.h>
 #include <stdio.h>
 #include <string.h>
-#include "gdk-pixbuf.h"
+#include "gdk-pixbuf-private.h"
 #include "gdk-pixbuf-io.h"
 
 \f
@@ -122,6 +123,8 @@ struct _GifContext
        int frame_len;
        int frame_height;
        int frame_interlace;
+       int x_offset;
+       int y_offset;
 
        /* Static read only */
        FILE *file;
@@ -129,6 +132,8 @@ struct _GifContext
        /* progressive read, only. */
        ModulePreparedNotifyFunc prepare_func;
        ModuleUpdatedNotifyFunc update_func;
+       ModuleFrameDoneNotifyFunc frame_done_func;
+       ModuleAnimationDoneNotifyFunc anim_done_func;
        gpointer user_data;
        guchar *buf;
        guint ptr;
@@ -175,6 +180,9 @@ struct _GifContext
        gint draw_xpos;
        gint draw_ypos;
        gint draw_pass;
+
+        /* error pointer */
+        GError **error;
 };
 
 static int GetDataBlock (GifContext *, unsigned char *);
@@ -199,7 +207,7 @@ gif_read (GifContext *context, guchar *buffer, size_t len)
                count += len;
                g_print ("Fsize :%d\tcount :%d\t", len, count);
 #endif
-               retval = (fread(buffer, len, 1, context->file) != 0);
+               retval = (fread(buffer, len, 1, context->file) != 0);                
 #ifdef IO_GIFDEBUG
                if (len < 100) {
                        for (i = 0; i < len; i++)
@@ -207,10 +215,11 @@ gif_read (GifContext *context, guchar *buffer, size_t len)
                }
                g_print ("\n");
 #endif
+                
                return retval;
        } else {
 #ifdef IO_GIFDEBUG
-//             g_print ("\tlooking for %d bytes.  size == %d, ptr == %d\n", len, context->size, context->ptr);
+/*             g_print ("\tlooking for %d bytes.  size == %d, ptr == %d\n", len, context->size, context->ptr); */
 #endif
                if ((context->size - context->ptr) >= len) {
 #ifdef IO_GIFDEBUG
@@ -349,7 +358,7 @@ gif_get_extension (GifContext *context)
                                if ((context->block_buf[0] & 0x1) != 0) {
                                        context->gif89.transparent = context->block_buf[3];
                                } else {
-                                       context->gif89.transparent = FALSE;
+                                       context->gif89.transparent = -1;
                                }
                        }
 
@@ -378,7 +387,7 @@ static int
 GetDataBlock (GifContext *context,
              unsigned char *buf)
 {
-//     unsigned char count;
+/*     unsigned char count; */
 
        if (!gif_read (context, &context->block_count, 1)) {
                /*g_message (_("GIF: error in getting DataBlock size\n"));*/
@@ -411,10 +420,21 @@ gif_lzw_fill_buffer (GifContext *context)
 
        if (context->code_done) {
                if (context->code_curbit >= context->code_lastbit) {
-                               g_message ("GIF: ran off the end of by bits\n");
+                        g_set_error (context->error,
+                                     GDK_PIXBUF_ERROR,
+                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                     _("GIF file was missing some data (perhaps it was truncated somehow?)"));
+
                        return -2;
                }
-               g_message ("trying to read more data after we've done stuff\n");
+                /* Is this supposed to be an error or what? */
+               /* g_message ("trying to read more data after we've done stuff\n"); */
+                g_set_error (context->error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_FAILED,
+                             _("Internal error in the GIF loader (%s)"),
+                             G_STRLOC);
+                
                return -2;
        }
 
@@ -532,7 +552,7 @@ lzw_read_byte (GifContext *context)
                        unsigned char buf[260];
 
                        /*g_error (" DID WE EVER EVER GET HERE\n");*/
-                       g_error ("Unhandled Case.  If you have an image that causes this, let me <jrb@redhat.com> know.\n");
+                       g_warning ("Unhandled Case.  If you have an image that causes this, let me <jrb@redhat.com> know.\n");
 
                        if (ZeroDataBlock) {
                                return -2;
@@ -558,8 +578,10 @@ lzw_read_byte (GifContext *context)
                        *(context->lzw_sp)++ = context->lzw_table[1][code];
 
                        if (code == context->lzw_table[0][code]) {
-                               /*g_message (_("GIF: circular table entry BIG ERROR\n"));*/
-                               /*gimp_quit ();*/
+                                g_set_error (context->error,
+                                             GDK_PIXBUF_ERROR,
+                                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                                             _("Circular table entry in GIF file"));
                                return -2;
                        }
                        code = context->lzw_table[0][code];
@@ -602,7 +624,7 @@ gif_fill_in_pixels (GifContext *context, guchar *dest, gint offset, guchar v)
 {
        guchar *pixel = NULL;
 
-       if (context->gif89.transparent) {
+       if (context->gif89.transparent != -1) {
                pixel = dest + (context->draw_ypos + offset) * gdk_pixbuf_get_rowstride (context->pixbuf) + context->draw_xpos * 4;
                *pixel = context->color_map [0][(guchar) v];
                *(pixel+1) = context->color_map [1][(guchar) v];
@@ -661,19 +683,18 @@ gif_get_lzw (GifContext *context)
        gint v;
 
        if (context->pixbuf == NULL) {
-               g_print ("making a new pixbuf\n");
-               context->pixbuf = gdk_pixbuf_new (ART_PIX_RGB,
-                                                 context->gif89.transparent,
+               context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
+                                                 context->gif89.transparent != -1,
                                                  8,
-                                                 context->width,
-                                                 context->height);
+                                                 context->frame_len,
+                                                 context->frame_height);
 
                if (context->prepare_func)
                        (* context->prepare_func) (context->pixbuf, context->user_data);
-               if (context->animation) {
+               if (context->animation || context->frame_done_func || context->anim_done_func) {
                        context->frame = g_new (GdkPixbufFrame, 1);
-                       context->frame->x_offset = 0;
-                       context->frame->y_offset = 0;
+                       context->frame->x_offset = context->x_offset;
+                       context->frame->y_offset = context->y_offset;;
                        context->frame->delay_time = context->gif89.delay_time;
                        switch (context->gif89.disposal) {
                        case 0:
@@ -691,8 +712,17 @@ gif_get_lzw (GifContext *context)
                                break;
                        }
                        context->frame->pixbuf = context->pixbuf;
-                       context->animation->n_frames ++;
-                       context->animation->frames = g_list_append (context->animation->frames, context->frame);
+                       if (context->animation) {
+                               int w,h;
+                               context->animation->n_frames ++;
+                               context->animation->frames = g_list_append (context->animation->frames, context->frame);
+                               w = gdk_pixbuf_get_width (context->pixbuf);
+                               h = gdk_pixbuf_get_height (context->pixbuf);
+                               if (w > context->animation->width)
+                                       context->animation->width = w;
+                               if (h > context->animation->height)
+                                       context->animation->height = h;
+                       }
                }
        }
        dest = gdk_pixbuf_get_pixels (context->pixbuf);
@@ -708,7 +738,7 @@ gif_get_lzw (GifContext *context)
                }
                bound_flag = TRUE;
 
-               if (context->gif89.transparent) {
+               if (context->gif89.transparent != -1) {
                        temp = dest + context->draw_ypos * gdk_pixbuf_get_rowstride (context->pixbuf) + context->draw_xpos * 4;
                        *temp = context->color_map [0][(guchar) v];
                        *(temp+1) = context->color_map [1][(guchar) v];
@@ -776,7 +806,7 @@ gif_get_lzw (GifContext *context)
        }
  done:
        /* we got enough data. there may be more (ie, newer layers) but we can quit now */
-       if (context->animation) {
+       if (context->animation || context->frame_done_func || context->anim_done_func) {
                context->state = GIF_GET_NEXT_STEP;
        } else
                context->state = GIF_DONE;
@@ -786,36 +816,42 @@ gif_get_lzw (GifContext *context)
                if (lower_bound <= upper_bound && first_pass == context->draw_pass) {
                        (* context->update_func)
                                (context->pixbuf,
-                                context->user_data,
                                 0, lower_bound,
                                 gdk_pixbuf_get_width (context->pixbuf),
-                                upper_bound - lower_bound);
+                                upper_bound - lower_bound,
+                                context->user_data);
                } else {
                        if (lower_bound <= upper_bound) {
                                (* context->update_func)
                                        (context->pixbuf,
-                                        context->user_data,
                                         0, 0,
                                         gdk_pixbuf_get_width (context->pixbuf),
-                                        gdk_pixbuf_get_height (context->pixbuf));
+                                        gdk_pixbuf_get_height (context->pixbuf),
+                                        context->user_data);
                        } else {
                                (* context->update_func)
                                        (context->pixbuf,
-                                        context->user_data,
                                         0, 0,
                                         gdk_pixbuf_get_width (context->pixbuf),
-                                        upper_bound);
+                                        upper_bound,
+                                        context->user_data);
                                (* context->update_func)
                                        (context->pixbuf,
-                                        context->user_data,
                                         0, lower_bound,
                                         gdk_pixbuf_get_width (context->pixbuf),
-                                        gdk_pixbuf_get_height (context->pixbuf));
+                                        gdk_pixbuf_get_height (context->pixbuf),
+                                        context->user_data);
                        }
                }
        }
 
-       if (context->animation && context->state == GIF_GET_NEXT_STEP) {
+       if ((context->animation || context->frame_done_func || context->anim_done_func)
+           && context->state == GIF_GET_NEXT_STEP) {
+               if (context->frame_done_func)
+                       (* context->frame_done_func) (context->frame,
+                                                     context->user_data);
+               if (context->frame_done_func)
+                       gdk_pixbuf_unref (context->pixbuf);
                context->pixbuf = NULL;
                context->frame = NULL;
        }
@@ -871,12 +907,18 @@ gif_init (GifContext *context)
        char version[4];
 
        if (!gif_read (context, buf, 6)) {
-               /* Unable to read magic number */
+               /* Unable to read magic number,
+                 * gif_read() should have set error
+                 */
                return -1;
        }
 
        if (strncmp ((char *) buf, "GIF", 3) != 0) {
                /* Not a GIF file */
+                g_set_error (context->error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("File does not appear to be a GIF file"));
                return -1;
        }
 
@@ -885,12 +927,17 @@ gif_init (GifContext *context)
 
        if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
                /* bad version number, not '87a' or '89a' */
+                g_set_error (context->error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("Version %s of the GIF file format is not supported"),
+                             version);
                return -1;
        }
 
        /* read the screen descriptor */
        if (!gif_read (context, buf, 7)) {
-               /* Failed to read screen descriptor */
+               /* Failed to read screen descriptor, error set */
                return -1;
        }
 
@@ -925,13 +972,18 @@ gif_get_frame_info (GifContext *context)
        /* Okay, we got all the info we need.  Lets record it */
        context->frame_len = LM_to_uint (buf[4], buf[5]);
        context->frame_height = LM_to_uint (buf[6], buf[7]);
-       if (context->frame) {
-               context->frame->x_offset = LM_to_uint (buf[0], buf[1]);
-               context->frame->y_offset = LM_to_uint (buf[2], buf[3]);
-       }
+       context->x_offset = LM_to_uint (buf[0], buf[1]);
+       context->y_offset = LM_to_uint (buf[2], buf[3]);
+
        if (context->frame_height > context->height) {
                /* we don't want to resize things.  So we exit */
                context->state = GIF_DONE;
+
+                g_set_error (context->error,
+                             GDK_PIXBUF_ERROR,
+                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
+                             _("GIF animation contained a frame with an incorrect size"));
+                
                return -2;
        }
 
@@ -964,7 +1016,7 @@ gif_get_next_step (GifContext *context)
                        /* hmm.  Not 100% sure what to do about this.  Should
                         * i try to return a blank image instead? */
                        context->state = GIF_DONE;
-                       return -2;
+                       return 0;
                }
 
                if (c == '!') {
@@ -1054,11 +1106,14 @@ new_context (void)
        GifContext *context;
 
        context = g_new0 (GifContext, 1);
+
        context->pixbuf = NULL;
        context->file = NULL;
        context->state = GIF_START;
        context->prepare_func = NULL;
        context->update_func = NULL;
+       context->frame_done_func = NULL;
+       context->anim_done_func = NULL;
        context->user_data = NULL;
        context->buf = NULL;
        context->amount_needed = 0;
@@ -1066,28 +1121,37 @@ new_context (void)
        context->gif89.delay_time = -1;
        context->gif89.input_flag = -1;
        context->gif89.disposal = -1;
+
        return context;
 }
 /* Shared library entry point */
 GdkPixbuf *
-image_load (FILE *file)
+gdk_pixbuf__gif_image_load (FILE *file, GError **error)
 {
        GifContext *context;
+       GdkPixbuf *pixbuf;
 
        g_return_val_if_fail (file != NULL, NULL);
 
        context = new_context ();
        context->file = file;
-
+        context->error = error;
+        
        gif_main_loop (context);
 
-       return context->pixbuf;
+       pixbuf = context->pixbuf;
+       g_free (context);
+       return pixbuf;
 }
 
 gpointer
-image_begin_load (ModulePreparedNotifyFunc prepare_func,
-                 ModuleUpdatedNotifyFunc update_func,
-                 gpointer user_data)
+gdk_pixbuf__gif_image_begin_load (ModulePreparedNotifyFunc prepare_func,
+                                 ModuleUpdatedNotifyFunc update_func,
+                                 ModuleFrameDoneNotifyFunc frame_done_func,
+                                 ModuleAnimationDoneNotifyFunc anim_done_func,
+                                 gpointer user_data,
+                                  GError **error)
 {
        GifContext *context;
 
@@ -1095,30 +1159,40 @@ image_begin_load (ModulePreparedNotifyFunc prepare_func,
        count = 0;
 #endif
        context = new_context ();
+        context->error = error;
        context->prepare_func = prepare_func;
        context->update_func = update_func;
+       context->frame_done_func = frame_done_func;
+       context->anim_done_func = anim_done_func;
        context->user_data = user_data;
 
        return (gpointer) context;
 }
 
 void
-image_stop_load (gpointer data)
+gdk_pixbuf__gif_image_stop_load (gpointer data)
 {
        GifContext *context = (GifContext *) data;
 
+       /* FIXME: free the animation data */
+
        if (context->pixbuf)
                gdk_pixbuf_unref (context->pixbuf);
-//     g_free (context->buf);
+       if (context->animation)
+               gdk_pixbuf_animation_unref (context->animation);
+/*     g_free (context->buf);*/
        g_free (context);
 }
 
 gboolean
-image_load_increment (gpointer data, guchar *buf, guint size)
+gdk_pixbuf__gif_image_load_increment (gpointer data, guchar *buf, guint size,
+                                      GError **error)
 {
        gint retval;
        GifContext *context = (GifContext *) data;
 
+        context->error = error;
+        
        if (context->amount_needed == 0) {
                /* we aren't looking for some bytes. */
                /* we can use buf now, but we don't want to keep it around at all.
@@ -1172,19 +1246,29 @@ image_load_increment (gpointer data, guchar *buf, guint size)
 }
 
 GdkPixbufAnimation *
-image_load_animation (FILE *file)
+gdk_pixbuf__gif_image_load_animation (FILE *file,
+                                      GError **error)
 {
        GifContext *context;
+       GdkPixbufAnimation *animation;
 
        g_return_val_if_fail (file != NULL, NULL);
 
        context = new_context ();
-       context->animation = g_new (GdkPixbufAnimation, 1);
+
+        context->error = error;
+        
+       context->animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
+
        context->animation->n_frames = 0;
        context->animation->frames = NULL;
+       context->animation->width = 0;
+       context->animation->height = 0;
        context->file = file;
 
        gif_main_loop (context);
-       
-       return context->animation;
+
+       animation = context->animation;
+       g_free (context);
+       return animation;
 }