Index: src/eog-image.c
===================================================================
--- src/eog-image.c	(revision 4878)
+++ src/eog-image.c	(working copy)
@@ -797,6 +797,69 @@ eog_image_get_dimension_from_thumbnail (
 	return (*width || *height);
 }
 
+static GdkPixbufLoader*
+eog_image_prepare_loader (EogImage *img, gpointer buffer, gsize buffer_size,
+			  gchar **mime_type, GError **error)
+{
+	gboolean checked_threadsafety = FALSE;
+	gchar *mtype = g_content_type_guess (NULL, buffer, buffer_size, NULL);
+	GdkPixbufLoader *loader = NULL;
+
+	loader = gdk_pixbuf_loader_new_with_mime_type (mtype, error);
+
+	if (error && *error) {
+		g_error_free (*error);
+		*error = NULL;
+
+		/* Watch out for compressed SVGs */
+		if (g_strcmp0 ("application/x-gzip", mtype) == 0) {
+			gchar *basename = NULL;
+			GFile *file = eog_image_get_file (img);
+
+			basename = g_file_get_basename (file);
+			g_object_unref (file);
+			g_free (mtype);
+			mtype = g_content_type_guess (basename, buffer, buffer_size, NULL);
+			g_free (basename);
+			loader = gdk_pixbuf_loader_new_with_mime_type (mtype, error);
+			if (error && *error) {
+				g_free (mtype);
+				return NULL;
+			}
+
+		} else 
+			loader = gdk_pixbuf_loader_new ();
+
+	} else {
+		/* The mimetype-based loader should know the
+		 * format here already. */
+		checked_threadsafety = check_loader_threadsafety (loader, &img->priv->threadsafe_format);
+	}
+
+	/* This is used to detect non-threadsafe loaders and disable
+	 * any possible asynchronous task that could bring deadlocks
+	 * to image loading process. */
+	if (!checked_threadsafety)
+		g_signal_connect (loader,
+				  "size-prepared",
+				  G_CALLBACK (eog_image_pre_size_prepared),
+				  img);
+
+	g_signal_connect_object (G_OBJECT (loader),
+				 "size-prepared",
+				 G_CALLBACK (eog_image_size_prepared),
+				 img,
+				 0);
+
+	if (mime_type)
+		*mime_type = mtype;
+	else
+		g_free (mtype);
+
+	return loader;
+}
+
+
 static gboolean
 eog_image_real_load (EogImage *img,
 		     guint     data2read,
@@ -807,7 +870,7 @@ eog_image_real_load (EogImage *img,
 	GFileInputStream *input_stream;
 	EogMetadataReader *md_reader = NULL;
 	GdkPixbufFormat *format;
-	gchar *mime_type;
+	gchar *mime_type = NULL;
 	GdkPixbufLoader *loader = NULL;
 	guchar *buffer;
 	goffset bytes_read, bytes_read_total = 0;
@@ -829,12 +892,7 @@ eog_image_real_load (EogImage *img,
 
 	priv->threadsafe_format = FALSE;
 
-	eog_image_get_file_info (img, &priv->bytes, &mime_type, error);
-
-	if (error && *error) {
-		g_free (mime_type);
-		return FALSE;
-	}
+	eog_image_get_file_info (img, &priv->bytes, NULL, error);
 
 	if (read_only_dimension) {
 		gint width, height;
@@ -856,8 +914,6 @@ eog_image_real_load (EogImage *img,
 	input_stream = g_file_read (priv->file, NULL, error);
 
 	if (input_stream == NULL) {
-		g_free (mime_type);
-
 		if (error != NULL) {
 			g_set_error (error,
 				     EOG_IMAGE_ERROR,
@@ -869,41 +925,7 @@ eog_image_real_load (EogImage *img,
 
 	buffer = g_new0 (guchar, EOG_IMAGE_READ_BUFFER_SIZE);
 
-	if (read_image_data || read_only_dimension) {
-		gboolean checked_threadsafety = FALSE;
-
-		loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error);
-
-		if (error && *error) {
-			g_error_free (*error);
-			*error = NULL;
-
-			loader = gdk_pixbuf_loader_new ();
-		} else {
-			/* The mimetype-based loader should know the
-			 * format here already. */
-			checked_threadsafety = check_loader_threadsafety (loader, &priv->threadsafe_format);
-		}
-
-		/* This is used to detect non-threadsafe loaders and disable
- 		 * any possible asyncronous task that could bring deadlocks
- 		 * to image loading process. */
-		if (!checked_threadsafety)
-			g_signal_connect (loader,
-					  "size-prepared",
-					  G_CALLBACK (eog_image_pre_size_prepared),
-					  img);
-
-		g_signal_connect_object (G_OBJECT (loader),
-					 "size-prepared",
-					 G_CALLBACK (eog_image_size_prepared),
-					 img,
-					 0);
-        }
-	g_free (mime_type);
-
 	while (!priv->cancel_loading) {
-		/* FIXME: make this async */
 		bytes_read = g_input_stream_read (G_INPUT_STREAM (input_stream),
 						  buffer,
 						  EOG_IMAGE_READ_BUFFER_SIZE,
@@ -923,10 +945,24 @@ eog_image_real_load (EogImage *img,
 			break;
 		}
 
-		if ((read_image_data || read_only_dimension) &&
-		    !gdk_pixbuf_loader_write (loader, buffer, bytes_read, error)) {
-			failed = TRUE;
-			break;
+		if (read_image_data || read_only_dimension) {
+			if (G_UNLIKELY (first_run)) {
+				loader = eog_image_prepare_loader (img,
+								   buffer,
+								   bytes_read,
+								   &mime_type,
+								   error);
+
+				if (!loader) {
+					failed = TRUE;
+					break;
+				}
+			}
+
+		    	if (!gdk_pixbuf_loader_write (loader, buffer, bytes_read, error)) {
+				failed = TRUE;
+				break;
+			}
 		}
 
 		bytes_read_total += bytes_read;
@@ -1067,6 +1103,8 @@ eog_image_real_load (EogImage *img,
 			     _("Image loading failed."));
 	}
 
+	g_free (mime_type);
+
 	return !failed;
 }
 
