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