Replace single value enums with constexpr <type>
[geeqie.git] / src / image-load.cc
index e559b3d..4e86ebb 100644 (file)
  * 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 <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <cstring>
+
+#include <config.h>
+
+#include "debug.h"
+#include "exif.h"
+#include "filedata.h"
+#include "gq-marshal.h"
+#include "image-load-collection.h"
 #include "image-load-dds.h"
-#include "image-load-djvu.h"
+#if HAVE_DJVU
+#  include "image-load-djvu.h"
+#endif
 #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-zxscr.h"
-#include "image-load-j2k.h"
-#include "image-load-jpegxl.h"
+#if HAVE_FFMPEGTHUMBNAILER
+#  include "image-load-ffmpegthumbnailer.h"
+#endif
+#include "image-load-gdk.h"
+#if HAVE_HEIF
+#  include "image-load-heif.h"
+#endif
+#if HAVE_J2K
+#  include "image-load-j2k.h"
+#endif
+#if HAVE_JPEG
+#  if !HAVE_RAW
+#    include "image-load-cr3.h"
+#  endif
+#  include "image-load-jpeg.h"
+#endif
+#if HAVE_JPEGXL
+#  include "image-load-jpegxl.h"
+#endif
 #include "image-load-libraw.h"
+#if HAVE_PDF
+#  include "image-load-pdf.h"
+#endif
+#include "image-load-psd.h"
 #include "image-load-svgz.h"
+#if HAVE_TIFF
+#  include "image-load-tiff.h"
+#endif
+#include "image-load-webp.h"
+#include "image-load-zxscr.h"
 #include "misc.h"
-
-#include "exif.h"
-#include "filedata.h"
+#include "options.h"
+#include "typedefs.h"
 #include "ui-fileops.h"
-#include "gq-marshal.h"
 
-#include <fcntl.h>
-#include <sys/mman.h>
+struct ExifData;
 
-#define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT  4096
-#define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT      1
+enum {
+       IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT =         4096,
+       IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT =     1
+};
 
 /* image loader class */
 
@@ -88,12 +119,12 @@ GType image_loader_get_type()
                        static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
                        nullptr /* value_table */
                        };
-               type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, GTypeFlags(0));
+               type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
                }
        return type;
 }
 
-static void image_loader_init(GTypeInstance *instance, gpointer UNUSED(g_class))
+static void image_loader_init(GTypeInstance *instance, gpointer)
 {
        auto il = reinterpret_cast<ImageLoader *>(instance);
 
@@ -101,7 +132,7 @@ static void image_loader_init(GTypeInstance *instance, gpointer UNUSED(g_class))
        il->idle_id = 0;
        il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
        il->done = FALSE;
-       il->loader = nullptr;
+       il->backend.reset(nullptr);
 
        il->bytes_read = 0;
        il->bytes_total = 0;
@@ -129,7 +160,7 @@ static void image_loader_init(GTypeInstance *instance, gpointer UNUSED(g_class))
        DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
 }
 
-static void image_loader_class_init_wrapper(void *data, void *UNUSED(user_data))
+static void image_loader_class_init_wrapper(void *data, void *)
 {
        image_loader_class_init(static_cast<ImageLoaderClass *>(data));
 }
@@ -138,9 +169,6 @@ static void image_loader_class_init(ImageLoaderClass *loader_class)
 {
        GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
 
-//     gobject_class->set_property = image_loader_set_property;
-//     gobject_class->get_property = image_loader_get_property;
-
        gobject_class->finalize = image_loader_finalize;
 
 
@@ -281,7 +309,10 @@ static gboolean image_loader_emit_area_ready_cb(gpointer data)
 {
        auto par = static_cast<ImageLoaderAreaParam *>(data);
        ImageLoader *il = par->il;
-       guint x, y, w, h;
+       guint x;
+       guint y;
+       guint w;
+       guint h;
        g_mutex_lock(il->data_mutex);
        il->area_param_list = g_list_remove(il->area_param_list, par);
        x = par->x;
@@ -319,7 +350,8 @@ static gboolean image_loader_emit_percent_cb(gpointer data)
 
 static gboolean image_loader_emit_size_cb(gpointer data)
 {
-       gint width, height;
+       gint width;
+       gint height;
        auto il = static_cast<ImageLoader *>(data);
        g_mutex_lock(il->data_mutex);
        width = il->actual_width;
@@ -443,13 +475,13 @@ static void image_loader_sync_pixbuf(ImageLoader *il)
 
        g_mutex_lock(il->data_mutex);
 
-       if (!il->loader)
+       if (!il->backend)
                {
                g_mutex_unlock(il->data_mutex);
                return;
                }
 
-       pb = il->backend.get_pixbuf(il->loader);
+       pb = il->backend->get_pixbuf();
 
        if (pb == il->pixbuf)
                {
@@ -470,7 +502,7 @@ static void image_loader_sync_pixbuf(ImageLoader *il)
        g_mutex_unlock(il->data_mutex);
 }
 
-static void image_loader_area_updated_cb(gpointer UNUSED(loader),
+static void image_loader_area_updated_cb(gpointer,
                                 guint x, guint y, guint w, guint h,
                                 gpointer data)
 {
@@ -491,23 +523,24 @@ static void image_loader_area_updated_cb(gpointer UNUSED(loader),
        else
                image_loader_emit_area_ready(il, x, y, w, h);
 
-       if (il->stopping) il->backend.abort(il->loader);
+       if (il->stopping) il->backend->abort();
 
        g_mutex_unlock(il->data_mutex);
 }
 
-static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
+static void image_loader_area_prepared_cb(gpointer, gpointer data)
 {
        auto il = static_cast<ImageLoader *>(data);
        GdkPixbuf *pb;
        guchar *pix;
-       size_t h, rs;
+       size_t h;
+       size_t rs;
 
        /* a workaround for
           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);
+       gchar *format = il->backend->get_format_name();
        if (strcmp(format, "svg") == 0 ||
            strcmp(format, "xpm") == 0)
                {
@@ -517,7 +550,7 @@ static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
 
        g_free(format);
 
-       pb = il->backend.get_pixbuf(loader);
+       pb = il->backend->get_pixbuf();
 
        h = gdk_pixbuf_get_height(pb);
        rs = gdk_pixbuf_get_rowstride(pb);
@@ -527,7 +560,7 @@ static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
 
 }
 
-static void image_loader_size_cb(gpointer loader,
+static void image_loader_size_cb(gpointer,
                                 gint width, gint height, gpointer data)
 {
        auto il = static_cast<ImageLoader *>(data);
@@ -546,11 +579,11 @@ static void image_loader_size_cb(gpointer loader,
                }
        g_mutex_unlock(il->data_mutex);
 
-#ifdef HAVE_FFMPEGTHUMBNAILER
+#if HAVE_FFMPEGTHUMBNAILER
        if (il->fd->format_class == FORMAT_CLASS_VIDEO)
                scale = TRUE;
 #endif
-       mime_types = il->backend.get_format_mime_types(loader);
+       mime_types = il->backend->get_format_mime_types();
        n = 0;
        while (mime_types[n] && !scale)
                {
@@ -567,7 +600,8 @@ static void image_loader_size_cb(gpointer loader,
 
        g_mutex_lock(il->data_mutex);
 
-       gint nw, nh;
+       gint nw;
+       gint nh;
        if (width > il->requested_width || height > il->requested_height)
                {
 
@@ -586,7 +620,7 @@ static void image_loader_size_cb(gpointer loader,
 
                il->actual_width = nw;
                il->actual_height = nh;
-               il->backend.set_size(loader, nw, nh);
+               il->backend->set_size(nw, nh);
                il->shrunk = TRUE;
                }
 
@@ -598,13 +632,12 @@ static void image_loader_stop_loader(ImageLoader *il)
 {
        if (!il) return;
 
-       if (il->loader)
+       if (il->backend)
                {
                /* some loaders do not have a pixbuf till close, order is important here */
-               il->backend.close(il->loader, il->error ? nullptr : &il->error); /* we are interested in the first error only */
+               il->backend->close(il->error ? nullptr : &il->error); /* we are interested in the first error only */
                image_loader_sync_pixbuf(il);
-               il->backend.free(il->loader);
-               il->loader = nullptr;
+               il->backend.reset(nullptr);
                }
        g_mutex_lock(il->data_mutex);
        il->done = TRUE;
@@ -613,10 +646,6 @@ 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);
@@ -638,178 +667,149 @@ static void image_loader_setup_loader(ImageLoader *il)
        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);
+               il->backend = get_image_loader_backend_external();
                }
        else
-#endif
-#ifdef HAVE_HEIF
-       if (il->bytes_total >= 12 &&
-               ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
-               (memcmp(il->mapped_file + 4, "ftypheix", 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
+#if HAVE_FFMPEGTHUMBNAILER
+               if (il->fd->format_class == FORMAT_CLASS_VIDEO)
+                       {
+                       DEBUG_1("Using custom ffmpegthumbnailer loader");
+                       il->backend = get_image_loader_backend_ft();
+                       }
+               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
+#if HAVE_PDF
+               if (il->bytes_total >= 4 &&
+                   (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
+                       {
+                       DEBUG_1("Using custom pdf loader");
+                       il->backend = get_image_loader_backend_pdf();
+                       }
+               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);
-               }
+#if HAVE_HEIF
+               if (il->bytes_total >= 12 &&
+                   ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
+                    (memcmp(il->mapped_file + 4, "ftypheix", 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");
+                       il->backend = get_image_loader_backend_heif();
+                       }
+               else
+       #endif
+       #if 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");
+                       il->backend = get_image_loader_backend_webp();
+                       }
+               else
 #endif
-       else
+#if 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");
+                       il->backend = get_image_loader_backend_djvu();
+                       }
+               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");
-               image_loader_backend_set_tiff(&il->backend);
-               }
-       else
+#if HAVE_JPEG
+               if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
+                       {
+                       DEBUG_1("Using custom jpeg loader");
+                       il->backend = get_image_loader_backend_jpeg();
+                       }
+#if !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");
+                       il->backend = get_image_loader_backend_cr3();
+                       }
 #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
+               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
+#if 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");
+                       il->backend = get_image_loader_backend_tiff();
+                       }
+               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);
-
-       il->loader = static_cast<void **>(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);
+               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");
+                       il->backend = get_image_loader_backend_dds();
+                       }
+               else
+               if (il->bytes_total >= 6 &&
+                       (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
+                       {
+                       DEBUG_1("Using custom psd loader");
+                       il->backend = get_image_loader_backend_psd();
+                       }
+               else
+#if 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");
+                       il->backend = get_image_loader_backend_j2k();
+                       }
+               else
 #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);
+#if 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)) ||
+                   (il->bytes_total >= 2 &&
+                    (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0)))
+                       {
+                       DEBUG_1("Using custom jpeg xl loader");
+                       il->backend = get_image_loader_backend_jpegxl();
+                       }
+               else
 #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);
