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