Trim trailing white spaces.
[geeqie.git] / src / image-load.c
index f4167b4..2c00f00 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2004 John Ellis
- * Copyright (C) 2008 - 2010 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
@@ -13,6 +13,9 @@
 
 #include "main.h"
 #include "image-load.h"
+#include "image_load_gdk.h"
+#include "image_load_jpeg.h"
+#include "image_load_tiff.h"
 
 #include "exif.h"
 #include "filedata.h"
@@ -35,6 +38,7 @@ enum {
        SIGNAL_ERROR,
        SIGNAL_DONE,
        SIGNAL_PERCENT,
+       SIGNAL_SIZE,
        SIGNAL_COUNT
 };
 
@@ -48,7 +52,7 @@ static void image_loader_stop(ImageLoader *il);
 GType image_loader_get_type(void)
 {
        static GType type = 0;
-       if (type == 0) 
+       if (type == 0)
                {
                static const GTypeInfo info = {
                        sizeof(ImageLoaderClass),
@@ -88,6 +92,8 @@ static void image_loader_init(GTypeInstance *instance, gpointer g_class)
 
        il->requested_width = 0;
        il->requested_height = 0;
+       il->actual_width = 0;
+       il->actual_height = 0;
        il->shrunk = FALSE;
 
        il->can_destroy = TRUE;
@@ -150,6 +156,17 @@ static void image_loader_class_init(ImageLoaderClass *class)
                             G_TYPE_NONE, 1,
                             G_TYPE_DOUBLE);
 
+       signals[SIGNAL_SIZE] =
+               g_signal_new("size_prepared",
+                            G_OBJECT_CLASS_TYPE(gobject_class),
+                            G_SIGNAL_RUN_LAST,
+                            G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
+                            NULL, NULL,
+                            gq_marshal_VOID__INT_INT,
+                            G_TYPE_NONE, 2,
+                            G_TYPE_INT,
+                            G_TYPE_INT);
+
 }
 
 static void image_loader_finalize(GObject *object)
@@ -168,12 +185,12 @@ static void image_loader_finalize(GObject *object)
                il->idle_done_id = 0;
                }
 
-       while (g_source_remove_by_user_data(il)) 
+       while (g_source_remove_by_user_data(il))
                {
                DEBUG_2("pending signals detected");
                }
        
-       while (il->area_param_list) 
+       while (il->area_param_list)
                {
                DEBUG_1("pending area_ready signals detected");
                while (g_source_remove_by_user_data(il->area_param_list->data)) {}
@@ -181,7 +198,7 @@ static void image_loader_finalize(GObject *object)
                il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
                }
 
-       while (il->area_param_delayed_list) 
+       while (il->area_param_delayed_list)
                {
                g_free(il->area_param_delayed_list->data);
                il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
@@ -235,11 +252,17 @@ static gboolean image_loader_emit_area_ready_cb(gpointer data)
 {
        ImageLoaderAreaParam *par = data;
        ImageLoader *il = par->il;
-       g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
+       guint x, y, w, h;
        g_mutex_lock(il->data_mutex);
        il->area_param_list = g_list_remove(il->area_param_list, par);
+       x = par->x;
+       y = par->y;
+       w = par->w;
+       h = par->h;
        g_free(par);
        g_mutex_unlock(il->data_mutex);
+
+       g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
        
        return FALSE;
 }
@@ -265,6 +288,19 @@ static gboolean image_loader_emit_percent_cb(gpointer data)
        return FALSE;
 }
 
+static gboolean image_loader_emit_size_cb(gpointer data)
+{
+       gint width, height;
+       ImageLoader *il = data;
+       g_mutex_lock(il->data_mutex);
+       width = il->actual_width;
+       height = il->actual_height;
+       g_mutex_unlock(il->data_mutex);
+       g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
+       return FALSE;
+}
+
+
 /* DONE and ERROR are emited only once, thus they can have normal priority
    PERCENT and AREA_READY should be processed ASAP
 */
@@ -284,9 +320,48 @@ static void image_loader_emit_percent(ImageLoader *il)
        g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
 }
 
-/* this function expects that il->data_mutex is locked by caller */
-static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
+static void image_loader_emit_size(ImageLoader *il)
 {
+       g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
+}
+
+static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
+{
+       if (*list)
+               {
+               ImageLoaderAreaParam *prev_par = (*list)->data;
+               if (prev_par->x == x && prev_par->w == w &&
+                   prev_par->y + prev_par->h == y)
+                       {
+                       /* we can merge the notifications */
+                       prev_par->h += h;
+                       return NULL;
+                       }
+               if (prev_par->x == x && prev_par->w == w &&
+                   y + h == prev_par->y)
+                       {
+                       /* we can merge the notifications */
+                       prev_par->h += h;
+                       prev_par->y = y;
+                       return NULL;
+                       }
+               if (prev_par->y == y && prev_par->h == h &&
+                   prev_par->x + prev_par->w == x)
+                       {
+                       /* we can merge the notifications */
+                       prev_par->w += w;
+                       return NULL;
+                       }
+               if (prev_par->y == y && prev_par->h == h &&
+                   x + w == prev_par->x)
+                       {
+                       /* we can merge the notifications */
+                       prev_par->w += w;
+                       prev_par->x = x;
+                       return NULL;
+                       }
+               }
+       
        ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
        par->il = il;
        par->x = x;
@@ -294,25 +369,28 @@ static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guin
        par->w = w;
        par->h = h;
        
-       il->area_param_list = g_list_prepend(il->area_param_list, par);
+       *list = g_list_prepend(*list, par);
+       return par;
+}
+
+/* this function expects that il->data_mutex is locked by caller */
+static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
+{
+       ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
        
-       g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
+       if (par)
+               {
+               g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
+               }
 }
 
 /**************************************************************************************/
 /* the following functions may be executed in separate thread */
 
 /* this function expects that il->data_mutex is locked by caller */
-static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
+static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
 {
-       ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
-       par->il = il;
-       par->x = x;
-       par->y = y;
-       par->w = w;
-       par->h = h;
-       
-       il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
+       image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
 }
 
 
@@ -336,13 +414,13 @@ static void image_loader_sync_pixbuf(ImageLoader *il)
        
        g_mutex_lock(il->data_mutex);
        
-       if (!il->loader) 
+       if (!il->loader)
                {
                g_mutex_unlock(il->data_mutex);
                return;
                }
 
-       pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
+       pb = il->backend.get_pixbuf(il->loader);
 
        if (pb == il->pixbuf)
                {
@@ -350,6 +428,11 @@ static void image_loader_sync_pixbuf(ImageLoader *il)
                return;
                }
 
+       if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
+               {
+               g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
+               }
+
        if (il->pixbuf) g_object_unref(il->pixbuf);
 
        il->pixbuf = pb;
@@ -358,7 +441,7 @@ static void image_loader_sync_pixbuf(ImageLoader *il)
        g_mutex_unlock(il->data_mutex);
 }
 
-static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
+static void image_loader_area_updated_cb(gpointer loader,
                                 guint x, guint y, guint w, guint h,
                                 gpointer data)
 {
@@ -375,23 +458,27 @@ static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
 
        g_mutex_lock(il->data_mutex);
        if (il->delay_area_ready)
-               image_loader_queue_delayed_erea_ready(il, x, y, w, h);
+               image_loader_queue_delayed_area_ready(il, x, y, w, h);
        else
                image_loader_emit_area_ready(il, x, y, w, h);
+       
+       if (il->stopping) il->backend.abort(il->loader);
+
        g_mutex_unlock(il->data_mutex);
 }
 