+               if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
+                       (file_extension_match(il->fd->path, ".scr")))
+                       {
+                       DEBUG_1("Using custom zxscr loader");
+                       il->backend = get_image_loader_backend_zxscr();
+                       }
+               else
+               if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
+                       {
+                       DEBUG_1("Using custom collection loader");
+                       il->backend = get_image_loader_backend_collection();
+                       }
+               else
+               if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
+                       {
+                       DEBUG_1("Using custom svgz loader");
+                       il->backend = get_image_loader_backend_svgz();
+                       }
+               else
+                       il->backend = get_image_loader_backend_default();
                }
-       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
+       il->backend->init(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
+       il->backend->set_page_num(il->fd->page_num);
 
-       il->fd->format_name = il->backend.get_format_name(il->loader);
+       il->fd->format_name = il->backend->get_format_name();
 
        g_mutex_unlock(il->data_mutex);
 }
@@ -833,7 +833,6 @@ static void image_loader_error(ImageLoader *il)
 
 static gboolean image_loader_continue(ImageLoader *il)
 {
-       gint b;
        gint c;
 
        if (!il) return G_SOURCE_REMOVE;
@@ -841,15 +840,21 @@ static gboolean image_loader_continue(ImageLoader *il)
        c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
        while (c > 0 && !image_loader_get_stopping(il))
                {
-               b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
-
-               if (b == 0)
+               if (il->bytes_total == il->bytes_read)
                        {
                        image_loader_done(il);
                        return G_SOURCE_REMOVE;
                        }
 
-               if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
+               if (il->bytes_total < il->bytes_read)
+                       {
+                       image_loader_error(il);
+                       return G_SOURCE_REMOVE;
+                       }
+
+               gsize b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
+
+               if (!il->backend->write(il->mapped_file + il->bytes_read, b, il->bytes_total, &il->error))
                        {
                        image_loader_error(il);
                        return G_SOURCE_REMOVE;
@@ -870,77 +875,36 @@ 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;
+       if (il->bytes_total <= il->bytes_read) return FALSE;
+
+       gsize b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
 
        image_loader_setup_loader(il);
 
        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))
+       if (!il->backend->write(il->mapped_file, b, il->bytes_total, &il->error))
                {
                image_loader_stop_loader(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
+       file_data_set_page_total(il->fd, il->backend->get_page_total());
 
        il->bytes_read += b;
 
        /* read until size is known */
-       while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
+       while (il->backend && !il->backend->get_pixbuf() && b > 0 && !image_loader_get_stopping(il))
                {
+               if (il->bytes_total < il->bytes_read)
+                       {
+                       image_loader_stop_loader(il);
+                       return FALSE;
+                       }
+
                b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
-               if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
+               if (b > 0 && !il->backend->write(il->mapped_file + il->bytes_read, b, il->bytes_total, &il->error))
                        {
                        image_loader_stop_loader(il);
                        return FALSE;
@@ -978,7 +942,7 @@ static gboolean image_loader_setup_source(ImageLoader *il)
        struct stat st;
        gchar *pathl;
 
-       if (!il || il->loader || il->mapped_file) return FALSE;
+       if (!il || il->backend || il->mapped_file) return FALSE;
 
        il->mapped_file = nullptr;
 
@@ -1003,7 +967,7 @@ static gboolean image_loader_setup_source(ImageLoader *il)
                                {
                                /* 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))
+                               if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
                                        {
                                        il->mapped_file = nullptr;
                                        }
@@ -1023,7 +987,7 @@ static gboolean image_loader_setup_source(ImageLoader *il)
                                {
                                /* 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))
+                               if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
                                        {
                                        il->mapped_file = nullptr;
                                        }
@@ -1128,7 +1092,8 @@ void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
        if (!enable)
                {
                /* send delayed */
-               GList *list, *work;
+               GList *list;
+               GList *work;
                list = g_list_reverse(il->area_param_delayed_list);
                il->area_param_delayed_list = nullptr;
                g_mutex_unlock(il->data_mutex);
@@ -1141,9 +1106,8 @@ void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
                        work = work->next;
 
                        g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
-                       g_free(par);
                        }
-               g_list_free(list);
+               g_list_free_full(list, g_free);
                }
        else
                {
@@ -1227,7 +1191,7 @@ static void image_loader_thread_wait_high()
 }
 
 
-static void image_loader_thread_run(gpointer data, gpointer UNUSED(user_data))
+static void image_loader_thread_run(gpointer data, gpointer)
 {
        auto il = static_cast<ImageLoader *>(data);
        gboolean cont;