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