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