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