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