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