-static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
+static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
 {
+       ImageLoader *il = data;
        GdkPixbuf *pb;
        guchar *pix;
        size_t h, rs;
        
-       /* a workaround for 
-          http://bugzilla.gnome.org/show_bug.cgi?id=547669 
+       /* a workaround for
+          http://bugzilla.gnome.org/show_bug.cgi?id=547669
           http://bugzilla.gnome.org/show_bug.cgi?id=589334
        */
-       gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
+       gchar *format = il->backend.get_format_name(loader);
        if (strcmp(format, "svg") == 0 ||
            strcmp(format, "xpm") == 0)
                {
@@ -401,7 +488,7 @@ static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data
        
        g_free(format);
 
-       pb = gdk_pixbuf_loader_get_pixbuf(loader);
+       pb = il->backend.get_pixbuf(loader);
        
        h = gdk_pixbuf_get_height(pb);
        rs = gdk_pixbuf_get_rowstride(pb);
@@ -411,27 +498,26 @@ static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data
 
 }
 
-static void image_loader_size_cb(GdkPixbufLoader *loader,
+static void image_loader_size_cb(gpointer loader,
                                 gint width, gint height, gpointer data)
 {
        ImageLoader *il = data;
-       GdkPixbufFormat *format;
        gchar **mime_types;
        gboolean scale = FALSE;
        gint n;
 
        g_mutex_lock(il->data_mutex);
-       if (il->requested_width < 1 || il->requested_height < 1) 
+       il->actual_width = width;
+       il->actual_height = height;
+       if (il->requested_width < 1 || il->requested_height < 1)
                {
                g_mutex_unlock(il->data_mutex);
+               image_loader_emit_size(il);
                return;
                }
        g_mutex_unlock(il->data_mutex);
 
-       format = gdk_pixbuf_loader_get_format(loader);
-       if (!format) return;
-
-       mime_types = gdk_pixbuf_format_get_mime_types(format);
+       mime_types = il->backend.get_format_mime_types(loader);
        n = 0;
        while (mime_types[n])
                {
@@ -440,13 +526,17 @@ static void image_loader_size_cb(GdkPixbufLoader *loader,
                }
        g_strfreev(mime_types);
 
-       if (!scale) return;
+       if (!scale)
+               {
+               image_loader_emit_size(il);
+               return;
+               }
 
        g_mutex_lock(il->data_mutex);
 
+       gint nw, nh;
        if (width > il->requested_width || height > il->requested_height)
                {
-               gint nw, nh;
 
                if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
                        {
@@ -461,11 +551,14 @@ static void image_loader_size_cb(GdkPixbufLoader *loader,
                        if (nw < 1) nw = 1;
                        }
 
-               gdk_pixbuf_loader_set_size(loader, nw, nh);
+               il->actual_width = nw;
+               il->actual_height = nh;
+               il->backend.set_size(loader, nw, nh);
                il->shrunk = TRUE;
                }
-       g_mutex_unlock(il->data_mutex);
 
+       g_mutex_unlock(il->data_mutex);
+       image_loader_emit_size(il);
 }
 
 static void image_loader_stop_loader(ImageLoader *il)
@@ -475,9 +568,9 @@ static void image_loader_stop_loader(ImageLoader *il)
        if (il->loader)
                {
                /* some loaders do not have a pixbuf till close, order is important here */
-               gdk_pixbuf_loader_close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
+               il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
                image_loader_sync_pixbuf(il);
-               g_object_unref(G_OBJECT(il->loader));
+               il->backend.free(il->loader);
                il->loader = NULL;
                }
        g_mutex_lock(il->data_mutex);
@@ -488,14 +581,27 @@ static void image_loader_stop_loader(ImageLoader *il)
 static void image_loader_setup_loader(ImageLoader *il)
 {
        g_mutex_lock(il->data_mutex);
-       il->loader = gdk_pixbuf_loader_new();
-
-       g_signal_connect(G_OBJECT(il->loader), "area_updated",
-                        G_CALLBACK(image_loader_area_updated_cb), il);
-       g_signal_connect(G_OBJECT(il->loader), "size_prepared",
-                        G_CALLBACK(image_loader_size_cb), il);
-       g_signal_connect(G_OBJECT(il->loader), "area_prepared",
-                        G_CALLBACK(image_loader_area_prepared_cb), il);
+#ifdef HAVE_JPEG
+       if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
+               {
+               DEBUG_1("Using custom jpeg loader");
+               image_loader_backend_set_jpeg(&il->backend);
+               }
+       else
+#endif
+#ifdef HAVE_TIFF
+       if (il->bytes_total >= 10 &&
+           (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
+            memcmp(il->mapped_file, "II*\0", 4) == 0))
+               {
+               DEBUG_1("Using custom tiff loader");
+               image_loader_backend_set_tiff(&il->backend);
+               }
+       else
+#endif
+               image_loader_backend_set_default(&il->backend);
+
+       il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
        g_mutex_unlock(il->data_mutex);
 }
 
@@ -534,7 +640,7 @@ static gboolean image_loader_continue(ImageLoader *il)
                        return FALSE;
                        }
 
-               if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
+               if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
                        {
                        image_loader_error(il);
                        return FALSE;
@@ -563,8 +669,17 @@ static gboolean image_loader_begin(ImageLoader *il)
        if (b < 1) return FALSE;
 
        image_loader_setup_loader(il);
-
-       if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
+       
+       g_assert(il->bytes_read == 0);
+       if (il->backend.load) {
+               b = il->bytes_total;
+               if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
+                       {
+                       image_loader_stop_loader(il);
+                       return FALSE;
+                       }
+       }
+       else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
                {
                image_loader_stop_loader(il);
                return FALSE;
@@ -573,10 +688,10 @@ static gboolean image_loader_begin(ImageLoader *il)
        il->bytes_read += b;
 
        /* read until size is known */
-       while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
+       while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
                {
                b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
-               if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
+               if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
                        {
                        image_loader_stop_loader(il);
                        return FALSE;
@@ -810,7 +925,7 @@ static void image_loader_thread_leave_high(void)
 static void image_loader_thread_wait_high(void)
 {
        g_mutex_lock(image_loader_prio_mutex);
-       while (image_loader_prio_num) 
+       while (image_loader_prio_num)
                {
                g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
                }
@@ -825,7 +940,7 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
        gboolean cont;
        gboolean err;
        
-       if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
+       if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
                {
                /* low prio, wait untill high prio tasks finishes */
                image_loader_thread_wait_high();
@@ -840,8 +955,8 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
        
        if (err)
                {
-               /* 
-               loader failed, we have to send signal 
+               /*
+               loader failed, we have to send signal
                (idle mode returns the image_loader_begin return value directly)
                (success is always reported indirectly from image_loader_begin)
                */
@@ -852,7 +967,7 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
        
        while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
                {
-               if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
+               if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
                        {
                        /* low prio, wait untill high prio tasks finishes */
                        image_loader_thread_wait_high();
@@ -861,7 +976,7 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
                }
        image_loader_stop_loader(il);
 
-       if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE) 
+       if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
                {
                /* high prio */
                image_loader_thread_leave_high();
@@ -885,7 +1000,7 @@ static gboolean image_loader_start_thread(ImageLoader *il)
        
        if (!image_loader_setup_source(il)) return FALSE;
 
-        if (!image_loader_thread_pool) 
+        if (!image_loader_thread_pool)
                {
                image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
                image_loader_prio_cond = g_cond_new();
@@ -934,16 +1049,12 @@ GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
 
 gchar *image_loader_get_format(ImageLoader *il)
 {
-       GdkPixbufFormat *format;
        gchar **mimev;
        gchar *mime;
 
        if (!il || !il->loader) return NULL;
 
-       format = gdk_pixbuf_loader_get_format(il->loader);
-       if (!format) return NULL;
-
-       mimev = gdk_pixbuf_format_get_mime_types(format);
+       mimev = il->backend.get_format_mime_types(il->loader);
        if (!mimev) return NULL;
 
        /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
@@ -987,7 +1098,7 @@ gdouble image_loader_get_percent(ImageLoader *il)
        if (!il) return 0.0;
        
        g_mutex_lock(il->data_mutex);
-       if (il->bytes_total == 0) 
+       if (il->bytes_total == 0)
                {
                ret = 0.0;
                }