Include a Other Software section in Help file
[geeqie.git] / src / image-load.c
index aaec9f9..07588cb 100644 (file)
@@ -1,21 +1,43 @@
 /*
- * Geeqie
- * (C) 2004 John Ellis
- * Copyright (C) 2008 - 2010 The Geeqie Team
+ * Copyright (C) 2004 John Ellis
+ * Copyright (C) 2008 - 2016 The Geeqie Team
  *
  * Author: John Ellis
  *
- * This software is released under the GNU General Public License (GNU GPL).
- * Please read the included file COPYING for more information.
- * This software comes with no warranty of any kind, use at your own risk!
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-
 #include "main.h"
 #include "image-load.h"
+#include "image_load_cr3.h"
 #include "image_load_gdk.h"
 #include "image_load_jpeg.h"
 #include "image_load_tiff.h"
+#include "image_load_dds.h"
+#include "image_load_djvu.h"
+#include "image_load_external.h"
+#include "image_load_pdf.h"
+#include "image_load_psd.h"
+#include "image_load_heif.h"
+#include "image_load_ffmpegthumbnailer.h"
+#include "image_load_collection.h"
+#include "image_load_webp.h"
+#include "image_load_j2k.h"
+#include "image_load_libraw.h"
+#include "image_load_svgz.h"
+#include "misc.h"
 
 #include "exif.h"
 #include "filedata.h"
 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT  4096
 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT      1
 
-
+/**
+ * @page diagrams Diagrams
+ * @section image_load_overview Image Load Overview
+ * @startuml
+ * object image_change
+ * object image_change_complete
+ * object image_load_begin
+ * object image_loader_start
+ * object image_loader_start_thread
+ * object image_loader_start_idle
+ * object image_loader_setup_source
+ * object image_loader_thread_run
+ * object image_loader_begin
+ * object image_loader_setuploader
+ * circle "il->memory_mapped"
+ * object exif_get_preview_
+ * object exif_get_preview
+ * object libraw_get_preview
+ * 
+ * image_change : image.c
+ * image_change_complete : image.c
+ * image_load_begin : image.c
+ * image_loader_start : image_load.c
+ * image_loader_start_thread : image_load.c
+ * image_loader_start_idle : image_load.c
+ * image_loader_thread_run : image_load.c
+ * image_loader_begin : image_load.c
+ * image_loader_setuploader : image_load.c
+ * image_loader_setuploader : -
+ * image_loader_setuploader : Select backend using magic
+ * image_loader_setup_source : image_load.c
+ * exif_get_preview : exiv2.cc
+ * exif_get_preview : EXIV2_TEST_VERSION(0,17,90)
+ * exif_get_preview_ : exif.c
+ * exif_get_preview_ : -
+ * exif_get_preview_ : If exiv2 not installed
+ * libraw_get_preview : image_load_libraw.c
+ * 
+ * image_change --> image_change_complete
+ * image_change_complete --> image_load_begin
+ * image_load_begin --> image_loader_start
+ * image_loader_start --> image_loader_start_thread
+ * image_loader_start --> image_loader_start_idle : Obsolete - no threads version
+ * image_loader_start_thread --> image_loader_thread_run
+ * image_loader_start_thread --> image_loader_setup_source
+ * image_loader_setup_source --> exif_get_preview_
+ * image_loader_setup_source --> exif_get_preview
+ * image_loader_setup_source --> libraw_get_preview : Try libraw if exiv2 fails
+ * exif_get_preview_ ..> "il->memory_mapped"
+ * exif_get_preview ..> "il->memory_mapped"
+ * libraw_get_preview ..> "il->memory_mapped"
+ * image_loader_thread_run --> image_loader_begin
+ * image_loader_begin --> image_loader_setuploader
+ * "il->memory_mapped" ..> image_loader_setuploader
+ * note left of "il->memory_mapped" : Points to first byte of embedded jpeg (#FFD8)\n if preview found, otherwise to first byte of file
+ * @enduml
+ */
+ /**
+  * @file
+  * @ref image_load_overview "Image Load Overview"
+  */
+  
 /**************************************************************************************/
 /* image loader class */
 
@@ -45,6 +128,7 @@ enum {
 static guint signals[SIGNAL_COUNT] = { 0 };
 
 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
+static void image_loader_class_init_wrapper(void *data, void *user_data);
 static void image_loader_class_init(ImageLoaderClass *class);
 static void image_loader_finalize(GObject *object);
 static void image_loader_stop(ImageLoader *il);
@@ -52,13 +136,13 @@ 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),
                        NULL,   /* base_init */
                        NULL,   /* base_finalize */
