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