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