Fix two minor compilation warnings.
[geeqie.git] / src / image-load.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "image-load.h"
16
17 #include "exif.h"
18 #include "filedata.h"
19 #include "ui_fileops.h"
20 #include "gq-marshal.h"
21
22 #include <fcntl.h>
23 #include <sys/mman.h>
24
25
26 /**************************************************************************************/
27 /* image looader class */
28
29
30 enum {
31         SIGNAL_AREA_READY = 0,
32         SIGNAL_ERROR,
33         SIGNAL_DONE,
34         SIGNAL_PERCENT,
35         SIGNAL_COUNT
36 };
37
38 static guint signals[SIGNAL_COUNT] = { 0 };
39
40 static void image_loader_init (GTypeInstance *instance, gpointer g_class);
41 static void image_loader_class_init (ImageLoaderClass *class);
42 static void image_loader_finalize(GObject *object);
43 static void image_loader_stop(ImageLoader *il);
44
45 GType image_loader_get_type (void)
46 {
47         static GType type = 0;
48         if (type == 0) 
49                 {
50                 static const GTypeInfo info = {
51                         sizeof (ImageLoaderClass),
52                         NULL,   /* base_init */
53                         NULL,   /* base_finalize */
54                         (GClassInitFunc)image_loader_class_init, /* class_init */
55                         NULL,   /* class_finalize */
56                         NULL,   /* class_data */
57                         sizeof (ImageLoader),
58                         0,      /* n_preallocs */
59                         (GInstanceInitFunc)image_loader_init, /* instance_init */
60                         NULL    /* value_table */
61                         };
62                 type = g_type_register_static (G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
63                 }
64         return type;
65 }
66
67 static void image_loader_init (GTypeInstance *instance, gpointer g_class)
68 {
69         ImageLoader *il = (ImageLoader *)instance;
70
71         il->pixbuf = NULL;
72         il->idle_id = -1;
73         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
74         il->done = FALSE;
75         il->loader = NULL;
76
77         il->bytes_read = 0;
78         il->bytes_total = 0;
79
80         il->idle_done_id = -1;
81
82         il->idle_read_loop_count = options->image.idle_read_loop_count;
83         il->read_buffer_size = options->image.read_buffer_size;
84         il->mapped_file = NULL;
85
86         il->requested_width = 0;
87         il->requested_height = 0;
88         il->shrunk = FALSE;
89
90 #ifdef HAVE_GTHREAD
91         il->data_mutex = g_mutex_new();
92 #endif
93         DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
94 }
95
96 static void image_loader_class_init (ImageLoaderClass *class)
97 {
98         GObjectClass *gobject_class = G_OBJECT_CLASS (class);
99
100 //      gobject_class->set_property = image_loader_set_property;
101 //      gobject_class->get_property = image_loader_get_property;
102
103         gobject_class->finalize = image_loader_finalize;
104
105
106         signals[SIGNAL_AREA_READY] =
107                 g_signal_new("area_ready",
108                              G_OBJECT_CLASS_TYPE(gobject_class),
109                              G_SIGNAL_RUN_LAST,
110                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
111                              NULL, NULL,
112                              gq_marshal_VOID__INT_INT_INT_INT,
113                              G_TYPE_NONE, 4,
114                              G_TYPE_INT,
115                              G_TYPE_INT,
116                              G_TYPE_INT,
117                              G_TYPE_INT);
118
119         signals[SIGNAL_ERROR] =
120                 g_signal_new("error",
121                              G_OBJECT_CLASS_TYPE(gobject_class),
122                              G_SIGNAL_RUN_LAST,
123                              G_STRUCT_OFFSET(ImageLoaderClass, error),
124                              NULL, NULL,
125                              g_cclosure_marshal_VOID__VOID,
126                              G_TYPE_NONE, 1,
127                              GDK_TYPE_EVENT);
128
129         signals[SIGNAL_DONE] =
130                 g_signal_new("done",
131                              G_OBJECT_CLASS_TYPE(gobject_class),
132                              G_SIGNAL_RUN_LAST,
133                              G_STRUCT_OFFSET(ImageLoaderClass, done),
134                              NULL, NULL,
135                              g_cclosure_marshal_VOID__VOID,
136                              G_TYPE_NONE, 0);
137
138         signals[SIGNAL_PERCENT] =
139                 g_signal_new("percent",
140                              G_OBJECT_CLASS_TYPE(gobject_class),
141                              G_SIGNAL_RUN_LAST,
142                              G_STRUCT_OFFSET(ImageLoaderClass, percent),
143                              NULL, NULL,
144                              g_cclosure_marshal_VOID__DOUBLE,
145                              G_TYPE_NONE, 1,
146                              G_TYPE_DOUBLE);
147
148 }
149
150 static void image_loader_finalize(GObject *object)
151 {
152         ImageLoader *il = (ImageLoader *)object;
153
154         image_loader_stop(il);
155
156         DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
157
158         if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
159
160         while (g_source_remove_by_user_data(il)) 
161                 {
162                 DEBUG_1("pending signals detected");
163                 }
164         
165         while (il->area_param_list) 
166                 {
167                 DEBUG_1("pending area_ready signals detected");
168                 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
169                 g_free(il->area_param_list->data);
170                 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
171                 }
172
173         if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
174         file_data_unref(il->fd);
175 #ifdef HAVE_GTHREAD
176         g_mutex_free(il->data_mutex);
177 #endif
178 }
179
180 void image_loader_free(ImageLoader *il)
181 {
182         if (!il) return;
183         g_object_unref(G_OBJECT(il));
184 }
185
186
187 ImageLoader *image_loader_new(FileData *fd)
188 {
189         ImageLoader *il;
190
191         if (!fd) return NULL;
192
193         il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
194         
195         il->fd = file_data_ref(fd);
196         
197         return il;
198 }
199
200 /**************************************************************************************/
201 /* send signals via idle calbacks - the callback are executed in the main thread */
202
203 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
204 struct _ImageLoaderAreaParam {
205         ImageLoader *il;
206         guint x;
207         guint y;
208         guint w;
209         guint h;
210 };
211
212
213 static gint image_loader_emit_area_ready_cb(gpointer data)
214 {
215         ImageLoaderAreaParam *par = data;
216         ImageLoader *il = par->il;
217         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
218         g_mutex_lock(il->data_mutex);
219         il->area_param_list = g_list_remove(il->area_param_list, par);
220         g_free(par);
221         g_mutex_unlock(il->data_mutex);
222         
223         return FALSE;
224 }
225
226 static gint image_loader_emit_done_cb(gpointer data)
227 {
228         ImageLoader *il = data;
229         g_signal_emit(il, signals[SIGNAL_DONE], 0);
230         return FALSE;
231 }
232
233 static gint image_loader_emit_error_cb(gpointer data)
234 {
235         ImageLoader *il = data;
236         g_signal_emit(il, signals[SIGNAL_ERROR], 0);
237         return FALSE;
238 }
239
240 static gint image_loader_emit_percent_cb(gpointer data)
241 {
242         ImageLoader *il = data;
243         g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
244         return FALSE;
245 }
246
247 /* DONE and ERROR are emited only once, thus they can have normal priority
248    PERCENT and AREA_READY should be processed ASAP
249 */
250
251 static void image_loader_emit_done(ImageLoader *il)
252 {
253         g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
254 }
255
256 static void image_loader_emit_error(ImageLoader *il)
257 {
258         g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
259 }
260
261 static void image_loader_emit_percent(ImageLoader *il)
262 {
263         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
264 }
265
266 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
267 {
268         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
269         par->il = il;
270         par->x = x;
271         par->y = y;
272         par->w = w;
273         par->h = h;
274         
275         g_mutex_lock(il->data_mutex);
276         il->area_param_list = g_list_prepend(il->area_param_list, par);
277         g_mutex_unlock(il->data_mutex);
278         
279         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
280 }
281
282 /**************************************************************************************/
283 /* the following functions may be executed in separate thread */
284
285 static void image_loader_sync_pixbuf(ImageLoader *il)
286 {
287         GdkPixbuf *pb;
288         
289         g_mutex_lock(il->data_mutex);
290         
291         if (!il->loader) 
292                 {
293                 g_mutex_unlock(il->data_mutex);
294                 return;
295                 }
296
297         pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
298
299         if (pb == il->pixbuf)
300                 {
301                 g_mutex_unlock(il->data_mutex);
302                 return;
303                 }
304
305         if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
306         il->pixbuf = pb;
307         if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
308         g_mutex_unlock(il->data_mutex);
309 }
310
311 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
312                                  guint x, guint y, guint w, guint h,
313                                  gpointer data)
314 {
315         ImageLoader *il = data;
316
317         if (!image_loader_get_pixbuf(il))
318                 {
319                 image_loader_sync_pixbuf(il);
320                 if (!image_loader_get_pixbuf(il))
321                         {
322                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
323                         }
324                 }
325         image_loader_emit_area_ready(il, x, y, w, h);
326 }
327
328 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
329 {
330         GdkPixbuf *pb;
331         guchar *pix;
332         size_t h, rs;
333         
334         /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
335         gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
336         if (strcmp(format, "svg") == 0)
337                 {
338                 g_free(format);
339                 return;
340                 }
341         
342         g_free(format);
343
344         pb = gdk_pixbuf_loader_get_pixbuf(loader);
345         
346         h = gdk_pixbuf_get_height(pb);
347         rs = gdk_pixbuf_get_rowstride(pb);
348         pix = gdk_pixbuf_get_pixels(pb);
349         
350         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
351
352 }
353
354 static void image_loader_size_cb(GdkPixbufLoader *loader,
355                                  gint width, gint height, gpointer data)
356 {
357         ImageLoader *il = data;
358         GdkPixbufFormat *format;
359         gchar **mime_types;
360         gint scale = FALSE;
361         gint n;
362
363         g_mutex_lock(il->data_mutex);
364         if (il->requested_width < 1 || il->requested_height < 1) 
365                 {
366                 g_mutex_unlock(il->data_mutex);
367                 return;
368                 }
369         g_mutex_unlock(il->data_mutex);
370
371         format = gdk_pixbuf_loader_get_format(loader);
372         if (!format) return;
373
374         mime_types = gdk_pixbuf_format_get_mime_types(format);
375         n = 0;
376         while (mime_types[n])
377                 {
378                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
379                 n++;
380                 }
381         g_strfreev(mime_types);
382
383         if (!scale) return;
384
385         g_mutex_lock(il->data_mutex);
386
387         if (width > il->requested_width || height > il->requested_height)
388                 {
389                 gint nw, nh;
390
391                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
392                         {
393                         nw = il->requested_width;
394                         nh = (gdouble)nw / width * height;
395                         if (nh < 1) nh = 1;
396                         }
397                 else
398                         {
399                         nh = il->requested_height;
400                         nw = (gdouble)nh / height * width;
401                         if (nw < 1) nw = 1;
402                         }
403
404                 gdk_pixbuf_loader_set_size(loader, nw, nh);
405                 il->shrunk = TRUE;
406                 }
407         g_mutex_unlock(il->data_mutex);
408
409 }
410
411 static void image_loader_stop_loader(ImageLoader *il)
412 {
413         if (!il) return;
414
415         if (il->loader)
416                 {
417                 /* some loaders do not have a pixbuf till close, order is important here */
418                 gdk_pixbuf_loader_close(il->loader, NULL);
419                 image_loader_sync_pixbuf(il);
420                 g_object_unref(G_OBJECT(il->loader));
421                 il->loader = NULL;
422                 }
423
424         il->done = TRUE;
425 }
426
427 static void image_loader_setup_loader(ImageLoader *il)
428 {
429         g_mutex_lock(il->data_mutex);
430         il->loader = gdk_pixbuf_loader_new();
431         g_mutex_unlock(il->data_mutex);
432
433         g_signal_connect(G_OBJECT(il->loader), "area_updated",
434                          G_CALLBACK(image_loader_area_updated_cb), il);
435         g_signal_connect(G_OBJECT(il->loader), "size_prepared",
436                          G_CALLBACK(image_loader_size_cb), il);
437         g_signal_connect(G_OBJECT(il->loader), "area_prepared",
438                          G_CALLBACK(image_loader_area_prepared_cb), il);
439 }
440
441
442 static void image_loader_done(ImageLoader *il)
443 {
444         image_loader_stop_loader(il);
445
446         image_loader_emit_done(il);
447 }
448
449 static void image_loader_error(ImageLoader *il)
450 {
451         image_loader_stop_loader(il);
452
453         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
454
455         image_loader_emit_error(il);
456 }
457
458 static gint image_loader_continue(ImageLoader *il)
459 {
460         gint b;
461         gint c;
462
463         if (!il) return FALSE;
464
465         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
466         while (c > 0)
467                 {
468                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
469
470                 if (b == 0)
471                         {
472                         image_loader_done(il);
473                         return FALSE;
474                         }
475
476                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
477                         {
478                         image_loader_error(il);
479                         return FALSE;
480                         }
481
482                 il->bytes_read += b;
483
484                 c--;
485                 }
486
487         if (il->bytes_total > 0)
488                 {
489                 image_loader_emit_percent(il);
490                 }
491
492         return TRUE;
493 }
494
495 static gint image_loader_begin(ImageLoader *il)
496 {
497         gint b;
498         
499         if (il->pixbuf) return FALSE;
500
501         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
502         if (b < 1) return FALSE;
503
504         image_loader_setup_loader(il);
505
506         if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
507                 {
508                 image_loader_stop_loader(il);
509                 return FALSE;
510                 }
511
512         il->bytes_read += b;
513
514         /* read until size is known */
515         while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0)
516                 {
517                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
518                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
519                         {
520                         image_loader_stop_loader(il);
521                         return FALSE;
522                         }
523                 il->bytes_read += b;
524                 }
525         if (!il->pixbuf) image_loader_sync_pixbuf(il);
526
527         if (il->bytes_read == il->bytes_total || b < 1)
528                 {
529                 /* done, handle (broken) loaders that do not have pixbuf till close */
530                 image_loader_stop_loader(il);
531
532                 if (!il->pixbuf) return FALSE;
533
534                 image_loader_done(il);
535                 return TRUE;
536                 }
537
538         if (!il->pixbuf)
539                 {
540                 image_loader_stop_loader(il);
541                 return FALSE;
542                 }
543
544         return TRUE;
545 }
546
547 /**************************************************************************************/
548 /* the following functions are always executed in the main thread */
549
550
551 static gint image_loader_setup_source(ImageLoader *il)
552 {
553         struct stat st;
554         gchar *pathl;
555
556         if (!il || il->loader || il->mapped_file) return FALSE;
557
558         il->mapped_file = NULL;
559
560         if (il->fd)
561                 {
562                 ExifData *exif = exif_read_fd(il->fd);
563
564                 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
565                 
566                 if (il->mapped_file)
567                         {
568                         il->preview = TRUE;
569                         DEBUG_1("Raw file %s contains embedded image", il->fd->path);
570                         }
571                 exif_free_fd(il->fd, exif);
572                 }
573
574         
575         if (!il->mapped_file)
576                 {
577                 /* normal file */
578                 gint load_fd;
579         
580                 pathl = path_from_utf8(il->fd->path);
581                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
582                 g_free(pathl);
583                 if (load_fd == -1) return FALSE;
584
585                 if (fstat(load_fd, &st) == 0)
586                         {
587                         il->bytes_total = st.st_size;
588                         }
589                 else
590                         {
591                         close(load_fd);
592                         return FALSE;
593                         }
594                 
595                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
596                 close(load_fd);
597                 if (il->mapped_file == MAP_FAILED)
598                         {
599                         il->mapped_file = 0;
600                         return FALSE;
601                         }
602                 il->preview = FALSE;
603                 }
604                 
605         return TRUE;
606 }
607
608 static void image_loader_stop_source(ImageLoader *il)
609 {
610         if (!il) return;
611         
612         if (il->mapped_file)
613                 {
614                 if (il->preview)
615                         {
616                         exif_free_preview(il->mapped_file);
617                         }
618                 else
619                         {
620                         munmap(il->mapped_file, il->bytes_total);
621                         }
622                 il->mapped_file = NULL;
623                 }
624 }
625
626
627 /*
628 static gint image_loader_setup(ImageLoader *il)
629 {
630         if (!image_loader_setup_source(il)) return FALSE;
631         
632         return image_loader_begin(il);
633 }
634 */
635
636 static void image_loader_stop(ImageLoader *il)
637 {
638         if (!il) return;
639
640         if (il->idle_id != -1)
641                 {
642                 g_source_remove(il->idle_id);
643                 il->idle_id = -1;
644                 }
645                 
646         if (il->thread)
647                 {
648                 il->stopping = TRUE;
649                 g_thread_join(il->thread);
650                 }
651
652         image_loader_stop_loader(il);
653         image_loader_stop_source(il);
654         
655 }
656
657 /**************************************************************************************/
658 /* execution via idle calls */
659
660 static gint image_loader_idle_cb(gpointer data)
661 {
662         gint ret = FALSE;
663         ImageLoader *il = data;
664         if (il->idle_id != -1)
665                 {
666                 ret = image_loader_continue(il);
667                 }
668         if (!ret)
669                 {
670                 image_loader_stop_source(il);
671                 }
672         return ret;
673 }
674
675
676 gint image_loader_start_idle(ImageLoader *il)
677 {
678         gint ret;
679         if (!il) return FALSE;
680
681         if (!il->fd) return FALSE;
682
683         if (!image_loader_setup_source(il)) return FALSE;
684         
685         ret = image_loader_begin(il);
686
687         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
688         return ret;
689 }
690
691 /**************************************************************************************/
692 /* execution via thread */
693
694 gpointer image_loader_thread_run(gpointer data)
695 {
696         ImageLoader *il = data;
697         gint cont = image_loader_begin(il);
698         
699         while (cont && !il->done && !il->stopping)
700                 {
701                 cont = image_loader_continue(il);
702                 }
703         return NULL;
704 }
705
706 gint image_loader_start_thread(ImageLoader *il)
707 {
708         if (!il) return FALSE;
709
710         if (!il->fd) return FALSE;
711
712         if (!image_loader_setup_source(il)) return FALSE;
713
714         il->thread = g_thread_create(image_loader_thread_run, il, TRUE, NULL);
715         if (!il->thread) return FALSE;
716                 
717         return TRUE;
718 }
719
720
721 /**************************************************************************************/
722 /* public interface */
723
724
725 gint image_loader_start(ImageLoader *il)
726 {
727         if (!il) return FALSE;
728
729         if (!il->fd) return FALSE;
730
731 #ifdef HAVE_GTHREAD
732         return image_loader_start_thread(il);
733 #else
734         return image_loader_start_idle(il);
735 #endif
736 }
737
738
739 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
740 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
741 {
742         GdkPixbuf *ret;
743         if (!il) return NULL;
744         
745         g_mutex_lock(il->data_mutex);
746         ret = il->pixbuf;
747         g_mutex_unlock(il->data_mutex);
748         return ret;
749 }
750
751 gchar *image_loader_get_format(ImageLoader *il)
752 {
753         GdkPixbufFormat *format;
754         gchar **mimev;
755         gchar *mime;
756
757         if (!il || !il->loader) return NULL;
758
759         format = gdk_pixbuf_loader_get_format(il->loader);
760         if (!format) return NULL;
761
762         mimev = gdk_pixbuf_format_get_mime_types(format);
763         if (!mimev) return NULL;
764
765         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
766         mime = g_strdup(mimev[0]);
767         g_strfreev(mimev);
768
769         return mime;
770 }
771
772 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
773 {
774         if (!il) return;
775
776         g_mutex_lock(il->data_mutex);
777         il->requested_width = width;
778         il->requested_height = height;
779         g_mutex_unlock(il->data_mutex);
780 }
781
782 void image_loader_set_buffer_size(ImageLoader *il, guint count)
783 {
784         if (!il) return;
785
786         g_mutex_lock(il->data_mutex);
787         il->idle_read_loop_count = count ? count : 1;
788         g_mutex_unlock(il->data_mutex);
789 }
790
791 void image_loader_set_priority(ImageLoader *il, gint priority)
792 {
793         if (!il) return;
794
795         g_mutex_lock(il->data_mutex);
796         il->idle_priority = priority;
797         g_mutex_unlock(il->data_mutex);
798 }
799
800
801 gdouble image_loader_get_percent(ImageLoader *il)
802 {
803         gdouble ret;
804         if (!il) return 0.0;
805         
806         g_mutex_lock(il->data_mutex);
807         if (il->bytes_total == 0) 
808                 {
809                 ret = 0.0;
810                 }
811         else
812                 {
813                 ret = (gdouble)il->bytes_read / il->bytes_total;
814                 }
815         g_mutex_unlock(il->data_mutex);
816         return ret;
817 }
818
819 gint image_loader_get_is_done(ImageLoader *il)
820 {
821         gint ret;
822         if (!il) return FALSE;
823
824         g_mutex_lock(il->data_mutex);
825         ret = il->done;
826         g_mutex_unlock(il->data_mutex);
827
828         return ret;
829 }
830
831 FileData *image_loader_get_fd(ImageLoader *il)
832 {
833         FileData *ret;
834         if (!il) return NULL;
835
836         g_mutex_lock(il->data_mutex);
837         ret = il->fd;
838         g_mutex_unlock(il->data_mutex);
839
840         return ret;
841 }
842
843 gint image_loader_get_shrunk(ImageLoader *il)
844 {
845         gint ret;
846         if (!il) return FALSE;
847
848         g_mutex_lock(il->data_mutex);
849         ret = il->shrunk;
850         g_mutex_unlock(il->data_mutex);
851         return ret;
852 }
853
854
855 /* FIXME - this can be rather slow and blocks until the size is known */
856 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
857 {
858         ImageLoader *il;
859         gint success;
860
861         il = image_loader_new(fd);
862
863         success = image_loader_start_idle(il);
864
865         if (success && il->pixbuf)
866                 {
867                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
868                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
869                 }
870         else
871                 {
872                 if (width) *width = -1;
873                 if (height) *height = -1;
874                 }
875
876         image_loader_free(il);
877
878         return success;
879 }