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