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"
27 #include "gq-marshal.h"
28 #include "image-load-collection.h"
29 #include "image-load-dds.h"
30 #include "image-load-external.h"
31 #include "image-load-gdk.h"
32 #include "image-load-libraw.h"
33 #include "image-load-psd.h"
34 #include "image-load-svgz.h"
35 #include "image-load-webp.h"
36 #include "image-load-zxscr.h"
38 #include "ui-fileops.h"
41 #include "image-load-djvu.h"
43 #ifdef HAVE_FFMPEGTHUMBNAILER
44 #include "image-load-ffmpegthumbnailer.h"
47 #include "image-load-heif.h"
50 #include "image-load-j2k.h"
53 #include "image-load-cr3.h"
54 #include "image-load-jpeg.h"
57 #include "image-load-jpegxl.h"
60 #include "image-load-pdf.h"
63 #include "image-load-tiff.h"
70 IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT = 4096,
71 IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT = 1
74 /* image loader class */
78 SIGNAL_AREA_READY = 0,
86 static guint signals[SIGNAL_COUNT] = { 0 };
88 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
89 static void image_loader_class_init_wrapper(void *data, void *user_data);
90 static void image_loader_class_init(ImageLoaderClass *loader_class);
91 static void image_loader_finalize(GObject *object);
92 static void image_loader_stop(ImageLoader *il);
94 GType image_loader_get_type()
96 static GType type = 0;
99 static const GTypeInfo info = {
100 sizeof(ImageLoaderClass),
101 nullptr, /* base_init */
102 nullptr, /* base_finalize */
103 static_cast<GClassInitFunc>(image_loader_class_init_wrapper), /* class_init */
104 nullptr, /* class_finalize */
105 nullptr, /* class_data */
108 static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
109 nullptr /* value_table */
111 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
116 static void image_loader_init(GTypeInstance *instance, gpointer)
118 auto il = reinterpret_cast<ImageLoader *>(instance);
120 il->pixbuf = nullptr;
122 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
124 il->loader = nullptr;
129 il->idle_done_id = 0;
131 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
132 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
133 il->mapped_file = nullptr;
134 il->preview = IMAGE_LOADER_PREVIEW_NONE;
136 il->requested_width = 0;
137 il->requested_height = 0;
138 il->actual_width = 0;
139 il->actual_height = 0;
142 il->can_destroy = TRUE;
144 il->data_mutex = g_new(GMutex, 1);
145 g_mutex_init(il->data_mutex);
146 il->can_destroy_cond = g_new(GCond, 1);
147 g_cond_init(il->can_destroy_cond);
149 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
152 static void image_loader_class_init_wrapper(void *data, void *)
154 image_loader_class_init(static_cast<ImageLoaderClass *>(data));
157 static void image_loader_class_init(ImageLoaderClass *loader_class)
159 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
161 gobject_class->finalize = image_loader_finalize;
164 signals[SIGNAL_AREA_READY] =
165 g_signal_new("area_ready",
166 G_OBJECT_CLASS_TYPE(gobject_class),
168 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
170 gq_marshal_VOID__INT_INT_INT_INT,
177 signals[SIGNAL_ERROR] =
178 g_signal_new("error",
179 G_OBJECT_CLASS_TYPE(gobject_class),
181 G_STRUCT_OFFSET(ImageLoaderClass, error),
183 g_cclosure_marshal_VOID__VOID,
186 signals[SIGNAL_DONE] =
188 G_OBJECT_CLASS_TYPE(gobject_class),
190 G_STRUCT_OFFSET(ImageLoaderClass, done),
192 g_cclosure_marshal_VOID__VOID,
195 signals[SIGNAL_PERCENT] =
196 g_signal_new("percent",
197 G_OBJECT_CLASS_TYPE(gobject_class),
199 G_STRUCT_OFFSET(ImageLoaderClass, percent),
201 g_cclosure_marshal_VOID__DOUBLE,
205 signals[SIGNAL_SIZE] =
206 g_signal_new("size_prepared",
207 G_OBJECT_CLASS_TYPE(gobject_class),
209 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
211 gq_marshal_VOID__INT_INT,
218 static void image_loader_finalize(GObject *object)
220 auto il = reinterpret_cast<ImageLoader *>(object);
222 image_loader_stop(il);
224 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
226 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
228 if (il->idle_done_id)
230 g_source_remove(il->idle_done_id);
231 il->idle_done_id = 0;
234 while (g_source_remove_by_user_data(il))
236 DEBUG_2("pending signals detected");
239 while (il->area_param_list)
241 DEBUG_1("pending area_ready signals detected");
242 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
243 g_free(il->area_param_list->data);
244 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
247 while (il->area_param_delayed_list)
249 g_free(il->area_param_delayed_list->data);
250 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
253 if (il->pixbuf) g_object_unref(il->pixbuf);
255 if (il->error) g_error_free(il->error);
257 file_data_unref(il->fd);
259 g_mutex_clear(il->data_mutex);
260 g_free(il->data_mutex);
261 g_cond_clear(il->can_destroy_cond);
262 g_free(il->can_destroy_cond);
265 void image_loader_free(ImageLoader *il)
268 g_object_unref(G_OBJECT(il));
272 ImageLoader *image_loader_new(FileData *fd)
276 if (!fd) return nullptr;
278 il = static_cast<ImageLoader *>(g_object_new(TYPE_IMAGE_LOADER, nullptr));
280 il->fd = file_data_ref(fd);
285 /**************************************************************************************/
286 /* send signals via idle calbacks - the callback are executed in the main thread */
288 struct ImageLoaderAreaParam {
297 static gboolean image_loader_emit_area_ready_cb(gpointer data)
299 auto par = static_cast<ImageLoaderAreaParam *>(data);
300 ImageLoader *il = par->il;
302 g_mutex_lock(il->data_mutex);
303 il->area_param_list = g_list_remove(il->area_param_list, par);
309 g_mutex_unlock(il->data_mutex);
311 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
313 return G_SOURCE_REMOVE;
316 static gboolean image_loader_emit_done_cb(gpointer data)
318 auto il = static_cast<ImageLoader *>(data);
319 g_signal_emit(il, signals[SIGNAL_DONE], 0);
320 return G_SOURCE_REMOVE;
323 static gboolean image_loader_emit_error_cb(gpointer data)
325 auto il = static_cast<ImageLoader *>(data);
326 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
327 return G_SOURCE_REMOVE;
330 static gboolean image_loader_emit_percent_cb(gpointer data)
332 auto il = static_cast<ImageLoader *>(data);
333 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
334 return G_SOURCE_REMOVE;
337 static gboolean image_loader_emit_size_cb(gpointer data)
340 auto il = static_cast<ImageLoader *>(data);
341 g_mutex_lock(il->data_mutex);
342 width = il->actual_width;
343 height = il->actual_height;
344 g_mutex_unlock(il->data_mutex);
345 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
346 return G_SOURCE_REMOVE;
350 /* DONE and ERROR are emitted only once, thus they can have normal priority
351 PERCENT and AREA_READY should be processed ASAP
354 static void image_loader_emit_done(ImageLoader *il)
356 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, nullptr);
359 static void image_loader_emit_error(ImageLoader *il)
361 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, nullptr);
364 static void image_loader_emit_percent(ImageLoader *il)
366 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, nullptr);
369 static void image_loader_emit_size(ImageLoader *il)
371 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, nullptr);
374 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
378 auto prev_par = static_cast<ImageLoaderAreaParam *>((*list)->data);
379 if (prev_par->x == x && prev_par->w == w &&
380 prev_par->y + prev_par->h == y)
382 /* we can merge the notifications */
386 if (prev_par->x == x && prev_par->w == w &&
387 y + h == prev_par->y)
389 /* we can merge the notifications */
394 if (prev_par->y == y && prev_par->h == h &&
395 prev_par->x + prev_par->w == x)
397 /* we can merge the notifications */
401 if (prev_par->y == y && prev_par->h == h &&
402 x + w == prev_par->x)
404 /* we can merge the notifications */
411 auto par = g_new0(ImageLoaderAreaParam, 1);
418 *list = g_list_prepend(*list, par);
422 /* this function expects that il->data_mutex is locked by caller */
423 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
425 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
429 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, nullptr);
433 /**************************************************************************************/
434 /* the following functions may be executed in separate thread */
436 /* this function expects that il->data_mutex is locked by caller */
437 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
439 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
444 static gboolean image_loader_get_stopping(ImageLoader *il)
447 if (!il) return FALSE;
449 g_mutex_lock(il->data_mutex);
451 g_mutex_unlock(il->data_mutex);
457 static void image_loader_sync_pixbuf(ImageLoader *il)
461 g_mutex_lock(il->data_mutex);
465 g_mutex_unlock(il->data_mutex);
469 pb = il->backend.get_pixbuf(il->loader);
471 if (pb == il->pixbuf)
473 g_mutex_unlock(il->data_mutex);
477 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
479 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
482 if (il->pixbuf) g_object_unref(il->pixbuf);
485 if (il->pixbuf) g_object_ref(il->pixbuf);
487 g_mutex_unlock(il->data_mutex);
490 static void image_loader_area_updated_cb(gpointer,
491 guint x, guint y, guint w, guint h,
494 auto il = static_cast<ImageLoader *>(data);
496 if (!image_loader_get_pixbuf(il))
498 image_loader_sync_pixbuf(il);
499 if (!image_loader_get_pixbuf(il))
501 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
505 g_mutex_lock(il->data_mutex);
506 if (il->delay_area_ready)
507 image_loader_queue_delayed_area_ready(il, x, y, w, h);
509 image_loader_emit_area_ready(il, x, y, w, h);
511 if (il->stopping) il->backend.abort(il->loader);
513 g_mutex_unlock(il->data_mutex);
516 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
518 auto il = static_cast<ImageLoader *>(data);
524 https://bugzilla.gnome.org/show_bug.cgi?id=547669
525 https://bugzilla.gnome.org/show_bug.cgi?id=589334
527 gchar *format = il->backend.get_format_name(loader);
528 if (strcmp(format, "svg") == 0 ||
529 strcmp(format, "xpm") == 0)
537 pb = il->backend.get_pixbuf(loader);
539 h = gdk_pixbuf_get_height(pb);
540 rs = gdk_pixbuf_get_rowstride(pb);
541 pix = gdk_pixbuf_get_pixels(pb);
543 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
547 static void image_loader_size_cb(gpointer loader,
548 gint width, gint height, gpointer data)
550 auto il = static_cast<ImageLoader *>(data);
552 gboolean scale = FALSE;
555 g_mutex_lock(il->data_mutex);
556 il->actual_width = width;
557 il->actual_height = height;
558 if (il->requested_width < 1 || il->requested_height < 1)
560 g_mutex_unlock(il->data_mutex);
561 image_loader_emit_size(il);
564 g_mutex_unlock(il->data_mutex);
566 #ifdef HAVE_FFMPEGTHUMBNAILER
567 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
570 mime_types = il->backend.get_format_mime_types(loader);
572 while (mime_types[n] && !scale)
574 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
577 g_strfreev(mime_types);
581 image_loader_emit_size(il);
585 g_mutex_lock(il->data_mutex);
588 if (width > il->requested_width || height > il->requested_height)
591 if ((static_cast<gdouble>(il->requested_width) / width) < (static_cast<gdouble>(il->requested_height) / height))
593 nw = il->requested_width;
594 nh = static_cast<gdouble>(nw) / width * height;
599 nh = il->requested_height;
600 nw = static_cast<gdouble>(nh) / height * width;
604 il->actual_width = nw;
605 il->actual_height = nh;
606 il->backend.set_size(loader, nw, nh);
610 g_mutex_unlock(il->data_mutex);
611 image_loader_emit_size(il);
614 static void image_loader_stop_loader(ImageLoader *il)
620 /* some loaders do not have a pixbuf till close, order is important here */
621 il->backend.close(il->loader, il->error ? nullptr : &il->error); /* we are interested in the first error only */
622 image_loader_sync_pixbuf(il);
623 il->backend.free(il->loader);
624 il->loader = nullptr;
626 g_mutex_lock(il->data_mutex);
628 g_mutex_unlock(il->data_mutex);
631 static void image_loader_setup_loader(ImageLoader *il)
633 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
637 gint external_preview = 1;
639 g_mutex_lock(il->data_mutex);
641 if (options->external_preview.enable)
644 gchar *tilde_filename;
646 tilde_filename = expand_tilde(options->external_preview.select);
648 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
650 external_preview = runcmd(cmd_line);
652 g_free(tilde_filename);
655 if (external_preview == 0)
657 DEBUG_1("Using custom external loader");
658 image_loader_backend_set_external(&il->backend);
662 #ifdef HAVE_FFMPEGTHUMBNAILER
663 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
665 DEBUG_1("Using custom ffmpegthumbnailer loader");
666 image_loader_backend_set_ft(&il->backend);
671 if (il->bytes_total >= 4 &&
672 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
674 DEBUG_1("Using custom pdf loader");
675 image_loader_backend_set_pdf(&il->backend);
680 if (il->bytes_total >= 12 &&
681 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
682 (memcmp(il->mapped_file + 4, "ftypheix", 8) == 0) ||
683 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
684 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
685 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
687 DEBUG_1("Using custom heif loader");
688 image_loader_backend_set_heif(&il->backend);
693 if (il->bytes_total >= 12 &&
694 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
695 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
697 DEBUG_1("Using custom webp loader");
698 image_loader_backend_set_webp(&il->backend);
703 if (il->bytes_total >= 16 &&
704 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
705 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
707 DEBUG_1("Using custom djvu loader");
708 image_loader_backend_set_djvu(&il->backend);
713 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
715 DEBUG_1("Using custom jpeg loader");
716 image_loader_backend_set_jpeg(&il->backend);
720 if (il->bytes_total >= 11 &&
721 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
722 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
724 DEBUG_1("Using custom cr3 loader");
725 image_loader_backend_set_cr3(&il->backend);
731 if (il->bytes_total >= 10 &&
732 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
733 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
734 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
735 memcmp(il->mapped_file, "II*\0", 4) == 0))
737 DEBUG_1("Using custom tiff loader");
738 image_loader_backend_set_tiff(&il->backend);
742 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
744 DEBUG_1("Using dds loader");
745 image_loader_backend_set_dds(&il->backend);
748 if (il->bytes_total >= 6 &&
749 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
751 DEBUG_1("Using custom psd loader");
752 image_loader_backend_set_psd(&il->backend);
756 if (il->bytes_total >= 12 &&
757 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
759 DEBUG_1("Using custom j2k loader");
760 image_loader_backend_set_j2k(&il->backend);
765 if (il->bytes_total >= 12 &&
766 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
768 DEBUG_1("Using custom jpeg xl loader");
769 image_loader_backend_set_jpegxl(&il->backend);
772 if (il->bytes_total >= 2 &&
773 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
775 DEBUG_1("Using custom jpeg xl loader");
776 image_loader_backend_set_jpegxl(&il->backend);
780 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
781 (file_extension_match(il->fd->path, ".scr")))
783 DEBUG_1("Using custom zxscr loader");
784 image_loader_backend_set_zxscr(&il->backend);
787 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
789 DEBUG_1("Using custom collection loader");
790 image_loader_backend_set_collection(&il->backend);
793 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
795 DEBUG_1("Using custom svgz loader");
796 image_loader_backend_set_svgz(&il->backend);
799 image_loader_backend_set_default(&il->backend);
802 il->loader = static_cast<void **>(il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il));
805 format = il->backend.get_format_name(il->loader);
806 if (g_strcmp0(format, "tiff") == 0)
808 il->backend.set_page_num(il->loader, il->fd->page_num);
814 format = il->backend.get_format_name(il->loader);
815 if (g_strcmp0(format, "pdf") == 0)
817 il->backend.set_page_num(il->loader, il->fd->page_num);
823 format = il->backend.get_format_name(il->loader);
824 if (g_strcmp0(format, "heif") == 0)
826 il->backend.set_page_num(il->loader, il->fd->page_num);
832 format = il->backend.get_format_name(il->loader);
833 if (g_strcmp0(format, "djvu") == 0)
835 il->backend.set_page_num(il->loader, il->fd->page_num);
840 il->fd->format_name = il->backend.get_format_name(il->loader);
842 g_mutex_unlock(il->data_mutex);
846 static void image_loader_done(ImageLoader *il)
848 image_loader_stop_loader(il);
850 image_loader_emit_done(il);
853 static void image_loader_error(ImageLoader *il)
855 image_loader_stop_loader(il);
857 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
859 image_loader_emit_error(il);
862 static gboolean image_loader_continue(ImageLoader *il)
867 if (!il) return G_SOURCE_REMOVE;
869 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
870 while (c > 0 && !image_loader_get_stopping(il))
872 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
876 image_loader_done(il);
877 return G_SOURCE_REMOVE;
880 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
882 image_loader_error(il);
883 return G_SOURCE_REMOVE;
891 if (il->bytes_total > 0)
893 image_loader_emit_percent(il);
896 return G_SOURCE_CONTINUE;
899 static gboolean image_loader_begin(ImageLoader *il)
901 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
906 if (il->pixbuf) return FALSE;
908 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
909 if (b < 1) return FALSE;
911 image_loader_setup_loader(il);
913 g_assert(il->bytes_read == 0);
914 if (il->backend.load) {
916 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
918 image_loader_stop_loader(il);
922 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
924 image_loader_stop_loader(il);
929 format = il->backend.get_format_name(il->loader);
930 if (g_strcmp0(format, "pdf") == 0)
932 gint i = il->backend.get_page_total(il->loader);
933 file_data_set_page_total(il->fd, i);
938 format = il->backend.get_format_name(il->loader);
939 if (g_strcmp0(format, "heif") == 0)
941 gint i = il->backend.get_page_total(il->loader);
942 file_data_set_page_total(il->fd, i);
947 format = il->backend.get_format_name(il->loader);
948 if (g_strcmp0(format, "djvu") == 0)
950 gint i = il->backend.get_page_total(il->loader);
951 file_data_set_page_total(il->fd, i);
956 format = il->backend.get_format_name(il->loader);
957 if (g_strcmp0(format, "tiff") == 0)
959 gint i = il->backend.get_page_total(il->loader);
960 file_data_set_page_total(il->fd, i);
967 /* read until size is known */
968 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
970 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
971 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
973 image_loader_stop_loader(il);
978 if (!il->pixbuf) image_loader_sync_pixbuf(il);
980 if (il->bytes_read == il->bytes_total || b < 1)
982 /* done, handle (broken) loaders that do not have pixbuf till close */
983 image_loader_stop_loader(il);
985 if (!il->pixbuf) return FALSE;
987 image_loader_done(il);
993 image_loader_stop_loader(il);
1000 /**************************************************************************************/
1001 /* the following functions are always executed in the main thread */
1004 static gboolean image_loader_setup_source(ImageLoader *il)
1009 if (!il || il->loader || il->mapped_file) return FALSE;
1011 il->mapped_file = nullptr;
1015 ExifData *exif = exif_read_fd(il->fd);
1017 if (options->thumbnails.use_exif)
1019 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), il->requested_width, il->requested_height);
1021 if (il->mapped_file)
1023 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1028 il->mapped_file = libraw_get_preview(il, reinterpret_cast<guint *>(&il->bytes_total));
1030 if (il->mapped_file)
1032 /* Both exiv2 and libraw sometimes return a pointer to a file
1033 * section that is not a jpeg */
1034 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1036 il->mapped_file = nullptr;
1040 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1045 /* If libraw does not find a thumbnail, try exiv2 */
1046 if (!il->mapped_file)
1048 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), 0, 0); /* get the largest available preview image or NULL for normal images*/
1050 if (il->mapped_file)
1052 /* Both exiv2 and libraw sometimes return a pointer to a file
1053 * section that is not a jpeg */
1054 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1056 il->mapped_file = nullptr;
1060 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1065 if (il->mapped_file)
1067 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1069 exif_free_fd(il->fd, exif);
1073 if (!il->mapped_file)
1078 pathl = path_from_utf8(il->fd->path);
1079 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1081 if (load_fd == -1) return FALSE;
1083 if (fstat(load_fd, &st) == 0)
1085 il->bytes_total = st.st_size;
1093 il->mapped_file = static_cast<guchar *>(mmap(nullptr, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0));
1095 if (il->mapped_file == MAP_FAILED)
1097 il->mapped_file = nullptr;
1100 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1106 static void image_loader_stop_source(ImageLoader *il)
1110 if (il->mapped_file)
1112 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1114 exif_free_preview(il->mapped_file);
1116 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1118 libraw_free_preview(il->mapped_file);
1122 munmap(il->mapped_file, il->bytes_total);
1124 il->mapped_file = nullptr;
1128 static void image_loader_stop(ImageLoader *il)
1134 g_source_remove(il->idle_id);
1140 /* stop loader in the other thread */
1141 g_mutex_lock(il->data_mutex);
1142 il->stopping = TRUE;
1143 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1144 g_mutex_unlock(il->data_mutex);
1147 image_loader_stop_loader(il);
1148 image_loader_stop_source(il);
1152 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1154 g_mutex_lock(il->data_mutex);
1155 il->delay_area_ready = enable;
1160 list = g_list_reverse(il->area_param_delayed_list);
1161 il->area_param_delayed_list = nullptr;
1162 g_mutex_unlock(il->data_mutex);
1168 auto par = static_cast<ImageLoaderAreaParam *>(work->data);
1171 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1173 g_list_free_full(list, g_free);
1178 g_mutex_unlock(il->data_mutex);
1183 /**************************************************************************************/
1184 /* execution via idle calls */
1186 static gboolean image_loader_idle_cb(gpointer data)
1188 gboolean ret = G_SOURCE_REMOVE;
1189 auto il = static_cast<ImageLoader *>(data);
1193 ret = image_loader_continue(il);
1198 image_loader_stop_source(il);
1204 static gboolean image_loader_start_idle(ImageLoader *il)
1208 if (!il) return FALSE;
1210 if (!il->fd) return FALSE;
1212 if (!image_loader_setup_source(il)) return FALSE;
1214 ret = image_loader_begin(il);
1216 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, nullptr);
1220 /**************************************************************************************/
1221 /* execution via thread */
1223 static GThreadPool *image_loader_thread_pool = nullptr;
1225 static GCond *image_loader_prio_cond = nullptr;
1226 static GMutex *image_loader_prio_mutex = nullptr;
1227 static gint image_loader_prio_num = 0;
1230 static void image_loader_thread_enter_high()
1232 g_mutex_lock(image_loader_prio_mutex);
1233 image_loader_prio_num++;
1234 g_mutex_unlock(image_loader_prio_mutex);
1237 static void image_loader_thread_leave_high()
1239 g_mutex_lock(image_loader_prio_mutex);
1240 image_loader_prio_num--;
1241 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1242 g_mutex_unlock(image_loader_prio_mutex);
1245 static void image_loader_thread_wait_high()
1247 g_mutex_lock(image_loader_prio_mutex);
1248 while (image_loader_prio_num)
1250 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1253 g_mutex_unlock(image_loader_prio_mutex);
1257 static void image_loader_thread_run(gpointer data, gpointer)
1259 auto il = static_cast<ImageLoader *>(data);
1263 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1265 /* low prio, wait until high prio tasks finishes */
1266 image_loader_thread_wait_high();
1271 image_loader_thread_enter_high();
1274 err = !image_loader_begin(il);
1279 loader failed, we have to send signal
1280 (idle mode returns the image_loader_begin return value directly)
1281 (success is always reported indirectly from image_loader_begin)
1283 image_loader_emit_error(il);
1288 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1290 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1292 /* low prio, wait until high prio tasks finishes */
1293 image_loader_thread_wait_high();
1295 cont = image_loader_continue(il);
1297 image_loader_stop_loader(il);
1299 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1302 image_loader_thread_leave_high();
1305 g_mutex_lock(il->data_mutex);
1306 il->can_destroy = TRUE;
1307 g_cond_signal(il->can_destroy_cond);
1308 g_mutex_unlock(il->data_mutex);
1313 static gboolean image_loader_start_thread(ImageLoader *il)
1315 if (!il) return FALSE;
1317 if (!il->fd) return FALSE;
1321 if (!image_loader_setup_source(il)) return FALSE;
1323 if (!image_loader_thread_pool)
1325 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, nullptr, -1, FALSE, nullptr);
1326 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1327 g_cond_init(image_loader_prio_cond);
1328 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1329 g_mutex_init(image_loader_prio_mutex);
1332 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1334 g_thread_pool_push(image_loader_thread_pool, il, nullptr);
1335 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1341 /**************************************************************************************/
1342 /* public interface */
1345 gboolean image_loader_start(ImageLoader *il)
1347 if (!il) return FALSE;
1349 if (!il->fd) return FALSE;
1351 return image_loader_start_thread(il);
1355 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1356 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1359 if (!il) return nullptr;
1361 g_mutex_lock(il->data_mutex);
1363 g_mutex_unlock(il->data_mutex);
1367 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1371 g_mutex_lock(il->data_mutex);
1372 il->requested_width = width;
1373 il->requested_height = height;
1374 g_mutex_unlock(il->data_mutex);
1377 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1381 g_mutex_lock(il->data_mutex);
1382 il->idle_read_loop_count = count ? count : 1;
1383 g_mutex_unlock(il->data_mutex);
1386 void image_loader_set_priority(ImageLoader *il, gint priority)
1390 if (il->thread) return; /* can't change prio if the thread already runs */
1391 il->idle_priority = priority;
1395 gdouble image_loader_get_percent(ImageLoader *il)
1398 if (!il) return 0.0;
1400 g_mutex_lock(il->data_mutex);
1401 if (il->bytes_total == 0)
1407 ret = static_cast<gdouble>(il->bytes_read) / il->bytes_total;
1409 g_mutex_unlock(il->data_mutex);
1413 gboolean image_loader_get_is_done(ImageLoader *il)
1416 if (!il) return FALSE;
1418 g_mutex_lock(il->data_mutex);
1420 g_mutex_unlock(il->data_mutex);
1425 FileData *image_loader_get_fd(ImageLoader *il)
1428 if (!il) return nullptr;
1430 g_mutex_lock(il->data_mutex);
1432 g_mutex_unlock(il->data_mutex);
1437 gboolean image_loader_get_shrunk(ImageLoader *il)
1440 if (!il) return FALSE;
1442 g_mutex_lock(il->data_mutex);
1444 g_mutex_unlock(il->data_mutex);
1448 const gchar *image_loader_get_error(ImageLoader *il)
1450 const gchar *ret = nullptr;
1451 if (!il) return nullptr;
1452 g_mutex_lock(il->data_mutex);
1453 if (il->error) ret = il->error->message;
1454 g_mutex_unlock(il->data_mutex);
1460 * @FIXME this can be rather slow and blocks until the size is known
1462 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1467 il = image_loader_new(fd);
1469 success = image_loader_start_idle(il);
1471 if (success && il->pixbuf)
1473 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1474 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1478 if (width) *width = -1;
1479 if (height) *height = -1;
1482 image_loader_free(il);
1486 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */