Let image loader backend decide how to process image buffer
[geeqie.git] / src / image-load.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "image-load.h"
23
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28
29 #include <cstring>
30
31 #include <config.h>
32
33 #include "debug.h"
34 #include "exif.h"
35 #include "filedata.h"
36 #include "gq-marshal.h"
37 #include "image-load-collection.h"
38 #include "image-load-dds.h"
39 #if HAVE_DJVU
40 #  include "image-load-djvu.h"
41 #endif
42 #include "image-load-external.h"
43 #if HAVE_FFMPEGTHUMBNAILER
44 #  include "image-load-ffmpegthumbnailer.h"
45 #endif
46 #include "image-load-gdk.h"
47 #if HAVE_HEIF
48 #  include "image-load-heif.h"
49 #endif
50 #if HAVE_J2K
51 #  include "image-load-j2k.h"
52 #endif
53 #if HAVE_JPEG
54 #  if !HAVE_RAW
55 #    include "image-load-cr3.h"
56 #  endif
57 #  include "image-load-jpeg.h"
58 #endif
59 #if HAVE_JPEGXL
60 #  include "image-load-jpegxl.h"
61 #endif
62 #include "image-load-libraw.h"
63 #if HAVE_PDF
64 #  include "image-load-pdf.h"
65 #endif
66 #include "image-load-psd.h"
67 #include "image-load-svgz.h"
68 #if HAVE_TIFF
69 #  include "image-load-tiff.h"
70 #endif
71 #include "image-load-webp.h"
72 #include "image-load-zxscr.h"
73 #include "misc.h"
74 #include "options.h"
75 #include "typedefs.h"
76 #include "ui-fileops.h"
77
78 struct ExifData;
79
80 enum {
81         IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT =         4096,
82         IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT =     1
83 };
84
85 /* image loader class */
86
87
88 enum {
89         SIGNAL_AREA_READY = 0,
90         SIGNAL_ERROR,
91         SIGNAL_DONE,
92         SIGNAL_PERCENT,
93         SIGNAL_SIZE,
94         SIGNAL_COUNT
95 };
96
97 static guint signals[SIGNAL_COUNT] = { 0 };
98
99 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
100 static void image_loader_class_init_wrapper(void *data, void *user_data);
101 static void image_loader_class_init(ImageLoaderClass *loader_class);
102 static void image_loader_finalize(GObject *object);
103 static void image_loader_stop(ImageLoader *il);
104
105 GType image_loader_get_type()
106 {
107         static GType type = 0;
108         if (type == 0)
109                 {
110                 static const GTypeInfo info = {
111                         sizeof(ImageLoaderClass),
112                         nullptr,   /* base_init */
113                         nullptr,   /* base_finalize */
114                         static_cast<GClassInitFunc>(image_loader_class_init_wrapper), /* class_init */
115                         nullptr,   /* class_finalize */
116                         nullptr,   /* class_data */
117                         sizeof(ImageLoader),
118                         0,      /* n_preallocs */
119                         static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
120                         nullptr /* value_table */
121                         };
122                 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
123                 }
124         return type;
125 }
126
127 static void image_loader_init(GTypeInstance *instance, gpointer)
128 {
129         auto il = reinterpret_cast<ImageLoader *>(instance);
130
131         il->pixbuf = nullptr;
132         il->idle_id = 0;
133         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
134         il->done = FALSE;
135         il->loader = nullptr;
136
137         il->bytes_read = 0;
138         il->bytes_total = 0;
139
140         il->idle_done_id = 0;
141
142         il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
143         il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
144         il->mapped_file = nullptr;
145         il->preview = IMAGE_LOADER_PREVIEW_NONE;
146
147         il->requested_width = 0;
148         il->requested_height = 0;
149         il->actual_width = 0;
150         il->actual_height = 0;
151         il->shrunk = FALSE;
152
153         il->can_destroy = TRUE;
154
155         il->data_mutex = g_new(GMutex, 1);
156         g_mutex_init(il->data_mutex);
157         il->can_destroy_cond = g_new(GCond, 1);
158         g_cond_init(il->can_destroy_cond);
159
160         DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
161 }
162
163 static void image_loader_class_init_wrapper(void *data, void *)
164 {
165         image_loader_class_init(static_cast<ImageLoaderClass *>(data));
166 }
167
168 static void image_loader_class_init(ImageLoaderClass *loader_class)
169 {
170         GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
171
172         gobject_class->finalize = image_loader_finalize;
173
174
175         signals[SIGNAL_AREA_READY] =
176                 g_signal_new("area_ready",
177                              G_OBJECT_CLASS_TYPE(gobject_class),
178                              G_SIGNAL_RUN_LAST,
179                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
180                              nullptr, nullptr,
181                              gq_marshal_VOID__INT_INT_INT_INT,
182                              G_TYPE_NONE, 4,
183                              G_TYPE_INT,
184                              G_TYPE_INT,
185                              G_TYPE_INT,
186                              G_TYPE_INT);
187
188         signals[SIGNAL_ERROR] =
189                 g_signal_new("error",
190                              G_OBJECT_CLASS_TYPE(gobject_class),
191                              G_SIGNAL_RUN_LAST,
192                              G_STRUCT_OFFSET(ImageLoaderClass, error),
193                              nullptr, nullptr,
194                              g_cclosure_marshal_VOID__VOID,
195                              G_TYPE_NONE, 0);
196
197         signals[SIGNAL_DONE] =
198                 g_signal_new("done",
199                              G_OBJECT_CLASS_TYPE(gobject_class),
200                              G_SIGNAL_RUN_LAST,
201                              G_STRUCT_OFFSET(ImageLoaderClass, done),
202                              nullptr, nullptr,
203                              g_cclosure_marshal_VOID__VOID,
204                              G_TYPE_NONE, 0);
205
206         signals[SIGNAL_PERCENT] =
207                 g_signal_new("percent",
208                              G_OBJECT_CLASS_TYPE(gobject_class),
209                              G_SIGNAL_RUN_LAST,
210                              G_STRUCT_OFFSET(ImageLoaderClass, percent),
211                              nullptr, nullptr,
212                              g_cclosure_marshal_VOID__DOUBLE,
213                              G_TYPE_NONE, 1,
214                              G_TYPE_DOUBLE);
215
216         signals[SIGNAL_SIZE] =
217                 g_signal_new("size_prepared",
218                              G_OBJECT_CLASS_TYPE(gobject_class),
219                              G_SIGNAL_RUN_LAST,
220                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
221                              nullptr, nullptr,
222                              gq_marshal_VOID__INT_INT,
223                              G_TYPE_NONE, 2,
224                              G_TYPE_INT,
225                              G_TYPE_INT);
226
227 }
228
229 static void image_loader_finalize(GObject *object)
230 {
231         auto il = reinterpret_cast<ImageLoader *>(object);
232
233         image_loader_stop(il);
234
235         if (il->error) DEBUG_1("%s", image_loader_get_error(il));
236
237         DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
238
239         if (il->idle_done_id)
240                 {
241                 g_source_remove(il->idle_done_id);
242                 il->idle_done_id = 0;
243                 }
244
245         while (g_source_remove_by_user_data(il))
246                 {
247                 DEBUG_2("pending signals detected");
248                 }
249
250         while (il->area_param_list)
251                 {
252                 DEBUG_1("pending area_ready signals detected");
253                 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
254                 g_free(il->area_param_list->data);
255                 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
256                 }
257
258         while (il->area_param_delayed_list)
259                 {
260                 g_free(il->area_param_delayed_list->data);
261                 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
262                 }
263
264         if (il->pixbuf) g_object_unref(il->pixbuf);
265
266         if (il->error) g_error_free(il->error);
267
268         file_data_unref(il->fd);
269
270         g_mutex_clear(il->data_mutex);
271         g_free(il->data_mutex);
272         g_cond_clear(il->can_destroy_cond);
273         g_free(il->can_destroy_cond);
274 }
275
276 void image_loader_free(ImageLoader *il)
277 {
278         if (!il) return;
279         g_object_unref(G_OBJECT(il));
280 }
281
282
283 ImageLoader *image_loader_new(FileData *fd)
284 {
285         ImageLoader *il;
286
287         if (!fd) return nullptr;
288
289         il = static_cast<ImageLoader *>(g_object_new(TYPE_IMAGE_LOADER, nullptr));
290
291         il->fd = file_data_ref(fd);
292
293         return il;
294 }
295
296 /**************************************************************************************/
297 /* send signals via idle calbacks - the callback are executed in the main thread */
298
299 struct ImageLoaderAreaParam {
300         ImageLoader *il;
301         guint x;
302         guint y;
303         guint w;
304         guint h;
305 };
306
307
308 static gboolean image_loader_emit_area_ready_cb(gpointer data)
309 {
310         auto par = static_cast<ImageLoaderAreaParam *>(data);
311         ImageLoader *il = par->il;
312         guint x;
313         guint y;
314         guint w;
315         guint h;
316         g_mutex_lock(il->data_mutex);
317         il->area_param_list = g_list_remove(il->area_param_list, par);
318         x = par->x;
319         y = par->y;
320         w = par->w;
321         h = par->h;
322         g_free(par);
323         g_mutex_unlock(il->data_mutex);
324
325         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
326
327         return G_SOURCE_REMOVE;
328 }
329
330 static gboolean image_loader_emit_done_cb(gpointer data)
331 {
332         auto il = static_cast<ImageLoader *>(data);
333         g_signal_emit(il, signals[SIGNAL_DONE], 0);
334         return G_SOURCE_REMOVE;
335 }
336
337 static gboolean image_loader_emit_error_cb(gpointer data)
338 {
339         auto il = static_cast<ImageLoader *>(data);
340         g_signal_emit(il, signals[SIGNAL_ERROR], 0);
341         return G_SOURCE_REMOVE;
342 }
343
344 static gboolean image_loader_emit_percent_cb(gpointer data)
345 {
346         auto il = static_cast<ImageLoader *>(data);
347         g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
348         return G_SOURCE_REMOVE;
349 }
350
351 static gboolean image_loader_emit_size_cb(gpointer data)
352 {
353         gint width;
354         gint height;
355         auto il = static_cast<ImageLoader *>(data);
356         g_mutex_lock(il->data_mutex);
357         width = il->actual_width;
358         height = il->actual_height;
359         g_mutex_unlock(il->data_mutex);
360         g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
361         return G_SOURCE_REMOVE;
362 }
363
364
365 /* DONE and ERROR are emitted only once, thus they can have normal priority
366    PERCENT and AREA_READY should be processed ASAP
367 */
368
369 static void image_loader_emit_done(ImageLoader *il)
370 {
371         g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, nullptr);
372 }
373
374 static void image_loader_emit_error(ImageLoader *il)
375 {
376         g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, nullptr);
377 }
378
379 static void image_loader_emit_percent(ImageLoader *il)
380 {
381         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, nullptr);
382 }
383
384 static void image_loader_emit_size(ImageLoader *il)
385 {
386         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, nullptr);
387 }
388
389 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
390 {
391         if (*list)
392                 {
393                 auto prev_par = static_cast<ImageLoaderAreaParam *>((*list)->data);
394                 if (prev_par->x == x && prev_par->w == w &&
395                     prev_par->y + prev_par->h == y)
396                         {
397                         /* we can merge the notifications */
398                         prev_par->h += h;
399                         return nullptr;
400                         }
401                 if (prev_par->x == x && prev_par->w == w &&
402                     y + h == prev_par->y)
403                         {
404                         /* we can merge the notifications */
405                         prev_par->h += h;
406                         prev_par->y = y;
407                         return nullptr;
408                         }
409                 if (prev_par->y == y && prev_par->h == h &&
410                     prev_par->x + prev_par->w == x)
411                         {
412                         /* we can merge the notifications */
413                         prev_par->w += w;
414                         return nullptr;
415                         }
416                 if (prev_par->y == y && prev_par->h == h &&
417                     x + w == prev_par->x)
418                         {
419                         /* we can merge the notifications */
420                         prev_par->w += w;
421                         prev_par->x = x;
422                         return nullptr;
423                         }
424                 }
425
426         auto par = g_new0(ImageLoaderAreaParam, 1);
427         par->il = il;
428         par->x = x;
429         par->y = y;
430         par->w = w;
431         par->h = h;
432
433         *list = g_list_prepend(*list, par);
434         return par;
435 }
436
437 /* this function expects that il->data_mutex is locked by caller */
438 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
439 {
440         ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
441
442         if (par)
443                 {
444                 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, nullptr);
445                 }
446 }
447
448 /**************************************************************************************/
449 /* the following functions may be executed in separate thread */
450
451 /* this function expects that il->data_mutex is locked by caller */
452 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
453 {
454         image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
455 }
456
457
458
459 static gboolean image_loader_get_stopping(ImageLoader *il)
460 {
461         gboolean ret;
462         if (!il) return FALSE;
463
464         g_mutex_lock(il->data_mutex);
465         ret = il->stopping;
466         g_mutex_unlock(il->data_mutex);
467
468         return ret;
469 }
470
471
472 static void image_loader_sync_pixbuf(ImageLoader *il)
473 {
474         GdkPixbuf *pb;
475
476         g_mutex_lock(il->data_mutex);
477
478         if (!il->loader)
479                 {
480                 g_mutex_unlock(il->data_mutex);
481                 return;
482                 }
483
484         pb = il->backend.get_pixbuf(il->loader);
485
486         if (pb == il->pixbuf)
487                 {
488                 g_mutex_unlock(il->data_mutex);
489                 return;
490                 }
491
492         if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
493                 {
494                 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
495                 }
496
497         if (il->pixbuf) g_object_unref(il->pixbuf);
498
499         il->pixbuf = pb;
500         if (il->pixbuf) g_object_ref(il->pixbuf);
501
502         g_mutex_unlock(il->data_mutex);
503 }
504
505 static void image_loader_area_updated_cb(gpointer,
506                                  guint x, guint y, guint w, guint h,
507                                  gpointer data)
508 {
509         auto il = static_cast<ImageLoader *>(data);
510
511         if (!image_loader_get_pixbuf(il))
512                 {
513                 image_loader_sync_pixbuf(il);
514                 if (!image_loader_get_pixbuf(il))
515                         {
516                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
517                         }
518                 }
519
520         g_mutex_lock(il->data_mutex);
521         if (il->delay_area_ready)
522                 image_loader_queue_delayed_area_ready(il, x, y, w, h);
523         else
524                 image_loader_emit_area_ready(il, x, y, w, h);
525
526         if (il->stopping) il->backend.abort(il->loader);
527
528         g_mutex_unlock(il->data_mutex);
529 }
530
531 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
532 {
533         auto il = static_cast<ImageLoader *>(data);
534         GdkPixbuf *pb;
535         guchar *pix;
536         size_t h;
537         size_t rs;
538
539         /* a workaround for
540            https://bugzilla.gnome.org/show_bug.cgi?id=547669
541            https://bugzilla.gnome.org/show_bug.cgi?id=589334
542         */
543         gchar *format = il->backend.get_format_name(loader);
544         if (strcmp(format, "svg") == 0 ||
545             strcmp(format, "xpm") == 0)
546                 {
547                 g_free(format);
548                 return;
549                 }
550
551         g_free(format);
552
553         pb = il->backend.get_pixbuf(loader);
554
555         h = gdk_pixbuf_get_height(pb);
556         rs = gdk_pixbuf_get_rowstride(pb);
557         pix = gdk_pixbuf_get_pixels(pb);
558
559         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
560
561 }
562
563 static void image_loader_size_cb(gpointer loader,
564                                  gint width, gint height, gpointer data)
565 {
566         auto il = static_cast<ImageLoader *>(data);
567         gchar **mime_types;
568         gboolean scale = FALSE;
569         gint n;
570
571         g_mutex_lock(il->data_mutex);
572         il->actual_width = width;
573         il->actual_height = height;
574         if (il->requested_width < 1 || il->requested_height < 1)
575                 {
576                 g_mutex_unlock(il->data_mutex);
577                 image_loader_emit_size(il);
578                 return;
579                 }
580         g_mutex_unlock(il->data_mutex);
581
582 #if HAVE_FFMPEGTHUMBNAILER
583         if (il->fd->format_class == FORMAT_CLASS_VIDEO)
584                 scale = TRUE;
585 #endif
586         mime_types = il->backend.get_format_mime_types(loader);
587         n = 0;
588         while (mime_types[n] && !scale)
589                 {
590                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
591                 n++;
592                 }
593         g_strfreev(mime_types);
594
595         if (!scale)
596                 {
597                 image_loader_emit_size(il);
598                 return;
599                 }
600
601         g_mutex_lock(il->data_mutex);
602
603         gint nw;
604         gint nh;
605         if (width > il->requested_width || height > il->requested_height)
606                 {
607
608                 if ((static_cast<gdouble>(il->requested_width) / width) < (static_cast<gdouble>(il->requested_height) / height))
609                         {
610                         nw = il->requested_width;
611                         nh = static_cast<gdouble>(nw) / width * height;
612                         if (nh < 1) nh = 1;
613                         }
614                 else
615                         {
616                         nh = il->requested_height;
617                         nw = static_cast<gdouble>(nh) / height * width;
618                         if (nw < 1) nw = 1;
619                         }
620
621                 il->actual_width = nw;
622                 il->actual_height = nh;
623                 il->backend.set_size(loader, nw, nh);
624                 il->shrunk = TRUE;
625                 }
626
627         g_mutex_unlock(il->data_mutex);
628         image_loader_emit_size(il);
629 }
630
631 static void image_loader_stop_loader(ImageLoader *il)
632 {
633         if (!il) return;
634
635         if (il->loader)
636                 {
637                 /* some loaders do not have a pixbuf till close, order is important here */
638                 il->backend.close(il->loader, il->error ? nullptr : &il->error); /* we are interested in the first error only */
639                 image_loader_sync_pixbuf(il);
640                 il->backend.free(il->loader);
641                 il->loader = nullptr;
642                 }
643         g_mutex_lock(il->data_mutex);
644         il->done = TRUE;
645         g_mutex_unlock(il->data_mutex);
646 }
647
648 static void image_loader_setup_loader(ImageLoader *il)
649 {
650 #if HAVE_TIFF || HAVE_PDF || HAVE_HEIF || HAVE_DJVU
651         gchar *format;
652 #endif
653
654         gint external_preview = 1;
655
656         g_mutex_lock(il->data_mutex);
657
658         if (options->external_preview.enable)
659                 {
660                 gchar *cmd_line;
661                 gchar *tilde_filename;
662
663                 tilde_filename = expand_tilde(options->external_preview.select);
664
665                 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
666
667                 external_preview = runcmd(cmd_line);
668                 g_free(cmd_line);
669                 g_free(tilde_filename);
670                 }
671
672         if (external_preview == 0)
673                 {
674                 DEBUG_1("Using custom external loader");
675                 image_loader_backend_set_external(&il->backend);
676                 }
677         else
678                 {
679 #if HAVE_FFMPEGTHUMBNAILER
680                 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
681                         {
682                         DEBUG_1("Using custom ffmpegthumbnailer loader");
683                         image_loader_backend_set_ft(&il->backend);
684                         }
685                 else
686 #endif
687 #if HAVE_PDF
688                 if (il->bytes_total >= 4 &&
689                     (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
690                         {
691                         DEBUG_1("Using custom pdf loader");
692                         image_loader_backend_set_pdf(&il->backend);
693                         }
694                 else
695 #endif
696 #if HAVE_HEIF
697                 if (il->bytes_total >= 12 &&
698                         ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
699                         (memcmp(il->mapped_file + 4, "ftypheix", 8) == 0) ||
700                         (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
701                         (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
702                         (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
703                         {
704                         DEBUG_1("Using custom heif loader");
705                         image_loader_backend_set_heif(&il->backend);
706                         }
707                 else
708         #endif
709         #if HAVE_WEBP
710                 if (il->bytes_total >= 12 &&
711                         (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
712                         (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
713                         {
714                         DEBUG_1("Using custom webp loader");
715                         image_loader_backend_set_webp(&il->backend);
716                         }
717                 else
718 #endif
719 #if HAVE_DJVU
720                 if (il->bytes_total >= 16 &&
721                         (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
722                         (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
723                         {
724                         DEBUG_1("Using custom djvu loader");
725                         image_loader_backend_set_djvu(&il->backend);
726                         }
727                 else
728 #endif
729 #if HAVE_JPEG
730                 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
731                         {
732                         DEBUG_1("Using custom jpeg loader");
733                         image_loader_backend_set_jpeg(&il->backend);
734                         }
735 #if !HAVE_RAW
736                 else
737                 if (il->bytes_total >= 11 &&
738                         (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
739                         (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
740                         {
741                         DEBUG_1("Using custom cr3 loader");
742                         image_loader_backend_set_cr3(&il->backend);
743                         }
744 #endif
745                 else
746 #endif
747 #if HAVE_TIFF
748                 if (il->bytes_total >= 10 &&
749                     (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
750                      memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
751                      memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
752                      memcmp(il->mapped_file, "II*\0", 4) == 0))
753                         {
754                         DEBUG_1("Using custom tiff loader");
755                         image_loader_backend_set_tiff(&il->backend);
756                         }
757                 else
758 #endif
759                 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
760                         {
761                         DEBUG_1("Using dds loader");
762                         image_loader_backend_set_dds(&il->backend);
763                         }
764                 else
765                 if (il->bytes_total >= 6 &&
766                         (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
767                         {
768                         DEBUG_1("Using custom psd loader");
769                         image_loader_backend_set_psd(&il->backend);
770                         }
771                 else
772 #if HAVE_J2K
773                 if (il->bytes_total >= 12 &&
774                         (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
775                         {
776                         DEBUG_1("Using custom j2k loader");
777                         image_loader_backend_set_j2k(&il->backend);
778                         }
779                 else
780 #endif
781 #if HAVE_JPEGXL
782                 if (il->bytes_total >= 12 &&
783                         (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
784                         {
785                         DEBUG_1("Using custom jpeg xl loader");
786                         image_loader_backend_set_jpegxl(&il->backend);
787                         }
788                 else
789                 if (il->bytes_total >= 2 &&
790                         (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
791                         {
792                         DEBUG_1("Using custom jpeg xl loader");
793                         image_loader_backend_set_jpegxl(&il->backend);
794                         }
795                 else
796 #endif
797                 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
798                         (file_extension_match(il->fd->path, ".scr")))
799                         {
800                         DEBUG_1("Using custom zxscr loader");
801                         image_loader_backend_set_zxscr(&il->backend);
802                         }
803                 else
804                 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
805                         {
806                         DEBUG_1("Using custom collection loader");
807                         image_loader_backend_set_collection(&il->backend);
808                         }
809                 else
810                 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
811                         {
812                         DEBUG_1("Using custom svgz loader");
813                         image_loader_backend_set_svgz(&il->backend);
814                         }
815                 else
816                         image_loader_backend_set_default(&il->backend);
817                 }
818
819         il->loader = static_cast<void **>(il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il));
820
821 #if HAVE_TIFF
822         format = il->backend.get_format_name(il->loader);
823         if (g_strcmp0(format, "tiff") == 0)
824                 {
825                 il->backend.set_page_num(il->loader, il->fd->page_num);
826                 }
827         g_free(format);
828 #endif
829
830 #if HAVE_PDF
831         format = il->backend.get_format_name(il->loader);
832         if (g_strcmp0(format, "pdf") == 0)
833                 {
834                 il->backend.set_page_num(il->loader, il->fd->page_num);
835                 }
836         g_free(format);
837 #endif
838
839 #if HAVE_HEIF
840         format = il->backend.get_format_name(il->loader);
841         if (g_strcmp0(format, "heif") == 0)
842                 {
843                 il->backend.set_page_num(il->loader, il->fd->page_num);
844                 }
845         g_free(format);
846 #endif
847
848 #if HAVE_DJVU
849         format = il->backend.get_format_name(il->loader);
850         if (g_strcmp0(format, "djvu") == 0)
851                 {
852                 il->backend.set_page_num(il->loader, il->fd->page_num);
853                 }
854         g_free(format);
855 #endif
856
857         il->fd->format_name = il->backend.get_format_name(il->loader);
858
859         g_mutex_unlock(il->data_mutex);
860 }
861
862
863 static void image_loader_done(ImageLoader *il)
864 {
865         image_loader_stop_loader(il);
866
867         image_loader_emit_done(il);
868 }
869
870 static void image_loader_error(ImageLoader *il)
871 {
872         image_loader_stop_loader(il);
873
874         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
875
876         image_loader_emit_error(il);
877 }
878
879 static gboolean image_loader_continue(ImageLoader *il)
880 {
881         gint c;
882
883         if (!il) return G_SOURCE_REMOVE;
884
885         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
886         while (c > 0 && !image_loader_get_stopping(il))
887                 {
888                 if (il->bytes_total == il->bytes_read)
889                         {
890                         image_loader_done(il);
891                         return G_SOURCE_REMOVE;
892                         }
893
894                 if (il->bytes_total < il->bytes_read)
895                         {
896                         image_loader_error(il);
897                         return G_SOURCE_REMOVE;
898                         }
899
900                 gsize b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
901
902                 if (!il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, il->bytes_total, &il->error))
903                         {
904                         image_loader_error(il);
905                         return G_SOURCE_REMOVE;
906                         }
907
908                 il->bytes_read += b;
909
910                 c--;
911                 }
912
913         if (il->bytes_total > 0)
914                 {
915                 image_loader_emit_percent(il);
916                 }
917
918         return G_SOURCE_CONTINUE;
919 }
920
921 static gboolean image_loader_begin(ImageLoader *il)
922 {
923 #if HAVE_TIFF || HAVE_PDF || HAVE_HEIF || HAVE_DJVU
924         gchar *format;
925 #endif
926
927         if (il->pixbuf) return FALSE;
928
929         if (il->bytes_total <= il->bytes_read) return FALSE;
930
931         gsize b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
932
933         image_loader_setup_loader(il);
934
935         g_assert(il->bytes_read == 0);
936         if (!il->backend.write(il->loader, il->mapped_file, b, il->bytes_total, &il->error))
937                 {
938                 image_loader_stop_loader(il);
939                 return FALSE;
940                 }
941
942 #if HAVE_PDF
943         format = il->backend.get_format_name(il->loader);
944         if (g_strcmp0(format, "pdf") == 0)
945                 {
946                 gint i = il->backend.get_page_total(il->loader);
947                 file_data_set_page_total(il->fd, i);
948                 }
949         g_free(format);
950 #endif
951 #if HAVE_HEIF
952         format = il->backend.get_format_name(il->loader);
953         if (g_strcmp0(format, "heif") == 0)
954                 {
955                 gint i = il->backend.get_page_total(il->loader);
956                 file_data_set_page_total(il->fd, i);
957                 }
958         g_free(format);
959 #endif
960 #if HAVE_DJVU
961         format = il->backend.get_format_name(il->loader);
962         if (g_strcmp0(format, "djvu") == 0)
963                 {
964                 gint i = il->backend.get_page_total(il->loader);
965                 file_data_set_page_total(il->fd, i);
966                 }
967         g_free(format);
968 #endif
969 #if HAVE_TIFF
970         format = il->backend.get_format_name(il->loader);
971         if (g_strcmp0(format, "tiff") == 0)
972                 {
973                 gint i = il->backend.get_page_total(il->loader);
974                 file_data_set_page_total(il->fd, i);
975                 }
976         g_free(format);
977 #endif
978
979         il->bytes_read += b;
980
981         /* read until size is known */
982         while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
983                 {
984                 if (il->bytes_total < il->bytes_read)
985                         {
986                         image_loader_stop_loader(il);
987                         return FALSE;
988                         }
989
990                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
991                 if (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, il->bytes_total, &il->error))
992                         {
993                         image_loader_stop_loader(il);
994                         return FALSE;
995                         }
996                 il->bytes_read += b;
997                 }
998         if (!il->pixbuf) image_loader_sync_pixbuf(il);
999
1000         if (il->bytes_read == il->bytes_total || b < 1)
1001                 {
1002                 /* done, handle (broken) loaders that do not have pixbuf till close */
1003                 image_loader_stop_loader(il);
1004
1005                 if (!il->pixbuf) return FALSE;
1006
1007                 image_loader_done(il);
1008                 return TRUE;
1009                 }
1010
1011         if (!il->pixbuf)
1012                 {
1013                 image_loader_stop_loader(il);
1014                 return FALSE;
1015                 }
1016
1017         return TRUE;
1018 }
1019
1020 /**************************************************************************************/
1021 /* the following functions are always executed in the main thread */
1022
1023
1024 static gboolean image_loader_setup_source(ImageLoader *il)
1025 {
1026         struct stat st;
1027         gchar *pathl;
1028
1029         if (!il || il->loader || il->mapped_file) return FALSE;
1030
1031         il->mapped_file = nullptr;
1032
1033         if (il->fd)
1034                 {
1035                 ExifData *exif = exif_read_fd(il->fd);
1036
1037                 if (options->thumbnails.use_exif)
1038                         {
1039                         il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), il->requested_width, il->requested_height);
1040
1041                         if (il->mapped_file)
1042                                 {
1043                                 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1044                                 }
1045                         }
1046                 else
1047                         {
1048                         il->mapped_file = libraw_get_preview(il, reinterpret_cast<guint *>(&il->bytes_total));
1049
1050                         if (il->mapped_file)
1051                                 {
1052                                 /* Both exiv2 and libraw sometimes return a pointer to a file
1053                                  * section that is not a jpeg */
1054                                 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1055                                         {
1056                                         il->mapped_file = nullptr;
1057                                         }
1058                                 else
1059                                         {
1060                                         il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1061                                         }
1062                                 }
1063                         }
1064
1065                 /* If libraw does not find a thumbnail, try exiv2 */
1066                 if (!il->mapped_file)
1067                         {
1068                         il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), 0, 0); /* get the largest available preview image or NULL for normal images*/
1069
1070                         if (il->mapped_file)
1071                                 {
1072                                 /* Both exiv2 and libraw sometimes return a pointer to a file
1073                                  * section that is not a jpeg */
1074                                 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1075                                         {
1076                                         il->mapped_file = nullptr;
1077                                         }
1078                                 else
1079                                         {
1080                                         il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1081                                         }
1082                                 }
1083                         }
1084
1085                 if (il->mapped_file)
1086                         {
1087                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1088                         }
1089                 exif_free_fd(il->fd, exif);
1090                 }
1091
1092
1093         if (!il->mapped_file)
1094                 {
1095                 /* normal file */
1096                 gint load_fd;
1097
1098                 pathl = path_from_utf8(il->fd->path);
1099                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1100                 g_free(pathl);
1101                 if (load_fd == -1) return FALSE;
1102
1103                 if (fstat(load_fd, &st) == 0)
1104                         {
1105                         il->bytes_total = st.st_size;
1106                         }
1107                 else
1108                         {
1109                         close(load_fd);
1110                         return FALSE;
1111                         }
1112
1113                 il->mapped_file = static_cast<guchar *>(mmap(nullptr, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0));
1114                 close(load_fd);
1115                 if (il->mapped_file == MAP_FAILED)
1116                         {
1117                         il->mapped_file = nullptr;
1118                         return FALSE;
1119                         }
1120                 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1121                 }
1122
1123         return TRUE;
1124 }
1125
1126 static void image_loader_stop_source(ImageLoader *il)
1127 {
1128         if (!il) return;
1129
1130         if (il->mapped_file)
1131                 {
1132                 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1133                         {
1134                         exif_free_preview(il->mapped_file);
1135                         }
1136                 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1137                         {
1138                         libraw_free_preview(il->mapped_file);
1139                         }
1140                 else
1141                         {
1142                         munmap(il->mapped_file, il->bytes_total);
1143                         }
1144                 il->mapped_file = nullptr;
1145                 }
1146 }
1147
1148 static void image_loader_stop(ImageLoader *il)
1149 {
1150         if (!il) return;
1151
1152         if (il->idle_id)
1153                 {
1154                 g_source_remove(il->idle_id);
1155                 il->idle_id = 0;
1156                 }
1157
1158         if (il->thread)
1159                 {
1160                 /* stop loader in the other thread */
1161                 g_mutex_lock(il->data_mutex);
1162                 il->stopping = TRUE;
1163                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1164                 g_mutex_unlock(il->data_mutex);
1165                 }
1166
1167         image_loader_stop_loader(il);
1168         image_loader_stop_source(il);
1169
1170 }
1171
1172 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1173 {
1174         g_mutex_lock(il->data_mutex);
1175         il->delay_area_ready = enable;
1176         if (!enable)
1177                 {
1178                 /* send delayed */
1179                 GList *list;
1180                 GList *work;
1181                 list = g_list_reverse(il->area_param_delayed_list);
1182                 il->area_param_delayed_list = nullptr;
1183                 g_mutex_unlock(il->data_mutex);
1184
1185                 work = list;
1186
1187                 while (work)
1188                         {
1189                         auto par = static_cast<ImageLoaderAreaParam *>(work->data);
1190                         work = work->next;
1191
1192                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1193                         }
1194                 g_list_free_full(list, g_free);
1195                 }
1196         else
1197                 {
1198                 /* just unlock */
1199                 g_mutex_unlock(il->data_mutex);
1200                 }
1201 }
1202
1203
1204 /**************************************************************************************/
1205 /* execution via idle calls */
1206
1207 static gboolean image_loader_idle_cb(gpointer data)
1208 {
1209         gboolean ret = G_SOURCE_REMOVE;
1210         auto il = static_cast<ImageLoader *>(data);
1211
1212         if (il->idle_id)
1213                 {
1214                 ret = image_loader_continue(il);
1215                 }
1216
1217         if (!ret)
1218                 {
1219                 image_loader_stop_source(il);
1220                 }
1221
1222         return ret;
1223 }
1224
1225 static gboolean image_loader_start_idle(ImageLoader *il)
1226 {
1227         gboolean ret;
1228
1229         if (!il) return FALSE;
1230
1231         if (!il->fd) return FALSE;
1232
1233         if (!image_loader_setup_source(il)) return FALSE;
1234
1235         ret = image_loader_begin(il);
1236
1237         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, nullptr);
1238         return ret;
1239 }
1240
1241 /**************************************************************************************/
1242 /* execution via thread */
1243
1244 static GThreadPool *image_loader_thread_pool = nullptr;
1245
1246 static GCond *image_loader_prio_cond = nullptr;
1247 static GMutex *image_loader_prio_mutex = nullptr;
1248 static gint image_loader_prio_num = 0;
1249
1250
1251 static void image_loader_thread_enter_high()
1252 {
1253         g_mutex_lock(image_loader_prio_mutex);
1254         image_loader_prio_num++;
1255         g_mutex_unlock(image_loader_prio_mutex);
1256 }
1257
1258 static void image_loader_thread_leave_high()
1259 {
1260         g_mutex_lock(image_loader_prio_mutex);
1261         image_loader_prio_num--;
1262         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1263         g_mutex_unlock(image_loader_prio_mutex);
1264 }
1265
1266 static void image_loader_thread_wait_high()
1267 {
1268         g_mutex_lock(image_loader_prio_mutex);
1269         while (image_loader_prio_num)
1270                 {
1271                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1272                 }
1273
1274         g_mutex_unlock(image_loader_prio_mutex);
1275 }
1276
1277
1278 static void image_loader_thread_run(gpointer data, gpointer)
1279 {
1280         auto il = static_cast<ImageLoader *>(data);
1281         gboolean cont;
1282         gboolean err;
1283
1284         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1285                 {
1286                 /* low prio, wait until high prio tasks finishes */
1287                 image_loader_thread_wait_high();
1288                 }
1289         else
1290                 {
1291                 /* high prio */
1292                 image_loader_thread_enter_high();
1293                 }
1294
1295         err = !image_loader_begin(il);
1296
1297         if (err)
1298                 {
1299                 /*
1300                 loader failed, we have to send signal
1301                 (idle mode returns the image_loader_begin return value directly)
1302                 (success is always reported indirectly from image_loader_begin)
1303                 */
1304                 image_loader_emit_error(il);
1305                 }
1306
1307         cont = !err;
1308
1309         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1310                 {
1311                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1312                         {
1313                         /* low prio, wait until high prio tasks finishes */
1314                         image_loader_thread_wait_high();
1315                         }
1316                 cont = image_loader_continue(il);
1317                 }
1318         image_loader_stop_loader(il);
1319
1320         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1321                 {
1322                 /* high prio */
1323                 image_loader_thread_leave_high();
1324                 }
1325
1326         g_mutex_lock(il->data_mutex);
1327         il->can_destroy = TRUE;
1328         g_cond_signal(il->can_destroy_cond);
1329         g_mutex_unlock(il->data_mutex);
1330
1331 }
1332
1333
1334 static gboolean image_loader_start_thread(ImageLoader *il)
1335 {
1336         if (!il) return FALSE;
1337
1338         if (!il->fd) return FALSE;
1339
1340         il->thread = TRUE;
1341
1342         if (!image_loader_setup_source(il)) return FALSE;
1343
1344         if (!image_loader_thread_pool)
1345                 {
1346                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, nullptr, -1, FALSE, nullptr);
1347                 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1348                 g_cond_init(image_loader_prio_cond);
1349                 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1350                 g_mutex_init(image_loader_prio_mutex);
1351                 }
1352
1353         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1354
1355         g_thread_pool_push(image_loader_thread_pool, il, nullptr);
1356         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1357
1358         return TRUE;
1359 }
1360
1361
1362 /**************************************************************************************/
1363 /* public interface */
1364
1365
1366 gboolean image_loader_start(ImageLoader *il)
1367 {
1368         if (!il) return FALSE;
1369
1370         if (!il->fd) return FALSE;
1371
1372         return image_loader_start_thread(il);
1373 }
1374
1375
1376 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1377 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1378 {
1379         GdkPixbuf *ret;
1380         if (!il) return nullptr;
1381
1382         g_mutex_lock(il->data_mutex);
1383         ret = il->pixbuf;
1384         g_mutex_unlock(il->data_mutex);
1385         return ret;
1386 }
1387
1388 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1389 {
1390         if (!il) return;
1391
1392         g_mutex_lock(il->data_mutex);
1393         il->requested_width = width;
1394         il->requested_height = height;
1395         g_mutex_unlock(il->data_mutex);
1396 }
1397
1398 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1399 {
1400         if (!il) return;
1401
1402         g_mutex_lock(il->data_mutex);
1403         il->idle_read_loop_count = count ? count : 1;
1404         g_mutex_unlock(il->data_mutex);
1405 }
1406
1407 void image_loader_set_priority(ImageLoader *il, gint priority)
1408 {
1409         if (!il) return;
1410
1411         if (il->thread) return; /* can't change prio if the thread already runs */
1412         il->idle_priority = priority;
1413 }
1414
1415
1416 gdouble image_loader_get_percent(ImageLoader *il)
1417 {
1418         gdouble ret;
1419         if (!il) return 0.0;
1420
1421         g_mutex_lock(il->data_mutex);
1422         if (il->bytes_total == 0)
1423                 {
1424                 ret = 0.0;
1425                 }
1426         else
1427                 {
1428                 ret = static_cast<gdouble>(il->bytes_read) / il->bytes_total;
1429                 }
1430         g_mutex_unlock(il->data_mutex);
1431         return ret;
1432 }
1433
1434 gboolean image_loader_get_is_done(ImageLoader *il)
1435 {
1436         gboolean ret;
1437         if (!il) return FALSE;
1438
1439         g_mutex_lock(il->data_mutex);
1440         ret = il->done;
1441         g_mutex_unlock(il->data_mutex);
1442
1443         return ret;
1444 }
1445
1446 FileData *image_loader_get_fd(ImageLoader *il)
1447 {
1448         FileData *ret;
1449         if (!il) return nullptr;
1450
1451         g_mutex_lock(il->data_mutex);
1452         ret = il->fd;
1453         g_mutex_unlock(il->data_mutex);
1454
1455         return ret;
1456 }
1457
1458 gboolean image_loader_get_shrunk(ImageLoader *il)
1459 {
1460         gboolean ret;
1461         if (!il) return FALSE;
1462
1463         g_mutex_lock(il->data_mutex);
1464         ret = il->shrunk;
1465         g_mutex_unlock(il->data_mutex);
1466         return ret;
1467 }
1468
1469 const gchar *image_loader_get_error(ImageLoader *il)
1470 {
1471         const gchar *ret = nullptr;
1472         if (!il) return nullptr;
1473         g_mutex_lock(il->data_mutex);
1474         if (il->error) ret = il->error->message;
1475         g_mutex_unlock(il->data_mutex);
1476         return ret;
1477 }
1478
1479
1480 /**
1481  *  @FIXME this can be rather slow and blocks until the size is known
1482  */
1483 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1484 {
1485         ImageLoader *il;
1486         gboolean success;
1487
1488         il = image_loader_new(fd);
1489
1490         success = image_loader_start_idle(il);
1491
1492         if (success && il->pixbuf)
1493                 {
1494                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1495                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1496                 }
1497         else
1498                 {
1499                 if (width) *width = -1;
1500                 if (height) *height = -1;
1501                 }
1502
1503         image_loader_free(il);
1504
1505         return success;
1506 }
1507 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */