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_zxscr.h"
38 #include "image_load_j2k.h"
39 #include "image_load_jpegxl.h"
40 #include "image_load_libraw.h"
41 #include "image_load_svgz.h"
46 #include "ui_fileops.h"
47 #include "gq-marshal.h"
52 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
53 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
56 * @page diagrams Diagrams
57 * @section image_load_overview Image Load Overview
60 * object image_change_complete
61 * object image_load_begin
62 * object image_loader_start
63 * object image_loader_start_thread
64 * object image_loader_start_idle
65 * object image_loader_setup_source
66 * object image_loader_thread_run
67 * object image_loader_begin
68 * object image_loader_setuploader
69 * circle "il->memory_mapped"
70 * object exif_get_preview_
71 * object exif_get_preview
72 * object libraw_get_preview
74 * image_change : image.c
75 * image_change_complete : image.c
76 * image_load_begin : image.c
77 * image_loader_start : image_load.c
78 * image_loader_start_thread : image_load.c
79 * image_loader_start_idle : image_load.c
80 * image_loader_thread_run : image_load.c
81 * image_loader_begin : image_load.c
82 * image_loader_setuploader : image_load.c
83 * image_loader_setuploader : -
84 * image_loader_setuploader : Select backend using magic
85 * image_loader_setup_source : image_load.c
86 * exif_get_preview : exiv2.cc
87 * exif_get_preview : EXIV2_TEST_VERSION(0,17,90)
88 * exif_get_preview_ : exif.c
89 * exif_get_preview_ : -
90 * exif_get_preview_ : If exiv2 not installed
91 * libraw_get_preview : image_load_libraw.c
93 * image_change --> image_change_complete
94 * image_change_complete --> image_load_begin
95 * image_load_begin --> image_loader_start
96 * image_loader_start --> image_loader_start_thread
97 * image_loader_start --> image_loader_start_idle : Obsolete - no threads version
98 * image_loader_start_thread --> image_loader_thread_run
99 * image_loader_start_thread --> image_loader_setup_source
100 * image_loader_setup_source --> exif_get_preview_
101 * image_loader_setup_source --> exif_get_preview
102 * image_loader_setup_source --> libraw_get_preview : Try libraw if exiv2 fails
103 * exif_get_preview_ ..> "il->memory_mapped"
104 * exif_get_preview ..> "il->memory_mapped"
105 * libraw_get_preview ..> "il->memory_mapped"
106 * image_loader_thread_run --> image_loader_begin
107 * image_loader_begin --> image_loader_setuploader
108 * "il->memory_mapped" ..> image_loader_setuploader
109 * note left of "il->memory_mapped" : Points to first byte of embedded jpeg (#FFD8)\n if preview found, otherwise to first byte of file
114 * @ref image_load_overview "Image Load Overview"
117 /**************************************************************************************/
118 /* image loader class */
122 SIGNAL_AREA_READY = 0,
130 static guint signals[SIGNAL_COUNT] = { 0 };
132 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
133 static void image_loader_class_init_wrapper(void *data, void *user_data);
134 static void image_loader_class_init(ImageLoaderClass *class);
135 static void image_loader_finalize(GObject *object);
136 static void image_loader_stop(ImageLoader *il);
138 GType image_loader_get_type(void)
140 static GType type = 0;
143 static const GTypeInfo info = {
144 sizeof(ImageLoaderClass),
145 NULL, /* base_init */
146 NULL, /* base_finalize */
147 (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
148 NULL, /* class_finalize */
149 NULL, /* class_data */
152 (GInstanceInitFunc)image_loader_init, /* instance_init */
153 NULL /* value_table */
155 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
160 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
162 ImageLoader *il = (ImageLoader *)instance;
166 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
173 il->idle_done_id = 0;
175 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
176 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
177 il->mapped_file = NULL;
178 il->preview = IMAGE_LOADER_PREVIEW_NONE;
180 il->requested_width = 0;
181 il->requested_height = 0;
182 il->actual_width = 0;
183 il->actual_height = 0;
186 il->can_destroy = TRUE;
189 #if GLIB_CHECK_VERSION(2,32,0)
190 il->data_mutex = g_new(GMutex, 1);
191 g_mutex_init(il->data_mutex);
192 il->can_destroy_cond = g_new(GCond, 1);
193 g_cond_init(il->can_destroy_cond);
195 il->data_mutex = g_mutex_new();
196 il->can_destroy_cond = g_cond_new();
199 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
202 static void image_loader_class_init_wrapper(void *data, void *user_data)
204 image_loader_class_init(data);
207 static void image_loader_class_init(ImageLoaderClass *class)
209 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
211 // gobject_class->set_property = image_loader_set_property;
212 // gobject_class->get_property = image_loader_get_property;
214 gobject_class->finalize = image_loader_finalize;
217 signals[SIGNAL_AREA_READY] =
218 g_signal_new("area_ready",
219 G_OBJECT_CLASS_TYPE(gobject_class),
221 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
223 gq_marshal_VOID__INT_INT_INT_INT,
230 signals[SIGNAL_ERROR] =
231 g_signal_new("error",
232 G_OBJECT_CLASS_TYPE(gobject_class),
234 G_STRUCT_OFFSET(ImageLoaderClass, error),
236 g_cclosure_marshal_VOID__VOID,
239 signals[SIGNAL_DONE] =
241 G_OBJECT_CLASS_TYPE(gobject_class),
243 G_STRUCT_OFFSET(ImageLoaderClass, done),
245 g_cclosure_marshal_VOID__VOID,
248 signals[SIGNAL_PERCENT] =
249 g_signal_new("percent",
250 G_OBJECT_CLASS_TYPE(gobject_class),
252 G_STRUCT_OFFSET(ImageLoaderClass, percent),
254 g_cclosure_marshal_VOID__DOUBLE,
258 signals[SIGNAL_SIZE] =
259 g_signal_new("size_prepared",
260 G_OBJECT_CLASS_TYPE(gobject_class),
262 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
264 gq_marshal_VOID__INT_INT,
271 static void image_loader_finalize(GObject *object)
273 ImageLoader *il = (ImageLoader *)object;
275 image_loader_stop(il);
277 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
279 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
281 if (il->idle_done_id)
283 g_source_remove(il->idle_done_id);
284 il->idle_done_id = 0;
287 while (g_source_remove_by_user_data(il))
289 DEBUG_2("pending signals detected");
292 while (il->area_param_list)
294 DEBUG_1("pending area_ready signals detected");
295 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
296 g_free(il->area_param_list->data);
297 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
300 while (il->area_param_delayed_list)
302 g_free(il->area_param_delayed_list->data);
303 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
306 if (il->pixbuf) g_object_unref(il->pixbuf);
308 if (il->error) g_error_free(il->error);
310 file_data_unref(il->fd);
312 #if GLIB_CHECK_VERSION(2,32,0)
313 g_mutex_clear(il->data_mutex);
314 g_free(il->data_mutex);
315 g_cond_clear(il->can_destroy_cond);
316 g_free(il->can_destroy_cond);
318 g_mutex_free(il->data_mutex);
319 g_cond_free(il->can_destroy_cond);
324 void image_loader_free(ImageLoader *il)
327 g_object_unref(G_OBJECT(il));
331 ImageLoader *image_loader_new(FileData *fd)
335 if (!fd) return NULL;
337 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
339 il->fd = file_data_ref(fd);
344 /**************************************************************************************/
345 /* send signals via idle calbacks - the callback are executed in the main thread */
347 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
348 struct _ImageLoaderAreaParam {
357 static gboolean image_loader_emit_area_ready_cb(gpointer data)
359 ImageLoaderAreaParam *par = data;
360 ImageLoader *il = par->il;
362 g_mutex_lock(il->data_mutex);
363 il->area_param_list = g_list_remove(il->area_param_list, par);
369 g_mutex_unlock(il->data_mutex);
371 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
376 static gboolean image_loader_emit_done_cb(gpointer data)
378 ImageLoader *il = data;
379 g_signal_emit(il, signals[SIGNAL_DONE], 0);
383 static gboolean image_loader_emit_error_cb(gpointer data)
385 ImageLoader *il = data;
386 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
390 static gboolean image_loader_emit_percent_cb(gpointer data)
392 ImageLoader *il = data;
393 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
397 static gboolean image_loader_emit_size_cb(gpointer data)
400 ImageLoader *il = data;
401 g_mutex_lock(il->data_mutex);
402 width = il->actual_width;
403 height = il->actual_height;
404 g_mutex_unlock(il->data_mutex);
405 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
410 /* DONE and ERROR are emitted only once, thus they can have normal priority
411 PERCENT and AREA_READY should be processed ASAP
414 static void image_loader_emit_done(ImageLoader *il)
416 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
419 static void image_loader_emit_error(ImageLoader *il)
421 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
424 static void image_loader_emit_percent(ImageLoader *il)
426 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
429 static void image_loader_emit_size(ImageLoader *il)
431 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
434 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
438 ImageLoaderAreaParam *prev_par = (*list)->data;
439 if (prev_par->x == x && prev_par->w == w &&
440 prev_par->y + prev_par->h == y)
442 /* we can merge the notifications */
446 if (prev_par->x == x && prev_par->w == w &&
447 y + h == prev_par->y)
449 /* we can merge the notifications */
454 if (prev_par->y == y && prev_par->h == h &&
455 prev_par->x + prev_par->w == x)
457 /* we can merge the notifications */
461 if (prev_par->y == y && prev_par->h == h &&
462 x + w == prev_par->x)
464 /* we can merge the notifications */
471 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
478 *list = g_list_prepend(*list, par);
482 /* this function expects that il->data_mutex is locked by caller */
483 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
485 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
489 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
493 /**************************************************************************************/
494 /* the following functions may be executed in separate thread */
496 /* this function expects that il->data_mutex is locked by caller */
497 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
499 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
504 static gboolean image_loader_get_stopping(ImageLoader *il)
507 if (!il) return FALSE;
509 g_mutex_lock(il->data_mutex);
511 g_mutex_unlock(il->data_mutex);
517 static void image_loader_sync_pixbuf(ImageLoader *il)
521 g_mutex_lock(il->data_mutex);
525 g_mutex_unlock(il->data_mutex);
529 pb = il->backend.get_pixbuf(il->loader);
531 if (pb == il->pixbuf)
533 g_mutex_unlock(il->data_mutex);
537 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
539 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
542 if (il->pixbuf) g_object_unref(il->pixbuf);
545 if (il->pixbuf) g_object_ref(il->pixbuf);
547 g_mutex_unlock(il->data_mutex);
550 static void image_loader_area_updated_cb(gpointer loader,
551 guint x, guint y, guint w, guint h,
554 ImageLoader *il = data;
556 if (!image_loader_get_pixbuf(il))
558 image_loader_sync_pixbuf(il);
559 if (!image_loader_get_pixbuf(il))
561 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
565 g_mutex_lock(il->data_mutex);
566 if (il->delay_area_ready)
567 image_loader_queue_delayed_area_ready(il, x, y, w, h);
569 image_loader_emit_area_ready(il, x, y, w, h);
571 if (il->stopping) il->backend.abort(il->loader);
573 g_mutex_unlock(il->data_mutex);
576 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
578 ImageLoader *il = data;
584 https://bugzilla.gnome.org/show_bug.cgi?id=547669
585 https://bugzilla.gnome.org/show_bug.cgi?id=589334
587 gchar *format = il->backend.get_format_name(loader);
588 if (strcmp(format, "svg") == 0 ||
589 strcmp(format, "xpm") == 0)
597 pb = il->backend.get_pixbuf(loader);
599 h = gdk_pixbuf_get_height(pb);
600 rs = gdk_pixbuf_get_rowstride(pb);
601 pix = gdk_pixbuf_get_pixels(pb);
603 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
607 static void image_loader_size_cb(gpointer loader,
608 gint width, gint height, gpointer data)
610 ImageLoader *il = data;
612 gboolean scale = FALSE;
615 g_mutex_lock(il->data_mutex);
616 il->actual_width = width;
617 il->actual_height = height;
618 if (il->requested_width < 1 || il->requested_height < 1)
620 g_mutex_unlock(il->data_mutex);
621 image_loader_emit_size(il);
624 g_mutex_unlock(il->data_mutex);
626 #ifdef HAVE_FFMPEGTHUMBNAILER
627 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
630 mime_types = il->backend.get_format_mime_types(loader);
632 while (mime_types[n] && !scale)
634 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
637 g_strfreev(mime_types);
641 image_loader_emit_size(il);
645 g_mutex_lock(il->data_mutex);
648 if (width > il->requested_width || height > il->requested_height)
651 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
653 nw = il->requested_width;
654 nh = (gdouble)nw / width * height;
659 nh = il->requested_height;
660 nw = (gdouble)nh / height * width;
664 il->actual_width = nw;
665 il->actual_height = nh;
666 il->backend.set_size(loader, nw, nh);
670 g_mutex_unlock(il->data_mutex);
671 image_loader_emit_size(il);
674 static void image_loader_stop_loader(ImageLoader *il)
680 /* some loaders do not have a pixbuf till close, order is important here */
681 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
682 image_loader_sync_pixbuf(il);
683 il->backend.free(il->loader);
686 g_mutex_lock(il->data_mutex);
688 g_mutex_unlock(il->data_mutex);
691 static void image_loader_setup_loader(ImageLoader *il)
693 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
697 gint external_preview = 1;
699 g_mutex_lock(il->data_mutex);
701 if (options->external_preview.enable)
704 gchar *tilde_filename;
706 tilde_filename = expand_tilde(options->external_preview.select);
708 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
710 external_preview = runcmd(cmd_line);
712 g_free(tilde_filename);
715 if (external_preview == 0)
717 DEBUG_1("Using custom external loader");
718 image_loader_backend_set_external(&il->backend);
722 #ifdef HAVE_FFMPEGTHUMBNAILER
723 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
725 DEBUG_1("Using custom ffmpegthumbnailer loader");
726 image_loader_backend_set_ft(&il->backend);
731 if (il->bytes_total >= 4 &&
732 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
734 DEBUG_1("Using custom pdf loader");
735 image_loader_backend_set_pdf(&il->backend);
740 if (il->bytes_total >= 12 &&
741 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
742 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
743 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
744 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
746 DEBUG_1("Using custom heif loader");
747 image_loader_backend_set_heif(&il->backend);
752 if (il->bytes_total >= 12 &&
753 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
754 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
756 DEBUG_1("Using custom webp loader");
757 image_loader_backend_set_webp(&il->backend);
762 if (il->bytes_total >= 16 &&
763 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
764 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
766 DEBUG_1("Using custom djvu loader");
767 image_loader_backend_set_djvu(&il->backend);
772 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
774 DEBUG_1("Using custom jpeg loader");
775 image_loader_backend_set_jpeg(&il->backend);
779 if (il->bytes_total >= 11 &&
780 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
781 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
783 DEBUG_1("Using custom cr3 loader");
784 image_loader_backend_set_cr3(&il->backend);
790 if (il->bytes_total >= 10 &&
791 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
792 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
793 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
794 memcmp(il->mapped_file, "II*\0", 4) == 0))
796 DEBUG_1("Using custom tiff loader");
797 image_loader_backend_set_tiff(&il->backend);
801 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
803 DEBUG_1("Using dds loader");
804 image_loader_backend_set_dds(&il->backend);
807 if (il->bytes_total >= 6 &&
808 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
810 DEBUG_1("Using custom psd loader");
811 image_loader_backend_set_psd(&il->backend);
815 if (il->bytes_total >= 12 &&
816 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
818 DEBUG_1("Using custom j2k loader");
819 image_loader_backend_set_j2k(&il->backend);
824 if (il->bytes_total >= 12 &&
825 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
827 DEBUG_1("Using custom jpeg xl loader");
828 image_loader_backend_set_jpegxl(&il->backend);
831 if (il->bytes_total >= 2 &&
832 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
834 DEBUG_1("Using custom jpeg xl loader");
835 image_loader_backend_set_jpegxl(&il->backend);
839 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
840 (file_extension_match(il->fd->path, ".scr")))
842 DEBUG_1("Using custom zxscr loader");
843 image_loader_backend_set_zxscr(&il->backend);
846 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
848 DEBUG_1("Using custom collection loader");
849 image_loader_backend_set_collection(&il->backend);
852 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
854 DEBUG_1("Using custom svgz loader");
855 image_loader_backend_set_svgz(&il->backend);
858 image_loader_backend_set_default(&il->backend);
860 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
863 format = il->backend.get_format_name(il->loader);
864 if (g_strcmp0(format, "tiff") == 0)
866 il->backend.set_page_num(il->loader, il->fd->page_num);
872 format = il->backend.get_format_name(il->loader);
873 if (g_strcmp0(format, "pdf") == 0)
875 il->backend.set_page_num(il->loader, il->fd->page_num);
881 format = il->backend.get_format_name(il->loader);
882 if (g_strcmp0(format, "heif") == 0)
884 il->backend.set_page_num(il->loader, il->fd->page_num);
890 format = il->backend.get_format_name(il->loader);
891 if (g_strcmp0(format, "djvu") == 0)
893 il->backend.set_page_num(il->loader, il->fd->page_num);
898 il->fd->format_name = il->backend.get_format_name(il->loader);
900 g_mutex_unlock(il->data_mutex);
904 static void image_loader_done(ImageLoader *il)
906 image_loader_stop_loader(il);
908 image_loader_emit_done(il);
911 static void image_loader_error(ImageLoader *il)
913 image_loader_stop_loader(il);
915 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
917 image_loader_emit_error(il);
920 static gboolean image_loader_continue(ImageLoader *il)
925 if (!il) return FALSE;
927 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
928 while (c > 0 && !image_loader_get_stopping(il))
930 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
934 image_loader_done(il);
938 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
940 image_loader_error(il);
949 if (il->bytes_total > 0)
951 image_loader_emit_percent(il);
957 static gboolean image_loader_begin(ImageLoader *il)
959 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
964 if (il->pixbuf) return FALSE;
966 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
967 if (b < 1) return FALSE;
969 image_loader_setup_loader(il);
971 g_assert(il->bytes_read == 0);
972 if (il->backend.load) {
974 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
976 image_loader_stop_loader(il);
980 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
982 image_loader_stop_loader(il);
987 format = il->backend.get_format_name(il->loader);
988 if (g_strcmp0(format, "pdf") == 0)
990 gint i = il->backend.get_page_total(il->loader);
991 file_data_set_page_total(il->fd, i);
996 format = il->backend.get_format_name(il->loader);
997 if (g_strcmp0(format, "heif") == 0)
999 gint i = il->backend.get_page_total(il->loader);
1000 file_data_set_page_total(il->fd, i);
1005 format = il->backend.get_format_name(il->loader);
1006 if (g_strcmp0(format, "djvu") == 0)
1008 gint i = il->backend.get_page_total(il->loader);
1009 file_data_set_page_total(il->fd, i);
1014 format = il->backend.get_format_name(il->loader);
1015 if (g_strcmp0(format, "tiff") == 0)
1017 gint i = il->backend.get_page_total(il->loader);
1018 file_data_set_page_total(il->fd, i);
1023 il->bytes_read += b;
1025 /* read until size is known */
1026 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
1028 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
1029 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
1031 image_loader_stop_loader(il);
1034 il->bytes_read += b;
1036 if (!il->pixbuf) image_loader_sync_pixbuf(il);
1038 if (il->bytes_read == il->bytes_total || b < 1)
1040 /* done, handle (broken) loaders that do not have pixbuf till close */
1041 image_loader_stop_loader(il);
1043 if (!il->pixbuf) return FALSE;
1045 image_loader_done(il);
1051 image_loader_stop_loader(il);
1058 /**************************************************************************************/
1059 /* the following functions are always executed in the main thread */
1062 static gboolean image_loader_setup_source(ImageLoader *il)
1067 if (!il || il->loader || il->mapped_file) return FALSE;
1069 il->mapped_file = NULL;
1073 ExifData *exif = exif_read_fd(il->fd);
1075 if (options->thumbnails.use_exif)
1077 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
1079 if (il->mapped_file)
1081 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1086 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
1088 if (il->mapped_file)
1090 /* Both exiv2 and libraw sometimes return a pointer to a file
1091 * section that is not a jpeg */
1092 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1094 il->mapped_file = NULL;
1098 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1103 /* If exiv2 does not find a thumbnail, try libraw (which may be slower) */
1104 if (!il->mapped_file)
1106 il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
1108 if (il->mapped_file)
1110 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1112 il->mapped_file = NULL;
1116 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1121 if (il->mapped_file)
1123 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1125 exif_free_fd(il->fd, exif);
1129 if (!il->mapped_file)
1134 pathl = path_from_utf8(il->fd->path);
1135 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1137 if (load_fd == -1) return FALSE;
1139 if (fstat(load_fd, &st) == 0)
1141 il->bytes_total = st.st_size;
1149 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
1151 if (il->mapped_file == MAP_FAILED)
1153 il->mapped_file = 0;
1156 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1162 static void image_loader_stop_source(ImageLoader *il)
1166 if (il->mapped_file)
1168 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1170 exif_free_preview(il->mapped_file);
1172 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1174 libraw_free_preview(il->mapped_file);
1178 munmap(il->mapped_file, il->bytes_total);
1180 il->mapped_file = NULL;
1184 static void image_loader_stop(ImageLoader *il)
1190 g_source_remove(il->idle_id);
1196 /* stop loader in the other thread */
1197 g_mutex_lock(il->data_mutex);
1198 il->stopping = TRUE;
1199 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1200 g_mutex_unlock(il->data_mutex);
1203 image_loader_stop_loader(il);
1204 image_loader_stop_source(il);
1208 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1210 g_mutex_lock(il->data_mutex);
1211 il->delay_area_ready = enable;
1216 list = g_list_reverse(il->area_param_delayed_list);
1217 il->area_param_delayed_list = NULL;
1218 g_mutex_unlock(il->data_mutex);
1224 ImageLoaderAreaParam *par = work->data;
1227 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1235 g_mutex_unlock(il->data_mutex);
1240 /**************************************************************************************/
1241 /* execution via idle calls */
1243 static gboolean image_loader_idle_cb(gpointer data)
1245 gboolean ret = FALSE;
1246 ImageLoader *il = data;
1250 ret = image_loader_continue(il);
1255 image_loader_stop_source(il);
1262 static gboolean image_loader_start_idle(ImageLoader *il)
1266 if (!il) return FALSE;
1268 if (!il->fd) return FALSE;
1270 if (!image_loader_setup_source(il)) return FALSE;
1272 ret = image_loader_begin(il);
1274 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1278 /**************************************************************************************/
1279 /* execution via thread */
1282 static GThreadPool *image_loader_thread_pool = NULL;
1284 static GCond *image_loader_prio_cond = NULL;
1285 static GMutex *image_loader_prio_mutex = NULL;
1286 static gint image_loader_prio_num = 0;
1289 static void image_loader_thread_enter_high(void)
1291 g_mutex_lock(image_loader_prio_mutex);
1292 image_loader_prio_num++;
1293 g_mutex_unlock(image_loader_prio_mutex);
1296 static void image_loader_thread_leave_high(void)
1298 g_mutex_lock(image_loader_prio_mutex);
1299 image_loader_prio_num--;
1300 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1301 g_mutex_unlock(image_loader_prio_mutex);
1304 static void image_loader_thread_wait_high(void)
1306 g_mutex_lock(image_loader_prio_mutex);
1307 while (image_loader_prio_num)
1309 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1312 g_mutex_unlock(image_loader_prio_mutex);
1316 static void image_loader_thread_run(gpointer data, gpointer user_data)
1318 ImageLoader *il = data;
1322 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1324 /* low prio, wait until high prio tasks finishes */
1325 image_loader_thread_wait_high();
1330 image_loader_thread_enter_high();
1333 err = !image_loader_begin(il);
1338 loader failed, we have to send signal
1339 (idle mode returns the image_loader_begin return value directly)
1340 (success is always reported indirectly from image_loader_begin)
1342 image_loader_emit_error(il);
1347 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1349 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1351 /* low prio, wait until high prio tasks finishes */
1352 image_loader_thread_wait_high();
1354 cont = image_loader_continue(il);
1356 image_loader_stop_loader(il);
1358 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1361 image_loader_thread_leave_high();
1364 g_mutex_lock(il->data_mutex);
1365 il->can_destroy = TRUE;
1366 g_cond_signal(il->can_destroy_cond);
1367 g_mutex_unlock(il->data_mutex);
1372 static gboolean image_loader_start_thread(ImageLoader *il)
1374 if (!il) return FALSE;
1376 if (!il->fd) return FALSE;
1380 if (!image_loader_setup_source(il)) return FALSE;
1382 if (!image_loader_thread_pool)
1384 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1385 #if GLIB_CHECK_VERSION(2,32,0)
1386 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1387 g_cond_init(image_loader_prio_cond);
1388 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1389 g_mutex_init(image_loader_prio_mutex);
1391 image_loader_prio_cond = g_cond_new();
1392 image_loader_prio_mutex = g_mutex_new();
1396 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1398 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1399 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1403 #endif /* HAVE_GTHREAD */
1406 /**************************************************************************************/
1407 /* public interface */
1410 gboolean image_loader_start(ImageLoader *il)
1412 if (!il) return FALSE;
1414 if (!il->fd) return FALSE;
1417 return image_loader_start_thread(il);
1419 return image_loader_start_idle(il);
1424 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1425 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1428 if (!il) return NULL;
1430 g_mutex_lock(il->data_mutex);
1432 g_mutex_unlock(il->data_mutex);
1436 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1440 g_mutex_lock(il->data_mutex);
1441 il->requested_width = width;
1442 il->requested_height = height;
1443 g_mutex_unlock(il->data_mutex);
1446 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1450 g_mutex_lock(il->data_mutex);
1451 il->idle_read_loop_count = count ? count : 1;
1452 g_mutex_unlock(il->data_mutex);
1455 void image_loader_set_priority(ImageLoader *il, gint priority)
1459 if (il->thread) return; /* can't change prio if the thread already runs */
1460 il->idle_priority = priority;
1464 gdouble image_loader_get_percent(ImageLoader *il)
1467 if (!il) return 0.0;
1469 g_mutex_lock(il->data_mutex);
1470 if (il->bytes_total == 0)
1476 ret = (gdouble)il->bytes_read / il->bytes_total;
1478 g_mutex_unlock(il->data_mutex);
1482 gboolean image_loader_get_is_done(ImageLoader *il)
1485 if (!il) return FALSE;
1487 g_mutex_lock(il->data_mutex);
1489 g_mutex_unlock(il->data_mutex);
1494 FileData *image_loader_get_fd(ImageLoader *il)
1497 if (!il) return NULL;
1499 g_mutex_lock(il->data_mutex);
1501 g_mutex_unlock(il->data_mutex);
1506 gboolean image_loader_get_shrunk(ImageLoader *il)
1509 if (!il) return FALSE;
1511 g_mutex_lock(il->data_mutex);
1513 g_mutex_unlock(il->data_mutex);
1517 const gchar *image_loader_get_error(ImageLoader *il)
1519 const gchar *ret = NULL;
1520 if (!il) return NULL;
1521 g_mutex_lock(il->data_mutex);
1522 if (il->error) ret = il->error->message;
1523 g_mutex_unlock(il->data_mutex);
1529 * @FIXME this can be rather slow and blocks until the size is known
1531 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1536 il = image_loader_new(fd);
1538 success = image_loader_start_idle(il);
1540 if (success && il->pixbuf)
1542 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1543 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1547 if (width) *width = -1;
1548 if (height) *height = -1;
1551 image_loader_free(il);
1555 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */