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