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