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