]> Pileus Git - ~andy/gtk/blobdiff - gdk-pixbuf/gdk-pixbuf-loader.c
Updated Bulgarian translation
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-loader.c
index 6f9d6461a30c863dccdc198873384c9c7afa8993..9ef23c1ad0f065074af1347ab38ffd78c1a3b85c 100644 (file)
  * Boston, MA 02111-1307, USA.
  */
 
-#include <config.h>
+#include "config.h"
 #include <string.h>
 
 #include "gdk-pixbuf-private.h"
 #include "gdk-pixbuf-animation.h"
+#include "gdk-pixbuf-scaled-anim.h"
 #include "gdk-pixbuf-io.h"
 #include "gdk-pixbuf-loader.h"
 #include "gdk-pixbuf-marshal.h"
@@ -43,16 +44,13 @@ enum {
 };
 
 
-static void gdk_pixbuf_loader_class_init    (GdkPixbufLoaderClass   *klass);
-static void gdk_pixbuf_loader_init          (GdkPixbufLoader        *loader);
-static void gdk_pixbuf_loader_finalize      (GObject                *loader);
+static void gdk_pixbuf_loader_finalize (GObject *loader);
 
-static gpointer parent_class = NULL;
 static guint    pixbuf_loader_signals[LAST_SIGNAL] = { 0 };
 
 /* Internal data */
 
-#define LOADER_HEADER_SIZE 128
+#define LOADER_HEADER_SIZE 1024
 
 typedef struct
 {
@@ -69,42 +67,7 @@ typedef struct
         gboolean needs_scale;
 } GdkPixbufLoaderPrivate;
 
-
-/**
- * gdk_pixbuf_loader_get_type:
- *
- * Registers the #GdkPixbufLoader class if necessary, and returns the type ID
- * associated to it.
- *
- * Return value: The type ID of the #GdkPixbufLoader class.
- **/
-GType
-gdk_pixbuf_loader_get_type (void)
-{
-        static GType loader_type = 0;
-  
-        if (!loader_type)
-                {
-                        static const GTypeInfo loader_info = {
-                                sizeof (GdkPixbufLoaderClass),
-                                (GBaseInitFunc) NULL,
-                                (GBaseFinalizeFunc) NULL,
-                                (GClassInitFunc) gdk_pixbuf_loader_class_init,
-                                NULL,           /* class_finalize */
-                                NULL,           /* class_data */
-                                sizeof (GdkPixbufLoader),
-                                0,              /* n_preallocs */
-                                (GInstanceInitFunc) gdk_pixbuf_loader_init
-                        };
-      
-                        loader_type = g_type_register_static (G_TYPE_OBJECT,
-                                                              g_intern_static_string ("GdkPixbufLoader"),
-                                                              &loader_info,
-                                                              0);
-                }
-  
-        return loader_type;
-}
+G_DEFINE_TYPE (GdkPixbufLoader, gdk_pixbuf_loader, G_TYPE_OBJECT)
 
 static void
 gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
@@ -113,8 +76,6 @@ gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
   
         object_class = (GObjectClass *) class;
   
-        parent_class = g_type_class_peek_parent (class);
-  
         object_class->finalize = gdk_pixbuf_loader_finalize;
 
         /**
@@ -130,7 +91,7 @@ gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
          * the desired size to which the image should be scaled.
          */
         pixbuf_loader_signals[SIZE_PREPARED] =
