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_pdf.h"
31 #include "image_load_psd.h"
32 #include "image_load_heif.h"
33 #include "image_load_ffmpegthumbnailer.h"
34 #include "image_load_collection.h"
35 #include "image_load_webp.h"
36 #include "image_load_j2k.h"
37 #include "image_load_libraw.h"
38 #include "image_load_svgz.h"
42 #include "ui_fileops.h"
43 #include "gq-marshal.h"
48 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
49 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
52 * @page diagrams Diagrams
53 * @section image_load_overview Image Load Overview
56 * object image_change_complete
57 * object image_load_begin
58 * object image_loader_start
59 * object image_loader_start_thread
60 * object image_loader_start_idle
61 * object image_loader_setup_source
62 * object image_loader_thread_run
63 * object image_loader_begin
64 * object image_loader_setuploader
65 * circle "il->memory_mapped"
66 * object exif_get_preview_
67 * object exif_get_preview
68 * object libraw_get_preview
70 * image_change : image.c
71 * image_change_complete : image.c
72 * image_load_begin : image.c
73 * image_loader_start : image_load.c
74 * image_loader_start_thread : image_load.c
75 * image_loader_start_idle : image_load.c
76 * image_loader_thread_run : image_load.c
77 * image_loader_begin : image_load.c
78 * image_loader_setuploader : image_load.c
79 * image_loader_setuploader : -
80 * image_loader_setuploader : Select backend using magic
81 * image_loader_setup_source : image_load.c
82 * exif_get_preview : exiv2.cc
83 * exif_get_preview : EXIV2_TEST_VERSION(0,17,90)
84 * exif_get_preview_ : exif.c
85 * exif_get_preview_ : -
86 * exif_get_preview_ : If exiv2 not installed
87 * libraw_get_preview : image_load_libraw.c
89 * image_change --> image_change_complete
90 * image_change_complete --> image_load_begin
91 * image_load_begin --> image_loader_start
92 * image_loader_start --> image_loader_start_thread
93 * image_loader_start --> image_loader_start_idle : Obsolete - no threads version
94 * image_loader_start_thread --> image_loader_thread_run
95 * image_loader_start_thread --> image_loader_setup_source
96 * image_loader_setup_source --> exif_get_preview_
97 * image_loader_setup_source --> exif_get_preview
98 * image_loader_setup_source --> libraw_get_preview : Try libraw if exiv2 fails
99 * exif_get_preview_ ..> "il->memory_mapped"
100 * exif_get_preview ..> "il->memory_mapped"
101 * libraw_get_preview ..> "il->memory_mapped"
102 * image_loader_thread_run --> image_loader_begin
103 * image_loader_begin --> image_loader_setuploader
104 * "il->memory_mapped" ..> image_loader_setuploader
105 * note left of "il->memory_mapped" : Points to first byte of embedded jpeg (#FFD8)\n if preview found, otherwise to first byte of file
110 * @ref image_load_overview "Image Load Overview"
113 /**************************************************************************************/
114 /* image loader class */
118 SIGNAL_AREA_READY = 0,
126 static guint signals[SIGNAL_COUNT] = { 0 };
128 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
129 static void image_loader_class_init_wrapper(void *data, void *user_data);
130 static void image_loader_class_init(ImageLoaderClass *class);
131 static void image_loader_finalize(GObject *object);
132 static void image_loader_stop(ImageLoader *il);
134 GType image_loader_get_type(void)
136 static GType type = 0;
139 static const GTypeInfo info = {
140 sizeof(ImageLoaderClass),
141 NULL, /* base_init */
142 NULL, /* base_finalize */
143 (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
144 NULL, /* class_finalize */
145 NULL, /* class_data */
148 (GInstanceInitFunc)image_loader_init, /* instance_init */
149 NULL /* value_table */
151 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
156 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
158 ImageLoader *il = (ImageLoader *)instance;
162 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
169 il->idle_done_id = 0;
171 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
172 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
173 il->mapped_file = NULL;
174 il->preview = IMAGE_LOADER_PREVIEW_NONE;
176 il->requested_width = 0;
177 il->requested_height = 0;
178 il->actual_width = 0;
179 il->actual_height = 0;
182 il->can_destroy = TRUE;
185 #if GLIB_CHECK_VERSION(2,32,0)
186 il->data_mutex = g_new(GMutex, 1);
187 g_mutex_init(il->data_mutex);
188 il->can_destroy_cond = g_new(GCond, 1);
189 g_cond_init(il->can_destroy_cond);
191 il->data_mutex = g_mutex_new();
192 il->can_destroy_cond = g_cond_new();
195 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
198 static void image_loader_class_init_wrapper(void *data, void *user_data)
200 image_loader_class_init(data);
203 static void image_loader_class_init(ImageLoaderClass *class)
205 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
207 // gobject_class->set_property = image_loader_set_property;
208 // gobject_class->get_property = image_loader_get_property;
210 gobject_class->finalize = image_loader_finalize;
213 signals[SIGNAL_AREA_READY] =
214 g_signal_new("area_ready",
215 G_OBJECT_CLASS_TYPE(gobject_class),
217 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
219 gq_marshal_VOID__INT_INT_INT_INT,
226 signals[SIGNAL_ERROR] =
227 g_signal_new("error",
228 G_OBJECT_CLASS_TYPE(gobject_class),
230 G_STRUCT_OFFSET(ImageLoaderClass, error),
232 g_cclosure_marshal_VOID__VOID,
235 signals[SIGNAL_DONE] =
237 G_OBJECT_CLASS_TYPE(gobject_class),
239 G_STRUCT_OFFSET(ImageLoaderClass, done),
241 g_cclosure_marshal_VOID__VOID,
244 signals[SIGNAL_PERCENT] =
245 g_signal_new("percent",
246 G_OBJECT_CLASS_TYPE(gobject_class),
248 G_STRUCT_OFFSET(ImageLoaderClass, percent),
250 g_cclosure_marshal_VOID__DOUBLE,
254 signals[SIGNAL_SIZE] =
255 g_signal_new("size_prepared",
256 G_OBJECT_CLASS_TYPE(gobject_class),
258 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
260 gq_marshal_VOID__INT_INT,
267 static void image_loader_finalize(GObject *object)
269 ImageLoader *il = (ImageLoader *)object;
271 image_loader_stop(il);
273 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
275 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
277 if (il->idle_done_id)
279 g_source_remove(il->idle_done_id);
280 il->idle_done_id = 0;
283 while (g_source_remove_by_user_data(il))
285 DEBUG_2("pending signals detected");
288 while (il->area_param_list)
290 DEBUG_1("pending area_ready signals detected");
291 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
292 g_free(il->area_param_list->data);
293 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
296 while (il->area_param_delayed_list)
298 g_free(il->area_param_delayed_list->data);
299 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
302 if (il->pixbuf) g_object_unref(il->pixbuf);
304 if (il->error) g_error_free(il->error);
306 file_data_unref(il->fd);
308 #if GLIB_CHECK_VERSION(2,32,0)
309 g_mutex_clear(il->data_mutex);
310 g_free(il->data_mutex);
311 g_cond_clear(il->can_destroy_cond);
312 g_free(il->can_destroy_cond);
314 g_mutex_free(il->data_mutex);
315 g_cond_free(il->can_destroy_cond);
320 void image_loader_free(ImageLoader *il)
323 g_object_unref(G_OBJECT(il));
327 ImageLoader *image_loader_new(FileData *fd)
331 if (!fd) return NULL;
333 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
335 il->fd = file_data_ref(fd);
340 /**************************************************************************************/
341 /* send signals via idle calbacks - the callback are executed in the main thread */
343 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
344 struct _ImageLoaderAreaParam {
353 static gboolean image_loader_emit_area_ready_cb(gpointer data)
355 ImageLoaderAreaParam *par = data;
356 ImageLoader *il = par->il;
358 g_mutex_lock(il->data_mutex);
359 il->area_param_list = g_list_remove(il->area_param_list, par);
365 g_mutex_unlock(il->data_mutex);
367 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
372 static gboolean image_loader_emit_done_cb(gpointer data)
374 ImageLoader *il = data;
375 g_signal_emit(il, signals[SIGNAL_DONE], 0);
379 static gboolean image_loader_emit_error_cb(gpointer data)
381 ImageLoader *il = data;
382 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
386 static gboolean image_loader_emit_percent_cb(gpointer data)
388 ImageLoader *il = data;
389 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
393 static gboolean image_loader_emit_size_cb(gpointer data)
396 ImageLoader *il = data;
397 g_mutex_lock(il->data_mutex);
398 width = il->actual_width;
399 height = il->actual_height;
400 g_mutex_unlock(il->data_mutex);
401 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
406 /* DONE and ERROR are emitted only once, thus they can have normal priority
407 PERCENT and AREA_READY should be processed ASAP
410 static void image_loader_emit_done(ImageLoader *il)
412 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
415 static void image_loader_emit_error(ImageLoader *il)
417 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
420 static void image_loader_emit_percent(ImageLoader *il)
422 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
425 static void image_loader_emit_size(ImageLoader *il)
427 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
430 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
434 ImageLoaderAreaParam *prev_par = (*list)->data;
435 if (prev_par->x == x && prev_par->w == w &&
436 prev_par->y + prev_par->h == y)
438 /* we can merge the notifications */
442 if (prev_par->x == x && prev_par->w == w &&
443 y + h == prev_par->y)
445 /* we can merge the notifications */
450 if (prev_par->y == y && prev_par->h == h &&
451 prev_par->x + prev_par->w == x)
453 /* we can merge the notifications */
457 if (prev_par->y == y && prev_par->h == h &&
458 x + w == prev_par->x)
460 /* we can merge the notifications */
467 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
474 *list = g_list_prepend(*list, par);
478 /* this function expects that il->data_mutex is locked by caller */
479 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
481 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
485 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
489 /**************************************************************************************/
490 /* the following functions may be executed in separate thread */
492 /* this function expects that il->data_mutex is locked by caller */
493 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
495 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
500 static gboolean image_loader_get_stopping(ImageLoader *il)
503 if (!il) return FALSE;
505 g_mutex_lock(il->data_mutex);
507 g_mutex_unlock(il->data_mutex);
513 static void image_loader_sync_pixbuf(ImageLoader *il)
517 g_mutex_lock(il->data_mutex);
521 g_mutex_unlock(il->data_mutex);
525 pb = il->backend.get_pixbuf(il->loader);
527 if (pb == il->pixbuf)
529 g_mutex_unlock(il->data_mutex);
533 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
535 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
538 if (il->pixbuf) g_object_unref(il->pixbuf);
541 if (il->pixbuf) g_object_ref(il->pixbuf);
543 g_mutex_unlock(il->data_mutex);
546 static void image_loader_area_updated_cb(gpointer loader,
547 guint x, guint y, guint w, guint h,
550 ImageLoader *il = data;
552 if (!image_loader_get_pixbuf(il))
554 image_loader_sync_pixbuf(il);
555 if (!image_loader_get_pixbuf(il))
557 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
561 g_mutex_lock(il->data_mutex);
562 if (il->delay_area_ready)
563 image_loader_queue_delayed_area_ready(il, x, y, w, h);
565 image_loader_emit_area_ready(il, x, y, w, h);
567 if (il->stopping) il->backend.abort(il->loader);
569 g_mutex_unlock(il->data_mutex);
572 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
574 ImageLoader *il = data;
580 http://bugzilla.gnome.org/show_bug.cgi?id=547669
581 http://bugzilla.gnome.org/show_bug.cgi?id=589334
583 gchar *format = il->backend.get_format_name(loader);
584 if (strcmp(format, "svg") == 0 ||
585 strcmp(format, "xpm") == 0)
593 pb = il->backend.get_pixbuf(loader);
595 h = gdk_pixbuf_get_height(pb);
596 rs = gdk_pixbuf_get_rowstride(pb);
597 pix = gdk_pixbuf_get_pixels(pb);
599 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
603 static void image_loader_size_cb(gpointer loader,
604 gint width, gint height, gpointer data)
606 ImageLoader *il = data;
608 gboolean scale = FALSE;
611 g_mutex_lock(il->data_mutex);
612 il->actual_width = width;
613 il->actual_height = height;
614 if (il->requested_width < 1 || il->requested_height < 1)
616 g_mutex_unlock(il->data_mutex);
617 image_loader_emit_size(il);
620 g_mutex_unlock(il->data_mutex);
622 #ifdef HAVE_FFMPEGTHUMBNAILER
623 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
626 mime_types = il->backend.get_format_mime_types(loader);
628 while (mime_types[n] && !scale)
630 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
633 g_strfreev(mime_types);
637 image_loader_emit_size(il);
641 g_mutex_lock(il->data_mutex);
644 if (width > il->requested_width || height > il->requested_height)
647 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
649 nw = il->requested_width;
650 nh = (gdouble)nw / width * height;
655 nh = il->requested_height;
656 nw = (gdouble)nh / height * width;
660 il->actual_width = nw;
661 il->actual_height = nh;
662 il->backend.set_size(loader, nw, nh);
666 g_mutex_unlock(il->data_mutex);
667 image_loader_emit_size(il);
670 static void image_loader_stop_loader(ImageLoader *il)
676 /* some loaders do not have a pixbuf till close, order is important here */
677 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
678 image_loader_sync_pixbuf(il);
679 il->backend.free(il->loader);
682 g_mutex_lock(il->data_mutex);
684 g_mutex_unlock(il->data_mutex);
687 static void image_loader_setup_loader(ImageLoader *il)
689 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
693 g_mutex_lock(il->data_mutex);
694 #ifdef HAVE_FFMPEGTHUMBNAILER
695 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
697 DEBUG_1("Using custom ffmpegthumbnailer loader");
698 image_loader_backend_set_ft(&il->backend);
703 if (il->bytes_total >= 4 &&
704 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
706 DEBUG_1("Using custom pdf loader");
707 image_loader_backend_set_pdf(&il->backend);
712 if (il->bytes_total >= 12 &&
713 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
714 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
715 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
716 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
718 DEBUG_1("Using custom heif loader");
719 image_loader_backend_set_heif(&il->backend);
724 if (il->bytes_total >= 12 &&
725 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
726 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
728 DEBUG_1("Using custom webp loader");
729 image_loader_backend_set_webp(&il->backend);
734 if (il->bytes_total >= 16 &&
735 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
736 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
738 DEBUG_1("Using custom djvu loader");
739 image_loader_backend_set_djvu(&il->backend);
744 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
746 DEBUG_1("Using custom jpeg loader");
747 image_loader_backend_set_jpeg(&il->backend);
751 if (il->bytes_total >= 11 &&
752 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
753 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
755 DEBUG_1("Using custom cr3 loader");
756 image_loader_backend_set_cr3(&il->backend);
762 if (il->bytes_total >= 10 &&
763 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
764 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
765 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
766 memcmp(il->mapped_file, "II*\0", 4) == 0))
768 DEBUG_1("Using custom tiff loader");
769 image_loader_backend_set_tiff(&il->backend);
773 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
775 DEBUG_1("Using dds loader");
776 image_loader_backend_set_dds(&il->backend);
779 if (il->bytes_total >= 6 &&
780 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
782 DEBUG_1("Using custom psd loader");
783 image_loader_backend_set_psd(&il->backend);
787 if (il->bytes_total >= 12 &&
788 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
790 DEBUG_1("Using custom j2k loader");
791 image_loader_backend_set_j2k(&il->backend);
795 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
797 DEBUG_1("Using custom collection loader");
798 image_loader_backend_set_collection(&il->backend);
801 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
803 DEBUG_1("Using custom svgz loader");
804 image_loader_backend_set_svgz(&il->backend);
807 image_loader_backend_set_default(&il->backend);
809 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
812 format = il->backend.get_format_name(il->loader);
813 if (g_strcmp0(format, "tiff") == 0)
815 il->backend.set_page_num(il->loader, il->fd->page_num);
821 format = il->backend.get_format_name(il->loader);
822 if (g_strcmp0(format, "pdf") == 0)
824 il->backend.set_page_num(il->loader, il->fd->page_num);
830 format = il->backend.get_format_name(il->loader);
831 if (g_strcmp0(format, "heif") == 0)
833 il->backend.set_page_num(il->loader, il->fd->page_num);
839 format = il->backend.get_format_name(il->loader);
840 if (g_strcmp0(format, "djvu") == 0)
842 il->backend.set_page_num(il->loader, il->fd->page_num);
847 g_mutex_unlock(il->data_mutex);
851 static void image_loader_done(ImageLoader *il)
853 image_loader_stop_loader(il);
855 image_loader_emit_done(il);
858 static void image_loader_error(ImageLoader *il)
860 image_loader_stop_loader(il);
862 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
864 image_loader_emit_error(il);
867 static gboolean image_loader_continue(ImageLoader *il)
872 if (!il) return FALSE;
874 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
875 while (c > 0 && !image_loader_get_stopping(il))
877 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
881 image_loader_done(il);
885 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
887 image_loader_error(il);
896 if (il->bytes_total > 0)
898 image_loader_emit_percent(il);
904 static gboolean image_loader_begin(ImageLoader *il)
906 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
911 if (il->pixbuf) return FALSE;
913 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
914 if (b < 1) return FALSE;
916 image_loader_setup_loader(il);
918 g_assert(il->bytes_read == 0);
919 if (il->backend.load) {
921 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
923 image_loader_stop_loader(il);
927 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
929 image_loader_stop_loader(il);
934 format = il->backend.get_format_name(il->loader);
935 if (g_strcmp0(format, "pdf") == 0)
937 gint i = il->backend.get_page_total(il->loader);
938 file_data_set_page_total(il->fd, i);
943 format = il->backend.get_format_name(il->loader);
944 if (g_strcmp0(format, "heif") == 0)
946 gint i = il->backend.get_page_total(il->loader);
947 file_data_set_page_total(il->fd, i);
952 format = il->backend.get_format_name(il->loader);
953 if (g_strcmp0(format, "djvu") == 0)
955 gint i = il->backend.get_page_total(il->loader);
956 file_data_set_page_total(il->fd, i);
961 format = il->backend.get_format_name(il->loader);
962 if (g_strcmp0(format, "tiff") == 0)
964 gint i = il->backend.get_page_total(il->loader);
965 file_data_set_page_total(il->fd, i);
972 /* read until size is known */
973 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
975 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
976 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
978 image_loader_stop_loader(il);
983 if (!il->pixbuf) image_loader_sync_pixbuf(il);
985 if (il->bytes_read == il->bytes_total || b < 1)
987 /* done, handle (broken) loaders that do not have pixbuf till close */
988 image_loader_stop_loader(il);
990 if (!il->pixbuf) return FALSE;
992 image_loader_done(il);
998 image_loader_stop_loader(il);
1005 /**************************************************************************************/
1006 /* the following functions are always executed in the main thread */
1009 static gboolean image_loader_setup_source(ImageLoader *il)
1014 if (!il || il->loader || il->mapped_file) return FALSE;
1016 il->mapped_file = NULL;
1020 ExifData *exif = exif_read_fd(il->fd);
1022 if (options->thumbnails.use_exif)
1024 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
1026 if (il->mapped_file)
1028 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1033 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
1035 if (il->mapped_file)
1037 /* Both exiv2 and libraw sometimes return a pointer to a file
1038 * section that is not a jpeg */
1039 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1041 il->mapped_file = NULL;
1045 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1050 /* If exiv2 does not find a thumbnail, try libraw (which may be slower) */
1051 if (!il->mapped_file)
1053 il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
1055 if (il->mapped_file)
1057 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1059 il->mapped_file = NULL;
1063 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1068 if (il->mapped_file)
1070 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1072 exif_free_fd(il->fd, exif);
1076 if (!il->mapped_file)
1081 pathl = path_from_utf8(il->fd->path);
1082 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1084 if (load_fd == -1) return FALSE;
1086 if (fstat(load_fd, &st) == 0)
1088 il->bytes_total = st.st_size;
1096 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
1098 if (il->mapped_file == MAP_FAILED)
1100 il->mapped_file = 0;
1103 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1109 static void image_loader_stop_source(ImageLoader *il)
1113 if (il->mapped_file)
1115 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1117 exif_free_preview(il->mapped_file);
1119 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1121 libraw_free_preview(il->mapped_file);
1125 munmap(il->mapped_file, il->bytes_total);
1127 il->mapped_file = NULL;
1131 static void image_loader_stop(ImageLoader *il)
1137 g_source_remove(il->idle_id);
1143 /* stop loader in the other thread */
1144 g_mutex_lock(il->data_mutex);
1145 il->stopping = TRUE;
1146 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1147 g_mutex_unlock(il->data_mutex);
1150 image_loader_stop_loader(il);
1151 image_loader_stop_source(il);
1155 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1157 g_mutex_lock(il->data_mutex);
1158 il->delay_area_ready = enable;
1163 list = g_list_reverse(il->area_param_delayed_list);
1164 il->area_param_delayed_list = NULL;
1165 g_mutex_unlock(il->data_mutex);
1171 ImageLoaderAreaParam *par = work->data;
1174 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1182 g_mutex_unlock(il->data_mutex);
1187 /**************************************************************************************/
1188 /* execution via idle calls */
1190 static gboolean image_loader_idle_cb(gpointer data)
1192 gboolean ret = FALSE;
1193 ImageLoader *il = data;
1197 ret = image_loader_continue(il);
1202 image_loader_stop_source(il);
1209 static gboolean image_loader_start_idle(ImageLoader *il)
1213 if (!il) return FALSE;
1215 if (!il->fd) return FALSE;
1217 if (!image_loader_setup_source(il)) return FALSE;
1219 ret = image_loader_begin(il);
1221 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1225 /**************************************************************************************/
1226 /* execution via thread */
1229 static GThreadPool *image_loader_thread_pool = NULL;
1231 static GCond *image_loader_prio_cond = NULL;
1232 static GMutex *image_loader_prio_mutex = NULL;
1233 static gint image_loader_prio_num = 0;
1236 static void image_loader_thread_enter_high(void)
1238 g_mutex_lock(image_loader_prio_mutex);
1239 image_loader_prio_num++;
1240 g_mutex_unlock(image_loader_prio_mutex);
1243 static void image_loader_thread_leave_high(void)
1245 g_mutex_lock(image_loader_prio_mutex);
1246 image_loader_prio_num--;
1247 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1248 g_mutex_unlock(image_loader_prio_mutex);
1251 static void image_loader_thread_wait_high(void)
1253 g_mutex_lock(image_loader_prio_mutex);
1254 while (image_loader_prio_num)
1256 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1259 g_mutex_unlock(image_loader_prio_mutex);
1263 static void image_loader_thread_run(gpointer data, gpointer user_data)
1265 ImageLoader *il = data;
1269 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1271 /* low prio, wait until high prio tasks finishes */
1272 image_loader_thread_wait_high();
1277 image_loader_thread_enter_high();
1280 err = !image_loader_begin(il);
1285 loader failed, we have to send signal
1286 (idle mode returns the image_loader_begin return value directly)
1287 (success is always reported indirectly from image_loader_begin)
1289 image_loader_emit_error(il);
1294 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1296 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1298 /* low prio, wait until high prio tasks finishes */
1299 image_loader_thread_wait_high();
1301 cont = image_loader_continue(il);
1303 image_loader_stop_loader(il);
1305 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1308 image_loader_thread_leave_high();
1311 g_mutex_lock(il->data_mutex);
1312 il->can_destroy = TRUE;
1313 g_cond_signal(il->can_destroy_cond);
1314 g_mutex_unlock(il->data_mutex);
1319 static gboolean image_loader_start_thread(ImageLoader *il)
1321 if (!il) return FALSE;
1323 if (!il->fd) return FALSE;
1327 if (!image_loader_setup_source(il)) return FALSE;
1329 if (!image_loader_thread_pool)
1331 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1332 #if GLIB_CHECK_VERSION(2,32,0)
1333 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1334 g_cond_init(image_loader_prio_cond);
1335 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1336 g_mutex_init(image_loader_prio_mutex);
1338 image_loader_prio_cond = g_cond_new();
1339 image_loader_prio_mutex = g_mutex_new();
1343 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1345 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1346 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1350 #endif /* HAVE_GTHREAD */
1353 /**************************************************************************************/
1354 /* public interface */
1357 gboolean image_loader_start(ImageLoader *il)
1359 if (!il) return FALSE;
1361 if (!il->fd) return FALSE;
1364 return image_loader_start_thread(il);
1366 return image_loader_start_idle(il);
1371 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1372 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1375 if (!il) return NULL;
1377 g_mutex_lock(il->data_mutex);
1379 g_mutex_unlock(il->data_mutex);
1383 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1387 g_mutex_lock(il->data_mutex);
1388 il->requested_width = width;
1389 il->requested_height = height;
1390 g_mutex_unlock(il->data_mutex);
1393 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1397 g_mutex_lock(il->data_mutex);
1398 il->idle_read_loop_count = count ? count : 1;
1399 g_mutex_unlock(il->data_mutex);
1402 void image_loader_set_priority(ImageLoader *il, gint priority)
1406 if (il->thread) return; /* can't change prio if the thread already runs */
1407 il->idle_priority = priority;
1411 gdouble image_loader_get_percent(ImageLoader *il)
1414 if (!il) return 0.0;
1416 g_mutex_lock(il->data_mutex);
1417 if (il->bytes_total == 0)
1423 ret = (gdouble)il->bytes_read / il->bytes_total;
1425 g_mutex_unlock(il->data_mutex);
1429 gboolean image_loader_get_is_done(ImageLoader *il)
1432 if (!il) return FALSE;
1434 g_mutex_lock(il->data_mutex);
1436 g_mutex_unlock(il->data_mutex);
1441 FileData *image_loader_get_fd(ImageLoader *il)
1444 if (!il) return NULL;
1446 g_mutex_lock(il->data_mutex);
1448 g_mutex_unlock(il->data_mutex);
1453 gboolean image_loader_get_shrunk(ImageLoader *il)
1456 if (!il) return FALSE;
1458 g_mutex_lock(il->data_mutex);
1460 g_mutex_unlock(il->data_mutex);
1464 const gchar *image_loader_get_error(ImageLoader *il)
1466 const gchar *ret = NULL;
1467 if (!il) return NULL;
1468 g_mutex_lock(il->data_mutex);
1469 if (il->error) ret = il->error->message;
1470 g_mutex_unlock(il->data_mutex);
1476 * @FIXME this can be rather slow and blocks until the size is known
1478 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1483 il = image_loader_new(fd);
1485 success = image_loader_start_idle(il);
1487 if (success && il->pixbuf)
1489 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1490 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1494 if (width) *width = -1;
1495 if (height) *height = -1;
1498 image_loader_free(il);
1502 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */