[gdk-pixbuf] gif: Replace old buffer management code with GByteArray



commit 5212d69f2362f9b68ccf9385277e5c4a744b2187
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Jul 14 15:03:23 2020 +1200

    gif: Replace old buffer management code with GByteArray

 gdk-pixbuf/io-gif.c | 121 ++++++----------------------------------------------
 1 file changed, 13 insertions(+), 108 deletions(-)
---
diff --git a/gdk-pixbuf/io-gif.c b/gdk-pixbuf/io-gif.c
index b08a00bd62..07fee59cfc 100644
--- a/gdk-pixbuf/io-gif.c
+++ b/gdk-pixbuf/io-gif.c
@@ -62,7 +62,6 @@
 
 
 #undef DUMP_IMAGE_DETAILS 
-#undef IO_GIFDEBUG
 
 #define MAXCOLORMAPSIZE  256
 
@@ -125,10 +124,7 @@ struct _GifContext
        GdkPixbufModulePreparedFunc prepared_func;
        GdkPixbufModuleUpdatedFunc updated_func;
        gpointer user_data;
-        guchar *buf;
-       gsize ptr;
-       gsize size;
-       gsize amount_needed;
+       GByteArray *buf;
 
        /* extension context */
        guchar extension_label;
@@ -145,26 +141,13 @@ struct _GifContext
         GError **error;
 };
 
-
-
-#ifdef IO_GIFDEBUG
-static int count = 0;
-#endif
-
 /* Returns TRUE if read is OK,
  * FALSE if more memory is needed. */
 static gboolean
 gif_read (GifContext *context, guchar *buffer, size_t len)
 {
        gboolean retval;
-#ifdef IO_GIFDEBUG
-       gint i;
-#endif
        if (context->file) {
-#ifdef IO_GIFDEBUG
-               count += len;
-               g_print ("Fsize :%zi\tcount :%d\t", len, count);
-#endif
                retval = (fread (buffer, 1, len, context->file) == len);
 
                 if (!retval && ferror (context->file)) {
@@ -175,38 +158,14 @@ gif_read (GifContext *context, guchar *buffer, size_t len)
                                      _("Failure reading GIF: %s"), 
                                      g_strerror (save_errno));
                 }
-                
-#ifdef IO_GIFDEBUG
-               if (len < 100) {
-                       for (i = 0; i < len; i++)
-                               g_print ("%d ", buffer[i]);
-               }
-               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); */
-#endif
-               if ((context->size - context->ptr) >= len) {
-#ifdef IO_GIFDEBUG
-                       count += len;
-#endif
-                       memcpy (buffer, context->buf + context->ptr, len);
-                       context->ptr += len;
-                       context->amount_needed = 0;
-#ifdef IO_GIFDEBUG
-                       g_print ("Psize :%zi\tcount :%d\t", len, count);
-                       if (len < 100) {
-                               for (i = 0; i < len; i++)
-                                       g_print ("%d ", buffer[i]);
-                       }
-                       g_print ("\n");
-#endif
+               if (context->buf->len >= len) {
+                       memcpy (buffer, context->buf->data, len);
+                       g_byte_array_remove_range (context->buf, 0, len);
                        return TRUE;
                }
-               context->amount_needed = len - (context->size - context->ptr);
        }
        return FALSE;
 }
@@ -839,12 +798,7 @@ new_context (GdkPixbufModuleSizeFunc size_func,
        context->prepared_func = prepared_func;
        context->updated_func = updated_func;
        context->user_data = user_data;
-       context->buf = NULL;
-       context->amount_needed = 13;
-       context->buf = g_new (guchar, context->amount_needed);
-       context->transparent_index = -1;
-       context->delay_time = -1;
-       context->disposal = -1;
+       context->buf = g_byte_array_new ();
         context->animation->loop = 1;
         context->in_loop_extension = FALSE;
 
@@ -925,7 +879,7 @@ gdk_pixbuf__gif_image_load (FILE *file, GError **error)
 out:
         g_object_unref (context->animation);
         
-        g_free (context->buf);
+       g_byte_array_unref (context->buf);
        g_free (context);
  
        return pixbuf;
@@ -944,9 +898,6 @@ gdk_pixbuf__gif_image_begin_load (GdkPixbufModuleSizeFunc size_func,
         g_assert (prepared_func != NULL);
         g_assert (updated_func != NULL);
 
-#ifdef IO_GIFDEBUG
-       count = 0;
-#endif
        context = new_context (size_func, prepared_func, updated_func, user_data);
 
         if (context == NULL) {
@@ -986,7 +937,7 @@ gdk_pixbuf__gif_image_stop_load (gpointer data, GError **error)
 
         g_object_unref (context->animation);
 
-       g_free (context->buf);
+       g_byte_array_unref (context->buf);
        g_free (context);
 
         return retval;
@@ -1001,59 +952,13 @@ gdk_pixbuf__gif_image_load_increment (gpointer data,
        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.
-                * it will be gone by the end of the call. */
-               context->buf = (guchar*) buf; /* very dubious const cast */
-               context->ptr = 0;
-               context->size = size;
-       } else {
-               /* we need some bytes */
-               if (size < context->amount_needed) {
-                       context->amount_needed -= size;
-                       /* copy it over and return */
-                       memcpy (context->buf + context->size, buf, size);
-                       context->size += size;
-                       return TRUE;
-               } else if (size == context->amount_needed) {
-                       memcpy (context->buf + context->size, buf, size);
-                       context->size += size;
-               } else {
-                       context->buf = g_realloc (context->buf, context->size + size);
-                       memcpy (context->buf + context->size, buf, size);
-                       context->size += size;
-               }
-       }
 
-       retval = gif_main_loop (context);
+       g_byte_array_append (context->buf, buf, size);
 
-       if (retval == -2) {
-               if (context->buf == buf)
-                        context->buf = NULL;
+       retval = gif_main_loop (context);
+       if (retval == -2)
                return FALSE;
-        }
-       if (retval == -1) {
-               /* we didn't have enough memory */
-               /* prepare for the next image_load_increment */
-               if (context->buf == buf) {
-                       g_assert (context->size == size);
-                       context->buf = g_new (guchar, context->amount_needed + (context->size - 
context->ptr));
-                       memcpy (context->buf, buf + context->ptr, context->size - context->ptr);
-               } else {
-                       /* copy the left overs to the begining of the buffer */
-                       /* and realloc the memory */
-                       memmove (context->buf, context->buf + context->ptr, context->size - context->ptr);
-                       context->buf = g_realloc (context->buf, context->amount_needed + (context->size - 
context->ptr));
-               }
-               context->size = context->size - context->ptr;
-               context->ptr = 0;
-       } else {
-               /* we are prolly all done */
-               if (context->buf == buf)
-                       context->buf = NULL;
-       }
+
        return TRUE;
 }
 
@@ -1098,7 +1003,7 @@ gdk_pixbuf__gif_image_load_animation (FILE *file,
         if (context->error && *(context->error))
                 g_print ("%s\n", (*(context->error))->message);
         
-        g_free (context->buf);
+       g_byte_array_unref (context->buf);
        g_free (context);
        return animation;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]