added custom jpeg 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
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         if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
517                 {
518                 DEBUG_1("Using custom jpeg loader");
519                 image_loader_backend_set_jpeg(&il->backend);
520                 }
521         else
522                 image_loader_backend_set_default(&il->backend);
523
524         il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
525         g_mutex_unlock(il->data_mutex);
526 }
527
528
529 static void image_loader_done(ImageLoader *il)
530 {
531         image_loader_stop_loader(il);
532
533         image_loader_emit_done(il);
534 }
535
536 static void image_loader_error(ImageLoader *il)
537 {
538         image_loader_stop_loader(il);
539
540         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
541
542         image_loader_emit_error(il);
543 }
544
545 static gboolean image_loader_continue(ImageLoader *il)
546 {
547         gint b;
548         gint c;
549
550         if (!il) return FALSE;
551
552         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
553         while (c > 0 && !image_loader_get_stopping(il))
554                 {
555                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
556
557                 if (b == 0)
558                         {
559                         image_loader_done(il);
560                         return FALSE;
561                         }
562
563                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
564                         {
565                         image_loader_error(il);
566                         return FALSE;
567                         }
568
569                 il->bytes_read += b;
570
571                 c--;
572                 }
573
574         if (il->bytes_total > 0)
575                 {
576                 image_loader_emit_percent(il);
577                 }
578
579         return TRUE;
580 }
581
582 static gboolean image_loader_begin(ImageLoader *il)
583 {
584         gssize b;
585         
586         if (il->pixbuf) return FALSE;
587
588         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
589         if (b < 1) return FALSE;
590
591         image_loader_setup_loader(il);
592         
593         g_assert(il->bytes_read == 0);
594         if (il->backend.load) {
595                 b = il->bytes_total;
596                 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
597                         {
598                         image_loader_stop_loader(il);
599                         return FALSE;
600                         }
601         }
602         else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
603                 {
604                 image_loader_stop_loader(il);
605                 return FALSE;
606                 }
607
608         il->bytes_read += b;
609
610         /* read until size is known */
611         while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
612                 {
613                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
614                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
615                         {
616                         image_loader_stop_loader(il);
617                         return FALSE;
618                         }
619                 il->bytes_read += b;
620                 }
621         if (!il->pixbuf) image_loader_sync_pixbuf(il);
622
623         if (il->bytes_read == il->bytes_total || b < 1)
624                 {
625                 /* done, handle (broken) loaders that do not have pixbuf till close */
626                 image_loader_stop_loader(il);
627
628                 if (!il->pixbuf) return FALSE;
629
630                 image_loader_done(il);
631                 return TRUE;
632                 }
633
634         if (!il->pixbuf)
635                 {
636                 image_loader_stop_loader(il);
637                 return FALSE;
638                 }
639
640         return TRUE;
641 }
642
643 /**************************************************************************************/
644 /* the following functions are always executed in the main thread */
645
646
647 static gboolean image_loader_setup_source(ImageLoader *il)
648 {
649         struct stat st;
650         gchar *pathl;
651
652         if (!il || il->loader || il->mapped_file) return FALSE;
653
654         il->mapped_file = NULL;
655
656         if (il->fd)
657                 {
658                 ExifData *exif = exif_read_fd(il->fd);
659
660                 if (options->thumbnails.use_exif)
661                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
662                 else
663                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
664
665                 if (il->mapped_file)
666                         {
667                         il->preview = TRUE;
668                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
669                         }
670                 exif_free_fd(il->fd, exif);
671                 }
672
673         
674         if (!il->mapped_file)
675                 {
676                 /* normal file */
677                 gint load_fd;
678         
679                 pathl = path_from_utf8(il->fd->path);
680                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
681                 g_free(pathl);
682                 if (load_fd == -1) return FALSE;
683
684                 if (fstat(load_fd, &st) == 0)
685                         {
686                         il->bytes_total = st.st_size;
687                         }
688                 else
689                         {
690                         close(load_fd);
691                         return FALSE;
692                         }
693                 
694                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
695                 close(load_fd);
696                 if (il->mapped_file == MAP_FAILED)
697                         {
698                         il->mapped_file = 0;
699                         return FALSE;
700                         }
701                 il->preview = FALSE;
702                 }
703                 
704         return TRUE;
705 }
706
707 static void image_loader_stop_source(ImageLoader *il)
708 {
709         if (!il) return;
710         
711         if (il->mapped_file)
712                 {
713                 if (il->preview)
714                         {
715                         exif_free_preview(il->mapped_file);
716                         }
717                 else
718                         {
719                         munmap(il->mapped_file, il->bytes_total);
720                         }
721                 il->mapped_file = NULL;
722                 }
723 }
724
725 static void image_loader_stop(ImageLoader *il)
726 {
727         if (!il) return;
728
729         if (il->idle_id)
730                 {
731                 g_source_remove(il->idle_id);
732                 il->idle_id = 0;
733                 }
734                 
735         if (il->thread)
736                 {
737                 /* stop loader in the other thread */
738                 g_mutex_lock(il->data_mutex);
739                 il->stopping = TRUE;
740                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
741                 g_mutex_unlock(il->data_mutex);
742                 }
743
744         image_loader_stop_loader(il);
745         image_loader_stop_source(il);
746         
747 }
748
749 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
750 {
751         g_mutex_lock(il->data_mutex);
752         il->delay_area_ready = enable;
753         if (!enable)
754                 {
755                 /* send delayed */
756                 GList *list, *work;
757                 list = g_list_reverse(il->area_param_delayed_list);
758                 il->area_param_delayed_list = NULL;
759                 g_mutex_unlock(il->data_mutex);
760
761                 work = list;
762
763                 while (work)
764                         {
765                         ImageLoaderAreaParam *par = work->data;
766                         work = work->next;
767                         
768                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
769                         g_free(par);
770                         }
771                 g_list_free(list);
772                 }
773         else
774                 {
775                 /* just unlock */
776                 g_mutex_unlock(il->data_mutex);
777                 }
778 }
779
780
781 /**************************************************************************************/
782 /* execution via idle calls */
783
784 static gboolean image_loader_idle_cb(gpointer data)
785 {
786         gboolean ret = FALSE;
787         ImageLoader *il = data;
788
789         if (il->idle_id)
790                 {
791                 ret = image_loader_continue(il);
792                 }
793         
794         if (!ret)
795                 {
796                 image_loader_stop_source(il);
797                 }
798         
799         return ret;
800 }
801
802
803 static gboolean image_loader_start_idle(ImageLoader *il)
804 {
805         gboolean ret;
806         
807         if (!il) return FALSE;
808
809         if (!il->fd) return FALSE;
810
811         if (!image_loader_setup_source(il)) return FALSE;
812         
813         ret = image_loader_begin(il);
814
815         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
816         return ret;
817 }
818
819 /**************************************************************************************/
820 /* execution via thread */
821
822 #ifdef HAVE_GTHREAD
823 static GThreadPool *image_loader_thread_pool = NULL;
824
825 static GCond *image_loader_prio_cond = NULL;
826 static GMutex *image_loader_prio_mutex = NULL;
827 static gint image_loader_prio_num = 0;
828
829
830 static void image_loader_thread_enter_high(void)
831 {
832         g_mutex_lock(image_loader_prio_mutex);
833         image_loader_prio_num++;
834         g_mutex_unlock(image_loader_prio_mutex);
835 }
836
837 static void image_loader_thread_leave_high(void)
838 {
839         g_mutex_lock(image_loader_prio_mutex);
840         image_loader_prio_num--;
841         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
842         g_mutex_unlock(image_loader_prio_mutex);
843 }
844
845 static void image_loader_thread_wait_high(void)
846 {
847         g_mutex_lock(image_loader_prio_mutex);
848         while (image_loader_prio_num) 
849                 {
850                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
851                 }
852
853         g_mutex_unlock(image_loader_prio_mutex);
854 }
855
856
857 static void image_loader_thread_run(gpointer data, gpointer user_data)
858 {
859         ImageLoader *il = data;
860         gboolean cont;
861         gboolean err;
862         
863         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
864                 {
865                 /* low prio, wait untill high prio tasks finishes */
866                 image_loader_thread_wait_high();
867                 }
868         else
869                 {
870                 /* high prio */
871                 image_loader_thread_enter_high();
872                 }
873         
874         err = !image_loader_begin(il);
875         
876         if (err)
877                 {
878                 /* 
879                 loader failed, we have to send signal 
880                 (idle mode returns the image_loader_begin return value directly)
881                 (success is always reported indirectly from image_loader_begin)
882                 */
883                 image_loader_emit_error(il);
884                 }
885         
886         cont = !err;
887         
888         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
889                 {
890                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
891                         {
892                         /* low prio, wait untill high prio tasks finishes */
893                         image_loader_thread_wait_high();
894                         }
895                 cont = image_loader_continue(il);
896                 }
897         image_loader_stop_loader(il);
898
899         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE) 
900                 {
901                 /* high prio */
902                 image_loader_thread_leave_high();
903                 }
904
905         g_mutex_lock(il->data_mutex);
906         il->can_destroy = TRUE;
907         g_cond_signal(il->can_destroy_cond);
908         g_mutex_unlock(il->data_mutex);
909
910 }
911
912
913 static gboolean image_loader_start_thread(ImageLoader *il)
914 {
915         if (!il) return FALSE;
916
917         if (!il->fd) return FALSE;
918
919         il->thread = TRUE;
920         
921         if (!image_loader_setup_source(il)) return FALSE;
922
923         if (!image_loader_thread_pool) 
924                 {
925                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
926                 image_loader_prio_cond = g_cond_new();
927                 image_loader_prio_mutex = g_mutex_new();
928                 }
929
930         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
931
932         g_thread_pool_push(image_loader_thread_pool, il, NULL);
933         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
934                 
935         return TRUE;
936 }
937 #endif /* HAVE_GTHREAD */
938
939
940 /**************************************************************************************/
941 /* public interface */
942
943
944 gboolean image_loader_start(ImageLoader *il)
945 {
946         if (!il) return FALSE;
947
948         if (!il->fd) return FALSE;
949
950 #ifdef HAVE_GTHREAD
951         return image_loader_start_thread(il);
952 #else
953         return image_loader_start_idle(il);
954 #endif
955 }
956
957
958 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
959 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
960 {
961         GdkPixbuf *ret;
962         if (!il) return NULL;
963         
964         g_mutex_lock(il->data_mutex);
965         ret = il->pixbuf;
966         g_mutex_unlock(il->data_mutex);
967         return ret;
968 }
969
970 gchar *image_loader_get_format(ImageLoader *il)
971 {
972         gchar **mimev;
973         gchar *mime;
974
975         if (!il || !il->loader) return NULL;
976
977         mimev = il->backend.get_format_mime_types(il->loader);
978         if (!mimev) return NULL;
979
980         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
981         mime = g_strdup(mimev[0]);
982         g_strfreev(mimev);
983
984         return mime;
985 }
986
987 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
988 {
989         if (!il) return;
990
991         g_mutex_lock(il->data_mutex);
992         il->requested_width = width;
993         il->requested_height = height;
994         g_mutex_unlock(il->data_mutex);
995 }
996
997 void image_loader_set_buffer_size(ImageLoader *il, guint count)
998 {
999         if (!il) return;
1000
1001         g_mutex_lock(il->data_mutex);
1002         il->idle_read_loop_count = count ? count : 1;
1003         g_mutex_unlock(il->data_mutex);
1004 }
1005
1006 void image_loader_set_priority(ImageLoader *il, gint priority)
1007 {
1008         if (!il) return;
1009
1010         if (il->thread) return; /* can't change prio if the thread already runs */
1011         il->idle_priority = priority;
1012 }
1013
1014
1015 gdouble image_loader_get_percent(ImageLoader *il)
1016 {
1017         gdouble ret;
1018         if (!il) return 0.0;
1019         
1020         g_mutex_lock(il->data_mutex);
1021         if (il->bytes_total == 0) 
1022                 {
1023                 ret = 0.0;
1024                 }
1025         else
1026                 {
1027                 ret = (gdouble)il->bytes_read / il->bytes_total;
1028                 }
1029         g_mutex_unlock(il->data_mutex);
1030         return ret;
1031 }
1032
1033 gboolean image_loader_get_is_done(ImageLoader *il)
1034 {
1035         gboolean ret;
1036         if (!il) return FALSE;
1037
1038         g_mutex_lock(il->data_mutex);
1039         ret = il->done;
1040         g_mutex_unlock(il->data_mutex);
1041
1042         return ret;
1043 }
1044
1045 FileData *image_loader_get_fd(ImageLoader *il)
1046 {
1047         FileData *ret;
1048         if (!il) return NULL;
1049
1050         g_mutex_lock(il->data_mutex);
1051         ret = il->fd;
1052         g_mutex_unlock(il->data_mutex);
1053
1054         return ret;
1055 }
1056
1057 gboolean image_loader_get_shrunk(ImageLoader *il)
1058 {
1059         gboolean ret;
1060         if (!il) return FALSE;
1061
1062         g_mutex_lock(il->data_mutex);
1063         ret = il->shrunk;
1064         g_mutex_unlock(il->data_mutex);
1065         return ret;
1066 }
1067
1068 const gchar *image_loader_get_error(ImageLoader *il)
1069 {
1070         const gchar *ret = NULL;
1071         if (!il) return NULL;
1072         g_mutex_lock(il->data_mutex);
1073         if (il->error) ret = il->error->message;
1074         g_mutex_unlock(il->data_mutex);
1075         return ret;
1076 }
1077
1078
1079 /* FIXME - this can be rather slow and blocks until the size is known */
1080 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1081 {
1082         ImageLoader *il;
1083         gboolean success;
1084
1085         il = image_loader_new(fd);
1086
1087         success = image_loader_start_idle(il);
1088
1089         if (success && il->pixbuf)
1090                 {
1091                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1092                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1093                 }
1094         else
1095                 {
1096                 if (width) *width = -1;
1097                 if (height) *height = -1;
1098                 }
1099
1100         image_loader_free(il);
1101
1102         return success;
1103 }
1104 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */