Fix #941: Update links from http: to https: where possible
[geeqie.git] / src / image-load.c
index 8cee6e7..a26929d 100644 (file)
 
 #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_zxscr.h"
+#include "image_load_j2k.h"
+#include "image_load_jpegxl.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 */
 
@@ -60,6 +130,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);
@@ -73,7 +144,7 @@ GType image_loader_get_type(void)
                        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),
@@ -104,6 +175,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;
@@ -127,6 +199,11 @@ static void image_loader_init(GTypeInstance *instance, gpointer g_class)
        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);
@@ -330,7 +407,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
 */
 
@@ -504,8 +581,8 @@ static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
        size_t h, rs;
 
        /* a workaround for
-          http://bugzilla.gnome.org/show_bug.cgi?id=547669
-          http://bugzilla.gnome.org/show_bug.cgi?id=589334
+          https://bugzilla.gnome.org/show_bug.cgi?id=547669
+          https://bugzilla.gnome.org/show_bug.cgi?id=589334
        */
        gchar *format = il->backend.get_format_name(loader);
        if (strcmp(format, "svg") == 0 ||
@@ -613,9 +690,35 @@ 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)
                {
@@ -634,8 +737,11 @@ static void image_loader_setup_loader(ImageLoader *il)
        else
 #endif
 #ifdef HAVE_HEIF
-       if (il->bytes_total >= 10 &&
-           (memcmp(il->mapped_file + 4, "ftypheic", 8) == 0))
+       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);
@@ -668,6 +774,16 @@ static void image_loader_setup_loader(ImageLoader *il)
                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
@@ -688,11 +804,56 @@ static void image_loader_setup_loader(ImageLoader *il)
                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
+#ifdef HAVE_JPEGXL
+       if (il->bytes_total >= 12 &&
+               (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
+               {
+               DEBUG_1("Using custom jpeg xl loader");
+               image_loader_backend_set_jpegxl(&il->backend);
+               }
+       else
+       if (il->bytes_total >= 2 &&
+               (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
+               {
+               DEBUG_1("Using custom jpeg xl loader");
+               image_loader_backend_set_jpegxl(&il->backend);
+               }
+       else
+#endif
+       if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
+               (file_extension_match(il->fd->path, ".scr")))
+               {
+               DEBUG_1("Using custom zxscr loader");
+               image_loader_backend_set_zxscr(&il->backend);
+               }
+       else
        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);
 
@@ -716,6 +877,15 @@ static void image_loader_setup_loader(ImageLoader *il)
        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)
@@ -725,6 +895,8 @@ static void image_loader_setup_loader(ImageLoader *il)
        g_free(format);
 #endif
 
+       il->fd->format_name = il->backend.get_format_name(il->loader);
+
        g_mutex_unlock(il->data_mutex);
 }
 
@@ -784,8 +956,10 @@ static gboolean image_loader_continue(ImageLoader *il)
 
 static gboolean image_loader_begin(ImageLoader *il)
 {
-       gssize b;
+#if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
        gchar *format;
+#endif
+       gssize b;
 
        if (il->pixbuf) return FALSE;
 
@@ -818,6 +992,15 @@ static gboolean image_loader_begin(ImageLoader *il)
                }
        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)
@@ -890,13 +1073,53 @@ 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);
@@ -930,7 +1153,7 @@ 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;
@@ -942,10 +1165,14 @@ static void image_loader_stop_source(ImageLoader *il)
 
        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);
@@ -1094,7 +1321,7 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
 
        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
@@ -1121,7 +1348,7 @@ static void image_loader_thread_run(gpointer data, gpointer user_data)
                {
                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);
@@ -1298,7 +1525,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;