-                g_signal_new ("size_prepared",
+                g_signal_new ("size-prepared",
                               G_TYPE_FROM_CLASS (object_class),
                               G_SIGNAL_RUN_LAST,
                               G_STRUCT_OFFSET (GdkPixbufLoaderClass, size_prepared),
@@ -150,7 +111,7 @@ gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
          * the partially-loaded pixbuf.
          */
         pixbuf_loader_signals[AREA_PREPARED] =
-                g_signal_new ("area_prepared",
+                g_signal_new ("area-prepared",
                               G_TYPE_FROM_CLASS (object_class),
                               G_SIGNAL_RUN_LAST,
                               G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_prepared),
@@ -173,7 +134,7 @@ gdk_pixbuf_loader_class_init (GdkPixbufLoaderClass *class)
          * areas of an image that is being loaded.
          */
         pixbuf_loader_signals[AREA_UPDATED] =
-                g_signal_new ("area_updated",
+                g_signal_new ("area-updated",
                               G_TYPE_FROM_CLASS (object_class),
                               G_SIGNAL_RUN_LAST,
                               G_STRUCT_OFFSET (GdkPixbufLoaderClass, area_updated),
@@ -236,7 +197,7 @@ gdk_pixbuf_loader_finalize (GObject *object)
   
         g_free (priv);
   
-        G_OBJECT_CLASS (parent_class)->finalize (object);
+        G_OBJECT_CLASS (gdk_pixbuf_loader_parent_class)->finalize (object);
 }
 
 /**
@@ -248,10 +209,10 @@ gdk_pixbuf_loader_finalize (GObject *object)
  * Causes the image to be scaled while it is loaded. The desired
  * image size can be determined relative to the original size of
  * the image by calling gdk_pixbuf_loader_set_size() from a
- * signal handler for the ::size_prepared signal.
+ * signal handler for the ::size-prepared signal.
  *
  * Attempts to set the desired image size  are ignored after the 
- * emission of the ::size_prepared signal.
+ * emission of the ::size-prepared signal.
  *
  * Since: 2.2
  */
@@ -317,7 +278,15 @@ gdk_pixbuf_loader_prepare (GdkPixbuf          *pixbuf,
         else
                 anim = gdk_pixbuf_non_anim_new (pixbuf);
   
-        priv->animation = anim;
+       if (priv->needs_scale) {
+               priv->animation  = GDK_PIXBUF_ANIMATION (_gdk_pixbuf_scaled_anim_new (anim,
+                                         (double) priv->width / gdk_pixbuf_get_width (pixbuf),
+                                         (double) priv->height / gdk_pixbuf_get_height (pixbuf),
+                                         1.0));
+                       g_object_unref (anim);
+       }
+       else
+               priv->animation = anim;
   
         if (!priv->needs_scale)
                 g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
@@ -343,6 +312,27 @@ gdk_pixbuf_loader_update (GdkPixbuf *pixbuf,
                                MIN (height, gdk_pixbuf_animation_get_height (priv->animation)));
 }
 
+/* Defense against broken loaders; DO NOT take this as a GError example! */
+static void
+gdk_pixbuf_loader_ensure_error (GdkPixbufLoader *loader,
+                                GError         **error)
+{ 
+        GdkPixbufLoaderPrivate *priv = loader->priv;
+
+        if (error == NULL || *error != NULL)
+                return;
+
+        g_warning ("Bug! loader '%s' didn't set an error on failure",
+                   priv->image_module->module_name);
+        g_set_error (error,
+                     GDK_PIXBUF_ERROR,
+                     GDK_PIXBUF_ERROR_FAILED,
+                     _("Internal error: Image loader module '%s' failed to"
+                       " complete an operation, but didn't give a reason for"
+                       " the failure"),
+                     priv->image_module->module_name);
+}
+
 static gint
 gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
                                const char      *image_type,
@@ -366,9 +356,8 @@ gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
         if (priv->image_module == NULL)
                 return 0;
   
-        if (priv->image_module->module == NULL)
-                if (!_gdk_pixbuf_load_module (priv->image_module, error))
-                        return 0;
+        if (!_gdk_pixbuf_load_module (priv->image_module, error))
+                return 0;
   
         if (priv->image_module->module == NULL)
                 return 0;
@@ -398,23 +387,7 @@ gdk_pixbuf_loader_load_module (GdkPixbufLoader *loader,
   
         if (priv->context == NULL)
                 {
-                        /* Defense against broken loaders; DO NOT take this as a GError
-                         * example
-                         */
-                        if (error && *error == NULL)
-                                {
-                                        g_warning ("Bug! loader '%s' didn't set an error on failure",
-                                                   priv->image_module->module_name);
-                                        g_set_error (error,
-                                                     GDK_PIXBUF_ERROR,
-                                                     GDK_PIXBUF_ERROR_FAILED,
-                                                     _("Internal error: Image loader module '%s'"
-                                                       " failed to begin loading an image, but didn't"
-                                                       " give a reason for the failure"),
-                                                     priv->image_module->module_name);
-
-                                }
-      
+                        gdk_pixbuf_loader_ensure_error (loader, error);
                         return 0;
                 }
   
@@ -473,11 +446,9 @@ gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
 {
         GdkPixbufLoaderPrivate *priv;
   
-        g_return_val_if_fail (loader != NULL, FALSE);
         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), FALSE);
   
         g_return_val_if_fail (buf != NULL, FALSE);
-        g_return_val_if_fail (count >= 0, FALSE);
         g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
   
         priv = loader->priv;
@@ -485,13 +456,13 @@ gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
         /* we expect it's not to be closed */
         g_return_val_if_fail (priv->closed == FALSE, FALSE);
   
-        if (priv->image_module == NULL)
+        if (count > 0 && priv->image_module == NULL)
                 {
                         gint eaten;
       
                         eaten = gdk_pixbuf_loader_eat_header_write (loader, buf, count, error);
                         if (eaten <= 0)
-                                return FALSE;
+                               goto fail; 
       
                         count -= eaten;
                         buf += eaten;
@@ -499,27 +470,18 @@ gdk_pixbuf_loader_write (GdkPixbufLoader *loader,
   
         if (count > 0 && priv->image_module->load_increment)
                 {
-                        gboolean retval;
-                        retval = priv->image_module->load_increment (priv->context, buf, count,
-                                                                     error);
-                        if (!retval && error && *error == NULL)
-                                {
-                                        /* Fix up busted image loader */
-                                        g_warning ("Bug! loader '%s' didn't set an error on failure",
-                                                   priv->image_module->module_name);
-                                        g_set_error (error,
-                                                     GDK_PIXBUF_ERROR,
-                                                     GDK_PIXBUF_ERROR_FAILED,
-                                                     _("Internal error: Image loader module '%s'"
-                                                       " failed to begin loading an image, but didn't"
-                                                       " give a reason for the failure"),
-                                                     priv->image_module->module_name);
-                                }
-
-                        return retval;
+                        if (!priv->image_module->load_increment (priv->context, buf, count,
+                                                                 error))
+                               goto fail;
                 }
       
         return TRUE;
+
+ fail:
+        gdk_pixbuf_loader_ensure_error (loader, error);
+        gdk_pixbuf_loader_close (loader, NULL);
+
+        return FALSE;
 }
 
 /**
@@ -538,7 +500,7 @@ gdk_pixbuf_loader_new (void)
 /**
  * gdk_pixbuf_loader_new_with_type:
  * @image_type: name of the image format to be loaded with the image
- * @error: return location for an allocated #GError, or %NULL to ignore errors
+ * @error: (allow-none): return location for an allocated #GError, or %NULL to ignore errors
  *
  * Creates a new pixbuf loader object that always attempts to parse
  * image data as if it were an image of type @image_type, instead of
@@ -547,6 +509,12 @@ gdk_pixbuf_loader_new (void)
  * that can't be reliably identified by looking at the data, or if
  * the user manually forces a specific type.
  *
+ * The list of supported image formats depends on what image loaders
+ * are installed, but typically "png", "jpeg", "gif", "tiff" and 
+ * "xpm" are among the supported formats. To obtain the full list of
+ * supported image formats, call gdk_pixbuf_format_get_name() on each 
+ * of the #GdkPixbufFormat structs returned by gdk_pixbuf_get_formats().
+ *
  * Return value: A newly-created pixbuf loader.
  **/
 GdkPixbufLoader *
@@ -575,7 +543,7 @@ gdk_pixbuf_loader_new_with_type (const char *image_type,
 /**
  * gdk_pixbuf_loader_new_with_mime_type:
  * @mime_type: the mime type to be loaded 
- * @error: return location for an allocated #GError, or %NULL to ignore errors
+ * @error: (allow-none): return location for an allocated #GError, or %NULL to ignore errors
  *
  * Creates a new pixbuf loader object that always attempts to parse
  * image data as if it were an image of mime type @mime_type, instead of
@@ -584,6 +552,13 @@ gdk_pixbuf_loader_new_with_type (const char *image_type,
  * that can't be reliably identified by looking at the data, or if
  * the user manually forces a specific mime type.
  *
+ * The list of supported mime types depends on what image loaders
+ * are installed, but typically "image/png", "image/jpeg", "image/gif", 
+ * "image/tiff" and "image/x-xpixmap" are among the supported mime types. 
+ * To obtain the full list of supported mime types, call 
+ * gdk_pixbuf_format_get_mime_types() on each of the #GdkPixbufFormat 
+ * structs returned by gdk_pixbuf_get_formats().
+ *
  * Return value: A newly-created pixbuf loader.
  * Since: 2.4
  **/
@@ -638,7 +613,7 @@ gdk_pixbuf_loader_new_with_mime_type (const char *mime_type,
  *
  * Queries the #GdkPixbuf that a pixbuf loader is currently creating.
  * In general it only makes sense to call this function after the
- * "area_prepared" signal has been emitted by the loader; this means
+ * "area-prepared" signal has been emitted by the loader; this means
  * that enough data has been read to know the size of the image that
  * will be allocated.  If the loader has not received enough data via
  * gdk_pixbuf_loader_write(), then this function returns %NULL.  The
@@ -656,7 +631,6 @@ gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
 {
         GdkPixbufLoaderPrivate *priv;
   
-        g_return_val_if_fail (loader != NULL, NULL);
         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
   
         priv = loader->priv;
@@ -672,9 +646,9 @@ gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)
  * @loader: A pixbuf loader
  *
  * Queries the #GdkPixbufAnimation that a pixbuf loader is currently creating.
- * In general it only makes sense to call this function after the "area_prepared"
+ * In general it only makes sense to call this function after the "area-prepared"
  * signal has been emitted by the loader. If the loader doesn't have enough
- * bytes yet (hasn't emitted the "area_prepared" signal) this function will 
+ * bytes yet (hasn't emitted the "area-prepared" signal) this function will 
  * return %NULL.
  *
  * Return value: The #GdkPixbufAnimation that the loader is loading, or %NULL if
@@ -685,7 +659,6 @@ gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
 {
         GdkPixbufLoaderPrivate *priv;
   
-        g_return_val_if_fail (loader != NULL, NULL);
         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
   
         priv = loader->priv;
@@ -696,7 +669,7 @@ gdk_pixbuf_loader_get_animation (GdkPixbufLoader *loader)
 /**
  * gdk_pixbuf_loader_close:
  * @loader: A pixbuf loader.
- * @error: return location for a #GError, or %NULL to ignore errors
+ * @error: (allow-none): return location for a #GError, or %NULL to ignore errors
  *
  * Informs a pixbuf loader that no further writes with
  * gdk_pixbuf_loader_write() will occur, so that it can free its
@@ -718,16 +691,17 @@ gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
         GdkPixbufLoaderPrivate *priv;
         gboolean retval = TRUE;
   
-        g_return_val_if_fail (loader != NULL, TRUE);
         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), TRUE);
         g_return_val_if_fail (error == NULL || *error == NULL, TRUE);
   
         priv = loader->priv;
   
-        /* we expect it's not closed */
-        g_return_val_if_fail (priv->closed == FALSE, TRUE);
+        if (priv->closed)
+                return TRUE;
   
-        /* We have less the 128 bytes in the image.  Flush it, and keep going. */
+        /* We have less the LOADER_HEADER_SIZE bytes in the image.  
+         * Flush it, and keep going. 
+         */
         if (priv->image_module == NULL)
                 {
                         GError *tmp = NULL;
@@ -741,8 +715,21 @@ gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
 
         if (priv->image_module && priv->image_module->stop_load && priv->context) 
                 {
-                        if (!priv->image_module->stop_load (priv->context, error))
-                                retval = FALSE;
+                        GError *tmp = NULL;
+                        if (!priv->image_module->stop_load (priv->context, &tmp) || tmp)
+                                {
+                                       /* don't call gdk_pixbuf_loader_ensure_error()
+                                        * here, since we might not get an error in the
+                                        * gdk_pixbuf_get_file_info() case
+                                        */
+                                       if (tmp) {
+                                               if (error && *error == NULL)
+                                                       g_propagate_error (error, tmp);
+                                               else
+                                                       g_error_free (tmp);
+                                       }
+                                        retval = FALSE;
+                                }
                 }
   
         priv->closed = TRUE;
@@ -753,21 +740,8 @@ gdk_pixbuf_loader_close (GdkPixbufLoader *loader,
 
         if (priv->needs_scale) 
                 {
-                        GdkPixbuf *tmp, *pixbuf;
-                        
-                        tmp = gdk_pixbuf_animation_get_static_image (priv->animation);
-                        g_object_ref (tmp);
-                        pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, tmp->has_alpha, 8, priv->width, priv->height);
-                        g_object_unref (priv->animation);
-                        priv->animation = gdk_pixbuf_non_anim_new (pixbuf);
-                        g_object_unref (pixbuf);
+
                         g_signal_emit (loader, pixbuf_loader_signals[AREA_PREPARED], 0);
-                        gdk_pixbuf_scale (tmp, pixbuf, 0, 0, priv->width, priv->height, 0, 0,
-                                          (double) priv->width / tmp->width,
-                                          (double) priv->height / tmp->height,
-                                          GDK_INTERP_BILINEAR); 
-                        g_object_unref (tmp);
-                        
                         g_signal_emit (loader, pixbuf_loader_signals[AREA_UPDATED], 0, 
                                        0, 0, priv->width, priv->height);
                 }
@@ -795,7 +769,6 @@ gdk_pixbuf_loader_get_format (GdkPixbufLoader *loader)
 {
         GdkPixbufLoaderPrivate *priv;
   
-        g_return_val_if_fail (loader != NULL, NULL);
         g_return_val_if_fail (GDK_IS_PIXBUF_LOADER (loader), NULL);
   
         priv = loader->priv;