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.
22 #include "image-load.h"
36 #include "gq-marshal.h"
37 #include "image-load-collection.h"
38 #include "image-load-dds.h"
40 # include "image-load-djvu.h"
42 #include "image-load-external.h"
43 #if HAVE_FFMPEGTHUMBNAILER
44 # include "image-load-ffmpegthumbnailer.h"
46 #include "image-load-gdk.h"
48 # include "image-load-heif.h"
51 # include "image-load-j2k.h"
55 # include "image-load-cr3.h"
57 # include "image-load-jpeg.h"
60 # include "image-load-jpegxl.h"
62 #include "image-load-libraw.h"
64 # include "image-load-pdf.h"
66 #include "image-load-psd.h"
67 #include "image-load-svgz.h"
69 # include "image-load-tiff.h"
71 #include "image-load-webp.h"
72 #include "image-load-zxscr.h"
76 #include "ui-fileops.h"
81 IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT = 4096,
82 IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT = 1
85 /* image loader class */
89 SIGNAL_AREA_READY = 0,
97 static guint signals[SIGNAL_COUNT] = { 0 };
99 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
100 static void image_loader_class_init_wrapper(void *data, void *user_data);
101 static void image_loader_class_init(ImageLoaderClass *loader_class);
102 static void image_loader_finalize(GObject *object);
103 static void image_loader_stop(ImageLoader *il);
105 GType image_loader_get_type()
107 static GType type = 0;
110 static const GTypeInfo info = {
111 sizeof(ImageLoaderClass),
112 nullptr, /* base_init */
113 nullptr, /* base_finalize */
114 static_cast<GClassInitFunc>(image_loader_class_init_wrapper), /* class_init */
115 nullptr, /* class_finalize */
116 nullptr, /* class_data */
119 static_cast<GInstanceInitFunc>(image_loader_init), /* instance_init */
120 nullptr /* value_table */
122 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, static_cast<GTypeFlags>(0));
127 static void image_loader_init(GTypeInstance *instance, gpointer)
129 auto il = reinterpret_cast<ImageLoader *>(instance);
131 il->pixbuf = nullptr;
133 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
135 il->backend.reset(nullptr);
140 il->idle_done_id = 0;
142 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
143 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
144 il->mapped_file = nullptr;
145 il->preview = IMAGE_LOADER_PREVIEW_NONE;
147 il->requested_width = 0;
148 il->requested_height = 0;
149 il->actual_width = 0;
150 il->actual_height = 0;
153 il->can_destroy = TRUE;
155 il->data_mutex = g_new(GMutex, 1);
156 g_mutex_init(il->data_mutex);
157 il->can_destroy_cond = g_new(GCond, 1);
158 g_cond_init(il->can_destroy_cond);
160 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
163 static void image_loader_class_init_wrapper(void *data, void *)
165 image_loader_class_init(static_cast<ImageLoaderClass *>(data));
168 static void image_loader_class_init(ImageLoaderClass *loader_class)
170 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
172 gobject_class->finalize = image_loader_finalize;
175 signals[SIGNAL_AREA_READY] =
176 g_signal_new("area_ready",
177 G_OBJECT_CLASS_TYPE(gobject_class),
179 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
181 gq_marshal_VOID__INT_INT_INT_INT,
188 signals[SIGNAL_ERROR] =
189 g_signal_new("error",
190 G_OBJECT_CLASS_TYPE(gobject_class),
192 G_STRUCT_OFFSET(ImageLoaderClass, error),
194 g_cclosure_marshal_VOID__VOID,
197 signals[SIGNAL_DONE] =
199 G_OBJECT_CLASS_TYPE(gobject_class),
201 G_STRUCT_OFFSET(ImageLoaderClass, done),
203 g_cclosure_marshal_VOID__VOID,
206 signals[SIGNAL_PERCENT] =
207 g_signal_new("percent",
208 G_OBJECT_CLASS_TYPE(gobject_class),
210 G_STRUCT_OFFSET(ImageLoaderClass, percent),
212 g_cclosure_marshal_VOID__DOUBLE,
216 signals[SIGNAL_SIZE] =
217 g_signal_new("size_prepared",
218 G_OBJECT_CLASS_TYPE(gobject_class),
220 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
222 gq_marshal_VOID__INT_INT,
229 static void image_loader_finalize(GObject *object)
231 auto il = reinterpret_cast<ImageLoader *>(object);
233 image_loader_stop(il);
235 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
237 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
239 if (il->idle_done_id)
241 g_source_remove(il->idle_done_id);
242 il->idle_done_id = 0;
245 while (g_source_remove_by_user_data(il))
247 DEBUG_2("pending signals detected");
250 while (il->area_param_list)
252 DEBUG_1("pending area_ready signals detected");
253 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
254 g_free(il->area_param_list->data);
255 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
258 while (il->area_param_delayed_list)
260 g_free(il->area_param_delayed_list->data);
261 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
264 if (il->pixbuf) g_object_unref(il->pixbuf);
266 if (il->error) g_error_free(il->error);
268 file_data_unref(il->fd);
270 g_mutex_clear(il->data_mutex);
271 g_free(il->data_mutex);
272 g_cond_clear(il->can_destroy_cond);
273 g_free(il->can_destroy_cond);
276 void image_loader_free(ImageLoader *il)
279 g_object_unref(G_OBJECT(il));
283 ImageLoader *image_loader_new(FileData *fd)
287 if (!fd) return nullptr;
289 il = static_cast<ImageLoader *>(g_object_new(TYPE_IMAGE_LOADER, nullptr));
291 il->fd = file_data_ref(fd);
296 /**************************************************************************************/
297 /* send signals via idle calbacks - the callback are executed in the main thread */
299 struct ImageLoaderAreaParam {
308 static gboolean image_loader_emit_area_ready_cb(gpointer data)
310 auto par = static_cast<ImageLoaderAreaParam *>(data);
311 ImageLoader *il = par->il;
316 g_mutex_lock(il->data_mutex);
317 il->area_param_list = g_list_remove(il->area_param_list, par);
323 g_mutex_unlock(il->data_mutex);
325 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
327 return G_SOURCE_REMOVE;
330 static gboolean image_loader_emit_done_cb(gpointer data)
332 auto il = static_cast<ImageLoader *>(data);
333 g_signal_emit(il, signals[SIGNAL_DONE], 0);
334 return G_SOURCE_REMOVE;
337 static gboolean image_loader_emit_error_cb(gpointer data)
339 auto il = static_cast<ImageLoader *>(data);
340 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
341 return G_SOURCE_REMOVE;
344 static gboolean image_loader_emit_percent_cb(gpointer data)
346 auto il = static_cast<ImageLoader *>(data);
347 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
348 return G_SOURCE_REMOVE;
351 static gboolean image_loader_emit_size_cb(gpointer data)
355 auto il = static_cast<ImageLoader *>(data);
356 g_mutex_lock(il->data_mutex);
357 width = il->actual_width;
358 height = il->actual_height;
359 g_mutex_unlock(il->data_mutex);
360 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
361 return G_SOURCE_REMOVE;
365 /* DONE and ERROR are emitted only once, thus they can have normal priority
366 PERCENT and AREA_READY should be processed ASAP
369 static void image_loader_emit_done(ImageLoader *il)
371 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, nullptr);
374 static void image_loader_emit_error(ImageLoader *il)
376 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, nullptr);
379 static void image_loader_emit_percent(ImageLoader *il)
381 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, nullptr);
384 static void image_loader_emit_size(ImageLoader *il)
386 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, nullptr);
389 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
393 auto prev_par = static_cast<ImageLoaderAreaParam *>((*list)->data);
394 if (prev_par->x == x && prev_par->w == w &&
395 prev_par->y + prev_par->h == y)
397 /* we can merge the notifications */
401 if (prev_par->x == x && prev_par->w == w &&
402 y + h == prev_par->y)
404 /* we can merge the notifications */
409 if (prev_par->y == y && prev_par->h == h &&
410 prev_par->x + prev_par->w == x)
412 /* we can merge the notifications */
416 if (prev_par->y == y && prev_par->h == h &&
417 x + w == prev_par->x)
419 /* we can merge the notifications */
426 auto par = g_new0(ImageLoaderAreaParam, 1);
433 *list = g_list_prepend(*list, par);
437 /* this function expects that il->data_mutex is locked by caller */
438 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
440 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
444 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, nullptr);
448 /**************************************************************************************/
449 /* the following functions may be executed in separate thread */
451 /* this function expects that il->data_mutex is locked by caller */
452 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
454 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
459 static gboolean image_loader_get_stopping(ImageLoader *il)
462 if (!il) return FALSE;
464 g_mutex_lock(il->data_mutex);
466 g_mutex_unlock(il->data_mutex);
472 static void image_loader_sync_pixbuf(ImageLoader *il)
476 g_mutex_lock(il->data_mutex);
480 g_mutex_unlock(il->data_mutex);
484 pb = il->backend->get_pixbuf();
486 if (pb == il->pixbuf)
488 g_mutex_unlock(il->data_mutex);
492 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
494 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
497 if (il->pixbuf) g_object_unref(il->pixbuf);
500 if (il->pixbuf) g_object_ref(il->pixbuf);
502 g_mutex_unlock(il->data_mutex);
505 static void image_loader_area_updated_cb(gpointer,
506 guint x, guint y, guint w, guint h,
509 auto il = static_cast<ImageLoader *>(data);
511 if (!image_loader_get_pixbuf(il))
513 image_loader_sync_pixbuf(il);
514 if (!image_loader_get_pixbuf(il))
516 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
520 g_mutex_lock(il->data_mutex);
521 if (il->delay_area_ready)
522 image_loader_queue_delayed_area_ready(il, x, y, w, h);
524 image_loader_emit_area_ready(il, x, y, w, h);
526 if (il->stopping) il->backend->abort();
528 g_mutex_unlock(il->data_mutex);
531 static void image_loader_area_prepared_cb(gpointer, gpointer data)
533 auto il = static_cast<ImageLoader *>(data);
540 https://bugzilla.gnome.org/show_bug.cgi?id=547669
541 https://bugzilla.gnome.org/show_bug.cgi?id=589334
543 gchar *format = il->backend->get_format_name();
544 if (strcmp(format, "svg") == 0 ||
545 strcmp(format, "xpm") == 0)
553 pb = il->backend->get_pixbuf();
555 h = gdk_pixbuf_get_height(pb);
556 rs = gdk_pixbuf_get_rowstride(pb);
557 pix = gdk_pixbuf_get_pixels(pb);
559 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
563 static void image_loader_size_cb(gpointer,
564 gint width, gint height, gpointer data)
566 auto il = static_cast<ImageLoader *>(data);
568 gboolean scale = FALSE;
571 g_mutex_lock(il->data_mutex);
572 il->actual_width = width;
573 il->actual_height = height;
574 if (il->requested_width < 1 || il->requested_height < 1)
576 g_mutex_unlock(il->data_mutex);
577 image_loader_emit_size(il);
580 g_mutex_unlock(il->data_mutex);
582 #if HAVE_FFMPEGTHUMBNAILER
583 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
586 mime_types = il->backend->get_format_mime_types();
588 while (mime_types[n] && !scale)
590 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
593 g_strfreev(mime_types);
597 image_loader_emit_size(il);
601 g_mutex_lock(il->data_mutex);
605 if (width > il->requested_width || height > il->requested_height)
608 if ((static_cast<gdouble>(il->requested_width) / width) < (static_cast<gdouble>(il->requested_height) / height))
610 nw = il->requested_width;
611 nh = static_cast<gdouble>(nw) / width * height;
616 nh = il->requested_height;
617 nw = static_cast<gdouble>(nh) / height * width;
621 il->actual_width = nw;
622 il->actual_height = nh;
623 il->backend->set_size(nw, nh);
627 g_mutex_unlock(il->data_mutex);
628 image_loader_emit_size(il);
631 static void image_loader_stop_loader(ImageLoader *il)
637 /* some loaders do not have a pixbuf till close, order is important here */
638 il->backend->close(il->error ? nullptr : &il->error); /* we are interested in the first error only */
639 image_loader_sync_pixbuf(il);
640 il->backend.reset(nullptr);
642 g_mutex_lock(il->data_mutex);
644 g_mutex_unlock(il->data_mutex);
647 static void image_loader_setup_loader(ImageLoader *il)
649 #if HAVE_TIFF || HAVE_PDF || HAVE_HEIF || HAVE_DJVU
653 gint external_preview = 1;
655 g_mutex_lock(il->data_mutex);
657 if (options->external_preview.enable)
660 gchar *tilde_filename;
662 tilde_filename = expand_tilde(options->external_preview.select);
664 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
666 external_preview = runcmd(cmd_line);
668 g_free(tilde_filename);
671 if (external_preview == 0)
673 DEBUG_1("Using custom external loader");
674 il->backend = get_image_loader_backend_external();
678 #if HAVE_FFMPEGTHUMBNAILER
679 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
681 DEBUG_1("Using custom ffmpegthumbnailer loader");
682 il->backend = get_image_loader_backend_ft();
687 if (il->bytes_total >= 4 &&
688 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
690 DEBUG_1("Using custom pdf loader");
691 il->backend = get_image_loader_backend_pdf();
696 if (il->bytes_total >= 12 &&
697 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
698 (memcmp(il->mapped_file + 4, "ftypheix", 8) == 0) ||
699 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
700 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
701 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
703 DEBUG_1("Using custom heif loader");
704 il->backend = get_image_loader_backend_heif();
709 if (il->bytes_total >= 12 &&
710 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
711 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
713 DEBUG_1("Using custom webp loader");
714 il->backend = get_image_loader_backend_webp();
719 if (il->bytes_total >= 16 &&
720 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
721 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
723 DEBUG_1("Using custom djvu loader");
724 il->backend = get_image_loader_backend_djvu();
729 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
731 DEBUG_1("Using custom jpeg loader");
732 il->backend = get_image_loader_backend_jpeg();
736 if (il->bytes_total >= 11 &&
737 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
738 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
740 DEBUG_1("Using custom cr3 loader");
741 il->backend = get_image_loader_backend_cr3();
747 if (il->bytes_total >= 10 &&
748 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
749 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
750 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
751 memcmp(il->mapped_file, "II*\0", 4) == 0))
753 DEBUG_1("Using custom tiff loader");
754 il->backend = get_image_loader_backend_tiff();
758 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
760 DEBUG_1("Using dds loader");
761 il->backend = get_image_loader_backend_dds();
764 if (il->bytes_total >= 6 &&
765 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
767 DEBUG_1("Using custom psd loader");
768 il->backend = get_image_loader_backend_psd();
772 if (il->bytes_total >= 12 &&
773 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
775 DEBUG_1("Using custom j2k loader");
776 il->backend = get_image_loader_backend_j2k();
781 if ((il->bytes_total >= 12 &&
782 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0)) ||
783 (il->bytes_total >= 2 &&
784 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0)))
786 DEBUG_1("Using custom jpeg xl loader");
787 il->backend = get_image_loader_backend_jpegxl();
791 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
792 (file_extension_match(il->fd->path, ".scr")))
794 DEBUG_1("Using custom zxscr loader");
795 il->backend = get_image_loader_backend_zxscr();
798 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
800 DEBUG_1("Using custom collection loader");
801 il->backend = get_image_loader_backend_collection();
804 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
806 DEBUG_1("Using custom svgz loader");
807 il->backend = get_image_loader_backend_svgz();
810 il->backend = get_image_loader_backend_default();
813 il->backend->init(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
816 format = il->backend->get_format_name();
817 if (g_strcmp0(format, "tiff") == 0)
819 il->backend->set_page_num(il->fd->page_num);
825 format = il->backend->get_format_name();
826 if (g_strcmp0(format, "pdf") == 0)
828 il->backend->set_page_num(il->fd->page_num);
834 format = il->backend->get_format_name();
835 if (g_strcmp0(format, "heif") == 0)
837 il->backend->set_page_num(il->fd->page_num);
843 format = il->backend->get_format_name();
844 if (g_strcmp0(format, "djvu") == 0)
846 il->backend->set_page_num(il->fd->page_num);
851 il->fd->format_name = il->backend->get_format_name();
853 g_mutex_unlock(il->data_mutex);
857 static void image_loader_done(ImageLoader *il)
859 image_loader_stop_loader(il);
861 image_loader_emit_done(il);
864 static void image_loader_error(ImageLoader *il)
866 image_loader_stop_loader(il);
868 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
870 image_loader_emit_error(il);
873 static gboolean image_loader_continue(ImageLoader *il)
877 if (!il) return G_SOURCE_REMOVE;
879 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
880 while (c > 0 && !image_loader_get_stopping(il))
882 if (il->bytes_total == il->bytes_read)
884 image_loader_done(il);
885 return G_SOURCE_REMOVE;
888 if (il->bytes_total < il->bytes_read)
890 image_loader_error(il);
891 return G_SOURCE_REMOVE;
894 gsize b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
896 if (!il->backend->write(il->mapped_file + il->bytes_read, b, il->bytes_total, &il->error))
898 image_loader_error(il);
899 return G_SOURCE_REMOVE;
907 if (il->bytes_total > 0)
909 image_loader_emit_percent(il);
912 return G_SOURCE_CONTINUE;
915 static gboolean image_loader_begin(ImageLoader *il)
917 #if HAVE_TIFF || HAVE_PDF || HAVE_HEIF || HAVE_DJVU
921 if (il->pixbuf) return FALSE;
923 if (il->bytes_total <= il->bytes_read) return FALSE;
925 gsize b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
927 image_loader_setup_loader(il);
929 g_assert(il->bytes_read == 0);
930 if (!il->backend->write(il->mapped_file, b, il->bytes_total, &il->error))
932 image_loader_stop_loader(il);
937 format = il->backend->get_format_name();
938 if (g_strcmp0(format, "pdf") == 0)
940 gint i = il->backend->get_page_total();
941 file_data_set_page_total(il->fd, i);
946 format = il->backend->get_format_name();
947 if (g_strcmp0(format, "heif") == 0)
949 gint i = il->backend->get_page_total();
950 file_data_set_page_total(il->fd, i);
955 format = il->backend->get_format_name();
956 if (g_strcmp0(format, "djvu") == 0)
958 gint i = il->backend->get_page_total();
959 file_data_set_page_total(il->fd, i);
964 format = il->backend->get_format_name();
965 if (g_strcmp0(format, "tiff") == 0)
967 gint i = il->backend->get_page_total();
968 file_data_set_page_total(il->fd, i);
975 /* read until size is known */
976 while (il->backend && !il->backend->get_pixbuf() && b > 0 && !image_loader_get_stopping(il))
978 if (il->bytes_total < il->bytes_read)
980 image_loader_stop_loader(il);
984 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
985 if (b > 0 && !il->backend->write(il->mapped_file + il->bytes_read, b, il->bytes_total, &il->error))
987 image_loader_stop_loader(il);
992 if (!il->pixbuf) image_loader_sync_pixbuf(il);
994 if (il->bytes_read == il->bytes_total || b < 1)
996 /* done, handle (broken) loaders that do not have pixbuf till close */
997 image_loader_stop_loader(il);
999 if (!il->pixbuf) return FALSE;
1001 image_loader_done(il);
1007 image_loader_stop_loader(il);
1014 /**************************************************************************************/
1015 /* the following functions are always executed in the main thread */
1018 static gboolean image_loader_setup_source(ImageLoader *il)
1023 if (!il || il->backend || il->mapped_file) return FALSE;
1025 il->mapped_file = nullptr;
1029 ExifData *exif = exif_read_fd(il->fd);
1031 if (options->thumbnails.use_exif)
1033 il->mapped_file = exif_get_preview(exif, reinterpret_cast<guint *>(&il->bytes_total), il->requested_width, il->requested_height);
1035 if (il->mapped_file)
1037 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1042 il->mapped_file = libraw_get_preview(il, reinterpret_cast<guint *>(&il->bytes_total));
1044 if (il->mapped_file)
1046 /* Both exiv2 and libraw sometimes return a pointer to a file
1047 * section that is not a jpeg */
1048 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1050 il->mapped_file = nullptr;
1054 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1059 /* If libraw does not find a thumbnail, try exiv2 */
1060 if (!il->mapped_file)
1062 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*/
1064 if (il->mapped_file)
1066 /* Both exiv2 and libraw sometimes return a pointer to a file
1067 * section that is not a jpeg */
1068 if (il->mapped_file[0] != 0xFF || il->mapped_file[1] != 0xD8)
1070 il->mapped_file = nullptr;
1074 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1079 if (il->mapped_file)
1081 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1083 exif_free_fd(il->fd, exif);
1087 if (!il->mapped_file)
1092 pathl = path_from_utf8(il->fd->path);
1093 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1095 if (load_fd == -1) return FALSE;
1097 if (fstat(load_fd, &st) == 0)
1099 il->bytes_total = st.st_size;
1107 il->mapped_file = static_cast<guchar *>(mmap(nullptr, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0));
1109 if (il->mapped_file == MAP_FAILED)
1111 il->mapped_file = nullptr;
1114 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1120 static void image_loader_stop_source(ImageLoader *il)
1124 if (il->mapped_file)
1126 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1128 exif_free_preview(il->mapped_file);
1130 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1132 libraw_free_preview(il->mapped_file);
1136 munmap(il->mapped_file, il->bytes_total);
1138 il->mapped_file = nullptr;
1142 static void image_loader_stop(ImageLoader *il)
1148 g_source_remove(il->idle_id);
1154 /* stop loader in the other thread */
1155 g_mutex_lock(il->data_mutex);
1156 il->stopping = TRUE;
1157 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1158 g_mutex_unlock(il->data_mutex);
1161 image_loader_stop_loader(il);
1162 image_loader_stop_source(il);
1166 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1168 g_mutex_lock(il->data_mutex);
1169 il->delay_area_ready = enable;
1175 list = g_list_reverse(il->area_param_delayed_list);
1176 il->area_param_delayed_list = nullptr;
1177 g_mutex_unlock(il->data_mutex);
1183 auto par = static_cast<ImageLoaderAreaParam *>(work->data);
1186 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1188 g_list_free_full(list, g_free);
1193 g_mutex_unlock(il->data_mutex);
1198 /**************************************************************************************/
1199 /* execution via idle calls */
1201 static gboolean image_loader_idle_cb(gpointer data)
1203 gboolean ret = G_SOURCE_REMOVE;
1204 auto il = static_cast<ImageLoader *>(data);
1208 ret = image_loader_continue(il);
1213 image_loader_stop_source(il);
1219 static gboolean image_loader_start_idle(ImageLoader *il)
1223 if (!il) return FALSE;
1225 if (!il->fd) return FALSE;
1227 if (!image_loader_setup_source(il)) return FALSE;
1229 ret = image_loader_begin(il);
1231 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, nullptr);
1235 /**************************************************************************************/
1236 /* execution via thread */
1238 static GThreadPool *image_loader_thread_pool = nullptr;
1240 static GCond *image_loader_prio_cond = nullptr;
1241 static GMutex *image_loader_prio_mutex = nullptr;
1242 static gint image_loader_prio_num = 0;
1245 static void image_loader_thread_enter_high()
1247 g_mutex_lock(image_loader_prio_mutex);
1248 image_loader_prio_num++;
1249 g_mutex_unlock(image_loader_prio_mutex);
1252 static void image_loader_thread_leave_high()
1254 g_mutex_lock(image_loader_prio_mutex);
1255 image_loader_prio_num--;
1256 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1257 g_mutex_unlock(image_loader_prio_mutex);
1260 static void image_loader_thread_wait_high()
1262 g_mutex_lock(image_loader_prio_mutex);
1263 while (image_loader_prio_num)
1265 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1268 g_mutex_unlock(image_loader_prio_mutex);
1272 static void image_loader_thread_run(gpointer data, gpointer)
1274 auto il = static_cast<ImageLoader *>(data);
1278 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1280 /* low prio, wait until high prio tasks finishes */
1281 image_loader_thread_wait_high();
1286 image_loader_thread_enter_high();
1289 err = !image_loader_begin(il);
1294 loader failed, we have to send signal
1295 (idle mode returns the image_loader_begin return value directly)
1296 (success is always reported indirectly from image_loader_begin)
1298 image_loader_emit_error(il);
1303 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1305 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1307 /* low prio, wait until high prio tasks finishes */
1308 image_loader_thread_wait_high();
1310 cont = image_loader_continue(il);
1312 image_loader_stop_loader(il);
1314 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1317 image_loader_thread_leave_high();
1320 g_mutex_lock(il->data_mutex);
1321 il->can_destroy = TRUE;
1322 g_cond_signal(il->can_destroy_cond);
1323 g_mutex_unlock(il->data_mutex);
1328 static gboolean image_loader_start_thread(ImageLoader *il)
1330 if (!il) return FALSE;
1332 if (!il->fd) return FALSE;
1336 if (!image_loader_setup_source(il)) return FALSE;
1338 if (!image_loader_thread_pool)
1340 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, nullptr, -1, FALSE, nullptr);
1341 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1342 g_cond_init(image_loader_prio_cond);
1343 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1344 g_mutex_init(image_loader_prio_mutex);
1347 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1349 g_thread_pool_push(image_loader_thread_pool, il, nullptr);
1350 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1356 /**************************************************************************************/
1357 /* public interface */
1360 gboolean image_loader_start(ImageLoader *il)
1362 if (!il) return FALSE;
1364 if (!il->fd) return FALSE;
1366 return image_loader_start_thread(il);
1370 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1371 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1374 if (!il) return nullptr;
1376 g_mutex_lock(il->data_mutex);
1378 g_mutex_unlock(il->data_mutex);
1382 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1386 g_mutex_lock(il->data_mutex);
1387 il->requested_width = width;
1388 il->requested_height = height;
1389 g_mutex_unlock(il->data_mutex);
1392 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1396 g_mutex_lock(il->data_mutex);
1397 il->idle_read_loop_count = count ? count : 1;
1398 g_mutex_unlock(il->data_mutex);
1401 void image_loader_set_priority(ImageLoader *il, gint priority)
1405 if (il->thread) return; /* can't change prio if the thread already runs */
1406 il->idle_priority = priority;
1410 gdouble image_loader_get_percent(ImageLoader *il)
1413 if (!il) return 0.0;
1415 g_mutex_lock(il->data_mutex);
1416 if (il->bytes_total == 0)
1422 ret = static_cast<gdouble>(il->bytes_read) / il->bytes_total;
1424 g_mutex_unlock(il->data_mutex);
1428 gboolean image_loader_get_is_done(ImageLoader *il)
1431 if (!il) return FALSE;
1433 g_mutex_lock(il->data_mutex);
1435 g_mutex_unlock(il->data_mutex);
1440 FileData *image_loader_get_fd(ImageLoader *il)
1443 if (!il) return nullptr;
1445 g_mutex_lock(il->data_mutex);
1447 g_mutex_unlock(il->data_mutex);
1452 gboolean image_loader_get_shrunk(ImageLoader *il)
1455 if (!il) return FALSE;
1457 g_mutex_lock(il->data_mutex);
1459 g_mutex_unlock(il->data_mutex);
1463 const gchar *image_loader_get_error(ImageLoader *il)
1465 const gchar *ret = nullptr;
1466 if (!il) return nullptr;
1467 g_mutex_lock(il->data_mutex);
1468 if (il->error) ret = il->error->message;
1469 g_mutex_unlock(il->data_mutex);
1475 * @FIXME this can be rather slow and blocks until the size is known
1477 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1482 il = image_loader_new(fd);
1484 success = image_loader_start_idle(il);
1486 if (success && il->pixbuf)
1488 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1489 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1493 if (width) *width = -1;
1494 if (height) *height = -1;
1497 image_loader_free(il);
1501 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */