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