-                       (GClassInitFunc)image_loader_class_init, /* class_init */
+                       (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
                        NULL,   /* class_finalize */
                        NULL,   /* class_data */
                        sizeof(ImageLoader),
@@ -89,6 +173,7 @@ static void image_loader_init(GTypeInstance *instance, gpointer g_class)
        il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
        il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
        il->mapped_file = NULL;
+       il->preview = IMAGE_LOADER_PREVIEW_NONE;
 
        il->requested_width = 0;
        il->requested_height = 0;
@@ -99,12 +184,24 @@ static void image_loader_init(GTypeInstance *instance, gpointer g_class)
        il->can_destroy = TRUE;
 
 #ifdef HAVE_GTHREAD
+#if GLIB_CHECK_VERSION(2,32,0)
+       il->data_mutex = g_new(GMutex, 1);
+       g_mutex_init(il->data_mutex);
+       il->can_destroy_cond = g_new(GCond, 1);
+       g_cond_init(il->can_destroy_cond);
+#else
        il->data_mutex = g_mutex_new();
        il->can_destroy_cond = g_cond_new();
+#endif
 #endif
        DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
 }
 
+static void image_loader_class_init_wrapper(void *data, void *user_data)
+{
+       image_loader_class_init(data);
+}
+
 static void image_loader_class_init(ImageLoaderClass *class)
 {
        GObjectClass *gobject_class = G_OBJECT_CLASS (class);
@@ -185,12 +282,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)) {}
@@ -198,21 +295,28 @@ 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);
                }
 
        if (il->pixbuf) g_object_unref(il->pixbuf);
-       
+
        if (il->error) g_error_free(il->error);
 
        file_data_unref(il->fd);
 #ifdef HAVE_GTHREAD
+#if GLIB_CHECK_VERSION(2,32,0)
+       g_mutex_clear(il->data_mutex);
+       g_free(il->data_mutex);
+       g_cond_clear(il->can_destroy_cond);
+       g_free(il->can_destroy_cond);
+#else
        g_mutex_free(il->data_mutex);
        g_cond_free(il->can_destroy_cond);
 #endif
+#endif
 }
 
 void image_loader_free(ImageLoader *il)
@@ -229,9 +333,9 @@ ImageLoader *image_loader_new(FileData *fd)
        if (!fd) return NULL;
 
        il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
-       
+
        il->fd = file_data_ref(fd);
-       
+
        return il;
 }
 
@@ -252,12 +356,18 @@ 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;
 }
 
@@ -295,7 +405,7 @@ static gboolean image_loader_emit_size_cb(gpointer data)
 }
 
 
-/* DONE and ERROR are emited only once, thus they can have normal priority
+/* DONE and ERROR are emitted only once, thus they can have normal priority
    PERCENT and AREA_READY should be processed ASAP
 */
 
@@ -319,19 +429,63 @@ static void image_loader_emit_size(ImageLoader *il)
        g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_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 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;
        par->y = y;
        par->w = w;
        par->h = h;
-       
-       il->area_param_list = g_list_prepend(il->area_param_list, par);
-       
-       g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
+
+       *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);
+
+       if (par)
+               {
+               g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
+               }
 }
 
 /**************************************************************************************/
@@ -340,14 +494,7 @@ static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guin
 /* this function expects that il->data_mutex is locked by caller */
 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);
 }
 
 
@@ -368,10 +515,10 @@ static gboolean image_loader_get_stopping(ImageLoader *il)
 static void image_loader_sync_pixbuf(ImageLoader *il)
 {
        GdkPixbuf *pb;
-       
+
        g_mutex_lock(il->data_mutex);
-       
-       if (!il->loader) 
+
+       if (!il->loader)
                {
                g_mutex_unlock(il->data_mutex);
                return;
@@ -418,7 +565,7 @@ static void image_loader_area_updated_cb(gpointer loader,
                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);
@@ -430,9 +577,9 @@ static void image_loader_area_prepared_cb(gpointer loader, gpointer 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 = il->backend.get_format_name(loader);
@@ -442,15 +589,15 @@ static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
                g_free(format);
                return;
                }
-       
+
        g_free(format);
 
        pb = il->backend.get_pixbuf(loader);
-       
+
        h = gdk_pixbuf_get_height(pb);
        rs = gdk_pixbuf_get_rowstride(pb);
        pix = gdk_pixbuf_get_pixels(pb);
-       
+
        memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
 
 }
@@ -466,7 +613,7 @@ static void image_loader_size_cb(gpointer loader,
        g_mutex_lock(il->data_mutex);
        il->actual_width = width;
        il->actual_height = height;
-       if (il->requested_width < 1 || il->requested_height < 1) 
+       if (il->requested_width < 1 || il->requested_height < 1)
                {
                g_mutex_unlock(il->data_mutex);
                image_loader_emit_size(il);
@@ -474,9 +621,13 @@ static void image_loader_size_cb(gpointer loader,
                }
        g_mutex_unlock(il->data_mutex);
 
+#ifdef HAVE_FFMPEGTHUMBNAILER
+       if (il->fd->format_class == FORMAT_CLASS_VIDEO)
+               scale = TRUE;
+#endif
        mime_types = il->backend.get_format_mime_types(loader);
        n = 0;
-       while (mime_types[n])
+       while (mime_types[n] && !scale)
                {
                if (strstr(mime_types[n], "jpeg")) scale = TRUE;
                n++;
@@ -537,18 +688,107 @@ static void image_loader_stop_loader(ImageLoader *il)
 
 static void image_loader_setup_loader(ImageLoader *il)
 {
+#if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
+       gchar *format;
+#endif
+
+       gint external_preview = 1;
+
        g_mutex_lock(il->data_mutex);
+
+       if (options->external_preview.enable)
+               {
+               gchar *cmd_line;
+               gchar *tilde_filename;
+
+               tilde_filename = expand_tilde(options->external_preview.select);
+
+               cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
+
+               external_preview = runcmd(cmd_line);
+               g_free(cmd_line);
+               g_free(tilde_filename);
+               }
+
+       if (external_preview == 0)
+               {
+               DEBUG_1("Using custom external loader");
+               image_loader_backend_set_external(&il->backend);
+               }
+       else
+
+#ifdef HAVE_FFMPEGTHUMBNAILER
+       if (il->fd->format_class == FORMAT_CLASS_VIDEO)
+               {
+               DEBUG_1("Using custom ffmpegthumbnailer loader");
+               image_loader_backend_set_ft(&il->backend);
+               }
+       else
+#endif
+#ifdef HAVE_PDF
+       if (il->bytes_total >= 4 &&
+           (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
+               {
+               DEBUG_1("Using custom pdf loader");
+               image_loader_backend_set_pdf(&il->backend);
+               }
+       else
+#endif
+#ifdef HAVE_HEIF
+       if (il->bytes_total >= 12 &&
+               ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
+               (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
+               (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
+               (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
+               {
+               DEBUG_1("Using custom heif loader");
+               image_loader_backend_set_heif(&il->backend);
+               }
+       else
+#endif
+#ifdef HAVE_WEBP
+       if (il->bytes_total >= 12 &&
+               (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
+               (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
+               {
+               DEBUG_1("Using custom webp loader");
+               image_loader_backend_set_webp(&il->backend);
+               }
+       else
+#endif
+#ifdef HAVE_DJVU
+       if (il->bytes_total >= 16 &&
+               (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
+               (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
+               {
+               DEBUG_1("Using custom djvu loader");
+               image_loader_backend_set_djvu(&il->backend);
+               }
+       else
+#endif
 #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);
                }
+#ifndef HAVE_RAW
+       else
+       if (il->bytes_total >= 11 &&
+               (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
+               (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
+               {
+               DEBUG_1("Using custom cr3 loader");
+               image_loader_backend_set_cr3(&il->backend);
+               }
+#endif
        else
 #endif
 #ifdef HAVE_TIFF
        if (il->bytes_total >= 10 &&
            (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
+            memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
+            memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
             memcmp(il->mapped_file, "II*\0", 4) == 0))
                {
                DEBUG_1("Using custom tiff loader");
@@ -556,9 +796,80 @@ static void image_loader_setup_loader(ImageLoader *il)
                }
        else
 #endif
+       if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
+               {
+               DEBUG_1("Using dds loader");
+               image_loader_backend_set_dds(&il->backend);
+               }
+       else
+       if (il->bytes_total >= 6 &&
+               (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
+               {
+               DEBUG_1("Using custom psd loader");
+               image_loader_backend_set_psd(&il->backend);
+               }
+       else
+#ifdef HAVE_J2K
+       if (il->bytes_total >= 12 &&
+               (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
+               {
+               DEBUG_1("Using custom j2k loader");
+               image_loader_backend_set_j2k(&il->backend);
+               }
+       else
+#endif
+       if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
+               {
+               DEBUG_1("Using custom collection loader");
+               image_loader_backend_set_collection(&il->backend);
+               }
+       else
+       if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
+               {
+               DEBUG_1("Using custom svgz loader");
+               image_loader_backend_set_svgz(&il->backend);
+               }
+       else
                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);
+
+#ifdef HAVE_TIFF
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "tiff") == 0)
+               {
+               il->backend.set_page_num(il->loader, il->fd->page_num);
+               }
+       g_free(format);
+#endif
+
+#ifdef HAVE_PDF
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "pdf") == 0)
+               {
+               il->backend.set_page_num(il->loader, il->fd->page_num);
+               }
+       g_free(format);
+#endif
+
+#ifdef HAVE_HEIF
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "heif") == 0)
+               {
+               il->backend.set_page_num(il->loader, il->fd->page_num);
+               }
+       g_free(format);
+#endif
+
+#ifdef HAVE_DJVU
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "djvu") == 0)
+               {
+               il->backend.set_page_num(il->loader, il->fd->page_num);
+               }
+       g_free(format);
+#endif
+
        g_mutex_unlock(il->data_mutex);
 }
 
@@ -618,15 +929,18 @@ static gboolean image_loader_continue(ImageLoader *il)
 
 static gboolean image_loader_begin(ImageLoader *il)
 {
+#if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
+       gchar *format;
+#endif
        gssize b;
-       
+
        if (il->pixbuf) return FALSE;
 
        b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
        if (b < 1) return FALSE;
 
        image_loader_setup_loader(il);
-       
+
        g_assert(il->bytes_read == 0);
        if (il->backend.load) {
                b = il->bytes_total;
@@ -642,6 +956,43 @@ static gboolean image_loader_begin(ImageLoader *il)
                return FALSE;
                }
 
+#ifdef HAVE_PDF
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "pdf") == 0)
+               {
+               gint i = il->backend.get_page_total(il->loader);
+               file_data_set_page_total(il->fd, i);
+               }
+       g_free(format);
+#endif
+#ifdef HAVE_HEIF
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "heif") == 0)
+               {
+               gint i = il->backend.get_page_total(il->loader);
+               file_data_set_page_total(il->fd, i);
+               }
+       g_free(format);
+#endif
+#ifdef HAVE_DJVU
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "djvu") == 0)
+               {
+               gint i = il->backend.get_page_total(il->loader);
+               file_data_set_page_total(il->fd, i);
+               }
+       g_free(format);
+#endif
+#ifdef HAVE_TIFF
+       format = il->backend.get_format_name(il->loader);
+       if (g_strcmp0(format, "tiff") == 0)
+               {
+               gint i = il->backend.get_page_total(il->loader);
+               file_data_set_page_total(il->fd, i);
+               }
+       g_free(format);
+#endif
+
        il->bytes_read += b;
 
        /* read until size is known */
@@ -695,24 +1046,64 @@ static gboolean image_loader_setup_source(ImageLoader *il)
                ExifData *exif = exif_read_fd(il->fd);
 
                if (options->thumbnails.use_exif)
+                       {
                        il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
+
+                       if (il->mapped_file)
+                               {
+                               il->preview = IMAGE_LOADER_PREVIEW_EXIF;
+                               }
+                       }
                else
+                       {
                        il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
 
+                       if (il->mapped_file)
+                               {
+                               /* Both exiv2 and libraw sometimes return a pointer to a file
+                                * section that is not a jpeg */
+                               if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
+                                       {
+                                       il->mapped_file = NULL;
+                                       }
+                               else
+                                       {
+                                       il->preview = IMAGE_LOADER_PREVIEW_EXIF;
+                                       }
+                               }
+                       }
+
+               /* If exiv2 does not find a thumbnail, try libraw (which may be slower) */
+               if (!il->mapped_file)
+                       {
+                       il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
+
+                       if (il->mapped_file)
+                               {
+                               if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
+                                       {
+                                       il->mapped_file = NULL;
+                                       }
+                               else
+                                       {
+                                       il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
+                                       }
+                               }
+                       }
+
                if (il->mapped_file)
                        {
-                       il->preview = TRUE;
                        DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
                        }
                exif_free_fd(il->fd, exif);
                }
 
-       
+
        if (!il->mapped_file)
                {
                /* normal file */
                gint load_fd;
-       
+
                pathl = path_from_utf8(il->fd->path);
                load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
                g_free(pathl);
@@ -727,7 +1118,7 @@ static gboolean image_loader_setup_source(ImageLoader *il)
                        close(load_fd);
                        return FALSE;
                        }
-               
+
                il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
                close(load_fd);
                if (il->mapped_file == MAP_FAILED)
@@ -735,22 +1126,26 @@ static gboolean image_loader_setup_source(ImageLoader *il)
                        il->mapped_file = 0;
                        return FALSE;
                        }
-               il->preview = FALSE;
+               il->preview = IMAGE_LOADER_PREVIEW_NONE;
                }
-               
+
        return TRUE;
 }
 
 static void image_loader_stop_source(ImageLoader *il)
 {
        if (!il) return;
-       
+
        if (il->mapped_file)
                {
-               if (il->preview)
+               if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
                        {
                        exif_free_preview(il->mapped_file);
                        }
+               else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
+                       {
+                       libraw_free_preview(il->mapped_file);
+                       }
                else
                        {
                        munmap(il->mapped_file, il->bytes_total);
@@ -768,7 +1163,7 @@ static void image_loader_stop(ImageLoader *il)
                g_source_remove(il->idle_id);
                il->idle_id = 0;
                }
-               
+
        if (il->thread)
                {
                /* stop loader in the other thread */
@@ -780,7 +1175,7 @@ static void image_loader_stop(ImageLoader *il)
 
        image_loader_stop_loader(il);
        image_loader_stop_source(il);
-       
+
 }
 
 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
@@ -801,7 +1196,7 @@ void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
                        {
                        ImageLoaderAreaParam *par = work->data;
                        work = work->next;
-                       
+
                        g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
                        g_free(par);
                        }
@@ -827,12 +1222,12 @@ static gboolean image_loader_idle_cb(gpointer data)
                {
                ret = image_loader_continue(il);
                }
-       
+
        if (!ret)
                {
                image_loader_stop_source(il);
                }
-       
+
        return ret;
 }
 
@@ -840,13 +1235,13 @@ static gboolean image_loader_idle_cb(gpointer data)
 static gboolean image_loader_start_idle(ImageLoader *il)
 {
        gboolean ret;
-       
+
        if (!il) return FALSE;
 
        if (!il->fd) return FALSE;
 
        if (!image_loader_setup_source(il)) return FALSE;
-       
+
        ret = image_loader_begin(il);
 
        if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
@@ -882,7 +1277,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);
                }
@@ -896,10 +1291,10 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
        ImageLoader *il = 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 */
+               /* low prio, wait until high prio tasks finishes */
                image_loader_thread_wait_high();
                }
        else
@@ -907,33 +1302,33 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
                /* high prio */
                image_loader_thread_enter_high();
                }
-       
+
        err = !image_loader_begin(il);
-       
+
        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)
                */
                image_loader_emit_error(il);
                }
-       
+
        cont = !err;
-       
+
        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 */
+                       /* low prio, wait until high prio tasks finishes */
                        image_loader_thread_wait_high();
                        }
                cont = image_loader_continue(il);
                }
        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();
@@ -954,21 +1349,28 @@ static gboolean image_loader_start_thread(ImageLoader *il)
        if (!il->fd) return FALSE;
 
        il->thread = TRUE;
-       
+
        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);
+#if GLIB_CHECK_VERSION(2,32,0)
+               if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
+               g_cond_init(image_loader_prio_cond);
+               if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
+               g_mutex_init(image_loader_prio_mutex);
+#else
                image_loader_prio_cond = g_cond_new();
                image_loader_prio_mutex = g_mutex_new();
+#endif
                }
 
        il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
 
        g_thread_pool_push(image_loader_thread_pool, il, NULL);
        DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
-               
+
        return TRUE;
 }
 #endif /* HAVE_GTHREAD */
@@ -997,30 +1399,13 @@ GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
 {
        GdkPixbuf *ret;
        if (!il) return NULL;
-       
+
        g_mutex_lock(il->data_mutex);
        ret = il->pixbuf;
        g_mutex_unlock(il->data_mutex);
        return ret;
 }
 
-gchar *image_loader_get_format(ImageLoader *il)
-{
-       gchar **mimev;
-       gchar *mime;
-
-       if (!il || !il->loader) return NULL;
-
-       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 ? */
-       mime = g_strdup(mimev[0]);
-       g_strfreev(mimev);
-
-       return mime;
-}
-
 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
 {
        if (!il) return;
@@ -1053,9 +1438,9 @@ gdouble image_loader_get_percent(ImageLoader *il)
 {
        gdouble ret;
        if (!il) return 0.0;
-       
+
        g_mutex_lock(il->data_mutex);
-       if (il->bytes_total == 0) 
+       if (il->bytes_total == 0)
                {
                ret = 0.0;
                }
@@ -1113,7 +1498,9 @@ const gchar *image_loader_get_error(ImageLoader *il)
 }
 
 
-/* FIXME - this can be rather slow and blocks until the size is known */
+/**
+ *  @FIXME this can be rather slow and blocks until the size is known
+ */
 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
 {
        ImageLoader *il;