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