improved thread support in image loader
[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 gint image_loader_get_stopping(ImageLoader *il)
286 {
287         gint ret;
288         if (!il) return FALSE;
289
290         g_mutex_lock(il->data_mutex);
291         ret = il->stopping;
292         g_mutex_unlock(il->data_mutex);
293
294         return ret;
295 }
296
297 static void image_loader_sync_pixbuf(ImageLoader *il)
298 {
299         GdkPixbuf *pb;
300         
301         g_mutex_lock(il->data_mutex);
302         
303         if (!il->loader) 
304                 {
305                 g_mutex_unlock(il->data_mutex);
306                 return;
307                 }
308
309         pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
310
311         if (pb == il->pixbuf)
312                 {
313                 g_mutex_unlock(il->data_mutex);
314                 return;
315                 }
316
317         if (il->pixbuf) gdk_pixbuf_unref(il->pixbuf);
318         il->pixbuf = pb;
319         if (il->pixbuf) gdk_pixbuf_ref(il->pixbuf);
320         g_mutex_unlock(il->data_mutex);
321 }
322
323 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
324                                  guint x, guint y, guint w, guint h,
325                                  gpointer data)
326 {
327         ImageLoader *il = data;
328
329         if (!image_loader_get_pixbuf(il))
330                 {
331                 image_loader_sync_pixbuf(il);
332                 if (!image_loader_get_pixbuf(il))
333                         {
334                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
335                         }
336                 }
337         image_loader_emit_area_ready(il, x, y, w, h);
338 }
339
340 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
341 {
342         GdkPixbuf *pb;
343         guchar *pix;
344         size_t h, rs;
345         
346         /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
347         gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
348         if (strcmp(format, "svg") == 0)
349                 {
350                 g_free(format);
351                 return;
352                 }
353         
354         g_free(format);
355
356         pb = gdk_pixbuf_loader_get_pixbuf(loader);
357         
358         h = gdk_pixbuf_get_height(pb);
359         rs = gdk_pixbuf_get_rowstride(pb);
360         pix = gdk_pixbuf_get_pixels(pb);
361         
362         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
363
364 }
365
366 static void image_loader_size_cb(GdkPixbufLoader *loader,
367                                  gint width, gint height, gpointer data)
368 {
369         ImageLoader *il = data;
370         GdkPixbufFormat *format;
371         gchar **mime_types;
372         gint scale = FALSE;
373         gint n;
374
375         g_mutex_lock(il->data_mutex);
376         if (il->requested_width < 1 || il->requested_height < 1) 
377                 {
378                 g_mutex_unlock(il->data_mutex);
379                 return;
380                 }
381         g_mutex_unlock(il->data_mutex);
382
383         format = gdk_pixbuf_loader_get_format(loader);
384         if (!format) return;
385
386         mime_types = gdk_pixbuf_format_get_mime_types(format);
387         n = 0;
388         while (mime_types[n])
389                 {
390                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
391                 n++;
392                 }
393         g_strfreev(mime_types);
394
395         if (!scale) return;
396
397         g_mutex_lock(il->data_mutex);
398
399         if (width > il->requested_width || height > il->requested_height)
400                 {
401                 gint nw, nh;
402
403                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
404                         {
405                         nw = il->requested_width;
406                         nh = (gdouble)nw / width * height;
407                         if (nh < 1) nh = 1;
408                         }
409                 else
410                         {
411                         nh = il->requested_height;
412                         nw = (gdouble)nh / height * width;
413                         if (nw < 1) nw = 1;
414                         }
415
416                 gdk_pixbuf_loader_set_size(loader, nw, nh);
417                 il->shrunk = TRUE;
418                 }
419         g_mutex_unlock(il->data_mutex);
420
421 }
422
423 static void image_loader_stop_loader(ImageLoader *il)
424 {
425         if (!il) return;
426
427         if (il->loader)
428                 {
429                 /* some loaders do not have a pixbuf till close, order is important here */
430                 gdk_pixbuf_loader_close(il->loader, NULL);
431                 image_loader_sync_pixbuf(il);
432                 g_object_unref(G_OBJECT(il->loader));
433                 il->loader = NULL;
434                 }
435         g_mutex_lock(il->data_mutex);
436         il->done = TRUE;
437         g_mutex_unlock(il->data_mutex);
438 }
439
440 static void image_loader_setup_loader(ImageLoader *il)
441 {
442         g_mutex_lock(il->data_mutex);
443         il->loader = gdk_pixbuf_loader_new();
444
445         g_signal_connect(G_OBJECT(il->loader), "area_updated",
446                          G_CALLBACK(image_loader_area_updated_cb), il);
447         g_signal_connect(G_OBJECT(il->loader), "size_prepared",
448                          G_CALLBACK(image_loader_size_cb), il);
449         g_signal_connect(G_OBJECT(il->loader), "area_prepared",
450                          G_CALLBACK(image_loader_area_prepared_cb), il);
451         g_mutex_unlock(il->data_mutex);
452 }
453
454
455 static void image_loader_done(ImageLoader *il)
456 {
457         image_loader_stop_loader(il);
458
459         image_loader_emit_done(il);
460 }
461
462 static void image_loader_error(ImageLoader *il)
463 {
464         image_loader_stop_loader(il);
465
466         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
467
468         image_loader_emit_error(il);
469 }
470
471 static gint image_loader_continue(ImageLoader *il)
472 {
473         gint b;
474         gint c;
475
476         if (!il) return FALSE;
477
478         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
479         while (c > 0 && !image_loader_get_stopping(il))
480                 {
481                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
482
483                 if (b == 0)
484                         {
485                         image_loader_done(il);
486                         return FALSE;
487                         }
488
489                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
490                         {
491                         image_loader_error(il);
492                         return FALSE;
493                         }
494
495                 il->bytes_read += b;
496
497                 c--;
498                 }
499
500         if (il->bytes_total > 0)
501                 {
502                 image_loader_emit_percent(il);
503                 }
504
505         return TRUE;
506 }
507
508 static gint image_loader_begin(ImageLoader *il)
509 {
510         gint b;
511         
512         if (il->pixbuf) return FALSE;
513
514         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
515         if (b < 1) return FALSE;
516
517         image_loader_setup_loader(il);
518
519         if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL))
520                 {
521                 image_loader_stop_loader(il);
522                 return FALSE;
523                 }
524
525         il->bytes_read += b;
526
527         /* read until size is known */
528         while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
529                 {
530                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
531                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, NULL)))
532                         {
533                         image_loader_stop_loader(il);
534                         return FALSE;
535                         }
536                 il->bytes_read += b;
537                 }
538         if (!il->pixbuf) image_loader_sync_pixbuf(il);
539
540         if (il->bytes_read == il->bytes_total || b < 1)
541                 {
542                 /* done, handle (broken) loaders that do not have pixbuf till close */
543                 image_loader_stop_loader(il);
544
545                 if (!il->pixbuf) return FALSE;
546
547                 image_loader_done(il);
548                 return TRUE;
549                 }
550
551         if (!il->pixbuf)
552                 {
553                 image_loader_stop_loader(il);
554                 return FALSE;
555                 }
556
557         return TRUE;
558 }
559
560 /**************************************************************************************/
561 /* the following functions are always executed in the main thread */
562
563
564 static gint image_loader_setup_source(ImageLoader *il)
565 {
566         struct stat st;
567         gchar *pathl;
568
569         if (!il || il->loader || il->mapped_file) return FALSE;
570
571         il->mapped_file = NULL;
572
573         if (il->fd)
574                 {
575                 ExifData *exif = exif_read_fd(il->fd);
576
577                 il->mapped_file = exif_get_preview(exif, &il->bytes_total);
578                 
579                 if (il->mapped_file)
580                         {
581                         il->preview = TRUE;
582                         DEBUG_1("Raw file %s contains embedded image", il->fd->path);
583                         }
584                 exif_free_fd(il->fd, exif);
585                 }
586
587         
588         if (!il->mapped_file)
589                 {
590                 /* normal file */
591                 gint load_fd;
592         
593                 pathl = path_from_utf8(il->fd->path);
594                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
595                 g_free(pathl);
596                 if (load_fd == -1) return FALSE;
597
598                 if (fstat(load_fd, &st) == 0)
599                         {
600                         il->bytes_total = st.st_size;
601                         }
602                 else
603                         {
604                         close(load_fd);
605                         return FALSE;
606                         }
607                 
608                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
609                 close(load_fd);
610                 if (il->mapped_file == MAP_FAILED)
611                         {
612                         il->mapped_file = 0;
613                         return FALSE;
614                         }
615                 il->preview = FALSE;
616                 }
617                 
618         return TRUE;
619 }
620
621 static void image_loader_stop_source(ImageLoader *il)
622 {
623         if (!il) return;
624         
625         if (il->mapped_file)
626                 {
627                 if (il->preview)
628                         {
629                         exif_free_preview(il->mapped_file);
630                         }
631                 else
632                         {
633                         munmap(il->mapped_file, il->bytes_total);
634                         }
635                 il->mapped_file = NULL;
636                 }
637 }
638
639 static void image_loader_stop(ImageLoader *il)
640 {
641         if (!il) return;
642
643         if (il->idle_id != -1)
644                 {
645                 g_source_remove(il->idle_id);
646                 il->idle_id = -1;
647                 }
648                 
649         if (il->thread)
650                 {
651                 g_mutex_lock(il->data_mutex);
652                 il->stopping = TRUE;
653                 g_mutex_unlock(il->data_mutex);
654                 g_thread_join(il->thread);
655                 }
656
657         image_loader_stop_loader(il);
658         image_loader_stop_source(il);
659         
660 }
661
662 /**************************************************************************************/
663 /* execution via idle calls */
664
665 static gint image_loader_idle_cb(gpointer data)
666 {
667         gint ret = FALSE;
668         ImageLoader *il = data;
669         if (il->idle_id != -1)
670                 {
671                 ret = image_loader_continue(il);
672                 }
673         if (!ret)
674                 {
675                 image_loader_stop_source(il);
676                 }
677         return ret;
678 }
679
680
681 gint image_loader_start_idle(ImageLoader *il)
682 {
683         gint ret;
684         if (!il) return FALSE;
685
686         if (!il->fd) return FALSE;
687
688         if (!image_loader_setup_source(il)) return FALSE;
689         
690         ret = image_loader_begin(il);
691
692         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
693         return ret;
694 }
695
696 /**************************************************************************************/
697 /* execution via thread */
698
699 gpointer image_loader_thread_run(gpointer data)
700 {
701         ImageLoader *il = data;
702         gint cont = image_loader_begin(il);
703         
704         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
705                 {
706                 cont = image_loader_continue(il);
707                 }
708         image_loader_stop_loader(il);
709         return NULL;
710 }
711
712 gint image_loader_start_thread(ImageLoader *il)
713 {
714         if (!il) return FALSE;
715
716         if (!il->fd) return FALSE;
717
718         if (!image_loader_setup_source(il)) return FALSE;
719
720         il->thread = g_thread_create(image_loader_thread_run, il, TRUE, NULL);
721         if (!il->thread) return FALSE;
722                 
723         return TRUE;
724 }
725
726
727 /**************************************************************************************/
728 /* public interface */
729
730
731 gint image_loader_start(ImageLoader *il)
732 {
733         if (!il) return FALSE;
734
735         if (!il->fd) return FALSE;
736
737 #ifdef HAVE_GTHREAD
738         return image_loader_start_thread(il);
739 #else
740         return image_loader_start_idle(il);
741 #endif
742 }
743
744
745 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
746 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
747 {
748         GdkPixbuf *ret;
749         if (!il) return NULL;
750         
751         g_mutex_lock(il->data_mutex);
752         ret = il->pixbuf;
753         g_mutex_unlock(il->data_mutex);
754         return ret;
755 }
756
757 gchar *image_loader_get_format(ImageLoader *il)
758 {
759         GdkPixbufFormat *format;
760         gchar **mimev;
761         gchar *mime;
762
763         if (!il || !il->loader) return NULL;
764
765         format = gdk_pixbuf_loader_get_format(il->loader);
766         if (!format) return NULL;
767
768         mimev = gdk_pixbuf_format_get_mime_types(format);
769         if (!mimev) return NULL;
770
771         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
772         mime = g_strdup(mimev[0]);
773         g_strfreev(mimev);
774
775         return mime;
776 }
777
778 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
779 {
780         if (!il) return;
781
782         g_mutex_lock(il->data_mutex);
783         il->requested_width = width;
784         il->requested_height = height;
785         g_mutex_unlock(il->data_mutex);
786 }
787
788 void image_loader_set_buffer_size(ImageLoader *il, guint count)
789 {
790         if (!il) return;
791
792         g_mutex_lock(il->data_mutex);
793         il->idle_read_loop_count = count ? count : 1;
794         g_mutex_unlock(il->data_mutex);
795 }
796
797 void image_loader_set_priority(ImageLoader *il, gint priority)
798 {
799         if (!il) return;
800
801         g_mutex_lock(il->data_mutex);
802         il->idle_priority = priority;
803         g_mutex_unlock(il->data_mutex);
804 }
805
806
807 gdouble image_loader_get_percent(ImageLoader *il)
808 {
809         gdouble ret;
810         if (!il) return 0.0;
811         
812         g_mutex_lock(il->data_mutex);
813         if (il->bytes_total == 0) 
814                 {
815                 ret = 0.0;
816                 }
817         else
818                 {
819                 ret = (gdouble)il->bytes_read / il->bytes_total;
820                 }
821         g_mutex_unlock(il->data_mutex);
822         return ret;
823 }
824
825 gint image_loader_get_is_done(ImageLoader *il)
826 {
827         gint ret;
828         if (!il) return FALSE;
829
830         g_mutex_lock(il->data_mutex);
831         ret = il->done;
832         g_mutex_unlock(il->data_mutex);
833
834         return ret;
835 }
836
837 FileData *image_loader_get_fd(ImageLoader *il)
838 {
839         FileData *ret;
840         if (!il) return NULL;
841
842         g_mutex_lock(il->data_mutex);
843         ret = il->fd;
844         g_mutex_unlock(il->data_mutex);
845
846         return ret;
847 }
848
849 gint image_loader_get_shrunk(ImageLoader *il)
850 {
851         gint ret;
852         if (!il) return FALSE;
853
854         g_mutex_lock(il->data_mutex);
855         ret = il->shrunk;
856         g_mutex_unlock(il->data_mutex);
857         return ret;
858 }
859
860
861 /* FIXME - this can be rather slow and blocks until the size is known */
862 gint image_load_dimensions(FileData *fd, gint *width, gint *height)
863 {
864         ImageLoader *il;
865         gint success;
866
867         il = image_loader_new(fd);
868
869         success = image_loader_start_idle(il);
870
871         if (success && il->pixbuf)
872                 {
873                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
874                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
875                 }
876         else
877                 {
878                 if (width) *width = -1;
879                 if (height) *height = -1;
880                 }
881
882         image_loader_free(il);
883
884         return success;
885 }