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