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