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