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