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