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