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