added custom tiff loader
[geeqie.git] / src / image-load.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2010 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 #include "image_load_gdk.h"
17 #include "image_load_jpeg.h"
18 #include "image_load_tiff.h"
19
20 #include "exif.h"
21 #include "filedata.h"
22 #include "ui_fileops.h"
23 #include "gq-marshal.h"
24
25 #include <fcntl.h>
26 #include <sys/mman.h>
27
28 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT   4096
29 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT       1
30
31
32 /**************************************************************************************/
33 /* image loader class */
34
35
36 enum {
37         SIGNAL_AREA_READY = 0,
38         SIGNAL_ERROR,
39         SIGNAL_DONE,
40         SIGNAL_PERCENT,
41         SIGNAL_SIZE,
42         SIGNAL_COUNT
43 };
44
45 static guint signals[SIGNAL_COUNT] = { 0 };
46
47 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
48 static void image_loader_class_init(ImageLoaderClass *class);
49 static void image_loader_finalize(GObject *object);
50 static void image_loader_stop(ImageLoader *il);
51
52 GType image_loader_get_type(void)
53 {
54         static GType type = 0;
55         if (type == 0) 
56                 {
57                 static const GTypeInfo info = {
58                         sizeof(ImageLoaderClass),
59                         NULL,   /* base_init */
60                         NULL,   /* base_finalize */
61                         (GClassInitFunc)image_loader_class_init, /* class_init */
62                         NULL,   /* class_finalize */
63                         NULL,   /* class_data */
64                         sizeof(ImageLoader),
65                         0,      /* n_preallocs */
66                         (GInstanceInitFunc)image_loader_init, /* instance_init */
67                         NULL    /* value_table */
68                         };
69                 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
70                 }
71         return type;
72 }
73
74 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
75 {
76         ImageLoader *il = (ImageLoader *)instance;
77
78         il->pixbuf = NULL;
79         il->idle_id = 0;
80         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
81         il->done = FALSE;
82         il->loader = NULL;
83
84         il->bytes_read = 0;
85         il->bytes_total = 0;
86
87         il->idle_done_id = 0;
88
89         il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
90         il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
91         il->mapped_file = NULL;
92
93         il->requested_width = 0;
94         il->requested_height = 0;
95         il->shrunk = FALSE;
96
97         il->can_destroy = TRUE;
98
99 #ifdef HAVE_GTHREAD
100         il->data_mutex = g_mutex_new();
101         il->can_destroy_cond = g_cond_new();
102 #endif
103         DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
104 }
105
106 static void image_loader_class_init(ImageLoaderClass *class)
107 {
108         GObjectClass *gobject_class = G_OBJECT_CLASS (class);
109
110 //      gobject_class->set_property = image_loader_set_property;
111 //      gobject_class->get_property = image_loader_get_property;
112
113         gobject_class->finalize = image_loader_finalize;
114
115
116         signals[SIGNAL_AREA_READY] =
117                 g_signal_new("area_ready",
118                              G_OBJECT_CLASS_TYPE(gobject_class),
119                              G_SIGNAL_RUN_LAST,
120                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
121                              NULL, NULL,
122                              gq_marshal_VOID__INT_INT_INT_INT,
123                              G_TYPE_NONE, 4,
124                              G_TYPE_INT,
125                              G_TYPE_INT,
126                              G_TYPE_INT,
127                              G_TYPE_INT);
128
129         signals[SIGNAL_ERROR] =
130                 g_signal_new("error",
131                              G_OBJECT_CLASS_TYPE(gobject_class),
132                              G_SIGNAL_RUN_LAST,
133                              G_STRUCT_OFFSET(ImageLoaderClass, error),
134                              NULL, NULL,
135                              g_cclosure_marshal_VOID__VOID,
136                              G_TYPE_NONE, 0);
137
138         signals[SIGNAL_DONE] =
139                 g_signal_new("done",
140                              G_OBJECT_CLASS_TYPE(gobject_class),
141                              G_SIGNAL_RUN_LAST,
142                              G_STRUCT_OFFSET(ImageLoaderClass, done),
143                              NULL, NULL,
144                              g_cclosure_marshal_VOID__VOID,
145                              G_TYPE_NONE, 0);
146
147         signals[SIGNAL_PERCENT] =
148                 g_signal_new("percent",
149                              G_OBJECT_CLASS_TYPE(gobject_class),
150                              G_SIGNAL_RUN_LAST,
151                              G_STRUCT_OFFSET(ImageLoaderClass, percent),
152                              NULL, NULL,
153                              g_cclosure_marshal_VOID__DOUBLE,
154                              G_TYPE_NONE, 1,
155                              G_TYPE_DOUBLE);
156
157         signals[SIGNAL_SIZE] =
158                 g_signal_new("size_prepared",
159                              G_OBJECT_CLASS_TYPE(gobject_class),
160                              G_SIGNAL_RUN_LAST,
161                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
162                              NULL, NULL,
163                              gq_marshal_VOID__INT_INT,
164                              G_TYPE_NONE, 2,
165                              G_TYPE_INT,
166                              G_TYPE_INT);
167
168 }
169
170 static void image_loader_finalize(GObject *object)
171 {
172         ImageLoader *il = (ImageLoader *)object;
173
174         image_loader_stop(il);
175
176         if (il->error) DEBUG_1("%s", image_loader_get_error(il));
177
178         DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
179
180         if (il->idle_done_id)
181                 {
182                 g_source_remove(il->idle_done_id);
183                 il->idle_done_id = 0;
184                 }
185
186         while (g_source_remove_by_user_data(il)) 
187                 {
188                 DEBUG_2("pending signals detected");
189                 }
190         
191         while (il->area_param_list) 
192                 {
193                 DEBUG_1("pending area_ready signals detected");
194                 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
195                 g_free(il->area_param_list->data);
196                 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
197                 }
198
199         while (il->area_param_delayed_list) 
200                 {
201                 g_free(il->area_param_delayed_list->data);
202                 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
203                 }
204
205         if (il->pixbuf) g_object_unref(il->pixbuf);
206         
207         if (il->error) g_error_free(il->error);
208
209         file_data_unref(il->fd);
210 #ifdef HAVE_GTHREAD
211         g_mutex_free(il->data_mutex);
212         g_cond_free(il->can_destroy_cond);
213 #endif
214 }
215
216 void image_loader_free(ImageLoader *il)
217 {
218         if (!il) return;
219         g_object_unref(G_OBJECT(il));
220 }
221
222
223 ImageLoader *image_loader_new(FileData *fd)
224 {
225         ImageLoader *il;
226
227         if (!fd) return NULL;
228
229         il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
230         
231         il->fd = file_data_ref(fd);
232         
233         return il;
234 }
235
236 /**************************************************************************************/
237 /* send signals via idle calbacks - the callback are executed in the main thread */
238
239 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
240 struct _ImageLoaderAreaParam {
241         ImageLoader *il;
242         guint x;
243         guint y;
244         guint w;
245         guint h;
246 };
247
248
249 static gboolean image_loader_emit_area_ready_cb(gpointer data)
250 {
251         ImageLoaderAreaParam *par = data;
252         ImageLoader *il = par->il;
253         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
254         g_mutex_lock(il->data_mutex);
255         il->area_param_list = g_list_remove(il->area_param_list, par);
256         g_free(par);
257         g_mutex_unlock(il->data_mutex);
258         
259         return FALSE;
260 }
261
262 static gboolean image_loader_emit_done_cb(gpointer data)
263 {
264         ImageLoader *il = data;
265         g_signal_emit(il, signals[SIGNAL_DONE], 0);
266         return FALSE;
267 }
268
269 static gboolean image_loader_emit_error_cb(gpointer data)
270 {
271         ImageLoader *il = data;
272         g_signal_emit(il, signals[SIGNAL_ERROR], 0);
273         return FALSE;
274 }
275
276 static gboolean image_loader_emit_percent_cb(gpointer data)
277 {
278         ImageLoader *il = data;
279         g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
280         return FALSE;
281 }
282
283 /* DONE and ERROR are emited only once, thus they can have normal priority
284    PERCENT and AREA_READY should be processed ASAP
285 */
286
287 static void image_loader_emit_done(ImageLoader *il)
288 {
289         g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
290 }
291
292 static void image_loader_emit_error(ImageLoader *il)
293 {
294         g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
295 }
296
297 static void image_loader_emit_percent(ImageLoader *il)
298 {
299         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
300 }
301
302 /* this function expects that il->data_mutex is locked by caller */
303 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
304 {
305         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
306         par->il = il;
307         par->x = x;
308         par->y = y;
309         par->w = w;
310         par->h = h;
311         
312         il->area_param_list = g_list_prepend(il->area_param_list, par);
313         
314         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
315 }
316
317 /**************************************************************************************/
318 /* the following functions may be executed in separate thread */
319
320 /* this function expects that il->data_mutex is locked by caller */
321 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
322 {
323         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
324         par->il = il;
325         par->x = x;
326         par->y = y;
327         par->w = w;
328         par->h = h;
329         
330         il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
331 }
332
333
334
335 static gboolean image_loader_get_stopping(ImageLoader *il)
336 {
337         gboolean ret;
338         if (!il) return FALSE;
339
340         g_mutex_lock(il->data_mutex);
341         ret = il->stopping;
342         g_mutex_unlock(il->data_mutex);
343
344         return ret;
345 }
346
347
348 static void image_loader_sync_pixbuf(ImageLoader *il)
349 {
350         GdkPixbuf *pb;
351         
352         g_mutex_lock(il->data_mutex);
353         
354         if (!il->loader) 
355                 {
356                 g_mutex_unlock(il->data_mutex);
357                 return;
358                 }
359
360         pb = il->backend.get_pixbuf(il->loader);
361
362         if (pb == il->pixbuf)
363                 {
364                 g_mutex_unlock(il->data_mutex);
365                 return;
366                 }
367
368         if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
369                 {
370                 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
371                 }
372
373         if (il->pixbuf) g_object_unref(il->pixbuf);
374
375         il->pixbuf = pb;
376         if (il->pixbuf) g_object_ref(il->pixbuf);
377
378         g_mutex_unlock(il->data_mutex);
379 }
380
381 static void image_loader_area_updated_cb(gpointer loader,
382                                  guint x, guint y, guint w, guint h,
383                                  gpointer data)
384 {
385         ImageLoader *il = data;
386
387         if (!image_loader_get_pixbuf(il))
388                 {
389                 image_loader_sync_pixbuf(il);
390                 if (!image_loader_get_pixbuf(il))
391                         {
392                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
393                         }
394                 }
395
396         g_mutex_lock(il->data_mutex);
397         if (il->delay_area_ready)
398                 image_loader_queue_delayed_area_ready(il, x, y, w, h);
399         else
400                 image_loader_emit_area_ready(il, x, y, w, h);
401         
402         if (il->stopping) il->backend.abort(il->loader);
403
404         g_mutex_unlock(il->data_mutex);
405 }
406
407 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
408 {
409         ImageLoader *il = data;
410         GdkPixbuf *pb;
411         guchar *pix;
412         size_t h, rs;
413         
414         /* a workaround for 
415            http://bugzilla.gnome.org/show_bug.cgi?id=547669 
416            http://bugzilla.gnome.org/show_bug.cgi?id=589334
417         */
418         gchar *format = il->backend.get_format_name(loader);
419         if (strcmp(format, "svg") == 0 ||
420             strcmp(format, "xpm") == 0)
421                 {
422                 g_free(format);
423                 return;
424                 }
425         
426         g_free(format);
427
428         pb = il->backend.get_pixbuf(loader);
429         
430         h = gdk_pixbuf_get_height(pb);
431         rs = gdk_pixbuf_get_rowstride(pb);
432         pix = gdk_pixbuf_get_pixels(pb);
433         
434         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
435
436 }
437
438 static void image_loader_size_cb(gpointer loader,
439                                  gint width, gint height, gpointer data)
440 {
441         ImageLoader *il = data;
442         gchar **mime_types;
443         gboolean scale = FALSE;
444         gint n;
445
446         g_mutex_lock(il->data_mutex);
447         if (il->requested_width < 1 || il->requested_height < 1) 
448                 {
449                 g_mutex_unlock(il->data_mutex);
450                 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
451                 return;
452                 }
453         g_mutex_unlock(il->data_mutex);
454
455         mime_types = il->backend.get_format_mime_types(loader);
456         n = 0;
457         while (mime_types[n])
458                 {
459                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
460                 n++;
461                 }
462         g_strfreev(mime_types);
463
464         if (!scale)
465                 {
466                 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
467                 return;
468                 }
469
470         g_mutex_lock(il->data_mutex);
471
472         gint nw, nh;
473         if (width > il->requested_width || height > il->requested_height)
474                 {
475
476                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
477                         {
478                         nw = il->requested_width;
479                         nh = (gdouble)nw / width * height;
480                         if (nh < 1) nh = 1;
481                         }
482                 else
483                         {
484                         nh = il->requested_height;
485                         nw = (gdouble)nh / height * width;
486                         if (nw < 1) nw = 1;
487                         }
488
489                 il->backend.set_size(loader, nw, nh);
490                 il->shrunk = TRUE;
491                 }
492         g_mutex_unlock(il->data_mutex);
493
494         g_signal_emit(il, signals[SIGNAL_SIZE], 0, nw, nh);
495 }
496
497 static void image_loader_stop_loader(ImageLoader *il)
498 {
499         if (!il) return;
500
501         if (il->loader)
502                 {
503                 /* some loaders do not have a pixbuf till close, order is important here */
504                 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
505                 image_loader_sync_pixbuf(il);
506                 il->backend.free(il->loader);
507                 il->loader = NULL;
508                 }
509         g_mutex_lock(il->data_mutex);
510         il->done = TRUE;
511         g_mutex_unlock(il->data_mutex);
512 }
513
514 static void image_loader_setup_loader(ImageLoader *il)
515 {
516         g_mutex_lock(il->data_mutex);
517 #ifdef HAVE_JPEG
518         if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
519                 {
520                 DEBUG_1("Using custom jpeg loader");
521                 image_loader_backend_set_jpeg(&il->backend);
522                 }
523         else
524 #endif
525 #ifdef HAVE_TIFF
526         if (il->bytes_total >= 10 &&
527             (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
528              memcmp(il->mapped_file, "II*\0", 4) == 0))
529                 {
530                 DEBUG_1("Using custom tiff loader");
531                 image_loader_backend_set_tiff(&il->backend);
532                 }
533         else
534 #endif
535                 image_loader_backend_set_default(&il->backend);
536
537         il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
538         g_mutex_unlock(il->data_mutex);
539 }
540
541
542 static void image_loader_done(ImageLoader *il)
543 {
544         image_loader_stop_loader(il);
545
546         image_loader_emit_done(il);
547 }
548
549 static void image_loader_error(ImageLoader *il)
550 {
551         image_loader_stop_loader(il);
552
553         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
554
555         image_loader_emit_error(il);
556 }
557
558 static gboolean image_loader_continue(ImageLoader *il)
559 {
560         gint b;
561         gint c;
562
563         if (!il) return FALSE;
564
565         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
566         while (c > 0 && !image_loader_get_stopping(il))
567                 {
568                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
569
570                 if (b == 0)
571                         {
572                         image_loader_done(il);
573                         return FALSE;
574                         }
575
576                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
577                         {
578                         image_loader_error(il);
579                         return FALSE;
580                         }
581
582                 il->bytes_read += b;
583
584                 c--;
585                 }
586
587         if (il->bytes_total > 0)
588                 {
589                 image_loader_emit_percent(il);
590                 }
591
592         return TRUE;
593 }
594
595 static gboolean image_loader_begin(ImageLoader *il)
596 {
597         gssize b;
598         
599         if (il->pixbuf) return FALSE;
600
601         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
602         if (b < 1) return FALSE;
603
604         image_loader_setup_loader(il);
605         
606         g_assert(il->bytes_read == 0);
607         if (il->backend.load) {
608                 b = il->bytes_total;
609                 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
610                         {
611                         image_loader_stop_loader(il);
612                         return FALSE;
613                         }
614         }
615         else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
616                 {
617                 image_loader_stop_loader(il);
618                 return FALSE;
619                 }
620
621         il->bytes_read += b;
622
623         /* read until size is known */
624         while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
625                 {
626                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
627                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
628                         {
629                         image_loader_stop_loader(il);
630                         return FALSE;
631                         }
632                 il->bytes_read += b;
633                 }
634         if (!il->pixbuf) image_loader_sync_pixbuf(il);
635
636         if (il->bytes_read == il->bytes_total || b < 1)
637                 {
638                 /* done, handle (broken) loaders that do not have pixbuf till close */
639                 image_loader_stop_loader(il);
640
641                 if (!il->pixbuf) return FALSE;
642
643                 image_loader_done(il);
644                 return TRUE;
645                 }
646
647         if (!il->pixbuf)
648                 {
649                 image_loader_stop_loader(il);
650                 return FALSE;
651                 }
652
653         return TRUE;
654 }
655
656 /**************************************************************************************/
657 /* the following functions are always executed in the main thread */
658
659
660 static gboolean image_loader_setup_source(ImageLoader *il)
661 {
662         struct stat st;
663         gchar *pathl;
664
665         if (!il || il->loader || il->mapped_file) return FALSE;
666
667         il->mapped_file = NULL;
668
669         if (il->fd)
670                 {
671                 ExifData *exif = exif_read_fd(il->fd);
672
673                 if (options->thumbnails.use_exif)
674                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
675                 else
676                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
677
678                 if (il->mapped_file)
679                         {
680                         il->preview = TRUE;
681                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
682                         }
683                 exif_free_fd(il->fd, exif);
684                 }
685
686         
687         if (!il->mapped_file)
688                 {
689                 /* normal file */
690                 gint load_fd;
691         
692                 pathl = path_from_utf8(il->fd->path);
693                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
694                 g_free(pathl);
695                 if (load_fd == -1) return FALSE;
696
697                 if (fstat(load_fd, &st) == 0)
698                         {
699                         il->bytes_total = st.st_size;
700                         }
701                 else
702                         {
703                         close(load_fd);
704                         return FALSE;
705                         }
706                 
707                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
708                 close(load_fd);
709                 if (il->mapped_file == MAP_FAILED)
710                         {
711                         il->mapped_file = 0;
712                         return FALSE;
713                         }
714                 il->preview = FALSE;
715                 }
716                 
717         return TRUE;
718 }
719
720 static void image_loader_stop_source(ImageLoader *il)
721 {
722         if (!il) return;
723         
724         if (il->mapped_file)
725                 {
726                 if (il->preview)
727                         {
728                         exif_free_preview(il->mapped_file);
729                         }
730                 else
731                         {
732                         munmap(il->mapped_file, il->bytes_total);
733                         }
734                 il->mapped_file = NULL;
735                 }
736 }
737
738 static void image_loader_stop(ImageLoader *il)
739 {
740         if (!il) return;
741
742         if (il->idle_id)
743                 {
744                 g_source_remove(il->idle_id);
745                 il->idle_id = 0;
746                 }
747                 
748         if (il->thread)
749                 {
750                 /* stop loader in the other thread */
751                 g_mutex_lock(il->data_mutex);
752                 il->stopping = TRUE;
753                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
754                 g_mutex_unlock(il->data_mutex);
755                 }
756
757         image_loader_stop_loader(il);
758         image_loader_stop_source(il);
759         
760 }
761
762 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
763 {
764         g_mutex_lock(il->data_mutex);
765         il->delay_area_ready = enable;
766         if (!enable)
767                 {
768                 /* send delayed */
769                 GList *list, *work;
770                 list = g_list_reverse(il->area_param_delayed_list);
771                 il->area_param_delayed_list = NULL;
772                 g_mutex_unlock(il->data_mutex);
773
774                 work = list;
775
776                 while (work)
777                         {
778                         ImageLoaderAreaParam *par = work->data;
779                         work = work->next;
780                         
781                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
782                         g_free(par);
783                         }
784                 g_list_free(list);
785                 }
786         else
787                 {
788                 /* just unlock */
789                 g_mutex_unlock(il->data_mutex);
790                 }
791 }
792
793
794 /**************************************************************************************/
795 /* execution via idle calls */
796
797 static gboolean image_loader_idle_cb(gpointer data)
798 {
799         gboolean ret = FALSE;
800         ImageLoader *il = data;
801
802         if (il->idle_id)
803                 {
804                 ret = image_loader_continue(il);
805                 }
806         
807         if (!ret)
808                 {
809                 image_loader_stop_source(il);
810                 }
811         
812         return ret;
813 }
814
815
816 static gboolean image_loader_start_idle(ImageLoader *il)
817 {
818         gboolean ret;
819         
820         if (!il) return FALSE;
821
822         if (!il->fd) return FALSE;
823
824         if (!image_loader_setup_source(il)) return FALSE;
825         
826         ret = image_loader_begin(il);
827
828         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
829         return ret;
830 }
831
832 /**************************************************************************************/
833 /* execution via thread */
834
835 #ifdef HAVE_GTHREAD
836 static GThreadPool *image_loader_thread_pool = NULL;
837
838 static GCond *image_loader_prio_cond = NULL;
839 static GMutex *image_loader_prio_mutex = NULL;
840 static gint image_loader_prio_num = 0;
841
842
843 static void image_loader_thread_enter_high(void)
844 {
845         g_mutex_lock(image_loader_prio_mutex);
846         image_loader_prio_num++;
847         g_mutex_unlock(image_loader_prio_mutex);
848 }
849
850 static void image_loader_thread_leave_high(void)
851 {
852         g_mutex_lock(image_loader_prio_mutex);
853         image_loader_prio_num--;
854         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
855         g_mutex_unlock(image_loader_prio_mutex);
856 }
857
858 static void image_loader_thread_wait_high(void)
859 {
860         g_mutex_lock(image_loader_prio_mutex);
861         while (image_loader_prio_num) 
862                 {
863                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
864                 }
865
866         g_mutex_unlock(image_loader_prio_mutex);
867 }
868
869
870 static void image_loader_thread_run(gpointer data, gpointer user_data)
871 {
872         ImageLoader *il = data;
873         gboolean cont;
874         gboolean err;
875         
876         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
877                 {
878                 /* low prio, wait untill high prio tasks finishes */
879                 image_loader_thread_wait_high();
880                 }
881         else
882                 {
883                 /* high prio */
884                 image_loader_thread_enter_high();
885                 }
886         
887         err = !image_loader_begin(il);
888         
889         if (err)
890                 {
891                 /* 
892                 loader failed, we have to send signal 
893                 (idle mode returns the image_loader_begin return value directly)
894                 (success is always reported indirectly from image_loader_begin)
895                 */
896                 image_loader_emit_error(il);
897                 }
898         
899         cont = !err;
900         
901         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
902                 {
903                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
904                         {
905                         /* low prio, wait untill high prio tasks finishes */
906                         image_loader_thread_wait_high();
907                         }
908                 cont = image_loader_continue(il);
909                 }
910         image_loader_stop_loader(il);
911
912         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE) 
913                 {
914                 /* high prio */
915                 image_loader_thread_leave_high();
916                 }
917
918         g_mutex_lock(il->data_mutex);
919         il->can_destroy = TRUE;
920         g_cond_signal(il->can_destroy_cond);
921         g_mutex_unlock(il->data_mutex);
922
923 }
924
925
926 static gboolean image_loader_start_thread(ImageLoader *il)
927 {
928         if (!il) return FALSE;
929
930         if (!il->fd) return FALSE;
931
932         il->thread = TRUE;
933         
934         if (!image_loader_setup_source(il)) return FALSE;
935
936         if (!image_loader_thread_pool) 
937                 {
938                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
939                 image_loader_prio_cond = g_cond_new();
940                 image_loader_prio_mutex = g_mutex_new();
941                 }
942
943         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
944
945         g_thread_pool_push(image_loader_thread_pool, il, NULL);
946         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
947                 
948         return TRUE;
949 }
950 #endif /* HAVE_GTHREAD */
951
952
953 /**************************************************************************************/
954 /* public interface */
955
956
957 gboolean image_loader_start(ImageLoader *il)
958 {
959         if (!il) return FALSE;
960
961         if (!il->fd) return FALSE;
962
963 #ifdef HAVE_GTHREAD
964         return image_loader_start_thread(il);
965 #else
966         return image_loader_start_idle(il);
967 #endif
968 }
969
970
971 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
972 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
973 {
974         GdkPixbuf *ret;
975         if (!il) return NULL;
976         
977         g_mutex_lock(il->data_mutex);
978         ret = il->pixbuf;
979         g_mutex_unlock(il->data_mutex);
980         return ret;
981 }
982
983 gchar *image_loader_get_format(ImageLoader *il)
984 {
985         gchar **mimev;
986         gchar *mime;
987
988         if (!il || !il->loader) return NULL;
989
990         mimev = il->backend.get_format_mime_types(il->loader);
991         if (!mimev) return NULL;
992
993         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
994         mime = g_strdup(mimev[0]);
995         g_strfreev(mimev);
996
997         return mime;
998 }
999
1000 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1001 {
1002         if (!il) return;
1003
1004         g_mutex_lock(il->data_mutex);
1005         il->requested_width = width;
1006         il->requested_height = height;
1007         g_mutex_unlock(il->data_mutex);
1008 }
1009
1010 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1011 {
1012         if (!il) return;
1013
1014         g_mutex_lock(il->data_mutex);
1015         il->idle_read_loop_count = count ? count : 1;
1016         g_mutex_unlock(il->data_mutex);
1017 }
1018
1019 void image_loader_set_priority(ImageLoader *il, gint priority)
1020 {
1021         if (!il) return;
1022
1023         if (il->thread) return; /* can't change prio if the thread already runs */
1024         il->idle_priority = priority;
1025 }
1026
1027
1028 gdouble image_loader_get_percent(ImageLoader *il)
1029 {
1030         gdouble ret;
1031         if (!il) return 0.0;
1032         
1033         g_mutex_lock(il->data_mutex);
1034         if (il->bytes_total == 0) 
1035                 {
1036                 ret = 0.0;
1037                 }
1038         else
1039                 {
1040                 ret = (gdouble)il->bytes_read / il->bytes_total;
1041                 }
1042         g_mutex_unlock(il->data_mutex);
1043         return ret;
1044 }
1045
1046 gboolean image_loader_get_is_done(ImageLoader *il)
1047 {
1048         gboolean ret;
1049         if (!il) return FALSE;
1050
1051         g_mutex_lock(il->data_mutex);
1052         ret = il->done;
1053         g_mutex_unlock(il->data_mutex);
1054
1055         return ret;
1056 }
1057
1058 FileData *image_loader_get_fd(ImageLoader *il)
1059 {
1060         FileData *ret;
1061         if (!il) return NULL;
1062
1063         g_mutex_lock(il->data_mutex);
1064         ret = il->fd;
1065         g_mutex_unlock(il->data_mutex);
1066
1067         return ret;
1068 }
1069
1070 gboolean image_loader_get_shrunk(ImageLoader *il)
1071 {
1072         gboolean ret;
1073         if (!il) return FALSE;
1074
1075         g_mutex_lock(il->data_mutex);
1076         ret = il->shrunk;
1077         g_mutex_unlock(il->data_mutex);
1078         return ret;
1079 }
1080
1081 const gchar *image_loader_get_error(ImageLoader *il)
1082 {
1083         const gchar *ret = NULL;
1084         if (!il) return NULL;
1085         g_mutex_lock(il->data_mutex);
1086         if (il->error) ret = il->error->message;
1087         g_mutex_unlock(il->data_mutex);
1088         return ret;
1089 }
1090
1091
1092 /* FIXME - this can be rather slow and blocks until the size is known */
1093 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1094 {
1095         ImageLoader *il;
1096         gboolean success;
1097
1098         il = image_loader_new(fd);
1099
1100         success = image_loader_start_idle(il);
1101
1102         if (success && il->pixbuf)
1103                 {
1104                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1105                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1106                 }
1107         else
1108                 {
1109                 if (width) *width = -1;
1110                 if (height) *height = -1;
1111                 }
1112
1113         image_loader_free(il);
1114
1115         return success;
1116 }
1117 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */