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