2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
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_webp.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"
45 #include "ui_fileops.h"
46 #include "gq-marshal.h"
51 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
52 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
55 * @page diagrams Diagrams
56 * @section image_load_overview Image Load Overview
59 * object image_change_complete
60 * object image_load_begin
61 * object image_loader_start
62 * object image_loader_start_thread
63 * object image_loader_start_idle
64 * object image_loader_setup_source
65 * object image_loader_thread_run
66 * object image_loader_begin
67 * object image_loader_setuploader
68 * circle "il->memory_mapped"
69 * object exif_get_preview_
70 * object exif_get_preview
71 * object libraw_get_preview
73 * image_change : image.c
74 * image_change_complete : image.c
75 * image_load_begin : image.c
76 * image_loader_start : image_load.c
77 * image_loader_start_thread : image_load.c
78 * image_loader_start_idle : image_load.c
79 * image_loader_thread_run : image_load.c
80 * image_loader_begin : image_load.c
81 * image_loader_setuploader : image_load.c
82 * image_loader_setuploader : -
83 * image_loader_setuploader : Select backend using magic
84 * image_loader_setup_source : image_load.c
85 * exif_get_preview : exiv2.cc
86 * exif_get_preview : EXIV2_TEST_VERSION(0,17,90)
87 * exif_get_preview_ : exif.c
88 * exif_get_preview_ : -
89 * exif_get_preview_ : If exiv2 not installed
90 * libraw_get_preview : image_load_libraw.c
92 * image_change --> image_change_complete
93 * image_change_complete --> image_load_begin
94 * image_load_begin --> image_loader_start
95 * image_loader_start --> image_loader_start_thread
96 * image_loader_start --> image_loader_start_idle : Obsolete - no threads version
97 * image_loader_start_thread --> image_loader_thread_run
98 * image_loader_start_thread --> image_loader_setup_source
99 * image_loader_setup_source --> exif_get_preview_
100 * image_loader_setup_source --> exif_get_preview
101 * image_loader_setup_source --> libraw_get_preview : Try libraw if exiv2 fails
102 * exif_get_preview_ ..> "il->memory_mapped"
103 * exif_get_preview ..> "il->memory_mapped"
104 * libraw_get_preview ..> "il->memory_mapped"
105 * image_loader_thread_run --> image_loader_begin
106 * image_loader_begin --> image_loader_setuploader
107 * "il->memory_mapped" ..> image_loader_setuploader
108 * note left of "il->memory_mapped" : Points to first byte of embedded jpeg (#FFD8)\n if preview found, otherwise to first byte of file
113 * @ref image_load_overview "Image Load Overview"
116 /**************************************************************************************/
117 /* image loader class */
121 SIGNAL_AREA_READY = 0,
129 static guint signals[SIGNAL_COUNT] = { 0 };
131 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
132 static void image_loader_class_init_wrapper(void *data, void *user_data);
133 static void image_loader_class_init(ImageLoaderClass *class);
134 static void image_loader_finalize(GObject *object);
135 static void image_loader_stop(ImageLoader *il);
137 GType image_loader_get_type(void)
139 static GType type = 0;
142 static const GTypeInfo info = {
143 sizeof(ImageLoaderClass),
144 NULL, /* base_init */
145 NULL, /* base_finalize */
146 (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
147 NULL, /* class_finalize */
148 NULL, /* class_data */
151 (GInstanceInitFunc)image_loader_init, /* instance_init */
152 NULL /* value_table */
154 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
159 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
161 ImageLoader *il = (ImageLoader *)instance;
165 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
172 il->idle_done_id = 0;
174 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
175 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
176 il->mapped_file = NULL;
177 il->preview = IMAGE_LOADER_PREVIEW_NONE;
179 il->requested_width = 0;
180 il->requested_height = 0;
181 il->actual_width = 0;
182 il->actual_height = 0;
185 il->can_destroy = TRUE;
188 #if GLIB_CHECK_VERSION(2,32,0)
189 il->data_mutex = g_new(GMutex, 1);
190 g_mutex_init(il->data_mutex);
191 il->can_destroy_cond = g_new(GCond, 1);
192 g_cond_init(il->can_destroy_cond);
194 il->data_mutex = g_mutex_new();
195 il->can_destroy_cond = g_cond_new();
198 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
201 static void image_loader_class_init_wrapper(void *data, void *user_data)
203 image_loader_class_init(data);
206 static void image_loader_class_init(ImageLoaderClass *class)
208 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
210 // gobject_class->set_property = image_loader_set_property;
211 // gobject_class->get_property = image_loader_get_property;
213 gobject_class->finalize = image_loader_finalize;
216 signals[SIGNAL_AREA_READY] =
217 g_signal_new("area_ready",
218 G_OBJECT_CLASS_TYPE(gobject_class),
220 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
222 gq_marshal_VOID__INT_INT_INT_INT,
229 signals[SIGNAL_ERROR] =
230 g_signal_new("error",
231 G_OBJECT_CLASS_TYPE(gobject_class),
233 G_STRUCT_OFFSET(ImageLoaderClass, error),
235 g_cclosure_marshal_VOID__VOID,
238 signals[SIGNAL_DONE] =
240 G_OBJECT_CLASS_TYPE(gobject_class),
242 G_STRUCT_OFFSET(ImageLoaderClass, done),
244 g_cclosure_marshal_VOID__VOID,
247 signals[SIGNAL_PERCENT] =
248 g_signal_new("percent",
249 G_OBJECT_CLASS_TYPE(gobject_class),
251 G_STRUCT_OFFSET(ImageLoaderClass, percent),
253 g_cclosure_marshal_VOID__DOUBLE,
257 signals[SIGNAL_SIZE] =
258 g_signal_new("size_prepared",
259 G_OBJECT_CLASS_TYPE(gobject_class),
261 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
263 gq_marshal_VOID__INT_INT,
270 static void image_loader_finalize(GObject *object)
272 ImageLoader *il = (ImageLoader *)object;
274 image_loader_stop(il);
276 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
278 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
280 if (il->idle_done_id)
282 g_source_remove(il->idle_done_id);
283 il->idle_done_id = 0;
286 while (g_source_remove_by_user_data(il))
288 DEBUG_2("pending signals detected");
291 while (il->area_param_list)
293 DEBUG_1("pending area_ready signals detected");
294 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
295 g_free(il->area_param_list->data);
296 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
299 while (il->area_param_delayed_list)
301 g_free(il->area_param_delayed_list->data);
302 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
305 if (il->pixbuf) g_object_unref(il->pixbuf);
307 if (il->error) g_error_free(il->error);
309 file_data_unref(il->fd);
311 #if GLIB_CHECK_VERSION(2,32,0)
312 g_mutex_clear(il->data_mutex);
313 g_free(il->data_mutex);
314 g_cond_clear(il->can_destroy_cond);
315 g_free(il->can_destroy_cond);
317 g_mutex_free(il->data_mutex);
318 g_cond_free(il->can_destroy_cond);
323 void image_loader_free(ImageLoader *il)
326 g_object_unref(G_OBJECT(il));
330 ImageLoader *image_loader_new(FileData *fd)
334 if (!fd) return NULL;
336 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
338 il->fd = file_data_ref(fd);
343 /**************************************************************************************/
344 /* send signals via idle calbacks - the callback are executed in the main thread */
346 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
347 struct _ImageLoaderAreaParam {
356 static gboolean image_loader_emit_area_ready_cb(gpointer data)
358 ImageLoaderAreaParam *par = data;
359 ImageLoader *il = par->il;
361 g_mutex_lock(il->data_mutex);
362 il->area_param_list = g_list_remove(il->area_param_list, par);
368 g_mutex_unlock(il->data_mutex);
370 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
375 static gboolean image_loader_emit_done_cb(gpointer data)
377 ImageLoader *il = data;
378 g_signal_emit(il, signals[SIGNAL_DONE], 0);
382 static gboolean image_loader_emit_error_cb(gpointer data)
384 ImageLoader *il = data;
385 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
389 static gboolean image_loader_emit_percent_cb(gpointer data)
391 ImageLoader *il = data;
392 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
396 static gboolean image_loader_emit_size_cb(gpointer data)
399 ImageLoader *il = data;
400 g_mutex_lock(il->data_mutex);
401 width = il->actual_width;
402 height = il->actual_height;
403 g_mutex_unlock(il->data_mutex);
404 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
409 /* DONE and ERROR are emitted only once, thus they can have normal priority
410 PERCENT and AREA_READY should be processed ASAP
413 static void image_loader_emit_done(ImageLoader *il)
415 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
418 static void image_loader_emit_error(ImageLoader *il)
420 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
423 static void image_loader_emit_percent(ImageLoader *il)
425 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
428 static void image_loader_emit_size(ImageLoader *il)
430 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
433 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
437 ImageLoaderAreaParam *prev_par = (*list)->data;
438 if (prev_par->x == x && prev_par->w == w &&
439 prev_par->y + prev_par->h == y)
441 /* we can merge the notifications */
445 if (prev_par->x == x && prev_par->w == w &&
446 y + h == prev_par->y)
448 /* we can merge the notifications */
453 if (prev_par->y == y && prev_par->h == h &&
454 prev_par->x + prev_par->w == x)
456 /* we can merge the notifications */
460 if (prev_par->y == y && prev_par->h == h &&
461 x + w == prev_par->x)
463 /* we can merge the notifications */
470 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
477 *list = g_list_prepend(*list, par);
481 /* this function expects that il->data_mutex is locked by caller */
482 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
484 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
488 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
492 /**************************************************************************************/
493 /* the following functions may be executed in separate thread */
495 /* this function expects that il->data_mutex is locked by caller */
496 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
498 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
503 static gboolean image_loader_get_stopping(ImageLoader *il)
506 if (!il) return FALSE;
508 g_mutex_lock(il->data_mutex);
510 g_mutex_unlock(il->data_mutex);
516 static void image_loader_sync_pixbuf(ImageLoader *il)
520 g_mutex_lock(il->data_mutex);
524 g_mutex_unlock(il->data_mutex);
528 pb = il->backend.get_pixbuf(il->loader);
530 if (pb == il->pixbuf)
532 g_mutex_unlock(il->data_mutex);
536 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
538 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
541 if (il->pixbuf) g_object_unref(il->pixbuf);
544 if (il->pixbuf) g_object_ref(il->pixbuf);
546 g_mutex_unlock(il->data_mutex);
549 static void image_loader_area_updated_cb(gpointer loader,
550 guint x, guint y, guint w, guint h,
553 ImageLoader *il = data;
555 if (!image_loader_get_pixbuf(il))
557 image_loader_sync_pixbuf(il);
558 if (!image_loader_get_pixbuf(il))
560 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
564 g_mutex_lock(il->data_mutex);
565 if (il->delay_area_ready)
566 image_loader_queue_delayed_area_ready(il, x, y, w, h);
568 image_loader_emit_area_ready(il, x, y, w, h);
570 if (il->stopping) il->backend.abort(il->loader);
572 g_mutex_unlock(il->data_mutex);
575 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
577 ImageLoader *il = data;
583 http://bugzilla.gnome.org/show_bug.cgi?id=547669
584 http://bugzilla.gnome.org/show_bug.cgi?id=589334
586 gchar *format = il->backend.get_format_name(loader);
587 if (strcmp(format, "svg") == 0 ||
588 strcmp(format, "xpm") == 0)
596 pb = il->backend.get_pixbuf(loader);
598 h = gdk_pixbuf_get_height(pb);
599 rs = gdk_pixbuf_get_rowstride(pb);
600 pix = gdk_pixbuf_get_pixels(pb);
602 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
606 static void image_loader_size_cb(gpointer loader,
607 gint width, gint height, gpointer data)
609 ImageLoader *il = data;
611 gboolean scale = FALSE;
614 g_mutex_lock(il->data_mutex);
615 il->actual_width = width;
616 il->actual_height = height;
617 if (il->requested_width < 1 || il->requested_height < 1)
619 g_mutex_unlock(il->data_mutex);
620 image_loader_emit_size(il);
623 g_mutex_unlock(il->data_mutex);
625 #ifdef HAVE_FFMPEGTHUMBNAILER
626 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
629 mime_types = il->backend.get_format_mime_types(loader);
631 while (mime_types[n] && !scale)
633 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
636 g_strfreev(mime_types);
640 image_loader_emit_size(il);
644 g_mutex_lock(il->data_mutex);
647 if (width > il->requested_width || height > il->requested_height)
650 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
652 nw = il->requested_width;
653 nh = (gdouble)nw / width * height;
658 nh = il->requested_height;
659 nw = (gdouble)nh / height * width;
663 il->actual_width = nw;
664 il->actual_height = nh;
665 il->backend.set_size(loader, nw, nh);
669 g_mutex_unlock(il->data_mutex);
670 image_loader_emit_size(il);
673 static void image_loader_stop_loader(ImageLoader *il)
679 /* some loaders do not have a pixbuf till close, order is important here */
680 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
681 image_loader_sync_pixbuf(il);
682 il->backend.free(il->loader);
685 g_mutex_lock(il->data_mutex);
687 g_mutex_unlock(il->data_mutex);
690 static void image_loader_setup_loader(ImageLoader *il)
692 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
696 gint external_preview = 1;
698 g_mutex_lock(il->data_mutex);
700 if (options->external_preview.enable)
703 gchar *tilde_filename;
705 tilde_filename = expand_tilde(options->external_preview.select);
707 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
709 external_preview = runcmd(cmd_line);
711 g_free(tilde_filename);
714 if (external_preview == 0)
716 DEBUG_1("Using custom external loader");
717 image_loader_backend_set_external(&il->backend);
721 #ifdef HAVE_FFMPEGTHUMBNAILER
722 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
724 DEBUG_1("Using custom ffmpegthumbnailer loader");
725 image_loader_backend_set_ft(&il->backend);
730 if (il->bytes_total >= 4 &&
731 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
733 DEBUG_1("Using custom pdf loader");
734 image_loader_backend_set_pdf(&il->backend);
739 if (il->bytes_total >= 12 &&
740 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
741 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
742 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
743 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
745 DEBUG_1("Using custom heif loader");
746 image_loader_backend_set_heif(&il->backend);
751 if (il->bytes_total >= 12 &&
752 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
753 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
755 DEBUG_1("Using custom webp loader");
756 image_loader_backend_set_webp(&il->backend);
761 if (il->bytes_total >= 16 &&
762 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
763 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
765 DEBUG_1("Using custom djvu loader");
766 image_loader_backend_set_djvu(&il->backend);
771 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
773 DEBUG_1("Using custom jpeg loader");
774 image_loader_backend_set_jpeg(&il->backend);
778 if (il->bytes_total >= 11 &&
779 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
780 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
782 DEBUG_1("Using custom cr3 loader");
783 image_loader_backend_set_cr3(&il->backend);
789 if (il->bytes_total >= 10 &&
790 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
791 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
792 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
793 memcmp(il->mapped_file, "II*\0", 4) == 0))
795 DEBUG_1("Using custom tiff loader");
796 image_loader_backend_set_tiff(&il->backend);
800 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
802 DEBUG_1("Using dds loader");
803 image_loader_backend_set_dds(&il->backend);
806 if (il->bytes_total >= 6 &&
807 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
809 DEBUG_1("Using custom psd loader");
810 image_loader_backend_set_psd(&il->backend);
814 if (il->bytes_total >= 12 &&
815 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
817 DEBUG_1("Using custom j2k loader");
818 image_loader_backend_set_j2k(&il->backend);
823 if (il->bytes_total >= 12 &&
824 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
826 DEBUG_1("Using custom jpeg xl loader");
827 image_loader_backend_set_jpegxl(&il->backend);
830 if (il->bytes_total >= 2 &&
831 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
833 DEBUG_1("Using custom jpeg xl loader");
834 image_loader_backend_set_jpegxl(&il->backend);
838 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
840 DEBUG_1("Using custom collection loader");
841 image_loader_backend_set_collection(&il->backend);
844 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
846 DEBUG_1("Using custom svgz loader");
847 image_loader_backend_set_svgz(&il->backend);
850 image_loader_backend_set_default(&il->backend);
852 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
855 format = il->backend.get_format_name(il->loader);
856 if (g_strcmp0(format, "tiff") == 0)
858 il->backend.set_page_num(il->loader, il->fd->page_num);
864 format = il->backend.get_format_name(il->loader);
865 if (g_strcmp0(format, "pdf") == 0)
867 il->backend.set_page_num(il->loader, il->fd->page_num);
873 format = il->backend.get_format_name(il->loader);
874 if (g_strcmp0(format, "heif") == 0)
876 il->backend.set_page_num(il->loader, il->fd->page_num);
882 format = il->backend.get_format_name(il->loader);
883 if (g_strcmp0(format, "djvu") == 0)
885 il->backend.set_page_num(il->loader, il->fd->page_num);
890 il->fd->format_name = il->backend.get_format_name(il->loader);
892 g_mutex_unlock(il->data_mutex);
896 static void image_loader_done(ImageLoader *il)
898 image_loader_stop_loader(il);
900 image_loader_emit_done(il);
903 static void image_loader_error(ImageLoader *il)
905 image_loader_stop_loader(il);
907 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
909 image_loader_emit_error(il);
912 static gboolean image_loader_continue(ImageLoader *il)
917 if (!il) return FALSE;
919 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
920 while (c > 0 && !image_loader_get_stopping(il))
922 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
926 image_loader_done(il);
930 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
932 image_loader_error(il);
941 if (il->bytes_total > 0)
943 image_loader_emit_percent(il);
949 static gboolean image_loader_begin(ImageLoader *il)
951 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
956 if (il->pixbuf) return FALSE;
958 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
959 if (b < 1) return FALSE;
961 image_loader_setup_loader(il);
963 g_assert(il->bytes_read == 0);
964 if (il->backend.load) {
966 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
968 image_loader_stop_loader(il);
972 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
974 image_loader_stop_loader(il);
979 format = il->backend.get_format_name(il->loader);
980 if (g_strcmp0(format, "pdf") == 0)
982 gint i = il->backend.get_page_total(il->loader);
983 file_data_set_page_total(il->fd, i);
988 format = il->backend.get_format_name(il->loader);
989 if (g_strcmp0(format, "heif") == 0)
991 gint i = il->backend.get_page_total(il->loader);
992 file_data_set_page_total(il->fd, i);
997 format = il->backend.get_format_name(il->loader);
998 if (g_strcmp0(format, "djvu") == 0)
1000 gint i = il->backend.get_page_total(il->loader);
1001 file_data_set_page_total(il->fd, i);
1006 format = il->backend.get_format_name(il->loader);
1007 if (g_strcmp0(format, "tiff") == 0)
1009 gint i = il->backend.get_page_total(il->loader);
1010 file_data_set_page_total(il->fd, i);
1015 il->bytes_read += b;
1017 /* read until size is known */
1018 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
1020 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
1021 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
1023 image_loader_stop_loader(il);
1026 il->bytes_read += b;
1028 if (!il->pixbuf) image_loader_sync_pixbuf(il);
1030 if (il->bytes_read == il->bytes_total || b < 1)
1032 /* done, handle (broken) loaders that do not have pixbuf till close */
1033 image_loader_stop_loader(il);
1035 if (!il->pixbuf) return FALSE;
1037 image_loader_done(il);
1043 image_loader_stop_loader(il);
1050 /**************************************************************************************/
1051 /* the following functions are always executed in the main thread */
1054 static gboolean image_loader_setup_source(ImageLoader *il)
1059 if (!il || il->loader || il->mapped_file) return FALSE;
1061 il->mapped_file = NULL;
1065 ExifData *exif = exif_read_fd(il->fd);
1067 if (options->thumbnails.use_exif)
1069 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
1071 if (il->mapped_file)
1073 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1078 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
1080 if (il->mapped_file)
1082 /* Both exiv2 and libraw sometimes return a pointer to a file
1083 * section that is not a jpeg */
1084 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1086 il->mapped_file = NULL;
1090 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1095 /* If exiv2 does not find a thumbnail, try libraw (which may be slower) */
1096 if (!il->mapped_file)
1098 il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
1100 if (il->mapped_file)
1102 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1104 il->mapped_file = NULL;
1108 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1113 if (il->mapped_file)
1115 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1117 exif_free_fd(il->fd, exif);
1121 if (!il->mapped_file)
1126 pathl = path_from_utf8(il->fd->path);
1127 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1129 if (load_fd == -1) return FALSE;
1131 if (fstat(load_fd, &st) == 0)
1133 il->bytes_total = st.st_size;
1141 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
1143 if (il->mapped_file == MAP_FAILED)
1145 il->mapped_file = 0;
1148 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1154 static void image_loader_stop_source(ImageLoader *il)
1158 if (il->mapped_file)
1160 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1162 exif_free_preview(il->mapped_file);
1164 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1166 libraw_free_preview(il->mapped_file);
1170 munmap(il->mapped_file, il->bytes_total);
1172 il->mapped_file = NULL;
1176 static void image_loader_stop(ImageLoader *il)
1182 g_source_remove(il->idle_id);
1188 /* stop loader in the other thread */
1189 g_mutex_lock(il->data_mutex);
1190 il->stopping = TRUE;
1191 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1192 g_mutex_unlock(il->data_mutex);
1195 image_loader_stop_loader(il);
1196 image_loader_stop_source(il);
1200 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1202 g_mutex_lock(il->data_mutex);
1203 il->delay_area_ready = enable;
1208 list = g_list_reverse(il->area_param_delayed_list);
1209 il->area_param_delayed_list = NULL;
1210 g_mutex_unlock(il->data_mutex);
1216 ImageLoaderAreaParam *par = work->data;
1219 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1227 g_mutex_unlock(il->data_mutex);
1232 /**************************************************************************************/
1233 /* execution via idle calls */
1235 static gboolean image_loader_idle_cb(gpointer data)
1237 gboolean ret = FALSE;
1238 ImageLoader *il = data;
1242 ret = image_loader_continue(il);
1247 image_loader_stop_source(il);
1254 static gboolean image_loader_start_idle(ImageLoader *il)
1258 if (!il) return FALSE;
1260 if (!il->fd) return FALSE;
1262 if (!image_loader_setup_source(il)) return FALSE;
1264 ret = image_loader_begin(il);
1266 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1270 /**************************************************************************************/
1271 /* execution via thread */
1274 static GThreadPool *image_loader_thread_pool = NULL;
1276 static GCond *image_loader_prio_cond = NULL;
1277 static GMutex *image_loader_prio_mutex = NULL;
1278 static gint image_loader_prio_num = 0;
1281 static void image_loader_thread_enter_high(void)
1283 g_mutex_lock(image_loader_prio_mutex);
1284 image_loader_prio_num++;
1285 g_mutex_unlock(image_loader_prio_mutex);
1288 static void image_loader_thread_leave_high(void)
1290 g_mutex_lock(image_loader_prio_mutex);
1291 image_loader_prio_num--;
1292 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1293 g_mutex_unlock(image_loader_prio_mutex);
1296 static void image_loader_thread_wait_high(void)
1298 g_mutex_lock(image_loader_prio_mutex);
1299 while (image_loader_prio_num)
1301 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1304 g_mutex_unlock(image_loader_prio_mutex);
1308 static void image_loader_thread_run(gpointer data, gpointer user_data)
1310 ImageLoader *il = data;
1314 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1316 /* low prio, wait until high prio tasks finishes */
1317 image_loader_thread_wait_high();
1322 image_loader_thread_enter_high();
1325 err = !image_loader_begin(il);
1330 loader failed, we have to send signal
1331 (idle mode returns the image_loader_begin return value directly)
1332 (success is always reported indirectly from image_loader_begin)
1334 image_loader_emit_error(il);
1339 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1341 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1343 /* low prio, wait until high prio tasks finishes */
1344 image_loader_thread_wait_high();
1346 cont = image_loader_continue(il);
1348 image_loader_stop_loader(il);
1350 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1353 image_loader_thread_leave_high();
1356 g_mutex_lock(il->data_mutex);
1357 il->can_destroy = TRUE;
1358 g_cond_signal(il->can_destroy_cond);
1359 g_mutex_unlock(il->data_mutex);
1364 static gboolean image_loader_start_thread(ImageLoader *il)
1366 if (!il) return FALSE;
1368 if (!il->fd) return FALSE;
1372 if (!image_loader_setup_source(il)) return FALSE;
1374 if (!image_loader_thread_pool)
1376 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1377 #if GLIB_CHECK_VERSION(2,32,0)
1378 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1379 g_cond_init(image_loader_prio_cond);
1380 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1381 g_mutex_init(image_loader_prio_mutex);
1383 image_loader_prio_cond = g_cond_new();
1384 image_loader_prio_mutex = g_mutex_new();
1388 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1390 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1391 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1395 #endif /* HAVE_GTHREAD */
1398 /**************************************************************************************/
1399 /* public interface */
1402 gboolean image_loader_start(ImageLoader *il)
1404 if (!il) return FALSE;
1406 if (!il->fd) return FALSE;
1409 return image_loader_start_thread(il);
1411 return image_loader_start_idle(il);
1416 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1417 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1420 if (!il) return NULL;
1422 g_mutex_lock(il->data_mutex);
1424 g_mutex_unlock(il->data_mutex);
1428 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1432 g_mutex_lock(il->data_mutex);
1433 il->requested_width = width;
1434 il->requested_height = height;
1435 g_mutex_unlock(il->data_mutex);
1438 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1442 g_mutex_lock(il->data_mutex);
1443 il->idle_read_loop_count = count ? count : 1;
1444 g_mutex_unlock(il->data_mutex);
1447 void image_loader_set_priority(ImageLoader *il, gint priority)
1451 if (il->thread) return; /* can't change prio if the thread already runs */
1452 il->idle_priority = priority;
1456 gdouble image_loader_get_percent(ImageLoader *il)
1459 if (!il) return 0.0;
1461 g_mutex_lock(il->data_mutex);
1462 if (il->bytes_total == 0)
1468 ret = (gdouble)il->bytes_read / il->bytes_total;
1470 g_mutex_unlock(il->data_mutex);
1474 gboolean image_loader_get_is_done(ImageLoader *il)
1477 if (!il) return FALSE;
1479 g_mutex_lock(il->data_mutex);
1481 g_mutex_unlock(il->data_mutex);
1486 FileData *image_loader_get_fd(ImageLoader *il)
1489 if (!il) return NULL;
1491 g_mutex_lock(il->data_mutex);
1493 g_mutex_unlock(il->data_mutex);
1498 gboolean image_loader_get_shrunk(ImageLoader *il)
1501 if (!il) return FALSE;
1503 g_mutex_lock(il->data_mutex);
1505 g_mutex_unlock(il->data_mutex);
1509 const gchar *image_loader_get_error(ImageLoader *il)
1511 const gchar *ret = NULL;
1512 if (!il) return NULL;
1513 g_mutex_lock(il->data_mutex);
1514 if (il->error) ret = il->error->message;
1515 g_mutex_unlock(il->data_mutex);
1521 * @FIXME this can be rather slow and blocks until the size is known
1523 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1528 il = image_loader_new(fd);
1530 success = image_loader_start_idle(il);
1532 if (success && il->pixbuf)
1534 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1535 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1539 if (width) *width = -1;
1540 if (height) *height = -1;
1543 image_loader_free(il);
1547 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */