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