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-zxscr.h"
37 #include "ui-fileops.h"
40 #include "image-load-djvu.h"
42 #ifdef HAVE_FFMPEGTHUMBNAILER
43 #include "image-load-ffmpegthumbnailer.h"
46 #include "image-load-heif.h"
49 #include "image-load-j2k.h"
52 #include "image-load-cr3.h"
53 #include "image-load-jpeg.h"
56 #include "image-load-jpegxl.h"
59 #include "image-load-pdf.h"
62 #include "image-load-tiff.h"
68 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
69 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
71 /* image loader class */
75 SIGNAL_AREA_READY = 0,
83 static guint signals[SIGNAL_COUNT] = { 0 };
85 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
86 static void image_loader_class_init_wrapper(void *data, void *user_data);
87 static void image_loader_class_init(ImageLoaderClass *loader_class);
88 static void image_loader_finalize(GObject *object);
89 static void image_loader_stop(ImageLoader *il);
91 GType image_loader_get_type()
93 static GType type = 0;
96 static const GTypeInfo info = {
97 sizeof(ImageLoaderClass),
98 nullptr, /* base_init */
99 nullptr, /* base_finalize */
100 static_cast<GClassInitFunc>(image_loader_class_init_wrapper), /* class_init */
101 nullptr, /* class_finalize */
102 nullptr, /* class_data */
105 static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
106 nullptr /* value_table */
108 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
113 static void image_loader_init(GTypeInstance *instance, gpointer)
115 auto il = reinterpret_cast<ImageLoader *>(instance);
117 il->pixbuf = nullptr;
119 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
121 il->loader = nullptr;
126 il->idle_done_id = 0;
128 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
129 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
130 il->mapped_file = nullptr;
131 il->preview = IMAGE_LOADER_PREVIEW_NONE;
133 il->requested_width = 0;
134 il->requested_height = 0;
135 il->actual_width = 0;
136 il->actual_height = 0;
139 il->can_destroy = TRUE;
141 il->data_mutex = g_new(GMutex, 1);
142 g_mutex_init(il->data_mutex);
143 il->can_destroy_cond = g_new(GCond, 1);
144 g_cond_init(il->can_destroy_cond);
146 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
149 static void image_loader_class_init_wrapper(void *data, void *)
151 image_loader_class_init(static_cast<ImageLoaderClass *>(data));
154 static void image_loader_class_init(ImageLoaderClass *loader_class)
156 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
158 gobject_class->finalize = image_loader_finalize;
161 signals[SIGNAL_AREA_READY] =
162 g_signal_new("area_ready",
163 G_OBJECT_CLASS_TYPE(gobject_class),
165 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
167 gq_marshal_VOID__INT_INT_INT_INT,
174 signals[SIGNAL_ERROR] =
175 g_signal_new("error",
176 G_OBJECT_CLASS_TYPE(gobject_class),
178 G_STRUCT_OFFSET(ImageLoaderClass, error),
180 g_cclosure_marshal_VOID__VOID,
183 signals[SIGNAL_DONE] =
185 G_OBJECT_CLASS_TYPE(gobject_class),
187 G_STRUCT_OFFSET(ImageLoaderClass, done),
189 g_cclosure_marshal_VOID__VOID,
192 signals[SIGNAL_PERCENT] =
193 g_signal_new("percent",
194 G_OBJECT_CLASS_TYPE(gobject_class),
196 G_STRUCT_OFFSET(ImageLoaderClass, percent),
198 g_cclosure_marshal_VOID__DOUBLE,
202 signals[SIGNAL_SIZE] =
203 g_signal_new("size_prepared",
204 G_OBJECT_CLASS_TYPE(gobject_class),
206 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
208 gq_marshal_VOID__INT_INT,
215 static void image_loader_finalize(GObject *object)
217 auto il = reinterpret_cast<ImageLoader *>(object);
219 image_loader_stop(il);
221 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
223 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
225 if (il->idle_done_id)
227 g_source_remove(il->idle_done_id);
228 il->idle_done_id = 0;
231 while (g_source_remove_by_user_data(il))
233 DEBUG_2("pending signals detected");
236 while (il->area_param_list)
238 DEBUG_1("pending area_ready signals detected");
239 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
240 g_free(il->area_param_list->data);
241 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
244 while (il->area_param_delayed_list)
246 g_free(il->area_param_delayed_list->data);
247 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
250 if (il->pixbuf) g_object_unref(il->pixbuf);
252 if (il->error) g_error_free(il->error);
254 file_data_unref(il->fd);
256 g_mutex_clear(il->data_mutex);
257 g_free(il->data_mutex);
258 g_cond_clear(il->can_destroy_cond);
259 g_free(il->can_destroy_cond);
262 void image_loader_free(ImageLoader *il)
265 g_object_unref(G_OBJECT(il));
269 ImageLoader *image_loader_new(FileData *fd)
273 if (!fd) return nullptr;
275 il = static_cast<ImageLoader *>(g_object_new(TYPE_IMAGE_LOADER, nullptr));
277 il->fd = file_data_ref(fd);
282 /**************************************************************************************/
283 /* send signals via idle calbacks - the callback are executed in the main thread */
285 struct ImageLoaderAreaParam {
294 static gboolean image_loader_emit_area_ready_cb(gpointer data)
296 auto par = static_cast<ImageLoaderAreaParam *>(data);
297 ImageLoader *il = par->il;
299 g_mutex_lock(il->data_mutex);
300 il->area_param_list = g_list_remove(il->area_param_list, par);
306 g_mutex_unlock(il->data_mutex);
308 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
310 return G_SOURCE_REMOVE;
313 static gboolean image_loader_emit_done_cb(gpointer data)
315 auto il = static_cast<ImageLoader *>(data);
316 g_signal_emit(il, signals[SIGNAL_DONE], 0);
317 return G_SOURCE_REMOVE;
320 static gboolean image_loader_emit_error_cb(gpointer data)
322 auto il = static_cast<ImageLoader *>(data);
323 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
324 return G_SOURCE_REMOVE;
327 static gboolean image_loader_emit_percent_cb(gpointer data)
329 auto il = static_cast<ImageLoader *>(data);
330 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
331 return G_SOURCE_REMOVE;
334 static gboolean image_loader_emit_size_cb(gpointer data)
337 auto il = static_cast<ImageLoader *>(data);
338 g_mutex_lock(il->data_mutex);
339 width = il->actual_width;
340 height = il->actual_height;
341 g_mutex_unlock(il->data_mutex);
342 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
343 return G_SOURCE_REMOVE;
347 /* DONE and ERROR are emitted only once, thus they can have normal priority
348 PERCENT and AREA_READY should be processed ASAP
351 static void image_loader_emit_done(ImageLoader *il)
353 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, nullptr);
356 static void image_loader_emit_error(ImageLoader *il)
358 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, nullptr);
361 static void image_loader_emit_percent(ImageLoader *il)
363 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, nullptr);
366 static void image_loader_emit_size(ImageLoader *il)
368 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, nullptr);
371 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
375 auto prev_par = static_cast<ImageLoaderAreaParam *>((*list)->data);
376 if (prev_par->x == x && prev_par->w == w &&
377 prev_par->y + prev_par->h == y)
379 /* we can merge the notifications */
383 if (prev_par->x == x && prev_par->w == w &&
384 y + h == prev_par->y)
386 /* we can merge the notifications */
391 if (prev_par->y == y && prev_par->h == h &&
392 prev_par->x + prev_par->w == x)
394 /* we can merge the notifications */
398 if (prev_par->y == y && prev_par->h == h &&
399 x + w == prev_par->x)
401 /* we can merge the notifications */
408 auto par = g_new0(ImageLoaderAreaParam, 1);
415 *list = g_list_prepend(*list, par);
419 /* this function expects that il->data_mutex is locked by caller */
420 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
422 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
426 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, nullptr);
430 /**************************************************************************************/
431 /* the following functions may be executed in separate thread */
433 /* this function expects that il->data_mutex is locked by caller */
434 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
436 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
441 static gboolean image_loader_get_stopping(ImageLoader *il)
444 if (!il) return FALSE;
446 g_mutex_lock(il->data_mutex);
448 g_mutex_unlock(il->data_mutex);
454 static void image_loader_sync_pixbuf(ImageLoader *il)
458 g_mutex_lock(il->data_mutex);
462 g_mutex_unlock(il->data_mutex);
466 pb = il->backend.get_pixbuf(il->loader);
468 if (pb == il->pixbuf)
470 g_mutex_unlock(il->data_mutex);
474 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
476 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
479 if (il->pixbuf) g_object_unref(il->pixbuf);
482 if (il->pixbuf) g_object_ref(il->pixbuf);
484 g_mutex_unlock(il->data_mutex);
487 static void image_loader_area_updated_cb(gpointer,
488 guint x, guint y, guint w, guint h,
491 auto il = static_cast<ImageLoader *>(data);
493 if (!image_loader_get_pixbuf(il))
495 image_loader_sync_pixbuf(il);
496 if (!image_loader_get_pixbuf(il))
498 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
502 g_mutex_lock(il->data_mutex);
503 if (il->delay_area_ready)
504 image_loader_queue_delayed_area_ready(il, x, y, w, h);
506 image_loader_emit_area_ready(il, x, y, w, h);
508 if (il->stopping) il->backend.abort(il->loader);
510 g_mutex_unlock(il->data_mutex);
513 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
515 auto il = static_cast<ImageLoader *>(data);
521 https://bugzilla.gnome.org/show_bug.cgi?id=547669
522 https://bugzilla.gnome.org/show_bug.cgi?id=589334
524 gchar *format = il->backend.get_format_name(loader);
525 if (strcmp(format, "svg") == 0 ||
526 strcmp(format, "xpm") == 0)
534 pb = il->backend.get_pixbuf(loader);
536 h = gdk_pixbuf_get_height(pb);
537 rs = gdk_pixbuf_get_rowstride(pb);
538 pix = gdk_pixbuf_get_pixels(pb);
540 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
544 static void image_loader_size_cb(gpointer loader,
545 gint width, gint height, gpointer data)
547 auto il = static_cast<ImageLoader *>(data);
549 gboolean scale = FALSE;
552 g_mutex_lock(il->data_mutex);
553 il->actual_width = width;
554 il->actual_height = height;
555 if (il->requested_width < 1 || il->requested_height < 1)
557 g_mutex_unlock(il->data_mutex);
558 image_loader_emit_size(il);
561 g_mutex_unlock(il->data_mutex);
563 #ifdef HAVE_FFMPEGTHUMBNAILER
564 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
567 mime_types = il->backend.get_format_mime_types(loader);
569 while (mime_types[n] && !scale)
571 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
574 g_strfreev(mime_types);
578 image_loader_emit_size(il);
582 g_mutex_lock(il->data_mutex);
585 if (width > il->requested_width || height > il->requested_height)
588 if ((static_cast<gdouble>(il->requested_width) / width) < (static_cast<gdouble>(il->requested_height) / height))
590 nw = il->requested_width;
591 nh = static_cast<gdouble>(nw) / width * height;
596 nh = il->requested_height;
597 nw = static_cast<gdouble>(nh) / height * width;
601 il->actual_width = nw;
602 il->actual_height = nh;
603 il->backend.set_size(loader, nw, nh);
607 g_mutex_unlock(il->data_mutex);
608 image_loader_emit_size(il);
611 static void image_loader_stop_loader(ImageLoader *il)
617 /* some loaders do not have a pixbuf till close, order is important here */
618 il->backend.close(il->loader, il->error ? nullptr : &il->error); /* we are interested in the first error only */
619 image_loader_sync_pixbuf(il);
620 il->backend.free(il->loader);
621 il->loader = nullptr;
623 g_mutex_lock(il->data_mutex);
625 g_mutex_unlock(il->data_mutex);
628 static void image_loader_setup_loader(ImageLoader *il)
630 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
634 gint external_preview = 1;
636 g_mutex_lock(il->data_mutex);
638 if (options->external_preview.enable)
641 gchar *tilde_filename;
643 tilde_filename = expand_tilde(options->external_preview.select);
645 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
647 external_preview = runcmd(cmd_line);
649 g_free(tilde_filename);
652 if (external_preview == 0)
654 DEBUG_1("Using custom external loader");
655 image_loader_backend_set_external(&il->backend);
659 #ifdef HAVE_FFMPEGTHUMBNAILER
660 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
662 DEBUG_1("Using custom ffmpegthumbnailer loader");
663 image_loader_backend_set_ft(&il->backend);
668 if (il->bytes_total >= 4 &&
669 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
671 DEBUG_1("Using custom pdf loader");
672 image_loader_backend_set_pdf(&il->backend);
677 if (il->bytes_total >= 12 &&
678 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
679 (memcmp(il->mapped_file + 4, "ftypheix", 8) == 0) ||
680 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
681 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
682 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
684 DEBUG_1("Using custom heif loader");
685 image_loader_backend_set_heif(&il->backend);
690 if (il->bytes_total >= 16 &&
691 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
692 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
694 DEBUG_1("Using custom djvu loader");
695 image_loader_backend_set_djvu(&il->backend);
700 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
702 DEBUG_1("Using custom jpeg loader");
703 image_loader_backend_set_jpeg(&il->backend);
707 if (il->bytes_total >= 11 &&
708 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
709 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
711 DEBUG_1("Using custom cr3 loader");
712 image_loader_backend_set_cr3(&il->backend);
718 if (il->bytes_total >= 10 &&
719 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
720 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
721 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
722 memcmp(il->mapped_file, "II*\0", 4) == 0))
724 DEBUG_1("Using custom tiff loader");
725 image_loader_backend_set_tiff(&il->backend);
729 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
731 DEBUG_1("Using dds loader");
732 image_loader_backend_set_dds(&il->backend);
735 if (il->bytes_total >= 6 &&
736 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
738 DEBUG_1("Using custom psd loader");
739 image_loader_backend_set_psd(&il->backend);
743 if (il->bytes_total >= 12 &&
744 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
746 DEBUG_1("Using custom j2k loader");
747 image_loader_backend_set_j2k(&il->backend);
752 if (il->bytes_total >= 12 &&
753 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
755 DEBUG_1("Using custom jpeg xl loader");
756 image_loader_backend_set_jpegxl(&il->backend);
759 if (il->bytes_total >= 2 &&
760 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
762 DEBUG_1("Using custom jpeg xl loader");
763 image_loader_backend_set_jpegxl(&il->backend);
767 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
768 (file_extension_match(il->fd->path, ".scr")))
770 DEBUG_1("Using custom zxscr loader");
771 image_loader_backend_set_zxscr(&il->backend);
774 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
776 DEBUG_1("Using custom collection loader");
777 image_loader_backend_set_collection(&il->backend);
780 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
782 DEBUG_1("Using custom svgz loader");
783 image_loader_backend_set_svgz(&il->backend);
786 image_loader_backend_set_default(&il->backend);
788 il->loader = static_cast<void **>(il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il));
791 format = il->backend.get_format_name(il->loader);
792 if (g_strcmp0(format, "tiff") == 0)
794 il->backend.set_page_num(il->loader, il->fd->page_num);
800 format = il->backend.get_format_name(il->loader);
801 if (g_strcmp0(format, "pdf") == 0)
803 il->backend.set_page_num(il->loader, il->fd->page_num);
809 format = il->backend.get_format_name(il->loader);
810 if (g_strcmp0(format, "heif") == 0)
812 il->backend.set_page_num(il->loader, il->fd->page_num);
818 format = il->backend.get_format_name(il->loader);
819 if (g_strcmp0(format, "djvu") == 0)
821 il->backend.set_page_num(il->loader, il->fd->page_num);
826 il->fd->format_name = il->backend.get_format_name(il->loader);
828 g_mutex_unlock(il->data_mutex);
832 static void image_loader_done(ImageLoader *il)
834 image_loader_stop_loader(il);
836 image_loader_emit_done(il);
839 static void image_loader_error(ImageLoader *il)
841 image_loader_stop_loader(il);
843 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
845 image_loader_emit_error(il);
848 static gboolean image_loader_continue(ImageLoader *il)
853 if (!il) return G_SOURCE_REMOVE;
855 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
856 while (c > 0 && !image_loader_get_stopping(il))
858 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
862 image_loader_done(il);
863 return G_SOURCE_REMOVE;
866 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
868 image_loader_error(il);
869 return G_SOURCE_REMOVE;
877 if (il->bytes_total > 0)
879 image_loader_emit_percent(il);
882 return G_SOURCE_CONTINUE;
885 static gboolean image_loader_begin(ImageLoader *il)
887 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
892 if (il->pixbuf) return FALSE;
894 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
895 if (b < 1) return FALSE;
897 image_loader_setup_loader(il);
899 g_assert(il->bytes_read == 0);
900 if (il->backend.load) {
902 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
904 image_loader_stop_loader(il);
908 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
910 image_loader_stop_loader(il);
915 format = il->backend.get_format_name(il->loader);
916 if (g_strcmp0(format, "pdf") == 0)
918 gint i = il->backend.get_page_total(il->loader);
919 file_data_set_page_total(il->fd, i);
924 format = il->backend.get_format_name(il->loader);
925 if (g_strcmp0(format, "heif") == 0)
927 gint i = il->backend.get_page_total(il->loader);
928 file_data_set_page_total(il->fd, i);
933 format = il->backend.get_format_name(il->loader);
934 if (g_strcmp0(format, "djvu") == 0)
936 gint i = il->backend.get_page_total(il->loader);
937 file_data_set_page_total(il->fd, i);
942 format = il->backend.get_format_name(il->loader);
943 if (g_strcmp0(format, "tiff") == 0)
945 gint i = il->backend.get_page_total(il->loader);
946 file_data_set_page_total(il->fd, i);
953 /* read until size is known */
954 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
956 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
957 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
959 image_loader_stop_loader(il);
964 if (!il->pixbuf) image_loader_sync_pixbuf(il);
966 if (il->bytes_read == il->bytes_total || b < 1)
968 /* done, handle (broken) loaders that do not have pixbuf till close */
969 image_loader_stop_loader(il);
971 if (!il->pixbuf) return FALSE;
973 image_loader_done(il);
979 image_loader_stop_loader(il);
986 /**************************************************************************************/
987 /* the following functions are always executed in the main thread */
990 static gboolean image_loader_setup_source(ImageLoader *il)
995 if (!il || il->loader || il->mapped_file) return FALSE;
997 il->mapped_file = nullptr;
1001 ExifData *exif = exif_read_fd(il->fd);
1003 if (options->thumbnails.use_exif)
1005 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), il->requested_width, il->requested_height);
1007 if (il->mapped_file)
1009 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1014 il->mapped_file = libraw_get_preview(il, reinterpret_cast<guint *>(&il->bytes_total));
1016 if (il->mapped_file)
1018 /* Both exiv2 and libraw sometimes return a pointer to a file
1019 * section that is not a jpeg */
1020 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1022 il->mapped_file = nullptr;
1026 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1031 /* If libraw does not find a thumbnail, try exiv2 */
1032 if (!il->mapped_file)
1034 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*/
1036 if (il->mapped_file)
1038 /* Both exiv2 and libraw sometimes return a pointer to a file
1039 * section that is not a jpeg */
1040 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1042 il->mapped_file = nullptr;
1046 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1051 if (il->mapped_file)
1053 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1055 exif_free_fd(il->fd, exif);
1059 if (!il->mapped_file)
1064 pathl = path_from_utf8(il->fd->path);
1065 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1067 if (load_fd == -1) return FALSE;
1069 if (fstat(load_fd, &st) == 0)
1071 il->bytes_total = st.st_size;
1079 il->mapped_file = static_cast<guchar *>(mmap(nullptr, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0));
1081 if (il->mapped_file == MAP_FAILED)
1083 il->mapped_file = nullptr;
1086 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1092 static void image_loader_stop_source(ImageLoader *il)
1096 if (il->mapped_file)
1098 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1100 exif_free_preview(il->mapped_file);
1102 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1104 libraw_free_preview(il->mapped_file);
1108 munmap(il->mapped_file, il->bytes_total);
1110 il->mapped_file = nullptr;
1114 static void image_loader_stop(ImageLoader *il)
1120 g_source_remove(il->idle_id);
1126 /* stop loader in the other thread */
1127 g_mutex_lock(il->data_mutex);
1128 il->stopping = TRUE;
1129 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1130 g_mutex_unlock(il->data_mutex);
1133 image_loader_stop_loader(il);
1134 image_loader_stop_source(il);
1138 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1140 g_mutex_lock(il->data_mutex);
1141 il->delay_area_ready = enable;
1146 list = g_list_reverse(il->area_param_delayed_list);
1147 il->area_param_delayed_list = nullptr;
1148 g_mutex_unlock(il->data_mutex);
1154 auto par = static_cast<ImageLoaderAreaParam *>(work->data);
1157 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1159 g_list_free_full(list, g_free);
1164 g_mutex_unlock(il->data_mutex);
1169 /**************************************************************************************/
1170 /* execution via idle calls */
1172 static gboolean image_loader_idle_cb(gpointer data)
1174 gboolean ret = G_SOURCE_REMOVE;
1175 auto il = static_cast<ImageLoader *>(data);
1179 ret = image_loader_continue(il);
1184 image_loader_stop_source(il);
1190 static gboolean image_loader_start_idle(ImageLoader *il)
1194 if (!il) return FALSE;
1196 if (!il->fd) return FALSE;
1198 if (!image_loader_setup_source(il)) return FALSE;
1200 ret = image_loader_begin(il);
1202 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, nullptr);
1206 /**************************************************************************************/
1207 /* execution via thread */
1209 static GThreadPool *image_loader_thread_pool = nullptr;
1211 static GCond *image_loader_prio_cond = nullptr;
1212 static GMutex *image_loader_prio_mutex = nullptr;
1213 static gint image_loader_prio_num = 0;
1216 static void image_loader_thread_enter_high()
1218 g_mutex_lock(image_loader_prio_mutex);
1219 image_loader_prio_num++;
1220 g_mutex_unlock(image_loader_prio_mutex);
1223 static void image_loader_thread_leave_high()
1225 g_mutex_lock(image_loader_prio_mutex);
1226 image_loader_prio_num--;
1227 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1228 g_mutex_unlock(image_loader_prio_mutex);
1231 static void image_loader_thread_wait_high()
1233 g_mutex_lock(image_loader_prio_mutex);
1234 while (image_loader_prio_num)
1236 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1239 g_mutex_unlock(image_loader_prio_mutex);
1243 static void image_loader_thread_run(gpointer data, gpointer)
1245 auto il = static_cast<ImageLoader *>(data);
1249 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1251 /* low prio, wait until high prio tasks finishes */
1252 image_loader_thread_wait_high();
1257 image_loader_thread_enter_high();
1260 err = !image_loader_begin(il);
1265 loader failed, we have to send signal
1266 (idle mode returns the image_loader_begin return value directly)
1267 (success is always reported indirectly from image_loader_begin)
1269 image_loader_emit_error(il);
1274 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1276 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1278 /* low prio, wait until high prio tasks finishes */
1279 image_loader_thread_wait_high();
1281 cont = image_loader_continue(il);
1283 image_loader_stop_loader(il);
1285 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1288 image_loader_thread_leave_high();
1291 g_mutex_lock(il->data_mutex);
1292 il->can_destroy = TRUE;
1293 g_cond_signal(il->can_destroy_cond);
1294 g_mutex_unlock(il->data_mutex);
1299 static gboolean image_loader_start_thread(ImageLoader *il)
1301 if (!il) return FALSE;
1303 if (!il->fd) return FALSE;
1307 if (!image_loader_setup_source(il)) return FALSE;
1309 if (!image_loader_thread_pool)
1311 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, nullptr, -1, FALSE, nullptr);
1312 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1313 g_cond_init(image_loader_prio_cond);
1314 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1315 g_mutex_init(image_loader_prio_mutex);
1318 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1320 g_thread_pool_push(image_loader_thread_pool, il, nullptr);
1321 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1327 /**************************************************************************************/
1328 /* public interface */
1331 gboolean image_loader_start(ImageLoader *il)
1333 if (!il) return FALSE;
1335 if (!il->fd) return FALSE;
1337 return image_loader_start_thread(il);
1341 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1342 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1345 if (!il) return nullptr;
1347 g_mutex_lock(il->data_mutex);
1349 g_mutex_unlock(il->data_mutex);
1353 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1357 g_mutex_lock(il->data_mutex);
1358 il->requested_width = width;
1359 il->requested_height = height;
1360 g_mutex_unlock(il->data_mutex);
1363 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1367 g_mutex_lock(il->data_mutex);
1368 il->idle_read_loop_count = count ? count : 1;
1369 g_mutex_unlock(il->data_mutex);
1372 void image_loader_set_priority(ImageLoader *il, gint priority)
1376 if (il->thread) return; /* can't change prio if the thread already runs */
1377 il->idle_priority = priority;
1381 gdouble image_loader_get_percent(ImageLoader *il)
1384 if (!il) return 0.0;
1386 g_mutex_lock(il->data_mutex);
1387 if (il->bytes_total == 0)
1393 ret = static_cast<gdouble>(il->bytes_read) / il->bytes_total;
1395 g_mutex_unlock(il->data_mutex);
1399 gboolean image_loader_get_is_done(ImageLoader *il)
1402 if (!il) return FALSE;
1404 g_mutex_lock(il->data_mutex);
1406 g_mutex_unlock(il->data_mutex);
1411 FileData *image_loader_get_fd(ImageLoader *il)
1414 if (!il) return nullptr;
1416 g_mutex_lock(il->data_mutex);
1418 g_mutex_unlock(il->data_mutex);
1423 gboolean image_loader_get_shrunk(ImageLoader *il)
1426 if (!il) return FALSE;
1428 g_mutex_lock(il->data_mutex);
1430 g_mutex_unlock(il->data_mutex);
1434 const gchar *image_loader_get_error(ImageLoader *il)
1436 const gchar *ret = nullptr;
1437 if (!il) return nullptr;
1438 g_mutex_lock(il->data_mutex);
1439 if (il->error) ret = il->error->message;
1440 g_mutex_unlock(il->data_mutex);
1446 * @FIXME this can be rather slow and blocks until the size is known
1448 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1453 il = image_loader_new(fd);
1455 success = image_loader_start_idle(il);
1457 if (success && il->pixbuf)
1459 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1460 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1464 if (width) *width = -1;
1465 if (height) *height = -1;
1468 image_loader_free(il);
1472 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */